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

cpu: add usage percent

This commit is contained in:
RGBCube 2025-05-22 18:01:10 +03:00
parent 571f172cc2
commit 4763b54c97
Signed by: RGBCube
SSH key fingerprint: SHA256:CzqbPcfwt+GxFYNnFVCqoN5Itn4YFrshg1TrnACpA5M
3 changed files with 4 additions and 21 deletions

View file

@ -6,7 +6,6 @@ pub struct SystemInfo {
pub struct CpuCoreInfo {
// Per-core data
pub core_id: u32,
pub usage_percent: Option<f32>,
pub temperature_celsius: Option<f32>,
}

View file

@ -49,6 +49,10 @@ impl Cpu {
pub fn time_idle(&self) -> u64 {
self.time_idle + self.time_iowait
}
pub fn usage(&self) -> f64 {
1.0 - self.time_idle() as f64 / self.time_total() as f64
}
}
impl fmt::Display for Cpu {

View file

@ -111,28 +111,8 @@ pub fn get_cpu_core_info(
}
}
let usage_percent: Option<f32> = {
let prev_idle = prev_times.idle_time();
let current_idle = current_times.idle_time();
let prev_total = prev_times.total_time();
let current_total = current_times.total_time();
let total_diff = current_total.saturating_sub(prev_total);
let idle_diff = current_idle.saturating_sub(prev_idle);
// Avoid division by zero if no time has passed or counters haven't changed
if total_diff == 0 {
None
} else {
let usage = 100.0 * (1.0 - (idle_diff as f32 / total_diff as f32));
Some(usage.clamp(0.0, 100.0)) // clamp between 0 and 100
}
};
Ok(CpuCoreInfo {
core_id,
usage_percent,
temperature_celsius,
})
}