From 514c39cc56d434d54145f30075bc6eb5a0f1a496 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Thu, 9 Jan 2020 21:59:52 -0500 Subject: [PATCH] Change how we call time in data_collection step --- src/app/data_collection.rs | 20 ++++++++++++++------ src/app/data_collection/cpu.rs | 6 ++++-- src/app/data_collection/mem.rs | 8 ++++---- src/app/data_collection/network.rs | 4 ++-- src/app/data_collection/processes.rs | 12 ++++++++---- src/utils.rs | 2 +- 6 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/app/data_collection.rs b/src/app/data_collection.rs index 3c7d9c70..cb5e395a 100644 --- a/src/app/data_collection.rs +++ b/src/app/data_collection.rs @@ -95,6 +95,9 @@ impl DataState { self.sys.refresh_network(); } + // Filter out stale timed entries + let current_instant = std::time::Instant::now(); + // What we want to do: For timed data, if there is an error, just do not add. For other data, just don't update! push_if_valid( &network::get_network_data( @@ -102,17 +105,24 @@ impl DataState { &mut self.prev_net_rx_bytes, &mut self.prev_net_tx_bytes, &mut self.prev_net_access_time, + ¤t_instant, ) .await, &mut self.data.network, ); push_if_valid( - &cpu::get_cpu_data_list(&self.sys), + &cpu::get_cpu_data_list(&self.sys, ¤t_instant), &mut self.data.list_of_cpu_packages, ); - push_if_valid(&mem::get_mem_data_list().await, &mut self.data.memory); - push_if_valid(&mem::get_swap_data_list().await, &mut self.data.swap); + push_if_valid( + &mem::get_mem_data_list(¤t_instant).await, + &mut self.data.memory, + ); + push_if_valid( + &mem::get_swap_data_list(¤t_instant).await, + &mut self.data.swap, + ); set_if_valid( &processes::get_sorted_processes_list( &self.sys, @@ -120,6 +130,7 @@ impl DataState { &mut self.prev_non_idle, &mut self.prev_pid_stats, self.use_current_cpu_total, + ¤t_instant, ), &mut self.data.list_of_processes, ); @@ -142,9 +153,6 @@ impl DataState { self.first_run = false; } - // Filter out stale timed entries - let current_instant = std::time::Instant::now(); - if current_instant.duration_since(self.last_clean).as_secs() > self.stale_max_seconds { let stale_list: Vec<_> = self .prev_pid_stats diff --git a/src/app/data_collection/cpu.rs b/src/app/data_collection/cpu.rs index 3ccfd0ad..4987a6a3 100644 --- a/src/app/data_collection/cpu.rs +++ b/src/app/data_collection/cpu.rs @@ -13,7 +13,9 @@ pub struct CPUPackage { pub instant: Instant, } -pub fn get_cpu_data_list(sys: &System) -> crate::utils::error::Result { +pub fn get_cpu_data_list( + sys: &System, curr_time: &Instant, +) -> crate::utils::error::Result { let cpu_data = sys.get_processor_list(); let mut cpu_vec = Vec::new(); @@ -26,6 +28,6 @@ pub fn get_cpu_data_list(sys: &System) -> crate::utils::error::Result crate::utils::error::Result { +pub async fn get_mem_data_list(curr_time: &Instant) -> crate::utils::error::Result { let memory = heim::memory::memory().await?; Ok(MemData { mem_total_in_mb: memory.total().get::(), mem_used_in_mb: memory.total().get::() - memory.available().get::(), - instant: Instant::now(), + instant: *curr_time, }) } -pub async fn get_swap_data_list() -> crate::utils::error::Result { +pub async fn get_swap_data_list(curr_time: &Instant) -> crate::utils::error::Result { let memory = heim::memory::swap().await?; Ok(MemData { mem_total_in_mb: memory.total().get::(), mem_used_in_mb: memory.used().get::(), - instant: Instant::now(), + instant: *curr_time, }) } diff --git a/src/app/data_collection/network.rs b/src/app/data_collection/network.rs index 3a622ecf..1ba7c90f 100644 --- a/src/app/data_collection/network.rs +++ b/src/app/data_collection/network.rs @@ -16,12 +16,12 @@ pub struct NetworkData { pub async fn get_network_data( sys: &System, prev_net_rx_bytes: &mut u64, prev_net_tx_bytes: &mut u64, - prev_net_access_time: &mut std::time::Instant, + prev_net_access_time: &mut Instant, curr_time: &Instant, ) -> crate::utils::error::Result { if cfg!(target_os = "windows") { let network_data = sys.get_network(); - *prev_net_access_time = Instant::now(); + *prev_net_access_time = *curr_time; Ok(NetworkData { rx: network_data.get_income(), tx: network_data.get_outcome(), diff --git a/src/app/data_collection/processes.rs b/src/app/data_collection/processes.rs index 8de9f4cb..efdd3a60 100644 --- a/src/app/data_collection/processes.rs +++ b/src/app/data_collection/processes.rs @@ -147,12 +147,13 @@ fn get_process_cpu_stats(pid: u32) -> std::io::Result { fn linux_cpu_usage( pid: u32, cpu_usage: f64, cpu_percentage: f64, previous_pid_stats: &mut HashMap, use_current_cpu_total: bool, + curr_time: &Instant, ) -> std::io::Result { // Based heavily on https://stackoverflow.com/a/23376195 and https://stackoverflow.com/a/1424556 let before_proc_val: f64 = if previous_pid_stats.contains_key(&pid.to_string()) { previous_pid_stats .get(&pid.to_string()) - .unwrap_or(&(0_f64, Instant::now())) + .unwrap_or(&(0_f64, *curr_time)) .0 } else { 0_f64 @@ -170,8 +171,8 @@ fn linux_cpu_usage( let entry = previous_pid_stats .entry(pid.to_string()) - .or_insert((after_proc_val, Instant::now())); - *entry = (after_proc_val, Instant::now()); + .or_insert((after_proc_val, *curr_time)); + *entry = (after_proc_val, *curr_time); if use_current_cpu_total { Ok((after_proc_val - before_proc_val) / cpu_usage * 100_f64) } else { @@ -182,6 +183,7 @@ fn linux_cpu_usage( fn convert_ps( process: &str, cpu_usage: f64, cpu_percentage: f64, prev_pid_stats: &mut HashMap, use_current_cpu_total: bool, + curr_time: &Instant, ) -> std::io::Result { if process.trim().to_string().is_empty() { return Ok(ProcessData { @@ -219,6 +221,7 @@ fn convert_ps( cpu_percentage, prev_pid_stats, use_current_cpu_total, + curr_time, )?, pid_vec: None, }) @@ -227,7 +230,7 @@ fn convert_ps( pub fn get_sorted_processes_list( sys: &System, prev_idle: &mut f64, prev_non_idle: &mut f64, prev_pid_stats: &mut std::collections::HashMap, - use_current_cpu_total: bool, + use_current_cpu_total: bool, curr_time: &Instant, ) -> crate::utils::error::Result> { let mut process_vector: Vec = Vec::new(); @@ -251,6 +254,7 @@ pub fn get_sorted_processes_list( cpu_percentage, prev_pid_stats, use_current_cpu_total, + curr_time, ) { if !process_object.name.is_empty() { process_vector.push(process_object); diff --git a/src/utils.rs b/src/utils.rs index 5aa52d90..22fbdf53 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,3 +1,3 @@ pub mod error; -pub mod general_utility; +pub mod gen_util; pub mod logging;