diff options
author | ClementTsang <cjhtsang@uwaterloo.ca> | 2020-02-17 17:42:51 -0500 |
---|---|---|
committer | ClementTsang <cjhtsang@uwaterloo.ca> | 2020-02-17 17:42:51 -0500 |
commit | 4485d1b380e45b4c86cbb41ba1d5f1f3f3799721 (patch) | |
tree | f9b57d13768d2fe010bdf5b56563b79d7464ebea /src/app/data_harvester/processes.rs | |
parent | c669b5337c5267120448b28b0cd9a48975e59dc9 (diff) |
Some clippy and refactoring.
Diffstat (limited to 'src/app/data_harvester/processes.rs')
-rw-r--r-- | src/app/data_harvester/processes.rs | 62 |
1 files changed, 26 insertions, 36 deletions
diff --git a/src/app/data_harvester/processes.rs b/src/app/data_harvester/processes.rs index cca65075..9a2bc548 100644 --- a/src/app/data_harvester/processes.rs +++ b/src/app/data_harvester/processes.rs @@ -116,9 +116,10 @@ fn get_process_cpu_stats(pid: u32) -> std::io::Result<f64> { /// Note that cpu_fraction should be represented WITHOUT the \times 100 factor! fn linux_cpu_usage<S: core::hash::BuildHasher>( pid: u32, cpu_usage: f64, cpu_fraction: f64, - prev_pid_stats: &HashMap<String, (f64, Instant), S>, use_current_cpu_total: bool, + prev_pid_stats: &HashMap<String, (f64, Instant), S>, + new_pid_stats: &mut HashMap<String, (f64, Instant), S>, use_current_cpu_total: bool, curr_time: Instant, -) -> std::io::Result<(f64, (String, (f64, 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 prev_pid_stats.contains_key(&pid.to_string()) { prev_pid_stats @@ -139,36 +140,28 @@ fn linux_cpu_usage<S: core::hash::BuildHasher>( (after_proc_val - before_proc_val) / cpu_usage * 100_f64 );*/ - let new_dict_entry = (pid.to_string(), (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, - new_dict_entry, - )) + Ok((after_proc_val - before_proc_val) / cpu_usage * 100_f64) } else { - Ok(( - (after_proc_val - before_proc_val) / cpu_usage * 100_f64 * cpu_fraction, - new_dict_entry, - )) + Ok((after_proc_val - before_proc_val) / cpu_usage * 100_f64 * cpu_fraction) } } fn convert_ps<S: core::hash::BuildHasher>( process: &str, cpu_usage: f64, cpu_fraction: f64, - prev_pid_stats: &HashMap<String, (f64, Instant), S>, use_current_cpu_total: bool, + prev_pid_stats: &HashMap<String, (f64, Instant), S>, + new_pid_stats: &mut HashMap<String, (f64, Instant), S>, use_current_cpu_total: bool, curr_time: Instant, -) -> std::io::Result<(ProcessHarvest, (String, (f64, Instant)))> { +) -> std::io::Result<ProcessHarvest> { if process.trim().to_string().is_empty() { - let dummy_result = (String::default(), (0.0, Instant::now())); - return Ok(( - ProcessHarvest { - pid: 0, - name: "".to_string(), - mem_usage_percent: 0.0, - cpu_usage_percent: 0.0, - }, - dummy_result, - )); + return Ok(ProcessHarvest { + pid: 0, + name: "".to_string(), + mem_usage_percent: 0.0, + cpu_usage_percent: 0.0, + }); } let pid = (&process[..11]) @@ -183,23 +176,21 @@ fn convert_ps<S: core::hash::BuildHasher>( .parse::<f64>() .unwrap_or(0_f64); - let (cpu_usage_percent, new_entry) = linux_cpu_usage( + let cpu_usage_percent = linux_cpu_usage( pid, cpu_usage, cpu_fraction, prev_pid_stats, + new_pid_stats, use_current_cpu_total, curr_time, )?; - Ok(( - ProcessHarvest { - pid, - name, - mem_usage_percent, - cpu_usage_percent: cpu_usage_percent, - }, - new_entry, - )) + Ok(ProcessHarvest { + pid, + name, + mem_usage_percent, + cpu_usage_percent, + }) } pub fn get_sorted_processes_list( @@ -222,19 +213,18 @@ pub fn get_sorted_processes_list( let mut new_pid_stats: HashMap<String, (f64, Instant), RandomState> = HashMap::new(); for process in process_stream { - if let Ok((process_object, new_entry)) = convert_ps( + if let Ok(process_object) = convert_ps( process, cpu_usage, cpu_fraction, &prev_pid_stats, + &mut new_pid_stats, use_current_cpu_total, curr_time, ) { if !process_object.name.is_empty() { process_vector.push(process_object); } - - new_pid_stats.insert(new_entry.0, new_entry.1); } } |