ctb_formats_pan/time/
tickcount.rs1#![allow(
29 clippy::module_name_repetitions,
30 reason = "idiomatic module structure names"
31)]
32
33#[allow(
34 unused_imports,
35 clippy::wildcard_imports,
36 reason = "Standard workspace module prelude"
37)]
38use crate::utilities::*;
39
40const TICKS_PER_SECOND: u128 = 60;
41const NANOS_PER_SECOND: u128 = 1_000_000_000;
42
43pub fn tickcount() -> Result<i64> {
45 let nanos = nanos_since_boot()?;
46 let ticks = nanos
47 .checked_mul(TICKS_PER_SECOND)
48 .context("tickcount overflow")?
49 / NANOS_PER_SECOND;
50
51 i64::try_from(ticks).context("tickcount did not fit into i64")
52}
53
54#[allow(unsafe_code)]
55#[cfg(target_os = "windows")]
56fn nanos_since_boot() -> Result<u128> {
57 #[link(name = "kernel32")]
58 unsafe extern "system" {
59 fn GetTickCount64() -> u64;
60 }
61
62 let ms = unsafe { GetTickCount64() };
63 let ms_u = u128::from(ms);
64 ms_u.checked_mul(1_000_000u128).context("nanos overflow")
65}
66
67#[cfg(all(unix, target_os = "macos"))]
68fn nanos_since_boot() -> Result<u128> {
69 #[repr(C)]
70 struct MachTimebaseInfo {
71 numer: u32,
72 denom: u32,
73 }
74
75 #[expect(
76 unsafe_code,
77 reason = "Mach monotonic clock FFI requires an unsafe extern block"
78 )]
79 unsafe extern "C" {
80 fn mach_absolute_time() -> u64;
81 fn mach_timebase_info(info: *mut MachTimebaseInfo) -> i32;
82 }
83
84 #[expect(unsafe_code, reason = "calls Darwin mach_absolute_time")]
86 let t = u128::from(unsafe { mach_absolute_time() });
87
88 let mut info = MachTimebaseInfo { numer: 0, denom: 0 };
89 #[expect(unsafe_code, reason = "calls Darwin mach_timebase_info")]
91 let rc = unsafe { mach_timebase_info(&raw mut info) };
92 if rc != 0 {
93 bail!("mach_timebase_info failed with code {rc}");
94 }
95
96 let numer = u128::from(info.numer);
97 let denom = u128::from(info.denom);
98 let scaled = t.checked_mul(numer).context("nanos overflow")?;
99 scaled
100 .checked_div(denom)
101 .context("mach_timebase_info returned denom=0")
102}
103
104#[cfg(all(unix, not(target_os = "macos")))]
105fn nanos_since_boot() -> Result<u128> {
106 #[cfg(any(target_os = "linux", target_os = "android"))]
107 use nix::libc::CLOCK_BOOTTIME;
108 #[cfg(not(any(target_os = "linux", target_os = "android")))]
109 use nix::libc::CLOCK_MONOTONIC;
110 use nix::time::clock_gettime;
111
112 #[repr(C)]
113 struct Timespec {
114 tv_sec: i64,
115 tv_nsec: i64,
116 }
117
118 #[cfg(any(target_os = "linux", target_os = "android"))]
119 const CLOCK_ID: i32 = CLOCK_BOOTTIME;
120
121 #[cfg(not(any(target_os = "linux", target_os = "android")))]
122 const CLOCK_ID: i32 = CLOCK_MONOTONIC;
123
124 let _ts = Timespec {
125 tv_sec: 0,
126 tv_nsec: 0,
127 };
128 let ts = clock_gettime(CLOCK_ID.into())?;
129
130 let sec = u128::try_from(ts.tv_sec()).context("tv_sec was negative")?;
131 let nsec = u128::try_from(ts.tv_nsec()).context("tv_nsec was negative")?;
132 sec.checked_mul(NANOS_PER_SECOND)
133 .context("nanos overflow")?
134 .checked_add(nsec)
135 .context("nanos overflow")
136}
137
138#[cfg(test)]
139#[allow(
140 clippy::panic,
141 clippy::expect_used,
142 clippy::unwrap_used,
143 clippy::unwrap_in_result,
144 clippy::panic_in_result_fn,
145 clippy::indexing_slicing,
146 clippy::arithmetic_side_effects,
147 reason = "Standard repository test boilerplate"
148)]
149mod tests {
150 use ctb_utilities::anyhow::ensure;
151
152 use super::*;
153
154 #[crate::ctb_test]
155 fn tickcount_is_monotonic() -> Result<()> {
156 let a = tickcount()?;
157 let b = tickcount()?;
158 ensure!(a >= 0, "got {a}");
159 ensure!(b >= a, "tickcount went backwards: {a} -> {b}");
160 Ok(())
161 }
162}