From 06234a9eb946e3bd73a9235a8ac837fc2b82844c Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Mon, 20 Jan 2020 07:29:47 -0800 Subject: [PATCH] Cleanup process updating using ProcessCollector --- Cargo.lock | 2 +- src/widgets/proc.rs | 46 ++++++++++++++------------------------------- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bea3a96..b2fbd64 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -602,7 +602,7 @@ dependencies = [ [[package]] name = "psutil" version = "1.7.0" -source = "git+https://github.com/borntyping/rust-psutil#5aa5613a3b3b2ea3f73909eb536103ced9dd9fa5" +source = "git+https://github.com/borntyping/rust-psutil#d74a2895d422d3a8c030af0f3d205c43786b0e98" dependencies = [ "glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.16.1 (git+https://github.com/nix-rust/nix)", diff --git a/src/widgets/proc.rs b/src/widgets/proc.rs index 960b48f..5029cc2 100644 --- a/src/widgets/proc.rs +++ b/src/widgets/proc.rs @@ -86,7 +86,8 @@ pub struct ProcWidget<'a> { procs: Vec, grouped_procs: HashMap, - processes: HashMap, + + process_collector: process::ProcessCollector, } impl ProcWidget<'_> { @@ -109,7 +110,8 @@ impl ProcWidget<'_> { procs: Vec::new(), grouped_procs: HashMap::new(), - processes: HashMap::new(), + + process_collector: process::ProcessCollector::new().unwrap(), } } @@ -209,39 +211,23 @@ impl ProcWidget<'_> { impl UpdatableWidget for ProcWidget<'_> { fn update(&mut self) { - process::processes() - .unwrap() - .into_iter() - .filter_map(|process| process.ok()) - .for_each(|process| { - if !self.processes.contains_key(&process.pid()) - || self.processes[&process.pid()] != process - { - self.processes.insert(process.pid(), process); - } - }); + self.process_collector.update().unwrap(); - let mut to_remove = Vec::new(); let cpu_count = self.cpu_count as f32; self.procs = self + .process_collector .processes .values_mut() .map(|process| { - let result = { - let name = process.name()?; - Ok(Proc { - num: process.pid(), - name: name.to_string(), - commandline: process.cmdline()?.unwrap_or_else(|| format!("[{}]", name)), - cpu: process.cpu_percent()? / cpu_count, - mem: process.memory_percent()?, - }) - }; - if result.is_err() { - to_remove.push(process.pid()); - } - result + let name = process.name()?; + Ok(Proc { + num: process.pid(), + name: name.to_string(), + commandline: process.cmdline()?.unwrap_or_else(|| format!("[{}]", name)), + cpu: process.cpu_percent()? / cpu_count, + mem: process.memory_percent()?, + }) }) .filter_map(|process: process::ProcessResult| process.ok()) .collect(); @@ -260,10 +246,6 @@ impl UpdatableWidget for ProcWidget<'_> { ..proc.clone() }); } - - for id in to_remove { - self.processes.remove(&id); - } } fn get_update_interval(&self) -> Ratio {