Skip to main content

parmesan/
lib.rs

1pub mod affine;
2pub mod affine2x;
3pub mod altmap;
4pub mod create;
5pub mod decoder;
6pub mod encoder;
7pub mod gf16;
8pub mod gf16_mac;
9pub mod layout;
10pub mod matrix;
11pub mod ops;
12pub mod packet;
13pub mod packet_reader;
14pub mod recovery_set;
15pub mod repair;
16pub mod shuffle2x;
17#[cfg(test)]
18pub(crate) mod test_support;
19pub mod verify;
20pub mod worker;
21pub mod yenc;
22
23pub use encoder::{
24    affine2x_buffer_size, affine_buffer_size, altmap_buffer_size, shuffle2x_buffer_size,
25};
26
27/// Shown by `--version` and banners.
28pub const DISPLAY_VERSION: &str = env!("CARGO_PKG_VERSION");
29
30/// Multiplication backend for the Reed-Solomon encoder.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, clap::ValueEnum)]
32pub enum SimdPath {
33    /// Auto-detect the fastest supported path for the current CPU.
34    #[default]
35    Auto,
36    /// Pure Rust scalar implementation (no SIMD).
37    Scalar,
38    /// Intel/AMD SSSE3 (128-bit shuffles).
39    Ssse3,
40    /// Intel/AMD AVX2 (256-bit shuffles).
41    Avx2,
42    /// Intel Ice Lake+ / Sapphire Rapids+ AVX2 with GFNI.
43    Avx2Gfni,
44    /// Intel Ice Lake+ / Sapphire Rapids+ AVX-512 with GFNI.
45    Avx512Gfni,
46    /// AVX-512 VL/BW nibble shuffle (no GFNI — Skylake-X / Cascade Lake).
47    Avx512Shuffle,
48    /// ARM NEON (AArch64 128-bit shuffles).
49    Neon,
50}
51
52impl std::fmt::Display for SimdPath {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        match self {
55            Self::Auto => write!(f, "auto"),
56            Self::Scalar => write!(f, "scalar"),
57            Self::Ssse3 => write!(f, "ssse3"),
58            Self::Avx2 => write!(f, "avx2"),
59            Self::Avx2Gfni => write!(f, "avx2-gfni"),
60            Self::Avx512Gfni => write!(f, "avx512-gfni"),
61            Self::Avx512Shuffle => write!(f, "avx512-shuffle"),
62            Self::Neon => write!(f, "neon"),
63        }
64    }
65}
66
67/// Which recovery-buffer layout `parmesan create` builds.
68///
69/// `smart` is the auto path (Affine512 packed on AVX-512+GFNI).
70/// `affine512` forces that layout even if `smart` would pick another.
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, clap::ValueEnum)]
72pub enum EncoderLayout {
73    #[default]
74    Smart,
75    Normal,
76    Affine,
77    Affine512,
78    Shuffle2x,
79}
80
81impl std::fmt::Display for EncoderLayout {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        match self {
84            Self::Smart => write!(f, "smart"),
85            Self::Normal => write!(f, "normal"),
86            Self::Affine => write!(f, "affine"),
87            Self::Affine512 => write!(f, "affine512"),
88            Self::Shuffle2x => write!(f, "shuffle2x"),
89        }
90    }
91}
92
93/// Returns the name of the SIMD path that the PAR2 encoder will use at runtime.
94pub fn detect_simd() -> &'static str {
95    #[cfg(target_arch = "x86_64")]
96    if std::is_x86_feature_detected!("avx512f")
97        && std::is_x86_feature_detected!("avx512bw")
98        && std::is_x86_feature_detected!("gfni")
99    {
100        return "AVX-512/GFNI";
101    }
102    #[cfg(target_arch = "x86_64")]
103    if std::is_x86_feature_detected!("avx2") && std::is_x86_feature_detected!("gfni") {
104        return "AVX2/GFNI";
105    }
106    #[cfg(target_arch = "x86_64")]
107    if std::is_x86_feature_detected!("avx512f") && std::is_x86_feature_detected!("avx512bw") {
108        return "AVX-512/Shuffle";
109    }
110    #[cfg(target_arch = "x86_64")]
111    if std::is_x86_feature_detected!("avx2") {
112        return "AVX2";
113    }
114    #[cfg(target_arch = "x86_64")]
115    if std::is_x86_feature_detected!("ssse3") {
116        return "SSSE3";
117    }
118    #[cfg(target_arch = "aarch64")]
119    {
120        return "NEON";
121    }
122    #[allow(unreachable_code)]
123    "scalar"
124}
125
126/// Number of physical CPU cores, derived from `/proc/cpuinfo` by counting
127/// distinct `(physical id, core id)` pairs. Falls back to the logical CPU
128/// count when that information is unavailable.
129pub fn physical_core_count() -> usize {
130    #[cfg(target_os = "linux")]
131    {
132        use std::collections::HashSet;
133        if let Ok(info) = std::fs::read_to_string("/proc/cpuinfo") {
134            let mut seen: HashSet<(String, String)> = HashSet::new();
135            let (mut phys, mut core): (Option<String>, Option<String>) = (None, None);
136            for line in info.lines() {
137                if line.trim().is_empty() {
138                    if let (Some(p), Some(c)) = (phys.take(), core.take()) {
139                        seen.insert((p, c));
140                    }
141                } else if let Some((key, val)) = line.split_once(':') {
142                    match key.trim() {
143                        "physical id" => phys = Some(val.trim().to_string()),
144                        "core id" => core = Some(val.trim().to_string()),
145                        _ => {}
146                    }
147                }
148            }
149            if !seen.is_empty() {
150                return seen.len();
151            }
152        }
153    }
154    std::thread::available_parallelism()
155        .map(|n| n.get())
156        .unwrap_or(1)
157}
158
159/// Number of physical cores on hybrid CPUs.
160/// On hybrid CPUs (Intel 12th gen+), detects P-cores (high-performance) vs
161/// E-cores (efficiency) via Linux topology: P-cores have 2+ thread_siblings
162/// (hyperthreading), E-cores have 1 (standalone).
163///
164/// Returns count of P-core leaders + E-cores (one thread per physical core)
165/// for work distribution. Hyperthreads are excluded as they contend for the
166/// same execution ports and add noise on CPU-bound workloads.
167/// On non-hybrid CPUs falls back to [`physical_core_count`].
168///
169/// The topology above describes the whole host, so the result is capped at
170/// [`std::thread::available_parallelism`], which honours the CPU affinity
171/// mask and cgroup CPU quotas. A process confined to 4 CPUs of a 64-core
172/// host gets 4, not 64.
173pub fn performance_core_count() -> usize {
174    let usable = std::thread::available_parallelism()
175        .map(|n| n.get())
176        .unwrap_or(usize::MAX);
177    host_performance_core_count().min(usable).max(1)
178}
179
180/// [`performance_core_count`] before the cap at the usable CPU count.
181fn host_performance_core_count() -> usize {
182    #[cfg(target_os = "linux")]
183    {
184        use std::collections::HashSet;
185
186        let Ok(entries) = std::fs::read_dir("/sys/devices/system/cpu") else {
187            return physical_core_count();
188        };
189
190        let mut paired_leaders: HashSet<usize> = HashSet::new();
191        let mut solo: HashSet<usize> = HashSet::new();
192
193        for entry in entries.flatten() {
194            let name = entry.file_name();
195            let name_s = name.to_string_lossy();
196            let Some(cpu_num) = name_s
197                .strip_prefix("cpu")
198                .and_then(|s| s.parse::<usize>().ok())
199            else {
200                continue;
201            };
202            let sib_path = entry.path().join("topology/thread_siblings_list");
203            let Ok(sib) = std::fs::read_to_string(sib_path) else {
204                continue;
205            };
206            let leader = sib
207                .trim()
208                .split([',', '-'])
209                .next()
210                .and_then(|s| s.parse::<usize>().ok())
211                .unwrap_or(cpu_num);
212            let count = sib.trim().split([',', '-']).count();
213            if count >= 2 {
214                paired_leaders.insert(leader);
215            } else {
216                solo.insert(cpu_num);
217            }
218        }
219
220        if !paired_leaders.is_empty() && !solo.is_empty() {
221            return paired_leaders.len() + solo.len();
222        }
223    }
224    physical_core_count()
225}
226
227#[cfg(test)]
228mod tests {
229    #[test]
230    fn performance_core_count_stays_within_usable_cpus() {
231        let usable = std::thread::available_parallelism().map_or(usize::MAX, |n| n.get());
232        let cores = super::performance_core_count();
233        assert!(
234            (1..=usable).contains(&cores),
235            "{cores} cores, {usable} usable"
236        );
237    }
238}