mirror of
https://github.com/NotAShelf/microfetch.git
synced 2026-04-12 12:57:41 +00:00
uptime: construct a raw buffer for the sysinfo syscall; drop libc::sysinfo
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I63887d9f73e6fed670e497086d30d2a46a6a6964
This commit is contained in:
parent
99f7be5aac
commit
17cd7530d6
1 changed files with 31 additions and 4 deletions
|
|
@ -17,14 +17,41 @@ fn itoa(mut n: u64, buf: &mut [u8]) -> &str {
|
||||||
unsafe { std::str::from_utf8_unchecked(&buf[i..]) }
|
unsafe { std::str::from_utf8_unchecked(&buf[i..]) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Direct `sysinfo` syscall using inline assembly
|
/// Raw buffer for the `sysinfo(2)` syscall.
|
||||||
|
///
|
||||||
|
/// In the Linux ABI `uptime` is a `long` at offset 0. The remaining fields are
|
||||||
|
/// not needed, but are declared to give the struct its correct size (112 bytes
|
||||||
|
/// on 64-bit Linux).
|
||||||
|
///
|
||||||
|
/// The layout matches the kernel's `struct sysinfo` *exactly*:
|
||||||
|
/// `mem_unit` ends at offset 108, then 4 bytes of implicit padding to 112.
|
||||||
|
#[repr(C)]
|
||||||
|
struct SysInfo {
|
||||||
|
uptime: i64,
|
||||||
|
loads: [u64; 3],
|
||||||
|
totalram: u64,
|
||||||
|
freeram: u64,
|
||||||
|
sharedram: u64,
|
||||||
|
bufferram: u64,
|
||||||
|
totalswap: u64,
|
||||||
|
freeswap: u64,
|
||||||
|
procs: u16,
|
||||||
|
_pad: u16,
|
||||||
|
_pad2: u32, // alignment padding to reach 8-byte boundary for totalhigh
|
||||||
|
totalhigh: u64,
|
||||||
|
freehigh: u64,
|
||||||
|
mem_unit: u32,
|
||||||
|
// 4 bytes implicit trailing padding to reach 112 bytes total; no field
|
||||||
|
// needed
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Direct `sysinfo(2)` syscall using inline assembly
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
///
|
///
|
||||||
/// This function uses inline assembly to make a direct syscall.
|
|
||||||
/// The caller must ensure the sysinfo pointer is valid.
|
/// The caller must ensure the sysinfo pointer is valid.
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn sys_sysinfo(info: *mut libc::sysinfo) -> i64 {
|
unsafe fn sys_sysinfo(info: *mut SysInfo) -> i64 {
|
||||||
#[cfg(target_arch = "x86_64")]
|
#[cfg(target_arch = "x86_64")]
|
||||||
{
|
{
|
||||||
let ret: i64;
|
let ret: i64;
|
||||||
|
|
@ -59,7 +86,7 @@ unsafe fn sys_sysinfo(info: *mut libc::sysinfo) -> i64 {
|
||||||
|
|
||||||
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
|
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
|
||||||
{
|
{
|
||||||
unsafe { libc::sysinfo(info) as i64 }
|
compile_error!("Unsupported architecture for inline assembly syscalls");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue