From 86d043bccf65501fabae7ee6894e388d238d51f9 Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Fri, 22 Dec 2023 18:47:40 +0530 Subject: [PATCH] Ensure CPU frequency gets dynamically updated - Fix state refresh logic for sysinfo system - Let each module refresh happen when needed. This requires mutable System instance to be passed around though. --- src/cpu.rs | 8 +++++--- src/main.rs | 9 ++++----- src/memory.rs | 4 +++- src/sensors.rs | 5 +++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index fa8c907..135a90d 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -18,9 +18,11 @@ this program. If not, see . use super::colors::colorize; use super::config::Config; use colored::*; -use sysinfo::{CpuExt, System, SystemExt}; +use sysinfo::{CpuExt, CpuRefreshKind, System, SystemExt}; + +pub fn overview(config: &Config, system: &mut System) -> String { + system.refresh_cpu_specifics(CpuRefreshKind::everything()); -pub fn overview(config: &Config, system: &System) -> String { if config.frequency { format!("{} {}", usage(config, system), frequency(system)) } else { @@ -28,7 +30,7 @@ pub fn overview(config: &Config, system: &System) -> String { } } -fn usage(config: &Config, system: &System) -> String { +fn usage(config: &Config, system: &mut System) -> String { let usage = system.global_cpu_info().cpu_usage(); colorize( diff --git a/src/main.rs b/src/main.rs index ba70ccb..5bf212b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,18 +40,17 @@ struct State { ping: ping::PingManager, } -fn line(state: &State) -> String { +fn line(state: &mut State) -> String { [ - memory::usage(&state.config, &state.system), - cpu::overview(&state.config, &state.system), - sensors::temperature(&state.config, &state.system), + memory::usage(&state.config, &mut state.system), + cpu::overview(&state.config, &mut state.system), + sensors::temperature(&state.config, &mut state.system), state.ping.current(), ] .join(" ") } fn render_line(delimiter: char, state: &mut State) -> () { - state.system.refresh_all(); print!("{}{}", line(state), delimiter); () } diff --git a/src/memory.rs b/src/memory.rs index f18fd6c..3b6d57d 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -19,7 +19,9 @@ use super::colors::colorize; use super::config::Config; use sysinfo::{System, SystemExt}; -pub fn usage(config: &Config, system: &System) -> String { +pub fn usage(config: &Config, system: &mut System) -> String { + system.refresh_memory(); + let total = system.total_memory() as f32; let available = system.available_memory() as f32; let usage = (100.0 * (total - available) / total).round(); diff --git a/src/sensors.rs b/src/sensors.rs index 07f50f7..a11d193 100644 --- a/src/sensors.rs +++ b/src/sensors.rs @@ -19,14 +19,15 @@ use super::colors::colorize; use super::config::Config; use sysinfo::{ComponentExt, System, SystemExt}; -fn max_temperature(system: &System) -> f32 { +fn max_temperature(system: &mut System) -> f32 { + system.refresh_components(); system .components() .iter() .rfold(0.0, |acc, x| acc.max(x.temperature())) } -pub fn temperature(config: &Config, system: &System) -> String { +pub fn temperature(config: &Config, system: &mut System) -> String { let temp = max_temperature(system); colorize(