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
use libc::{self, SI_LOAD_SHIFT};
use std::{cmp, mem};
use std::time::Duration;
use Result;
use errno::Errno;
#[derive(Copy, Clone)]
#[allow(missing_debug_implementations)]
pub struct SysInfo(libc::sysinfo);
impl SysInfo {
pub fn load_average(&self) -> (f64, f64, f64) {
(
self.0.loads[0] as f64 / (1 << SI_LOAD_SHIFT) as f64,
self.0.loads[1] as f64 / (1 << SI_LOAD_SHIFT) as f64,
self.0.loads[2] as f64 / (1 << SI_LOAD_SHIFT) as f64,
)
}
pub fn uptime(&self) -> Duration {
Duration::from_secs(cmp::max(self.0.uptime, 0) as u64)
}
pub fn process_count(&self) -> u16 {
self.0.procs
}
pub fn swap_total(&self) -> u64 {
self.scale_mem(self.0.totalswap)
}
pub fn swap_free(&self) -> u64 {
self.scale_mem(self.0.freeswap)
}
pub fn ram_total(&self) -> u64 {
self.scale_mem(self.0.totalram)
}
pub fn ram_unused(&self) -> u64 {
self.scale_mem(self.0.freeram)
}
fn scale_mem(&self, units: libc::c_ulong) -> u64 {
units as u64 * self.0.mem_unit as u64
}
}
pub fn sysinfo() -> Result<SysInfo> {
let mut info: libc::sysinfo = unsafe { mem::uninitialized() };
let res = unsafe { libc::sysinfo(&mut info) };
Errno::result(res).map(|_| SysInfo(info))
}