Addition of Process mtime function and expect calls

This commit is contained in:
Xavier Moffett 2024-06-11 20:51:32 -04:00
parent c956e7761f
commit ae8c37e951
Signed by: Sapphirus
GPG key ID: A6C061B2CEA1A7AC
2 changed files with 18 additions and 14 deletions

View file

@ -35,17 +35,16 @@ pub struct ProcessList {
groups: IndexMap<String, Vec<i32>>,
}
#[derive(Clone)]
pub struct Process {
pid: i32,
mtime: u64,
depth: u32,
fork: bool,
cmd: Vec<String>,
stat: ProcStat,
instance: String,
depth: u32,
fork: bool,
}
#[derive(Clone)]
pub struct ProcStat {
parent: i32,
thread_name: String,
@ -77,14 +76,15 @@ impl ProcessList {
}
impl Process {
fn new(id: i32, level: u32, cmdline: Vec<String>, procstat: ProcStat, ins: String, forked: bool) -> Self {
fn new(id: i32, time: u64, level: u32, cmdline: Vec<String>, procstat: ProcStat, ins: String, forked: bool) -> Self {
Self {
pid: id,
mtime: time,
fork: forked,
depth: level,
cmd: cmdline,
stat: procstat,
instance: ins,
fork: forked,
}
}
@ -92,6 +92,10 @@ impl Process {
self.pid
}
pub fn mtime(&self) -> u64 {
self.mtime
}
pub fn exec_path(&self) -> &str {
&self.cmd[0]
}
@ -107,14 +111,14 @@ impl Process {
self.cmd.iter().map(|a| a.as_str()).collect()
}
pub fn cmdlist_string(&self, start: usize) -> Result<String, Error> {
pub fn cmdlist_string(&self, start: usize) -> String {
let mut string = String::new();
for idx in start .. self.cmd.len() {
write!(string, "{} ", self.cmd[idx]).prepend(|| format!("Failure writing string to cmd buffer."))?;
write!(string, "{} ", self.cmd[idx]).expect("Writing substring to string buffer failed");
}
Ok(string)
string
}
pub fn stat(&self) -> &ProcStat {
@ -169,7 +173,7 @@ pub fn list<'a>(cache: &'a ContainerCache<'a>) -> Result<ProcessList, Error> {
processes.sort_by_key(|m| Reverse(m.1));
for pid in processes.iter().map(|m| m.0) {
for (pid, mtime) in processes {
let cmdlist = match cmdlist(pid) {
Some(cmdlist) => cmdlist,
None => continue,
@ -200,7 +204,7 @@ pub fn list<'a>(cache: &'a ContainerCache<'a>) -> Result<ProcessList, Error> {
}
}
map.insert(pid, Process::new(pid, depth, cmdlist, stat, ins, fork));
map.insert(pid, Process::new(pid, mtime, depth, cmdlist, stat, ins, fork));
}
Ok(ProcessList::new(map, groups))

View file

@ -157,8 +157,8 @@ fn summary(args: &mut Arguments) -> Result<()> {
let ins = process.instance().to_string();
let row = table.insert(match col {
(true, false, _) => vec![pid, ins, process.exec().into()],
(false, true, i) => vec![pid, ins, process.cmdlist_string(i)?],
(true, true, i) => vec![pid, ins, process.exec().into(), process.cmdlist_string(i)?],
(false, true, i) => vec![pid, ins, process.cmdlist_string(i)],
(true, true, i) => vec![pid, ins, process.exec().into(), process.cmdlist_string(i)],
_ => vec![pid, ins],
});
@ -168,7 +168,7 @@ fn summary(args: &mut Arguments) -> Result<()> {
}
}
print!("{}{}", if table.marked() { "\n" } else { "" }, table.build().unwrap());
print!("{}{}", if table.marked() { "\n" } else { "" }, table.build().expect("Failed to build table"));
Ok(())
}