monitord/
pid1.rs

1//! # pid1 module
2//!
3//! `pid1` uses procfs to get some statistics on Linux's more important
4//! process pid1. These metrics can help ensure newer systemds don't regress
5//! or show stange behavior. E.g. more file descriptors without more units.
6
7use std::sync::Arc;
8
9#[cfg(target_os = "linux")]
10use procfs::process::Process;
11use tokio::sync::RwLock;
12use tracing::error;
13
14use crate::MachineStats;
15
16#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Default, Eq, PartialEq)]
17pub struct Pid1Stats {
18    pub cpu_time_kernel: u64,
19    pub cpu_time_user: u64,
20    pub memory_usage_bytes: u64,
21    pub fd_count: u64,
22    pub tasks: u64,
23}
24
25/// Get procfs info on pid 1 - <https://manpages.debian.org/buster/manpages/procfs.5.en.html>
26#[cfg(target_os = "linux")]
27pub fn get_pid_stats(pid: i32) -> anyhow::Result<Pid1Stats> {
28    let bytes_per_page = procfs::page_size();
29    let ticks_per_second = procfs::ticks_per_second();
30
31    let pid1_proc = Process::new(pid)?;
32    let stat_file = pid1_proc.stat()?;
33
34    // Living with integer rounding
35    Ok(Pid1Stats {
36        cpu_time_kernel: (stat_file.stime) / (ticks_per_second),
37        cpu_time_user: (stat_file.utime) / (ticks_per_second),
38        memory_usage_bytes: (stat_file.rss) * (bytes_per_page),
39        fd_count: pid1_proc.fd_count()?.try_into()?,
40        // Using 0 as impossible number of tasks
41        tasks: pid1_proc
42            .tasks()?
43            .flatten()
44            .collect::<Vec<_>>()
45            .len()
46            .try_into()?,
47    })
48}
49
50#[cfg(not(target_os = "linux"))]
51pub fn get_pid_stats(_pid: i32) -> anyhow::Result<Pid1Stats> {
52    error!("pid1 stats not supported on this OS");
53    Ok(Pid1Stats::default())
54}
55
56/// Async wrapper than can update PID1 stats when passed a locked struct
57pub async fn update_pid1_stats(
58    pid: i32,
59    locked_machine_stats: Arc<RwLock<MachineStats>>,
60) -> anyhow::Result<()> {
61    let pid1_stats = match tokio::task::spawn_blocking(move || get_pid_stats(pid)).await {
62        Ok(p1s) => p1s,
63        Err(err) => return Err(err.into()),
64    };
65
66    let mut machine_stats = locked_machine_stats.write().await;
67    machine_stats.pid1 = match pid1_stats {
68        Ok(s) => Some(s),
69        Err(err) => {
70            error!("Unable to set pid1 stats: {:?}", err);
71            None
72        }
73    };
74
75    Ok(())
76}
77
78#[cfg(target_os = "linux")]
79#[cfg(test)]
80pub mod tests {
81    use super::*;
82
83    #[test]
84    pub fn test_get_stats() -> anyhow::Result<()> {
85        let pid1_stats = get_pid_stats(1)?;
86        assert!(pid1_stats.tasks > 0);
87        Ok(())
88    }
89}