1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-28 01:17:45 +00:00

cpu: create get_logical_core_count

This commit is contained in:
Bloxx12 2025-05-13 20:44:52 +02:00
parent 7d6164c2fe
commit 7e537016d2
No known key found for this signature in database

View file

@ -20,16 +20,13 @@ fn write_sysfs_value(path: impl AsRef<Path>, value: &str) -> Result<()> {
}) })
} }
fn for_each_cpu_core<F>(mut action: F) -> Result<()> pub fn get_logical_core_count() -> Result<u32> {
where
F: FnMut(u32) -> Result<()>,
{
// Using num_cpus::get() for a reliable count of logical cores accessible. // 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, // 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. // but for applying settings, we might want to iterate over all reported by OS.
// However, settings usually apply to cores with cpufreq. // However, settings usually apply to cores with cpufreq.
// Let's use a similar discovery to monitor's get_logical_core_count // 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"); let path = Path::new("/sys/devices/system/cpu");
if !path.exists() { if !path.exists() {
return Err(ControlError::NotSupported(format!( return Err(ControlError::NotSupported(format!(
@ -59,20 +56,25 @@ where
continue; continue;
} }
if let Ok(core_id) = name[3..].parse::<u32>() { if name[3..].parse::<u32>().is_ok() {
cores_to_act_on.push(core_id); 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 // Fallback if sysfs iteration above fails to find any cpufreq cores
#[allow(clippy::cast_possible_truncation)] num_cores = num_cpus::get() as u32;
let num_cores = num_cpus::get() as u32;
for core_id in 0..num_cores {
cores_to_act_on.push(core_id);
}
} }
for core_id in cores_to_act_on { Ok(num_cores)
}
fn for_each_cpu_core<F>(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)?; action(core_id)?;
} }
Ok(()) Ok(())