use std::time::Instant;
use crate::process::{Process, ProcessCpuTimes};
use crate::utils::duration_percent;
use crate::Percent;
pub trait Oneshot {
fn name_oneshot(&self) -> String;
fn cpu_times_oneshot(&self) -> ProcessCpuTimes;
fn cpu_percent_oneshot(&mut self) -> Percent;
}
impl Oneshot for Process {
fn name_oneshot(&self) -> String {
self.procfs_stat.comm.to_string()
}
fn cpu_times_oneshot(&self) -> ProcessCpuTimes {
ProcessCpuTimes::from(&self.procfs_stat)
}
fn cpu_percent_oneshot(&mut self) -> Percent {
let busy = self.cpu_times_oneshot().busy();
let instant = Instant::now();
let percent = duration_percent(
busy.checked_sub(self.busy).unwrap_or_default(),
instant - self.instant,
);
self.busy = busy;
self.instant = instant;
percent
}
}