diff options
author | ClementTsang <cjhtsang@uwaterloo.ca> | 2020-01-31 20:49:30 -0500 |
---|---|---|
committer | ClementTsang <cjhtsang@uwaterloo.ca> | 2020-01-31 20:49:30 -0500 |
commit | 971384cf3a0b30c1e3456a508fb475c6ef606f5a (patch) | |
tree | 6e668b9aa68251a20e1c3b9197274b6b0156cd3c | |
parent | 4f31c6ee02e7f31c84f894d9e83106d7b039108e (diff) |
New way of doing referencing previous pid stats without having to GC - just write a new one every time...
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/app/data_farmer.rs | 2 | ||||
-rw-r--r-- | src/app/data_harvester/processes.rs | 27 |
3 files changed, 18 insertions, 13 deletions
@@ -80,7 +80,7 @@ Run using `btm`. - `-g`, `--group` will group together processes with the same name by default (equivalent to pressing `Tab`). -- `-i`, `--case_insensitive` will default to not matching cases when searching processes. +- `-i`, `--case_insensitive` will default to not matching case when searching processes. ### Keybindings 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<f64> { /// 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<String, (f64, Instant)>, use_current_cpu_total: bool, + prev_pid_stats: &HashMap<String, (f64, Instant)>, + new_pid_stats: &mut HashMap<String, (f64, Instant)>, use_current_cpu_total: bool, curr_time: &Instant, ) -> std::io::Result<f64> { // 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<String, (f64, Instant)>, use_current_cpu_total: bool, + prev_pid_stats: &HashMap<String, (f64, Instant)>, + new_pid_stats: &mut HashMap<String, (f64, Instant)>, use_current_cpu_total: bool, curr_time: &Instant, ) -> std::io::Result<ProcessHarvest> { 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<String, (f64, Instant)>, - use_current_cpu_total: bool, mem_total_kb: u64, curr_time: &Instant, + prev_pid_stats: &mut HashMap<String, (f64, Instant)>, use_current_cpu_total: bool, + mem_total_kb: u64, curr_time: &Instant, ) -> crate::utils::error::Result<Vec<ProcessHarvest>> { let mut process_vector: Vec<ProcessHarvest> = 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::<Vec<&str>>(); + let mut new_pid_stats: HashMap<String, (f64, Instant)> = 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()); |