/rust/registry/src/index.crates.io-6f17d22bba15001f/nix-0.28.0/src/sys/sysinfo.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use libc::{self, SI_LOAD_SHIFT}; |
2 | | use std::time::Duration; |
3 | | use std::{cmp, mem}; |
4 | | |
5 | | use crate::errno::Errno; |
6 | | use crate::Result; |
7 | | |
8 | | /// System info structure returned by `sysinfo`. |
9 | | #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)] |
10 | | #[repr(transparent)] |
11 | | pub 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"))] |
15 | | type mem_blocks_t = u64; |
16 | | #[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))] |
17 | | type mem_blocks_t = libc::c_ulong; |
18 | | |
19 | | impl 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 | 0 | pub fn load_average(&self) -> (f64, f64, f64) { |
25 | 0 | ( |
26 | 0 | self.0.loads[0] as f64 / (1 << SI_LOAD_SHIFT) as f64, |
27 | 0 | self.0.loads[1] as f64 / (1 << SI_LOAD_SHIFT) as f64, |
28 | 0 | self.0.loads[2] as f64 / (1 << SI_LOAD_SHIFT) as f64, |
29 | 0 | ) |
30 | 0 | } |
31 | | |
32 | | /// Returns the time since system boot. |
33 | | // The cast is not unnecessary on all platforms. |
34 | | #[allow(clippy::unnecessary_cast)] |
35 | 0 | pub fn uptime(&self) -> Duration { |
36 | 0 | // Truncate negative values to 0 |
37 | 0 | Duration::from_secs(cmp::max(self.0.uptime, 0) as u64) |
38 | 0 | } |
39 | | |
40 | | /// Current number of processes. |
41 | 0 | pub fn process_count(&self) -> u16 { |
42 | 0 | self.0.procs |
43 | 0 | } |
44 | | |
45 | | /// Returns the amount of swap memory in Bytes. |
46 | 0 | pub fn swap_total(&self) -> u64 { |
47 | 0 | self.scale_mem(self.0.totalswap) |
48 | 0 | } |
49 | | |
50 | | /// Returns the amount of unused swap memory in Bytes. |
51 | 0 | pub fn swap_free(&self) -> u64 { |
52 | 0 | self.scale_mem(self.0.freeswap) |
53 | 0 | } |
54 | | |
55 | | /// Returns the total amount of installed RAM in Bytes. |
56 | 0 | pub fn ram_total(&self) -> u64 { |
57 | 0 | self.scale_mem(self.0.totalram) |
58 | 0 | } |
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 | 0 | pub fn ram_unused(&self) -> u64 { |
66 | 0 | self.scale_mem(self.0.freeram) |
67 | 0 | } |
68 | | |
69 | | // The cast is not unnecessary on all platforms. |
70 | | #[allow(clippy::unnecessary_cast)] |
71 | 0 | fn scale_mem(&self, units: mem_blocks_t) -> u64 { |
72 | 0 | units as u64 * self.0.mem_unit as u64 |
73 | 0 | } |
74 | | } |
75 | | |
76 | | /// Returns system information. |
77 | | /// |
78 | | /// [See `sysinfo(2)`](https://man7.org/linux/man-pages/man2/sysinfo.2.html). |
79 | 0 | pub fn sysinfo() -> Result<SysInfo> { |
80 | 0 | let mut info = mem::MaybeUninit::uninit(); |
81 | 0 | let res = unsafe { libc::sysinfo(info.as_mut_ptr()) }; |
82 | 0 | Errno::result(res).map(|_| unsafe { SysInfo(info.assume_init()) }) |
83 | 0 | } |