1
0
mirror of https://github.com/danog/ytop.git synced 2024-11-26 20:15:03 +01:00

Cleanup process updating using ProcessCollector

This commit is contained in:
Caleb Bassi 2020-01-20 07:29:47 -08:00
parent d4d886ad25
commit 06234a9eb9
2 changed files with 15 additions and 33 deletions

2
Cargo.lock generated
View File

@ -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)",

View File

@ -86,7 +86,8 @@ pub struct ProcWidget<'a> {
procs: Vec<Proc>,
grouped_procs: HashMap<String, Proc>,
processes: HashMap<u32, process::Process>,
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<Proc>| 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<u64> {