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`.
15pub fn boottime_ns() -> u64 {
16 let mut ts = libc::timespec {
17 tv_sec: 0,
18 tv_nsec: 0,
19 };
20 let ret = unsafe { libc::clock_gettime(libc::CLOCK_BOOTTIME, &mut ts) };
21 assert!(ret == 0, "clock_gettime(CLOCK_BOOTTIME) failed");
22 (ts.tv_sec as u64) * 1_000_000_000 + (ts.tv_nsec as u64)
23}