From 4763b54c971b2705f3fe71944eaeb65a2f6543b1 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Thu, 22 May 2025 18:01:10 +0300 Subject: [PATCH] cpu: add usage percent --- src/core.rs | 1 - src/cpu.rs | 4 ++++ src/monitor.rs | 20 -------------------- 3 files changed, 4 insertions(+), 21 deletions(-) diff --git a/src/core.rs b/src/core.rs index e3773c5..bdd7a24 100644 --- a/src/core.rs +++ b/src/core.rs @@ -6,7 +6,6 @@ pub struct SystemInfo { pub struct CpuCoreInfo { // Per-core data pub core_id: u32, - pub usage_percent: Option, pub temperature_celsius: Option, } diff --git a/src/cpu.rs b/src/cpu.rs index 3d6fd85..89e9ea9 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -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 { diff --git a/src/monitor.rs b/src/monitor.rs index 4286f0b..7ab1bb0 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -111,28 +111,8 @@ pub fn get_cpu_core_info( } } - let usage_percent: Option = { - 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, }) }