|
| 1 | +// This file is part of the uutils coreutils package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +//! Heuristics for determining buffer size for external sorting. |
| 7 | +use std::ffi::OsString; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + FALLBACK_AUTOMATIC_BUF_SIZE, MAX_AUTOMATIC_BUF_SIZE, MIN_AUTOMATIC_BUF_SIZE, STDIN_FILE, |
| 11 | +}; |
| 12 | + |
| 13 | +// Heuristics to size the external sort buffer without overcommit memory. |
| 14 | +pub(crate) fn automatic_buffer_size(files: &[OsString]) -> usize { |
| 15 | + let file_hint = file_size_hint(files); |
| 16 | + let mem_hint = available_memory_hint(); |
| 17 | + |
| 18 | + // Prefer the tighter bound when both hints exist, otherwise fall back to whichever hint is available. |
| 19 | + match (file_hint, mem_hint) { |
| 20 | + (Some(file), Some(mem)) => file.min(mem), |
| 21 | + (Some(file), None) => file, |
| 22 | + (None, Some(mem)) => mem, |
| 23 | + (None, None) => FALLBACK_AUTOMATIC_BUF_SIZE, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn file_size_hint(files: &[OsString]) -> Option<usize> { |
| 28 | + // Estimate total bytes across real files; non-regular inputs are skipped. |
| 29 | + let mut total_bytes: u128 = 0; |
| 30 | + |
| 31 | + for file in files { |
| 32 | + if file == STDIN_FILE { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + let Ok(metadata) = std::fs::metadata(file) else { |
| 37 | + continue; |
| 38 | + }; |
| 39 | + |
| 40 | + if !metadata.is_file() { |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + total_bytes = total_bytes.saturating_add(metadata.len() as u128); |
| 45 | + |
| 46 | + if total_bytes >= (MAX_AUTOMATIC_BUF_SIZE as u128) * 8 { |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if total_bytes == 0 { |
| 52 | + return None; |
| 53 | + } |
| 54 | + |
| 55 | + let desired_bytes = desired_file_buffer_bytes(total_bytes); |
| 56 | + Some(clamp_hint(desired_bytes)) |
| 57 | +} |
| 58 | + |
| 59 | +fn available_memory_hint() -> Option<usize> { |
| 60 | + #[cfg(target_os = "linux")] |
| 61 | + if let Some(bytes) = uucore::parser::parse_size::available_memory_bytes() { |
| 62 | + return Some(clamp_hint(bytes / 4)); |
| 63 | + } |
| 64 | + |
| 65 | + physical_memory_bytes().map(|bytes| clamp_hint(bytes / 4)) |
| 66 | +} |
| 67 | + |
| 68 | +fn clamp_hint(bytes: u128) -> usize { |
| 69 | + let min = MIN_AUTOMATIC_BUF_SIZE as u128; |
| 70 | + let max = MAX_AUTOMATIC_BUF_SIZE as u128; |
| 71 | + let clamped = bytes.clamp(min, max); |
| 72 | + clamped.min(usize::MAX as u128) as usize |
| 73 | +} |
| 74 | + |
| 75 | +fn desired_file_buffer_bytes(total_bytes: u128) -> u128 { |
| 76 | + if total_bytes == 0 { |
| 77 | + return 0; |
| 78 | + } |
| 79 | + |
| 80 | + let max = MAX_AUTOMATIC_BUF_SIZE as u128; |
| 81 | + |
| 82 | + if total_bytes <= max { |
| 83 | + return total_bytes.saturating_mul(12).clamp(total_bytes, max); |
| 84 | + } |
| 85 | + |
| 86 | + let quarter = total_bytes / 4; |
| 87 | + quarter.max(max) |
| 88 | +} |
| 89 | + |
| 90 | +fn physical_memory_bytes() -> Option<u128> { |
| 91 | + #[cfg(all( |
| 92 | + target_family = "unix", |
| 93 | + not(target_os = "redox"), |
| 94 | + any(target_os = "linux", target_os = "android") |
| 95 | + ))] |
| 96 | + { |
| 97 | + physical_memory_bytes_unix() |
| 98 | + } |
| 99 | + |
| 100 | + #[cfg(any( |
| 101 | + not(target_family = "unix"), |
| 102 | + target_os = "redox", |
| 103 | + not(any(target_os = "linux", target_os = "android")) |
| 104 | + ))] |
| 105 | + { |
| 106 | + // No portable or safe API is available here to detect total physical memory. |
| 107 | + None |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg(all( |
| 112 | + target_family = "unix", |
| 113 | + not(target_os = "redox"), |
| 114 | + any(target_os = "linux", target_os = "android") |
| 115 | +))] |
| 116 | +fn physical_memory_bytes_unix() -> Option<u128> { |
| 117 | + use nix::unistd::{SysconfVar, sysconf}; |
| 118 | + |
| 119 | + let pages = match sysconf(SysconfVar::_PHYS_PAGES) { |
| 120 | + Ok(Some(pages)) if pages > 0 => u128::try_from(pages).ok()?, |
| 121 | + _ => return None, |
| 122 | + }; |
| 123 | + |
| 124 | + let page_size = match sysconf(SysconfVar::PAGE_SIZE) { |
| 125 | + Ok(Some(page_size)) if page_size > 0 => u128::try_from(page_size).ok()?, |
| 126 | + _ => return None, |
| 127 | + }; |
| 128 | + |
| 129 | + Some(pages.saturating_mul(page_size)) |
| 130 | +} |
| 131 | + |
| 132 | +#[cfg(test)] |
| 133 | +mod tests { |
| 134 | + use super::*; |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn desired_buffer_matches_total_when_small() { |
| 138 | + let six_mebibytes = 6 * 1024 * 1024; |
| 139 | + let expected = ((six_mebibytes as u128) * 12) |
| 140 | + .clamp(six_mebibytes as u128, crate::MAX_AUTOMATIC_BUF_SIZE as u128); |
| 141 | + assert_eq!(desired_file_buffer_bytes(six_mebibytes as u128), expected); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn desired_buffer_caps_at_max_for_large_inputs() { |
| 146 | + let large = 256 * 1024 * 1024; // 256 MiB |
| 147 | + assert_eq!( |
| 148 | + desired_file_buffer_bytes(large as u128), |
| 149 | + crate::MAX_AUTOMATIC_BUF_SIZE as u128 |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
0 commit comments