use sysinfo::{Pid, Process};
#[derive(Debug, Clone, PartialEq)]
pub struct ProcessInfo {
pub pid: u32,
pub parent_pid: Option<u32>,
pub name: String,
pub cmd: Vec<String>,
pub exe_path: Option<String>,
pub cwd: Option<String>,
pub cpu_usage: f32,
pub memory_bytes: u64,
pub status: String,
pub environ_count: usize,
pub start_time: u64,
pub run_time: u64,
}
impl ProcessInfo {
pub fn from_sysinfo(pid: Pid, proc: &Process) -> Self {
let name = proc.name().to_string_lossy().into_owned();
let cmd = proc
.cmd()
.iter()
.map(|s| s.to_string_lossy().into_owned())
.collect();
let exe_path = proc.exe().map(|p| p.to_string_lossy().into_owned());
let cwd = proc.cwd().map(|p| p.to_string_lossy().into_owned());
let parent_pid = proc.parent().map(|p| p.as_u32());
Self {
pid: pid.as_u32(),
parent_pid,
name,
cmd,
exe_path,
cwd,
cpu_usage: proc.cpu_usage(),
memory_bytes: proc.memory(),
status: proc.status().to_string(),
environ_count: proc.environ().len(),
start_time: proc.start_time(),
run_time: proc.run_time(),
}
}
}