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
use std::fs::File;
use std::io::Result;
use libc::pid_t;
use nom::{line_ending, space};
use parsers::{map_result, parse_f32, parse_i32, parse_u32, read_to_end};
#[derive(Debug, Default, PartialEq)]
pub struct LoadAvg {
pub load_avg_1_min: f32,
pub load_avg_5_min: f32,
pub load_avg_10_min: f32,
pub tasks_runnable: u32,
pub tasks_total: u32,
pub last_created_pid: pid_t,
}
named!(parse_loadavg<LoadAvg>,
chain!(load_avg_1_min: parse_f32 ~ space ~
load_avg_5_min: parse_f32 ~ space ~
load_avg_10_min: parse_f32 ~ space ~
tasks_runnable: parse_u32 ~ tag!("/") ~
tasks_total: parse_u32 ~ space ~
last_created_pid: parse_i32 ~ line_ending,
|| { LoadAvg { load_avg_1_min: load_avg_1_min,
load_avg_5_min: load_avg_5_min,
load_avg_10_min: load_avg_10_min,
tasks_runnable: tasks_runnable,
tasks_total: tasks_total,
last_created_pid: last_created_pid } }));
pub fn loadavg() -> Result<LoadAvg> {
let mut buf = [0; 128];
let mut file = try!(File::open("/proc/loadavg"));
map_result(parse_loadavg(try!(read_to_end(&mut file, &mut buf))))
}
#[cfg(test)]
mod tests {
use super::{loadavg, parse_loadavg};
use parsers::tests::unwrap;
#[test]
fn test_loadavg() {
loadavg().unwrap();
}
#[test]
fn test_parse_loadavg() {
let loadavg_text = b"0.46 0.33 0.28 34/625 8435\n";
let loadavg = unwrap(parse_loadavg(loadavg_text));
assert_eq!(0.46, loadavg.load_avg_1_min);
assert_eq!(0.33, loadavg.load_avg_5_min);
assert_eq!(0.28, loadavg.load_avg_10_min);
assert_eq!(34, loadavg.tasks_runnable);
assert_eq!(625, loadavg.tasks_total);
assert_eq!(8435, loadavg.last_created_pid);
}
}
#[cfg(all(test, rustc_nightly))]
mod benches {
extern crate test;
use std::fs::File;
use parsers::read_to_end;
use super::{loadavg, parse_loadavg};
#[bench]
fn bench_loadavg(b: &mut test::Bencher) {
b.iter(|| test::black_box(loadavg()));
}
#[bench]
fn bench_loadavg_parse(b: &mut test::Bencher) {
let mut buf = [0; 128];
let statm = read_to_end(&mut File::open("/proc/loadavg").unwrap(), &mut buf).unwrap();
b.iter(|| test::black_box(parse_loadavg(statm)));
}
}