diff --git a/src/app/data_collection.rs b/src/app/data_collection.rs index 642fc723..05e86586 100644 --- a/src/app/data_collection.rs +++ b/src/app/data_collection.rs @@ -1,6 +1,6 @@ //! This is the main file to house data collection functions. -use std::collections::HashMap; +use std::{collections::HashMap, time::Instant}; use sysinfo::{System, SystemExt}; pub mod cpu; @@ -40,7 +40,7 @@ pub struct DataState { first_run : bool, sys : System, stale_max_seconds : u64, - prev_pid_stats : HashMap, // TODO: Purge list? + prev_pid_stats : HashMap, prev_idle : f64, prev_non_idle : f64, temperature_type : temperature::TemperatureType, @@ -112,6 +112,18 @@ impl DataState { // Filter out stale timed entries let current_instant = std::time::Instant::now(); + + let stale_list : Vec<_> = self + .prev_pid_stats + .iter() + .filter(|&(_, &v)| current_instant.duration_since(v.1).as_secs() > self.stale_max_seconds) + .map(|(k, _)| k.clone()) + .collect(); + for stale in stale_list { + debug!("Removing: {:?}", self.prev_pid_stats[&stale]); + self.prev_pid_stats.remove(&stale); + } + self.data.list_of_cpu_packages = self .data .list_of_cpu_packages diff --git a/src/app/data_collection/processes.rs b/src/app/data_collection/processes.rs index 6de81df2..695f17b2 100644 --- a/src/app/data_collection/processes.rs +++ b/src/app/data_collection/processes.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, process::Command}; +use std::{collections::HashMap, process::Command, time::Instant}; use sysinfo::{ProcessExt, System, SystemExt}; #[derive(Clone)] @@ -115,10 +115,10 @@ fn get_process_cpu_stats(pid : u32) -> std::io::Result { Ok(utime + stime) // This seems to match top... } -fn linux_cpu_usage(pid : u32, cpu_usage : f64, previous_pid_stats : &mut HashMap) -> std::io::Result { +fn linux_cpu_usage(pid : u32, cpu_usage : f64, previous_pid_stats : &mut HashMap) -> 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) + previous_pid_stats.get(&pid.to_string()).unwrap_or(&(0_f64, Instant::now())).0 } else { 0_f64 @@ -134,12 +134,12 @@ fn linux_cpu_usage(pid : u32, cpu_usage : f64, previous_pid_stats : &mut HashMap (after_proc_val - before_proc_val) / cpu_usage * 100_f64 );*/ - let entry = previous_pid_stats.entry(pid.to_string()).or_insert(after_proc_val); - *entry = after_proc_val; + let entry = previous_pid_stats.entry(pid.to_string()).or_insert((after_proc_val, Instant::now())); + *entry = (after_proc_val, Instant::now()); Ok((after_proc_val - before_proc_val) / cpu_usage * 100_f64) } -fn convert_ps(process : &str, cpu_usage_percentage : f64, prev_pid_stats : &mut HashMap) -> std::io::Result { +fn convert_ps(process : &str, cpu_usage_percentage : f64, prev_pid_stats : &mut HashMap) -> std::io::Result { if process.trim().to_string().is_empty() { return Ok(ProcessData { pid : 0, @@ -164,7 +164,7 @@ fn convert_ps(process : &str, cpu_usage_percentage : f64, prev_pid_stats : &mut } pub async fn get_sorted_processes_list( - sys : &System, prev_idle : &mut f64, prev_non_idle : &mut f64, prev_pid_stats : &mut std::collections::HashMap, + sys : &System, prev_idle : &mut f64, prev_non_idle : &mut f64, prev_pid_stats : &mut std::collections::HashMap, ) -> crate::utils::error::Result> { let mut process_vector : Vec = Vec::new();