1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +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<()>
where
F: FnMut(u32) -> Result<()>,
{
pub fn get_logical_core_count() -> Result<u32> {
// 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::<u32>() {
cores_to_act_on.push(core_id);
if name[3..].parse::<u32>().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<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)?;
}
Ok(())