use crate::error::{Error, Result};
use crate::util::Kibibytes;
use crate::util::parse;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProcessState {
Running,
Sleeping,
Waiting,
Zombie,
Stopped,
Dead,
Idle,
WakeKill,
Waking,
Parked,
}
impl ProcessState {
fn from_char(c: char) -> Self {
match c {
'R' => ProcessState::Running,
'S' => ProcessState::Sleeping,
'D' => ProcessState::Waiting,
'Z' => ProcessState::Zombie,
'T' | 't' => ProcessState::Stopped,
'X' | 'x' => ProcessState::Dead,
'I' => ProcessState::Idle,
'K' => ProcessState::WakeKill,
'W' => ProcessState::Waking,
'P' => ProcessState::Parked,
_ => ProcessState::Dead,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Uids {
pub real: u32,
pub effective: u32,
pub saved: u32,
pub filesystem: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Gids {
pub real: u32,
pub effective: u32,
pub saved: u32,
pub filesystem: u32,
}
#[derive(Debug)]
pub struct ProcessStatus {
pub name: Box<str>,
pub state: ProcessState,
pub pid: u32,
pub ppid: u32,
pub uid: Uids,
pub gid: Gids,
pub vm_peak: Option<Kibibytes>,
pub vm_rss: Kibibytes,
pub vm_size: Kibibytes,
pub threads: u32,
pub voluntary_ctxt_switches: u64,
pub nonvoluntary_ctxt_switches: u64,
}
impl ProcessStatus {
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
let mut name = Box::from("");
let mut state = ProcessState::Dead;
let mut pid = 0u32;
let mut ppid = 0u32;
let mut uid = Uids {
real: 0,
effective: 0,
saved: 0,
filesystem: 0,
};
let mut gid = Gids {
real: 0,
effective: 0,
saved: 0,
filesystem: 0,
};
let mut vm_peak = None;
let mut vm_rss = None;
let mut vm_size = None;
let mut threads = 0u32;
let mut voluntary_ctxt_switches = 0u64;
let mut nonvoluntary_ctxt_switches = 0u64;
for line in bytes.split(|&b| b == b'\n') {
if line.is_empty() {
continue;
}
let (key, value) = match parse::parse_key_value_line(line) {
Some(kv) => kv,
None => continue,
};
match key {
b"Name" => {
name = std::str::from_utf8(parse::trim_start(value))
.map_err(|_| Error::Parse {
path: std::path::PathBuf::from("<status>"),
line: 0,
msg: "invalid utf8 in Name",
})?
.to_string()
.into_boxed_str();
}
b"State" => {
let trimmed = parse::trim_start(value);
if let Some(&c) = trimmed.first() {
state = ProcessState::from_char(c as char);
}
}
b"Tgid" => {
pid = parse::parse_dec_u32(parse::trim_start(value)).unwrap_or(0);
}
b"PPid" => {
ppid = parse::parse_dec_u32(parse::trim_start(value)).unwrap_or(0);
}
b"Uid" => {
uid = parse_uid_line(value);
}
b"Gid" => {
gid = parse_gid_line(value);
}
b"VmPeak" => {
vm_peak = Some(parse_kib(value));
}
b"VmRSS" => {
vm_rss = Some(parse_kib(value));
}
b"VmSize" => {
vm_size = Some(parse_kib(value));
}
b"Threads" => {
threads = parse::parse_dec_u32(parse::trim_start(value)).unwrap_or(0);
}
b"voluntary_ctxt_switches" => {
voluntary_ctxt_switches =
parse::parse_dec_u64(parse::trim_start(value)).unwrap_or(0);
}
b"nonvoluntary_ctxt_switches" => {
nonvoluntary_ctxt_switches =
parse::parse_dec_u64(parse::trim_start(value)).unwrap_or(0);
}
_ => {}
}
}
Ok(ProcessStatus {
name,
state,
pid,
ppid,
uid,
gid,
vm_peak,
vm_rss: vm_rss.unwrap_or(Kibibytes(0)),
vm_size: vm_size.unwrap_or(Kibibytes(0)),
threads,
voluntary_ctxt_switches,
nonvoluntary_ctxt_switches,
})
}
}
fn parse_uid_line(value: &[u8]) -> Uids {
let fields: Vec<&[u8]> = parse::split_spaces(parse::trim_start(value));
let get = |i: usize| -> u32 {
if i < fields.len() {
parse::parse_dec_u32(fields[i]).unwrap_or(0)
} else {
0
}
};
Uids {
real: get(0),
effective: get(1),
saved: get(2),
filesystem: get(3),
}
}
fn parse_gid_line(value: &[u8]) -> Gids {
let fields: Vec<&[u8]> = parse::split_spaces(parse::trim_start(value));
let get = |i: usize| -> u32 {
if i < fields.len() {
parse::parse_dec_u32(fields[i]).unwrap_or(0)
} else {
0
}
};
Gids {
real: get(0),
effective: get(1),
saved: get(2),
filesystem: get(3),
}
}
fn parse_kib(value: &[u8]) -> Kibibytes {
let trimmed = parse::trim_start(value);
let num = trimmed.split(|&b| b == b' ').next().unwrap_or(trimmed);
Kibibytes(parse::parse_dec_u64(num).unwrap_or(0))
}