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
//! Routines for parsing /proc/meminfo.

use std::path::PathBuf;

pub fn get_path() -> PathBuf {
    PathBuf::from("/proc/meminfo")
}

/// Some number of Kilobytes
#[derive(Debug, Clone, Copy)]
pub struct KiloBytes(usize);

impl KiloBytes {
    pub fn kilobytes(self) -> usize {
        self.0
    }
}

impl std::str::FromStr for KiloBytes {
    type Err = std::num::ParseIntError;

    // Parse a string like "93 kB"
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut parts = s.split(' ');
        let value: usize = parts.next().expect("expected value").parse().unwrap();
        let as_kb = match parts
            .next()
            .expect("expected units")
            .to_lowercase()
            .as_ref()
        {
            "kb" => value,
            "mb" => 1024 * value,
            "gb" => 1024 * 1024 * value,
            "tb" => 1024 * 1024 * 1024 * value,
            "pb" => 1024 * 1024 * 1024 * 1024 * value,
            other => panic!("unexpected units: `{}`", other),
        };

        Ok(KiloBytes(as_kb))
    }
}

/// Generates a parser for files that contains "maps" of values (e.g. /proc/meminfo).
macro_rules! map_parser {
    (struct $struct:ident; $path_fn:ident($($args:ident : $pty:ty),*); $($lit:literal $field:ident : $ty:ty),+,) => {
        #[derive(Clone, Debug)]
        pub struct $struct {
            $(
                pub $field: $ty
            ),*
        }

        impl $struct {
            pub fn read($($args: $pty),*) -> Result<$struct, ::std::io::Error> {
                use std::io::{BufRead, BufReader};

                const BUFFER_CAP: usize = 4096; // Make all buffers 1 page

                let path = $path_fn($($args),*);
                let file = std::fs::File::open(&path)?;

                // Use a bounded-size buffer to limit disruption of the measurement
                let buf = BufReader::with_capacity(BUFFER_CAP, file);

                // split into different lines.
                let reader = buf.lines();

                // compute the length needed.
                #[allow(clippy::let_and_return)]
                const NUM_ENTRIES: usize = {
                    0
                    $(+ {
                        let $field = 1;
                        $field
                    })+
                };

                let mut map = std::collections::HashMap::with_capacity(NUM_ENTRIES);

                for line in reader {
                    // Split the line at the `:`
                    let line = line.expect("unable to read procfs");
                    let mut split = line.split(":");

                    let name = split.next().expect("Malformed input").trim().to_owned();
                    let value = split.next().expect("Malformed input").trim().to_owned();

                    map.insert(name, value);
                }

                Ok($struct {
                    $(
                        $field: {
                            map.get($lit).map(|s| s.parse::<$ty>().unwrap()).expect("field not found")
                        }
                    ),*
                })
            }
        }
    }
}

map_parser! {
    struct ProcMeminfo;

    get_path();

    "MemTotal"          mem_total: KiloBytes,
    "MemFree"           mem_free: KiloBytes,
    "MemAvailable"      mem_available: KiloBytes,
    "Buffers"           buffers: KiloBytes,
    "Cached"            cached: KiloBytes,
    "SwapCached"        swap_cached: KiloBytes,
    "Active"            active: KiloBytes,
    "Inactive"          inactive: KiloBytes,
    "Active(anon)"      active_anon: KiloBytes,
    "Inactive(anon)"    inactive_anon: KiloBytes,
    "Active(file)"      active_file: KiloBytes,
    "Inactive(file)"    inactive_file: KiloBytes,
    "Unevictable"       unevictable: KiloBytes,
    "Mlocked"           mlocked: KiloBytes,
    "SwapTotal"         swap_total: KiloBytes,
    "SwapFree"          swap_free: KiloBytes,
    "Dirty"             dirty: KiloBytes,
    "Writeback"         writeback: KiloBytes,
    "AnonPages"         anon_pages: KiloBytes,
    "Mapped"            mapped: KiloBytes,
    "Shmem"             shmem: KiloBytes,
    "Slab"              slab: KiloBytes,
    "SReclaimable"      sreclaimable: KiloBytes,
    "SUnreclaim"        sunreclaimable: KiloBytes,
    "KernelStack"       kernel_stack: KiloBytes,
    "PageTables"        page_tables: KiloBytes,
    "NFS_Unstable"      nfs_unstable: KiloBytes,
    "Bounce"            bounce: KiloBytes,
    "WritebackTmp"      writeback_tmp: KiloBytes,
    "CommitLimit"       commit_limit: KiloBytes,
    "Committed_AS"      committed_as: KiloBytes,
    "VmallocTotal"      vmalloc_total: KiloBytes,
    "VmallocUsed"       vmalloc_used: KiloBytes,
    "VmallocChunk"      vmalloc_chunk: KiloBytes,
    "HardwareCorrupted" hardware_corrupted: KiloBytes,
    "AnonHugePages"     anon_huge_pages: KiloBytes,
    "CmaTotal"          cma_total: KiloBytes,
    "CmaFree"           cma_free: KiloBytes,
    "HugePages_Total"   huge_pages_total: usize,
    "HugePages_Free"    huge_pages_free: usize,
    "HugePages_Rsvd"    huge_pages_rsvd: usize,
    "HugePages_Surp"    huge_pages_surp: usize,
    "Hugepagesize"      huge_page_size: KiloBytes,
    "DirectMap4k"       direct_map_4k: KiloBytes,
    "DirectMap2M"       direct_map_2m: KiloBytes,
    "DirectMap1G"       direct_map_1g: KiloBytes,
}