1use libc::{self, SI_LOAD_SHIFT};
2use std::time::Duration;
3use std::{cmp, mem};
4
5use crate::errno::Errno;
6use crate::Result;
7
8/// System info structure returned by `sysinfo`.
9#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
10#[repr(transparent)]
11pub struct SysInfo(libc::sysinfo);
12
13// The fields are c_ulong on 32-bit linux, u64 on 64-bit linux; x32's ulong is u32
14#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
15type mem_blocks_t = u64;
16#[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))]
17type mem_blocks_t = libc::c_ulong;
18
19impl SysInfo {
20 /// Returns the load average tuple.
21 ///
22 /// The returned values represent the load average over time intervals of
23 /// 1, 5, and 15 minutes, respectively.
24 pub fn load_average(&self) -> (f64, f64, f64) {
25 (
26 self.0.loads[0] as f64 / (1 << SI_LOAD_SHIFT) as f64,
27 self.0.loads[1] as f64 / (1 << SI_LOAD_SHIFT) as f64,
28 self.0.loads[2] as f64 / (1 << SI_LOAD_SHIFT) as f64,
29 )
30 }
31
32 /// Returns the time since system boot.
33 // The cast is not unnecessary on all platforms.
34 #[allow(clippy::unnecessary_cast)]
35 pub fn uptime(&self) -> Duration {
36 // Truncate negative values to 0
37 Duration::from_secs(cmp::max(self.0.uptime, 0) as u64)
38 }
39
40 /// Current number of processes.
41 pub fn process_count(&self) -> u16 {
42 self.0.procs
43 }
44
45 /// Returns the amount of swap memory in Bytes.
46 pub fn swap_total(&self) -> u64 {
47 self.scale_mem(self.0.totalswap)
48 }
49
50 /// Returns the amount of unused swap memory in Bytes.
51 pub fn swap_free(&self) -> u64 {
52 self.scale_mem(self.0.freeswap)
53 }
54
55 /// Returns the total amount of installed RAM in Bytes.
56 pub fn ram_total(&self) -> u64 {
57 self.scale_mem(self.0.totalram)
58 }
59
60 /// Returns the amount of completely unused RAM in Bytes.
61 ///
62 /// "Unused" in this context means that the RAM in neither actively used by
63 /// programs, nor by the operating system as disk cache or buffer. It is
64 /// "wasted" RAM since it currently serves no purpose.
65 pub fn ram_unused(&self) -> u64 {
66 self.scale_mem(self.0.freeram)
67 }
68
69 // The cast is not unnecessary on all platforms.
70 #[allow(clippy::unnecessary_cast)]
71 fn scale_mem(&self, units: mem_blocks_t) -> u64 {
72 units as u64 * self.0.mem_unit as u64
73 }
74}
75
76/// Returns system information.
77///
78/// [See `sysinfo(2)`](https://man7.org/linux/man-pages/man2/sysinfo.2.html).
79pub fn sysinfo() -> Result<SysInfo> {
80 let mut info: MaybeUninit = mem::MaybeUninit::uninit();
81 let res: i32 = unsafe { libc::sysinfo(info:info.as_mut_ptr()) };
82 Errno::result(res).map(|_| unsafe { SysInfo(info.assume_init()) })
83}
84