Skip to main content

procinfo/
loadavg.rs

1//! System load and task statistics from `/proc/loadavg`.
2
3use std::fs::File;
4use std::io::Result;
5
6use libc::pid_t;
7use nom::{line_ending, space};
8
9use parsers::{map_result, parse_f32, parse_i32, parse_u32, read_to_end};
10
11/// System load and task statistics.
12///
13/// The load average is the ratio of runnable and uninterruptible (waiting on IO) tasks to total
14/// tasks on the system.
15///
16/// See `man 5 proc` and `Linux/fs/proc/loadavg.c`.
17#[derive(Debug, Default, PartialEq)]
18pub struct LoadAvg {
19    /// Load average over the last minute.
20    pub load_avg_1_min: f32,
21    /// Load average of the last 5 minutes.
22    pub load_avg_5_min: f32,
23    /// Load average of the last 10 minutes
24    pub load_avg_10_min: f32,
25    /// the number of currently runnable kernel scheduling entities (processes, threads).
26    pub tasks_runnable: u32,
27    /// the number of kernel scheduling entities that currently exist on the system.
28    pub tasks_total: u32,
29    /// the PID of the process that was most recently created on the system.
30    pub last_created_pid: pid_t,
31}
32
33/// Parses the loadavg file format.
34named!(parse_loadavg<LoadAvg>,
35       chain!(load_avg_1_min:   parse_f32   ~ space ~
36              load_avg_5_min:   parse_f32   ~ space ~
37              load_avg_10_min:  parse_f32   ~ space ~
38              tasks_runnable:   parse_u32   ~ tag!("/") ~
39              tasks_total:      parse_u32   ~ space ~
40              last_created_pid: parse_i32   ~ line_ending,
41              || { LoadAvg { load_avg_1_min: load_avg_1_min,
42                             load_avg_5_min: load_avg_5_min,
43                             load_avg_10_min: load_avg_10_min,
44                             tasks_runnable: tasks_runnable,
45                             tasks_total: tasks_total,
46                             last_created_pid: last_created_pid } }));
47
48/// Returns the system load average.
49pub fn loadavg() -> Result<LoadAvg> {
50    let mut buf = [0; 128]; // A typical loadavg file is about 32 bytes.
51    let mut file = try!(File::open("/proc/loadavg"));
52    map_result(parse_loadavg(try!(read_to_end(&mut file, &mut buf))))
53}
54
55#[cfg(test)]
56mod tests {
57    use super::{loadavg, parse_loadavg};
58    use parsers::tests::unwrap;
59
60    /// Test that the system loadavg file can be parsed.
61    #[test]
62    fn test_loadavg() {
63        loadavg().unwrap();
64    }
65
66    #[test]
67    fn test_parse_loadavg() {
68        let loadavg_text = b"0.46 0.33 0.28 34/625 8435\n";
69        let loadavg = unwrap(parse_loadavg(loadavg_text));
70        assert_eq!(0.46, loadavg.load_avg_1_min);
71        assert_eq!(0.33, loadavg.load_avg_5_min);
72        assert_eq!(0.28, loadavg.load_avg_10_min);
73        assert_eq!(34, loadavg.tasks_runnable);
74        assert_eq!(625, loadavg.tasks_total);
75        assert_eq!(8435, loadavg.last_created_pid);
76    }
77}
78
79#[cfg(all(test, rustc_nightly))]
80mod benches {
81    extern crate test;
82
83    use std::fs::File;
84
85    use parsers::read_to_end;
86    use super::{loadavg, parse_loadavg};
87
88    #[bench]
89    fn bench_loadavg(b: &mut test::Bencher) {
90        b.iter(|| test::black_box(loadavg()));
91    }
92
93    #[bench]
94    fn bench_loadavg_parse(b: &mut test::Bencher) {
95        let mut buf = [0; 128];
96        let statm = read_to_end(&mut File::open("/proc/loadavg").unwrap(), &mut buf).unwrap();
97        b.iter(|| test::black_box(parse_loadavg(statm)));
98    }
99}