/*
* bb
*
* Copyright 2019 Manos Pitsidianakis
*
* This file is part of bb.
*
* bb is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* bb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bb. If not, see <http://www.gnu.org/licenses/>.
*/
use super::*;
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::str::FromStr;
#[derive(Debug)]
pub struct ProcessData {
cpu_stat: Stat,
processes_times: HashMap<Pid, usize>,
}
const SIGNAL_LIST: &[(i32, &'static str)] = &[
(1, "1 HUP"),
(2, "2 INT"),
(3, "3 QUIT"),
(4, "4 ILL"),
(5, "5 TRAP"),
(6, "6 ABRT"),
(7, "7 BUS"),
(8, "8 FPE"),
(9, "9 KILL"),
(10, "10 USR1"),
(11, "11 SEGV"),
(12, "12 USR2"),
(13, "13 PIPE"),
(14, "14 ALRM"),
(15, "15 TERM"),
(16, "16 STKFLT"),
(17, "17 CHLD"),
(18, "18 CONT"),
(19, "19 STOP"),
(20, "20 TSTP"),
(21, "21 TTIN"),
(22, "22 TTOU"),
(23, "23 URG"),
(24, "24 XCPU"),
(25, "25 XFSZ"),
(26, "26 VTALRM"),
(27, "27 PROF"),
(28, "28 WINCH"),
(29, "29 POLL"),
(30, "30 PWR"),
(31, "31 SYS"),
];
/* Hold maximum width for each column */
#[derive(Debug)]
pub struct ColumnWidthMaxima {
pid: usize,
ppid: usize,
vm_rss: usize,
cpu_percent: usize,
state: usize,
username: usize,
}
impl ColumnWidthMaxima {
fn new() -> ColumnWidthMaxima {
ColumnWidthMaxima {
pid: "PID".len(),
ppid: "PPID".len(),
vm_rss: "VM_RSS".len(),
cpu_percent: " CPU%".len(),
state: 1,
username: "USER".len(),
}
}
}
macro_rules! define_column_string {
($($typename: tt),+) => {
$(
#[derive(Debug)]
pub struct $typename(pub String);
impl $typename {
#[allow(dead_code)]
pub fn len(&self) -> usize {
self.0.len()
}
}
impl std::fmt::Display for $typename {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(width) = f.width() {
write!(f, "{:>width$}", self.0, width = width)
} else {
write!(f, "{:>}", self.0)
}
}
})+
}
}
define_column_string!(
PidString,
PpidString,
VmRssString,
CmdLineString,
UserString
);
pub type Pid = i32;
/* Wrapper