From 971384cf3a0b30c1e3456a508fb475c6ef606f5a Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Fri, 31 Jan 2020 20:49:30 -0500 Subject: New way of doing referencing previous pid stats without having to GC - just write a new one every time... --- src/app/data_farmer.rs | 2 +- src/app/data_harvester/processes.rs | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/app/data_farmer.rs b/src/app/data_farmer.rs index 369845e3..02b725cc 100644 --- a/src/app/data_farmer.rs +++ b/src/app/data_farmer.rs @@ -276,7 +276,7 @@ pub fn generate_joining_points( 500, ); - for itx in (0..num_points).step_by(1) { + for itx in 0..num_points { points.push(( time_difference - (itx as f64 / num_points as f64 * time_difference), start_y + (itx as f64 / num_points as f64 * value_difference), diff --git a/src/app/data_harvester/processes.rs b/src/app/data_harvester/processes.rs index c72c6d01..8f24b94d 100644 --- a/src/app/data_harvester/processes.rs +++ b/src/app/data_harvester/processes.rs @@ -144,12 +144,13 @@ fn get_process_cpu_stats(pid: u32) -> std::io::Result { /// Note that cpu_percentage should be represented WITHOUT the \times 100 factor! fn linux_cpu_usage( pid: u32, cpu_usage: f64, cpu_percentage: f64, - previous_pid_stats: &mut HashMap, use_current_cpu_total: bool, + prev_pid_stats: &HashMap, + new_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 + let before_proc_val: f64 = if prev_pid_stats.contains_key(&pid.to_string()) { + prev_pid_stats .get(&pid.to_string()) .unwrap_or(&(0_f64, *curr_time)) .0 @@ -167,10 +168,7 @@ fn linux_cpu_usage( (after_proc_val - before_proc_val) / cpu_usage * 100_f64 );*/ - let entry = previous_pid_stats - .entry(pid.to_string()) - .or_insert((after_proc_val, *curr_time)); - *entry = (after_proc_val, *curr_time); + new_pid_stats.insert(pid.to_string(), (after_proc_val, *curr_time)); if use_current_cpu_total { Ok((after_proc_val - before_proc_val) / cpu_usage * 100_f64) } else { @@ -180,7 +178,8 @@ 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, + prev_pid_stats: &HashMap, + new_pid_stats: &mut HashMap, use_current_cpu_total: bool, curr_time: &Instant, ) -> std::io::Result { if process.trim().to_string().is_empty() { @@ -214,6 +213,7 @@ fn convert_ps( cpu_usage, cpu_percentage, prev_pid_stats, + new_pid_stats, use_current_cpu_total, curr_time, )?, @@ -223,8 +223,8 @@ 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, mem_total_kb: u64, curr_time: &Instant, + prev_pid_stats: &mut HashMap, use_current_cpu_total: bool, + mem_total_kb: u64, curr_time: &Instant, ) -> crate::utils::error::Result> { let mut process_vector: Vec = Vec::new(); @@ -240,12 +240,15 @@ pub fn get_sorted_processes_list( if let Ok((cpu_usage, cpu_percentage)) = cpu_calc { let process_stream = split_string.collect::>(); + let mut new_pid_stats: HashMap = HashMap::new(); + for process in process_stream { if let Ok(process_object) = convert_ps( process, cpu_usage, cpu_percentage, - prev_pid_stats, + &prev_pid_stats, + &mut new_pid_stats, use_current_cpu_total, curr_time, ) { @@ -254,6 +257,8 @@ pub fn get_sorted_processes_list( } } } + + *prev_pid_stats = new_pid_stats; } else { error!("Unable to properly parse CPU data in Linux."); error!("Result: {:?}", cpu_calc.err()); -- cgit v1.2.3