Skip to main content

rigtop_core/linux/
mod.rs

1//! Linux-specific readers for `/sys` and `/proc`.
2//!
3//! These cover what `sysinfo` does not expose: hwmon sensors, RAPL energy
4//! counters, per-device disk I/O, btrfs and mdadm state.
5//!
6//! Every reader takes its root directories from [`SysRoots`] instead of
7//! hard-coding `/sys` and `/proc`, so tests can point them at a fixture tree.
8
9pub mod btrfs;
10pub mod diskstats;
11pub mod hwmon;
12pub mod mdstat;
13pub mod meminfo;
14pub mod mounts;
15pub mod net;
16pub mod rapl;
17
18use std::fs;
19use std::path::{Path, PathBuf};
20
21/// Root directories of the kernel pseudo filesystems.
22#[derive(Debug, Clone)]
23pub struct SysRoots {
24    /// Normally `/sys`.
25    pub sys: PathBuf,
26    /// Normally `/proc`.
27    pub proc: PathBuf,
28}
29
30impl Default for SysRoots {
31    fn default() -> Self {
32        Self {
33            sys: PathBuf::from("/sys"),
34            proc: PathBuf::from("/proc"),
35        }
36    }
37}
38
39/// Reads a sysfs attribute and trims the trailing newline.
40///
41/// Returns `None` on any error: sysfs attributes may disappear at any time
42/// (hot-unplug) or be unreadable without root, and callers treat both as
43/// "not available".
44pub(crate) fn read_trimmed(path: &Path) -> Option<String> {
45    fs::read_to_string(path).ok().map(|s| s.trim().to_owned())
46}
47
48/// Reads a sysfs attribute holding a single unsigned integer.
49pub(crate) fn read_u64(path: &Path) -> Option<u64> {
50    read_trimmed(path)?.parse().ok()
51}
52
53/// Reads a sysfs attribute holding a single signed integer.
54pub(crate) fn read_i64(path: &Path) -> Option<i64> {
55    read_trimmed(path)?.parse().ok()
56}
57
58/// Returns the file names in `dir`, sorted, or an empty list if it cannot be read.
59pub(crate) fn sorted_entries(dir: &Path) -> Vec<String> {
60    let mut names: Vec<String> = match fs::read_dir(dir) {
61        Ok(entries) => entries
62            .filter_map(|e| e.ok())
63            .filter_map(|e| e.file_name().into_string().ok())
64            .collect(),
65        Err(_) => Vec::new(),
66    };
67    names.sort_by(|a, b| natural_cmp(a, b));
68    names
69}
70
71/// Compares strings so that embedded numbers sort numerically
72/// (`hwmon2` < `hwmon10`, `fan2` < `fan10`).
73pub(crate) fn natural_cmp(a: &str, b: &str) -> std::cmp::Ordering {
74    // Split into (non-digit prefix, trailing number). Kernel object names
75    // ("hwmon10", "nvme2", "md127") only carry a number at the end.
76    fn split(s: &str) -> (&str, Option<u64>) {
77        let digits_start = s
78            .char_indices()
79            .rev()
80            .take_while(|(_, c)| c.is_ascii_digit())
81            .last()
82            .map(|(i, _)| i)
83            .unwrap_or(s.len());
84        let (prefix, digits) = s.split_at(digits_start);
85        (prefix, digits.parse().ok())
86    }
87    let (pa, na) = split(a);
88    let (pb, nb) = split(b);
89    pa.cmp(pb).then(na.cmp(&nb)).then_with(|| a.cmp(b))
90}
91
92#[cfg(test)]
93pub(crate) mod testutil {
94    //! Helpers to build fake sysfs/procfs trees in a temporary directory.
95
96    use std::fs;
97    use std::path::Path;
98
99    /// Writes `content` to `root/rel`, creating parent directories.
100    pub fn write(root: &Path, rel: &str, content: &str) {
101        let path = root.join(rel);
102        fs::create_dir_all(path.parent().unwrap()).unwrap();
103        fs::write(path, content).unwrap();
104    }
105
106    /// Creates a symlink at `root/rel` pointing to `target` (absolute or relative).
107    pub fn symlink(root: &Path, rel: &str, target: &Path) {
108        let path = root.join(rel);
109        fs::create_dir_all(path.parent().unwrap()).unwrap();
110        std::os::unix::fs::symlink(target, path).unwrap();
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::natural_cmp;
117
118    #[test]
119    fn natural_order_sorts_numbers_numerically() {
120        let mut names = vec!["hwmon10", "hwmon2", "hwmon1", "fan"];
121        names.sort_by(|a, b| natural_cmp(a, b));
122        assert_eq!(names, vec!["fan", "hwmon1", "hwmon2", "hwmon10"]);
123    }
124}