irox_safe_linux/
sys.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2024 IROX Contributors
3//
4
5//!
6//! System Calls from `sys.c`
7
8use crate::errno::Errno;
9use crate::syscall_1;
10
11pub const SYSCALL_SYSINFO: u64 = 99;
12
13/// System-info shuttle structure.
14#[repr(C)]
15#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
16pub struct SysInfo {
17    /// Seconds since boot.
18    pub uptime: i64,
19    /// 1m, 5m, and 15m load averages
20    pub loads: [u64; 3],
21    /// Total usable main memory size in 'mem_unit's
22    pub total_ram: u64,
23    /// Available memory size in 'mem_unit's
24    pub free_ram: u64,
25    /// Amount of shared memory in 'mem_unit's
26    pub shared_ram: u64,
27    /// Memory used by buffers in 'mem_unit's
28    pub buffer_ram: u64,
29    /// Total swap space size
30    pub total_swap: u64,
31    /// Swap space still available
32    pub free_swap: u64,
33    /// Number of current processes
34    pub num_procs: u16,
35    pub _pad: u16,
36    /// Total high memory size
37    pub total_high_mem: u64,
38    /// Available high memory size
39    pub free_high_mem: u64,
40    /// Memory unit size in bytes
41    pub mem_unit: u32,
42}
43
44///
45/// Linux `sysinfo` Syscall, returns most of the values in the 'top' command.
46pub fn sysinfo() -> Result<SysInfo, Errno> {
47    let mut sysinfo = SysInfo::default();
48    let ret = unsafe {
49        let ptr = core::ptr::from_mut(&mut sysinfo);
50        syscall_1!(SYSCALL_SYSINFO, ptr)
51    };
52
53    if ret < 0 {
54        return Err(ret.into());
55    }
56
57    Ok(sysinfo)
58}
59
60#[cfg(test)]
61mod tests {
62    use crate::errno::Errno;
63    use crate::sys::sysinfo;
64
65    #[test]
66    pub fn test_sysinfo() -> Result<(), Errno> {
67        let res = sysinfo()?;
68        println!("{res:#?}");
69        Ok(())
70    }
71}