Skip to content

Commit e0577cc

Browse files
Merge #922
922: Add a sysinfo wrapper r=asomers a=jonas-schievink Closes #505 Returned values were also inspected manually to be correct. Co-authored-by: Jonas Schievink <[email protected]>
2 parents c62f1f8 + 5091847 commit e0577cc

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
## [Unreleased]
77

88
### Added
9+
- Added a `sysinfo` wrapper.
10+
([#922](https://github.com/nix-rust/nix/pull/922))
911

1012
### Changed
1113

src/sys/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ pub mod statfs;
7070

7171
pub mod statvfs;
7272

73+
#[cfg(any(target_os = "android", target_os = "linux"))]
74+
pub mod sysinfo;
75+
7376
pub mod termios;
7477

7578
pub mod time;

src/sys/sysinfo.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use libc::{self, SI_LOAD_SHIFT};
2+
use std::{cmp, mem};
3+
use std::time::Duration;
4+
5+
use Result;
6+
use errno::Errno;
7+
8+
/// System info structure returned by `sysinfo`.
9+
#[derive(Copy, Clone)]
10+
#[allow(missing_debug_implementations)] // libc::sysinfo doesn't impl Debug
11+
pub struct SysInfo(libc::sysinfo);
12+
13+
impl SysInfo {
14+
/// Returns the load average tuple.
15+
///
16+
/// The returned values represent the load average over time intervals of
17+
/// 1, 5, and 15 minutes, respectively.
18+
pub fn load_average(&self) -> (f64, f64, f64) {
19+
(
20+
self.0.loads[0] as f64 / (1 << SI_LOAD_SHIFT) as f64,
21+
self.0.loads[1] as f64 / (1 << SI_LOAD_SHIFT) as f64,
22+
self.0.loads[2] as f64 / (1 << SI_LOAD_SHIFT) as f64,
23+
)
24+
}
25+
26+
/// Returns the time since system boot.
27+
pub fn uptime(&self) -> Duration {
28+
// Truncate negative values to 0
29+
Duration::from_secs(cmp::max(self.0.uptime, 0) as u64)
30+
}
31+
32+
/// Current number of processes.
33+
pub fn process_count(&self) -> u16 {
34+
self.0.procs
35+
}
36+
37+
/// Returns the amount of swap memory in Bytes.
38+
pub fn swap_total(&self) -> u64 {
39+
self.scale_mem(self.0.totalswap)
40+
}
41+
42+
/// Returns the amount of unused swap memory in Bytes.
43+
pub fn swap_free(&self) -> u64 {
44+
self.scale_mem(self.0.freeswap)
45+
}
46+
47+
/// Returns the total amount of installed RAM in Bytes.
48+
pub fn ram_total(&self) -> u64 {
49+
self.scale_mem(self.0.totalram)
50+
}
51+
52+
/// Returns the amount of completely unused RAM in Bytes.
53+
///
54+
/// "Unused" in this context means that the RAM in neither actively used by
55+
/// programs, nor by the operating system as disk cache or buffer. It is
56+
/// "wasted" RAM since it currently serves no purpose.
57+
pub fn ram_unused(&self) -> u64 {
58+
self.scale_mem(self.0.freeram)
59+
}
60+
61+
fn scale_mem(&self, units: libc::c_ulong) -> u64 {
62+
units as u64 * self.0.mem_unit as u64
63+
}
64+
}
65+
66+
/// Returns system information.
67+
///
68+
/// [See `sysinfo(2)`](http://man7.org/linux/man-pages/man2/sysinfo.2.html).
69+
pub fn sysinfo() -> Result<SysInfo> {
70+
let mut info: libc::sysinfo = unsafe { mem::uninitialized() };
71+
let res = unsafe { libc::sysinfo(&mut info) };
72+
Errno::result(res).map(|_| SysInfo(info))
73+
}

test/sys/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ mod test_signalfd;
1616
mod test_socket;
1717
mod test_sockopt;
1818
mod test_select;
19+
#[cfg(any(target_os = "android", target_os = "linux"))]
20+
mod test_sysinfo;
1921
mod test_termios;
2022
mod test_ioctl;
2123
mod test_wait;

test/sys/test_sysinfo.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use nix::sys::sysinfo::*;
2+
3+
#[test]
4+
fn sysinfo_works() {
5+
let info = sysinfo().unwrap();
6+
7+
let (l1, l5, l15) = info.load_average();
8+
assert!(l1 >= 0.0);
9+
assert!(l5 >= 0.0);
10+
assert!(l15 >= 0.0);
11+
12+
info.uptime(); // just test Duration construction
13+
14+
assert!(info.swap_free() <= info.swap_total(),
15+
"more swap available than installed (free: {}, total: {})",
16+
info.swap_free(),
17+
info.swap_total());
18+
}

0 commit comments

Comments
 (0)