1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::{io::ErrorKind, path::Path, str::from_utf8_unchecked};

use page_size::get as get_page_size;

use crate::{
    process::ProcessState,
    scanner_rust::{
        generic_array::typenum::{U192, U32},
        Scanner, ScannerError,
    },
};

#[derive(Default, Debug, Clone)]
pub struct ProcessStat {
    pub state:        ProcessState,
    pub comm:         String,
    pub ppid:         u32,
    pub pgrp:         u32,
    pub session:      u32,
    pub tty_nr_major: u8,
    pub tty_nr_minor: u32,
    pub tpgid:        Option<u32>,
    pub utime:        u32,
    pub stime:        u32,
    pub cutime:       u32,
    pub cstime:       u32,
    pub priority:     i8,
    pub nice:         i8,
    pub num_threads:  usize,
    pub starttime:    u64,
    /// size, VmSize (total program size)
    pub vsize:        usize,
    /// resident, VmRSS (resident set size)
    pub rss:          usize,
    pub rsslim:       usize,
    pub processor:    usize,
    pub rt_priority:  u8,
    /// RssFile + RssShmem (resident shared size)
    pub shared:       usize,
    /// VmRSS - RssFile - RssShmem = RssAnon (resident anonymous memory, process occupied memory)
    pub rss_anon:     usize,
}

/// Get the stat of a specific process found by ID by reading the `/proc/PID/stat` file and the `/proc/PID/statm` file.
///
/// ```rust
/// use mprober_lib::process;
///
/// let process_stat = process::get_process_stat(1).unwrap();
///
/// println!("{process_stat:#?}");
/// ```
pub fn get_process_stat(pid: u32) -> Result<ProcessStat, ScannerError> {
    let mut stat = ProcessStat::default();

    let stat_path = Path::new("/proc").join(pid.to_string()).join("stat");

    let mut sc: Scanner<_, U192> = Scanner::scan_path2(stat_path)?;

    sc.drop_next()?.ok_or(ErrorKind::UnexpectedEof)?;

    sc.drop_next_until("(")?.ok_or(ErrorKind::UnexpectedEof)?;

    loop {
        let comm = sc.next_raw()?.ok_or(ErrorKind::UnexpectedEof)?;

        if comm.ends_with(b")") {
            stat.comm.push_str(unsafe { from_utf8_unchecked(&comm[..(comm.len() - 1)]) });
            break;
        } else {
            stat.comm.push_str(unsafe { from_utf8_unchecked(comm.as_ref()) });
        }
    }

    stat.state = ProcessState::from_str(unsafe {
        from_utf8_unchecked(&sc.next_raw()?.ok_or(ErrorKind::UnexpectedEof)?)
    })
    .ok_or(ErrorKind::InvalidData)?;

    stat.ppid = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
    stat.pgrp = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
    stat.session = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;

    {
        let tty_nr = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;

        stat.tty_nr_major = (tty_nr >> 8) as u8;
        stat.tty_nr_minor = ((tty_nr >> 20) << 8) | (tty_nr & 0xFF);
    }

    {
        let tpgid = sc.next_i32()?.ok_or(ErrorKind::UnexpectedEof)?;

        if tpgid >= 0 {
            stat.tpgid = Some(tpgid as u32);
        }
    }

    for _ in 0..5 {
        sc.drop_next()?.ok_or(ErrorKind::UnexpectedEof)?;
    }

    stat.utime = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
    stat.stime = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
    stat.cutime = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
    stat.cstime = sc.next_u32()?.ok_or(ErrorKind::UnexpectedEof)?;
    stat.priority = sc.next_i8()?.ok_or(ErrorKind::UnexpectedEof)?;
    stat.nice = sc.next_i8()?.ok_or(ErrorKind::UnexpectedEof)?;
    stat.num_threads = sc.next_usize()?.ok_or(ErrorKind::UnexpectedEof)?;

    sc.drop_next()?.ok_or(ErrorKind::UnexpectedEof)?;

    stat.starttime = sc.next_u64()?.ok_or(ErrorKind::UnexpectedEof)?;
    stat.vsize = sc.next_usize()?.ok_or(ErrorKind::UnexpectedEof)?;
    stat.rss = sc.next_usize()?.ok_or(ErrorKind::UnexpectedEof)? * get_page_size();
    stat.rsslim = sc.next_usize()?.ok_or(ErrorKind::UnexpectedEof)?;

    for _ in 0..13 {
        sc.drop_next()?.ok_or(ErrorKind::UnexpectedEof)?;
    }

    stat.processor = sc.next_usize()?.ok_or(ErrorKind::UnexpectedEof)?;
    stat.rt_priority = sc.next_u8()?.ok_or(ErrorKind::UnexpectedEof)?;

    drop(sc);

    let statm_path = Path::new("/proc").join(pid.to_string()).join("statm");

    let mut sc: Scanner<_, U32> = Scanner::scan_path2(statm_path)?;

    for _ in 0..2 {
        sc.drop_next()?.ok_or(ErrorKind::UnexpectedEof)?;
    }

    stat.shared = sc.next_usize()?.ok_or(ErrorKind::UnexpectedEof)? * get_page_size();

    stat.rss_anon = stat.rss - stat.shared;

    Ok(stat)
}