use std::fmt::{self, Formatter, Debug};
use std::collections::HashMap;
use libc::{c_int, gid_t, kill, pid_t, uid_t};
use ::ProcessExt;
#[derive(Clone, Debug)]
pub enum ProcessStatus {
Idle,
Run,
Sleep,
Stop,
Zombie,
Tracing,
Dead,
Wakekill,
Waking,
Parked,
Unknown(u32),
}
impl From<u32> for ProcessStatus {
fn from(status: u32) -> ProcessStatus {
match status {
1 => ProcessStatus::Idle,
2 => ProcessStatus::Run,
3 => ProcessStatus::Sleep,
4 => ProcessStatus::Stop,
5 => ProcessStatus::Zombie,
x => ProcessStatus::Unknown(x),
}
}
}
impl From<char> for ProcessStatus {
fn from(status: char) -> ProcessStatus {
match status {
'R' => ProcessStatus::Run,
'S' => ProcessStatus::Sleep,
'D' => ProcessStatus::Idle,
'Z' => ProcessStatus::Zombie,
'T' => ProcessStatus::Stop,
't' => ProcessStatus::Tracing,
'X' | 'x' => ProcessStatus::Dead,
'K' => ProcessStatus::Wakekill,
'W' => ProcessStatus::Waking,
'P' => ProcessStatus::Parked,
x => ProcessStatus::Unknown(x as u32),
}
}
}
impl ProcessStatus {
pub fn to_string(&self) -> &str {
match *self {
ProcessStatus::Idle => "Idle",
ProcessStatus::Run => "Runnable",
ProcessStatus::Sleep => "Sleeping",
ProcessStatus::Stop => "Stopped",
ProcessStatus::Zombie => "Zombie",
ProcessStatus::Tracing => "Tracing",
ProcessStatus::Dead => "Dead",
ProcessStatus::Wakekill => "Wakekill",
ProcessStatus::Waking => "Waking",
ProcessStatus::Parked => "Parked",
ProcessStatus::Unknown(_) => "Unknown",
}
}
}
impl fmt::Display for ProcessStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_string())
}
}
#[derive(Clone)]
pub struct Process {
pub name: String,
pub cmd: Vec<String>,
pub exe: String,
pub pid: pid_t,
pub parent: Option<pid_t>,
pub environ: Vec<String>,
pub cwd: String,
pub root: String,
pub memory: u64,
utime: u64,
stime: u64,
old_utime: u64,
old_stime: u64,
pub start_time: u64,
updated: bool,
pub cpu_usage: f32,
pub uid: uid_t,
pub gid: gid_t,
pub status: Option<ProcessStatus>,
pub tasks: HashMap<pid_t, Process>,
}
impl ProcessExt for Process {
fn new(pid: pid_t, parent: Option<pid_t>, start_time: u64) -> Process {
Process {
name: String::new(),
pid: pid,
parent: parent,
cmd: Vec::new(),
environ: Vec::new(),
exe: String::new(),
cwd: String::new(),
root: String::new(),
memory: 0,
cpu_usage: 0.,
utime: 0,
stime: 0,
old_utime: 0,
old_stime: 0,
updated: true,
start_time: start_time,
uid: 0,
gid: 0,
status: None,
tasks: HashMap::new(),
}
}
fn kill(&self, signal: ::Signal) -> bool {
unsafe { kill(self.pid, signal as c_int) == 0 }
}
}
#[allow(unused_must_use)]
impl Debug for Process {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
writeln!(f, "pid: {}", self.pid);
writeln!(f, "parent: {:?}", self.parent);
writeln!(f, "name: {}", self.name);
writeln!(f, "environment:");
for var in &self.environ {
if !var.is_empty() {
writeln!(f, "\t{}", var);
}
}
writeln!(f, "command:");
for arg in &self.cmd {
writeln!(f, "\t{}", arg);
}
writeln!(f, "executable path: {}", self.exe);
writeln!(f, "current working directory: {}", self.cwd);
writeln!(f, "owner/group: {}:{}", self.uid, self.gid);
writeln!(f, "memory usage: {} kB", self.memory);
writeln!(f, "cpu usage: {}%", self.cpu_usage);
writeln!(f, "status: {}", match self.status {
Some(ref v) => v.to_string(),
None => "Unknown",
});
write!(f, "root path: {}", self.root)
}
}
pub fn compute_cpu_usage(p: &mut Process, nb_processors: u64, total_time: f32) {
p.cpu_usage = ((p.utime - p.old_utime + p.stime - p.old_stime) * nb_processors * 100) as f32 / total_time;
p.updated = false;
}
pub fn set_time(p: &mut Process, utime: u64, stime: u64) {
p.old_utime = p.utime;
p.old_stime = p.stime;
p.utime = utime;
p.stime = stime;
p.updated = true;
}
pub fn has_been_updated(p: &Process) -> bool {
p.updated
}