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:
parent
571f172cc2
commit
4763b54c97
3 changed files with 4 additions and 21 deletions
|
@ -6,7 +6,6 @@ pub struct SystemInfo {
|
||||||
pub struct CpuCoreInfo {
|
pub struct CpuCoreInfo {
|
||||||
// Per-core data
|
// Per-core data
|
||||||
pub core_id: u32,
|
pub core_id: u32,
|
||||||
pub usage_percent: Option<f32>,
|
|
||||||
pub temperature_celsius: Option<f32>,
|
pub temperature_celsius: Option<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,10 @@ impl Cpu {
|
||||||
pub fn time_idle(&self) -> u64 {
|
pub fn time_idle(&self) -> u64 {
|
||||||
self.time_idle + self.time_iowait
|
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 {
|
impl fmt::Display for Cpu {
|
||||||
|
|
|
@ -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 {
|
Ok(CpuCoreInfo {
|
||||||
core_id,
|
core_id,
|
||||||
usage_percent,
|
|
||||||
temperature_celsius,
|
temperature_celsius,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue