microsandbox_agentd/clock.rs
1//! Guest clock utilities for boot timing measurement.
2
3//--------------------------------------------------------------------------------------------------
4// Functions
5//--------------------------------------------------------------------------------------------------
6
7/// Returns the current `CLOCK_BOOTTIME` value in nanoseconds.
8///
9/// `CLOCK_BOOTTIME` counts from kernel boot and includes time spent in suspend,
10/// making it ideal for measuring total time since the VM kernel started.
11///
12/// # Panics
13///
14/// Panics if `clock_gettime` fails, which should never happen for `CLOCK_BOOTTIME`.
15#[cfg(target_os = "linux")]
16pub fn boottime_ns() -> u64 {
17 let mut ts = libc::timespec {
18 tv_sec: 0,
19 tv_nsec: 0,
20 };
21 let ret = unsafe { libc::clock_gettime(libc::CLOCK_BOOTTIME, &mut ts) };
22 assert!(ret == 0, "clock_gettime(CLOCK_BOOTTIME) failed");
23 (ts.tv_sec as u64) * 1_000_000_000 + (ts.tv_nsec as u64)
24}
25
26/// No-op on non-Linux platforms (for macOS IDE/development ergonomics only).
27#[cfg(not(target_os = "linux"))]
28pub fn boottime_ns() -> u64 {
29 0
30}