feat: process list
this is unnacceptably unperformant; I think I've reached the point at which sysinfo doesn't do what I want, which is keep track of a table of processes efficiently enough.
This commit is contained in:
parent
afa6104c28
commit
b20158820b
4 changed files with 105 additions and 22 deletions
|
|
@ -1,7 +1,20 @@
|
|||
use std::time::Duration;
|
||||
|
||||
pub fn refreshes() -> sysinfo::RefreshKind {
|
||||
sysinfo::RefreshKind::nothing()
|
||||
.with_memory(sysinfo::MemoryRefreshKind::everything())
|
||||
.with_cpu(sysinfo::CpuRefreshKind::nothing().with_cpu_usage())
|
||||
.with_processes(
|
||||
sysinfo::ProcessRefreshKind::nothing()
|
||||
.with_cpu()
|
||||
.with_cmd(sysinfo::UpdateKind::OnlyIfNotSet)
|
||||
.with_exe(sysinfo::UpdateKind::OnlyIfNotSet),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct SysinfoSnapshot {
|
||||
/// in seconds
|
||||
uptime: u64,
|
||||
|
||||
pub cpu_total: f32,
|
||||
|
|
@ -11,6 +24,8 @@ pub struct SysinfoSnapshot {
|
|||
pub memory_total: u64,
|
||||
pub swap: u64,
|
||||
pub swap_total: u64,
|
||||
|
||||
pub processes: Vec<ProcessSnapshot>,
|
||||
}
|
||||
impl SysinfoSnapshot {
|
||||
#[cfg_attr(not(feature = "app"), allow(dead_code))]
|
||||
|
|
@ -31,6 +46,42 @@ impl From<&sysinfo::System> for SysinfoSnapshot {
|
|||
memory_total: value.total_memory(),
|
||||
swap: value.used_swap(),
|
||||
swap_total: value.total_swap(),
|
||||
|
||||
processes: value
|
||||
.processes()
|
||||
.iter()
|
||||
.map(|(_pid, process)| ProcessSnapshot::from(process))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ProcessSnapshot {
|
||||
pub pid: usize,
|
||||
|
||||
/// in CPU-milliseconds
|
||||
accumulated_cpu_time: u64,
|
||||
|
||||
/// note: it's by core, and can be up to eg 1600 on a 16-core device
|
||||
pub cpu_usage: f32,
|
||||
|
||||
pub name: String,
|
||||
}
|
||||
impl ProcessSnapshot {
|
||||
pub fn cpu_time(&self) -> Duration {
|
||||
Duration::from_millis(self.accumulated_cpu_time)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&sysinfo::Process> for ProcessSnapshot {
|
||||
fn from(value: &sysinfo::Process) -> Self {
|
||||
Self {
|
||||
pid: value.pid().into(),
|
||||
accumulated_cpu_time: value.accumulated_cpu_time(),
|
||||
cpu_usage: value.cpu_usage(),
|
||||
|
||||
name: value.name().to_string_lossy().into_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue