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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
extern crate chrono;
extern crate regex;

use std::collections::BTreeMap;
use std::fs;
use std::hash::{Hash, Hasher};
use std::io::ErrorKind;
use std::mem::replace;
use std::path::Path;
use std::thread::sleep;
use std::time::Duration;

use crate::btime::get_btime;
use crate::cpu::get_average_cpu_stat;
use crate::process::{
    get_process_stat, get_process_status, get_process_time_stat, ProcessFilter, ProcessStat,
    ProcessState, ProcessTimeStat,
};

use crate::scanner_rust::ScannerError;

use chrono::prelude::*;

#[derive(Debug, Clone, Eq)]
pub struct Process {
    pub pid: u32,
    pub effective_uid: u32,
    pub effective_gid: u32,
    pub state: ProcessState,
    pub ppid: u32,
    pub program: String,
    pub cmdline: String,
    pub tty: Option<String>,
    pub priority: i8,
    pub real_time_priority: Option<u8>,
    pub nice: i8,
    pub threads: usize,
    /// Virtual Set Size (VIRT)
    pub vsz: usize,
    /// Resident Set Size (RES)
    pub rss: usize,
    /// Resident Shared Size (SHR)
    pub rss_shared: usize,
    /// Resident Anonymous Memory
    pub rss_anon: usize,
    pub start_time: DateTime<Utc>,
}

impl Hash for Process {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.pid.hash(state)
    }
}

impl PartialEq for Process {
    #[inline]
    fn eq(&self, other: &Process) -> bool {
        self.pid.eq(&other.pid)
    }
}

fn get_process_with_stat_inner<P: AsRef<Path>>(
    pid: u32,
    process_path: P,
    process_filter: &ProcessFilter,
) -> Result<Option<(Process, ProcessStat)>, ScannerError> {
    let process_path = process_path.as_ref();

    let mut program_filter_match = true;

    let status = get_process_status(pid)?;

    if let Some(uid_filter) = process_filter.uid_filter {
        if status.real_uid != uid_filter
            && status.effective_uid != uid_filter
            && status.saved_set_uid != uid_filter
            && status.fs_uid != uid_filter
        {
            return Ok(None);
        }
    }

    if let Some(gid_filter) = process_filter.gid_filter {
        if status.real_gid != gid_filter
            && status.effective_gid != gid_filter
            && status.saved_set_gid != gid_filter
            && status.fs_gid != gid_filter
        {
            return Ok(None);
        }
    }

    let cmdline = {
        let mut data = fs::read(process_path.join("cmdline"))?;

        for e in data.iter_mut() {
            if *e == 0 {
                *e = b' ';
            }
        }

        unsafe { String::from_utf8_unchecked(data) }
    };

    if let Some(program_filter) = process_filter.program_filter.as_ref() {
        if !program_filter.is_match(&cmdline) {
            program_filter_match = false;
        }
    }

    let mut stat = get_process_stat(pid)?;

    if !program_filter_match {
        if let Some(program_filter) = process_filter.program_filter.as_ref() {
            if !program_filter.is_match(&stat.comm) {
                return Ok(None);
            }
        }
    }

    let effective_uid = status.effective_uid;
    let effective_gid = status.effective_gid;
    let state = stat.state;
    let ppid = stat.ppid;
    let program = replace(&mut stat.comm, String::new());

    let tty = {
        match stat.tty_nr_major {
            4 => {
                if stat.tty_nr_minor < 64 {
                    Some(format!("tty{}", stat.tty_nr_minor))
                } else {
                    Some(format!("ttyS{}", stat.tty_nr_minor - 64))
                }
            }
            136..=143 => Some(format!("pts/{}", stat.tty_nr_minor)),
            _ => None,
        }
    };

    if let Some(tty_filter) = process_filter.tty_filter.as_ref() {
        match tty.as_ref() {
            Some(tty) => {
                if !tty_filter.is_match(tty) {
                    return Ok(None);
                }
            }
            None => return Ok(None),
        }
    }

    let priority = stat.priority;
    let real_time_priority = if stat.rt_priority > 0 {
        Some(stat.rt_priority)
    } else {
        None
    };
    let nice = stat.nice;
    let threads = stat.num_threads;
    let vsz = stat.vsize;
    let rss = stat.rss;
    let rss_shared = stat.shared;
    let rss_anon = stat.rss_anon;

    let start_time =
        get_btime() + chrono::Duration::from_std(Duration::from_millis(stat.starttime)).unwrap();

    let process = Process {
        pid,
        effective_uid,
        effective_gid,
        state,
        ppid,
        program,
        cmdline,
        tty,
        priority,
        real_time_priority,
        nice,
        threads,
        start_time,
        vsz,
        rss,
        rss_shared,
        rss_anon,
    };

    Ok(Some((process, stat)))
}

/// Get information of a specific process found by ID by reading files in the `/proc/PID` folder.
///
/// ```rust
/// extern crate mprober_lib;
///
/// use mprober_lib::process;
///
/// // let (process, _) = process::get_process_with_stat(1).unwrap();
///
/// // println!("{:#?}", process);
/// ```
#[inline]
pub fn get_process_with_stat(pid: u32) -> Result<(Process, ProcessStat), ScannerError> {
    let process_path = Path::new("/proc").join(pid.to_string());

    get_process_with_stat_inner(pid, process_path, &ProcessFilter::default()).map(|o| o.unwrap())
}

/// Get process information by reading files in the `/proc/PID` folders.
///
/// ```rust
/// extern crate mprober_lib;
///
/// use mprober_lib::process;
///
/// let processes_with_stat =
///     process::get_processes_with_stat(&process::ProcessFilter::default()).unwrap();
///
/// println!("{:#?}", processes_with_stat);
/// ```
pub fn get_processes_with_stat(
    process_filter: &ProcessFilter,
) -> Result<Vec<(Process, ProcessStat)>, ScannerError> {
    let mut processes_with_stats = Vec::new();

    let proc = Path::new("/proc");

    if let Some(pid_filter) = process_filter.pid_filter.as_ref().copied() {
        let mut pid_ppid_map: BTreeMap<u32, u32> = BTreeMap::new();

        for dir_entry in proc.read_dir()? {
            let dir_entry = dir_entry?;

            if let Some(file_name) = dir_entry.file_name().to_str() {
                if let Ok(pid) = file_name.parse::<u32>() {
                    let process_path = dir_entry.path();

                    match get_process_with_stat_inner(pid, process_path, process_filter) {
                        Ok(r) => {
                            if let Some((process, stat)) = r {
                                if pid != pid_filter && process.ppid != pid_filter {
                                    let mut not_related = true;

                                    let mut p_ppid = pid_ppid_map.get(&process.ppid);

                                    while let Some(ppid) = p_ppid.copied() {
                                        if ppid == pid_filter {
                                            not_related = false;

                                            break;
                                        }

                                        p_ppid = pid_ppid_map.get(&ppid);
                                    }

                                    if not_related {
                                        continue;
                                    }
                                }

                                pid_ppid_map.insert(pid, process.ppid);

                                processes_with_stats.push((process, stat));
                            }
                        }
                        Err(err) => {
                            if let ScannerError::IOError(err) = &err {
                                if err.kind() == ErrorKind::NotFound {
                                    continue;
                                }
                            }

                            return Err(err);
                        }
                    }
                }
            }
        }
    } else {
        for dir_entry in proc.read_dir()? {
            let dir_entry = dir_entry?;

            if let Some(file_name) = dir_entry.file_name().to_str() {
                if let Ok(pid) = file_name.parse::<u32>() {
                    let process_path = dir_entry.path();

                    match get_process_with_stat_inner(pid, process_path, process_filter) {
                        Ok(r) => {
                            if let Some((process, stat)) = r {
                                processes_with_stats.push((process, stat));
                            }
                        }
                        Err(err) => {
                            if let ScannerError::IOError(err) = &err {
                                if err.kind() == ErrorKind::NotFound {
                                    continue;
                                }
                            }

                            return Err(err);
                        }
                    }
                }
            }
        }
    }

    Ok(processes_with_stats)
}

/// Get process information by reading files in the `/proc/PID` folders and measure the cpu utilization in percentage within a specific time interval. If the number it returns is `1.0`, means `100%`.
///
/// ```rust
/// extern crate mprober_lib;
///
/// use std::thread::sleep;
/// use std::time::Duration;
///
/// use mprober_lib::process;
///
/// let processes_with_cpu_percentage = process::get_processes_with_cpu_utilization_in_percentage(
///     &process::ProcessFilter::default(),
///     Duration::from_millis(100),
/// )
/// .unwrap();
///
/// for (process, cpu_percentage) in processes_with_cpu_percentage {
///     println!("{}: {:.1}%", process.pid, cpu_percentage * 100.0);
/// }
/// ```
pub fn get_processes_with_cpu_utilization_in_percentage(
    process_filter: &ProcessFilter,
    interval: Duration,
) -> Result<Vec<(Process, f64)>, ScannerError> {
    let pre_average_cpu_stat = get_average_cpu_stat()?;
    let processes_with_stat = get_processes_with_stat(process_filter).unwrap();

    let mut processes_with_cpu_percentage = Vec::with_capacity(processes_with_stat.len());

    sleep(interval);

    let average_cpu_stat = get_average_cpu_stat()?;

    let total_cpu_time_f64 = {
        let pre_average_cpu_time = pre_average_cpu_stat.compute_cpu_time();
        let average_cpu_time = average_cpu_stat.compute_cpu_time();

        (average_cpu_time.get_total_time() - pre_average_cpu_time.get_total_time()) as f64
    };

    for (process, pre_process_stat) in processes_with_stat {
        if let Ok(process_time_stat) = get_process_time_stat(process.pid) {
            let pre_process_time_stat: ProcessTimeStat = pre_process_stat.into();

            let cpu_percentage = pre_process_time_stat
                .compute_cpu_utilization_in_percentage(&process_time_stat, total_cpu_time_f64);

            processes_with_cpu_percentage.push((process, cpu_percentage));
        }
    }

    Ok(processes_with_cpu_percentage)
}