Added way of removing stale entries in the old PID list.

This commit is contained in:
ClementTsang 2019-10-06 21:06:53 -04:00
parent 7bd49be49a
commit f55d2fff3f
2 changed files with 21 additions and 9 deletions

View File

@ -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<String, f64>, // TODO: Purge list?
prev_pid_stats : HashMap<String, (f64, Instant)>,
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

View File

@ -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<f64> {
Ok(utime + stime) // This seems to match top...
}
fn linux_cpu_usage(pid : u32, cpu_usage : f64, previous_pid_stats : &mut HashMap<String, f64>) -> std::io::Result<f64> {
fn linux_cpu_usage(pid : u32, cpu_usage : f64, previous_pid_stats : &mut HashMap<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 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<String, f64>) -> std::io::Result<ProcessData> {
fn convert_ps(process : &str, cpu_usage_percentage : f64, prev_pid_stats : &mut HashMap<String, (f64, Instant)>) -> std::io::Result<ProcessData> {
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<String, f64>,
sys : &System, prev_idle : &mut f64, prev_non_idle : &mut f64, prev_pid_stats : &mut std::collections::HashMap<String, (f64, Instant)>,
) -> crate::utils::error::Result<Vec<ProcessData>> {
let mut process_vector : Vec<ProcessData> = Vec::new();