summaryrefslogtreecommitdiff
path: root/src/sys
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-07-05 13:42:07 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-07-05 13:42:07 +0000
commite0577cc89059267e996027f45578f09f34b7347b (patch)
tree43a232c0f5091d0f7792f5bf8cbe1c743f565db9 /src/sys
parentc62f1f82f61295f9608b675570e56892b1d1eeb9 (diff)
parent50918476f31bb07bfd9eddb6939f3687c5302ff6 (diff)
downloadnix-e0577cc89059267e996027f45578f09f34b7347b.zip
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 <jonasschievink@gmail.com>
Diffstat (limited to 'src/sys')
-rw-r--r--src/sys/mod.rs3
-rw-r--r--src/sys/sysinfo.rs73
2 files changed, 76 insertions, 0 deletions
diff --git a/src/sys/mod.rs b/src/sys/mod.rs
index e75e9d88..e6c7880c 100644
--- a/src/sys/mod.rs
+++ b/src/sys/mod.rs
@@ -70,6 +70,9 @@ pub mod statfs;
pub mod statvfs;
+#[cfg(any(target_os = "android", target_os = "linux"))]
+pub mod sysinfo;
+
pub mod termios;
pub mod time;
diff --git a/src/sys/sysinfo.rs b/src/sys/sysinfo.rs
new file mode 100644
index 00000000..98ef7bd5
--- /dev/null
+++ b/src/sys/sysinfo.rs
@@ -0,0 +1,73 @@
+use libc::{self, SI_LOAD_SHIFT};
+use std::{cmp, mem};
+use std::time::Duration;
+
+use Result;
+use errno::Errno;
+
+/// System info structure returned by `sysinfo`.
+#[derive(Copy, Clone)]
+#[allow(missing_debug_implementations)] // libc::sysinfo doesn't impl Debug
+pub struct SysInfo(libc::sysinfo);
+
+impl SysInfo {
+ /// Returns the load average tuple.
+ ///
+ /// The returned values represent the load average over time intervals of
+ /// 1, 5, and 15 minutes, respectively.
+ 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,
+ )
+ }
+
+ /// Returns the time since system boot.
+ pub fn uptime(&self) -> Duration {
+ // Truncate negative values to 0
+ Duration::from_secs(cmp::max(self.0.uptime, 0) as u64)
+ }
+
+ /// Current number of processes.
+ pub fn process_count(&self) -> u16 {
+ self.0.procs
+ }
+
+ /// Returns the amount of swap memory in Bytes.
+ pub fn swap_total(&self) -> u64 {
+ self.scale_mem(self.0.totalswap)
+ }
+
+ /// Returns the amount of unused swap memory in Bytes.
+ pub fn swap_free(&self) -> u64 {
+ self.scale_mem(self.0.freeswap)
+ }
+
+ /// Returns the total amount of installed RAM in Bytes.
+ pub fn ram_total(&self) -> u64 {
+ self.scale_mem(self.0.totalram)
+ }
+
+ /// Returns the amount of completely unused RAM in Bytes.
+ ///
+ /// "Unused" in this context means that the RAM in neither actively used by
+ /// programs, nor by the operating system as disk cache or buffer. It is
+ /// "wasted" RAM since it currently serves no purpose.
+ 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
+ }
+}
+
+/// Returns system information.
+///
+/// [See `sysinfo(2)`](http://man7.org/linux/man-pages/man2/sysinfo.2.html).
+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))
+}