From 7e537016d20646fb7aabcdb6db131c5101998834 Mon Sep 17 00:00:00 2001 From: Bloxx12 Date: Tue, 13 May 2025 20:44:52 +0200 Subject: [PATCH] cpu: create get_logical_core_count --- src/cpu.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index 36f9667..30165bd 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -20,16 +20,13 @@ fn write_sysfs_value(path: impl AsRef, value: &str) -> Result<()> { }) } -fn for_each_cpu_core(mut action: F) -> Result<()> -where - F: FnMut(u32) -> Result<()>, -{ +pub fn get_logical_core_count() -> Result { // Using num_cpus::get() for a reliable count of logical cores accessible. // The monitor module's get_logical_core_count might be more specific to cpufreq-capable cores, // but for applying settings, we might want to iterate over all reported by OS. // However, settings usually apply to cores with cpufreq. // Let's use a similar discovery to monitor's get_logical_core_count - let mut cores_to_act_on = Vec::new(); + let mut num_cores: u32 = 0; let path = Path::new("/sys/devices/system/cpu"); if !path.exists() { return Err(ControlError::NotSupported(format!( @@ -59,20 +56,25 @@ where continue; } - if let Ok(core_id) = name[3..].parse::() { - cores_to_act_on.push(core_id); + if name[3..].parse::().is_ok() { + num_cores += 1; } } - if cores_to_act_on.is_empty() { + if num_cores == 0 { // Fallback if sysfs iteration above fails to find any cpufreq cores - #[allow(clippy::cast_possible_truncation)] - let num_cores = num_cpus::get() as u32; - for core_id in 0..num_cores { - cores_to_act_on.push(core_id); - } + num_cores = num_cpus::get() as u32; } - for core_id in cores_to_act_on { + Ok(num_cores) +} + +fn for_each_cpu_core(mut action: F) -> Result<()> +where + F: FnMut(u32) -> Result<()>, +{ + let num_cores: u32 = get_logical_core_count()?; + + for core_id in 0u32..num_cores { action(core_id)?; } Ok(())