diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2021-05-09 01:39:42 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-09 01:39:42 -0400 |
commit | 574c2c1df7616ed9f80831360cd97aa2d0d9ae44 (patch) | |
tree | 961cb3bdd91e0760b0064de15a87397ffd1eab0f /src/app | |
parent | e367a37b1a9e5fa08fd2624c52f8abf5e46ade6c (diff) |
change: switch from sysinfo to heim for cpu usage in macOS and Windows (#467)
Due to #404, I've just moved all CPU usage calculations over to heim.
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/data_harvester.rs | 29 | ||||
-rw-r--r-- | src/app/data_harvester/cpu.rs | 66 |
2 files changed, 32 insertions, 63 deletions
diff --git a/src/app/data_harvester.rs b/src/app/data_harvester.rs index 65fc680f..209cbb05 100644 --- a/src/app/data_harvester.rs +++ b/src/app/data_harvester.rs @@ -80,9 +80,7 @@ pub struct DataCollector { pub data: Data, #[cfg(not(target_os = "linux"))] sys: System, - #[cfg(target_os = "linux")] previous_cpu_times: Vec<(cpu::PastCpuWork, cpu::PastCpuTotal)>, - #[cfg(target_os = "linux")] previous_average_cpu_time: Option<(cpu::PastCpuWork, cpu::PastCpuTotal)>, #[cfg(target_os = "linux")] pid_mapping: FxHashMap<crate::Pid, processes::PrevProcDetails>, @@ -111,9 +109,7 @@ impl DataCollector { data: Data::default(), #[cfg(not(target_os = "linux"))] sys: System::new_with_specifics(sysinfo::RefreshKind::new()), - #[cfg(target_os = "linux")] previous_cpu_times: vec![], - #[cfg(target_os = "linux")] previous_average_cpu_time: None, #[cfg(target_os = "linux")] pid_mapping: FxHashMap::default(), @@ -216,9 +212,6 @@ impl DataCollector { pub async fn update_data(&mut self) { #[cfg(not(target_os = "linux"))] { - if self.widgets_to_harvest.use_cpu { - self.sys.refresh_cpu(); - } if self.widgets_to_harvest.use_proc { self.sys.refresh_processes(); } @@ -235,22 +228,14 @@ impl DataCollector { // CPU if self.widgets_to_harvest.use_cpu { - #[cfg(not(target_os = "linux"))] + if let Ok(cpu_data) = cpu::get_cpu_data_list( + self.show_average_cpu, + &mut self.previous_cpu_times, + &mut self.previous_average_cpu_time, + ) + .await { - self.data.cpu = Some(cpu::get_cpu_data_list(&self.sys, self.show_average_cpu)); - } - - #[cfg(target_os = "linux")] - { - if let Ok(cpu_data) = cpu::get_cpu_data_list( - self.show_average_cpu, - &mut self.previous_cpu_times, - &mut self.previous_average_cpu_time, - ) - .await - { - self.data.cpu = Some(cpu_data); - } + self.data.cpu = Some(cpu_data); } #[cfg(target_family = "unix")] diff --git a/src/app/data_harvester/cpu.rs b/src/app/data_harvester/cpu.rs index bbc17509..61e79e28 100644 --- a/src/app/data_harvester/cpu.rs +++ b/src/app/data_harvester/cpu.rs @@ -10,56 +10,40 @@ pub type CpuHarvest = Vec<CpuData>; pub type PastCpuWork = f64; pub type PastCpuTotal = f64; -#[cfg(not(target_os = "linux"))] -use sysinfo::{ProcessorExt, System, SystemExt}; - -#[cfg(not(target_os = "linux"))] -pub fn get_cpu_data_list(sys: &System, show_average_cpu: bool) -> CpuHarvest { - let cpu_data = sys.get_processors(); - let avg_cpu_usage = sys.get_global_processor_info().get_cpu_usage(); - let mut cpu_vec = vec![]; - - if show_average_cpu { - cpu_vec.push(CpuData { - cpu_prefix: "AVG".to_string(), - cpu_count: None, - cpu_usage: avg_cpu_usage as f64, - }); - } - - for (itx, cpu) in cpu_data.iter().enumerate() { - cpu_vec.push(CpuData { - cpu_prefix: "CPU".to_string(), - cpu_count: Some(itx), - cpu_usage: f64::from(cpu.get_cpu_usage()), - }); - } - - cpu_vec -} - -#[cfg(target_os = "linux")] pub async fn get_cpu_data_list( show_average_cpu: bool, previous_cpu_times: &mut Vec<(PastCpuWork, PastCpuTotal)>, previous_average_cpu_time: &mut Option<(PastCpuWork, PastCpuTotal)>, ) -> crate::error::Result<CpuHarvest> { use futures::StreamExt; + #[cfg(target_os = "linux")] use heim::cpu::os::linux::CpuTimeExt; use std::collections::VecDeque; fn convert_cpu_times(cpu_time: &heim::cpu::CpuTime) -> (f64, f64) { - let working_time: f64 = (cpu_time.user() - + cpu_time.nice() - + cpu_time.system() - + cpu_time.irq() - + cpu_time.soft_irq() - + cpu_time.steal()) - .get::<heim::units::time::second>(); - ( - working_time, - working_time - + (cpu_time.idle() + cpu_time.io_wait()).get::<heim::units::time::second>(), - ) + #[cfg(not(target_os = "linux"))] + { + let working_time: f64 = + (cpu_time.user() + cpu_time.system()).get::<heim::units::time::second>(); + ( + working_time, + working_time + cpu_time.idle().get::<heim::units::time::second>(), + ) + } + #[cfg(target_os = "linux")] + { + let working_time: f64 = (cpu_time.user() + + cpu_time.nice() + + cpu_time.system() + + cpu_time.irq() + + cpu_time.soft_irq() + + cpu_time.steal()) + .get::<heim::units::time::second>(); + ( + working_time, + working_time + + (cpu_time.idle() + cpu_time.io_wait()).get::<heim::units::time::second>(), + ) + } } fn calculate_cpu_usage_percentage( |