feat(threads): measure thread CPU time on Windows from cycle counts

thread_cpu() returned None on Windows. Implement it from thread cycle counts,
inside horizon-threads so horizon-client keeps forbid(unsafe_code).

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
DanielGodriNeto
2026-10-26 19:17:58 -03:00
parent f12e457a25
commit 9ae37fcb50
3 changed files with 41 additions and 3 deletions
+3 -2
View File
@@ -52,8 +52,9 @@ pub fn thread_cpu() -> Option<Duration> {
Some(Duration::new(t.tv_sec as u64, t.tv_nsec as u32))
}
#[cfg(not(unix))]
None
horizon_threads::thread_cpu()
}
/// The frame's spans, summed by name, slowest first; clears them.
pub fn take() -> Vec<Span> {
let spans = SPANS.with(|s| std::mem::take(&mut *s.borrow_mut()));
@@ -135,7 +136,7 @@ mod tests {
/// The thread's CPU clock moves with work done on it, not with time
/// spent asleep.
#[cfg(unix)]
#[cfg(any(unix, all(windows, target_arch = "x86_64")))]
#[test]
fn thread_cpu_counts_work_not_sleep() {
let start = super::thread_cpu().unwrap();
+1 -1
View File
@@ -10,7 +10,7 @@ description = "Scheduling hints for the game's threads: the OS calls the crates
libc = "0.2"
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.61", features = ["Win32_System_Threading"] }
windows-sys = { version = "0.61", features = ["Win32_System_Threading", "Win32_System_WindowsProgramming"] }
[lints]
workspace = true
+37
View File
@@ -41,6 +41,43 @@ fn favour() -> bool {
false
}
/// The CPU time the calling thread has used, where only an OS call gives it
/// precisely. Windows counts it exactly only in cycles of the time-stamp
/// counter (`QueryThreadCycleTime`; `GetThreadTimes` moves in scheduler ticks
/// of ~15.6 ms), so the counter's rate is measured once against the wall
/// clock and cycles convert to time. `None` elsewhere: unix callers read
/// `CLOCK_THREAD_CPUTIME_ID` themselves.
#[cfg(all(windows, target_arch = "x86_64"))]
pub fn thread_cpu() -> Option<std::time::Duration> {
use std::{
sync::OnceLock,
time::{Duration, Instant},
};
use windows_sys::Win32::System::{
Threading::GetCurrentThread, WindowsProgramming::QueryThreadCycleTime,
};
// Time-stamp counter cycles per nanosecond.
static RATE: OnceLock<f64> = OnceLock::new();
let rate = *RATE.get_or_init(|| {
// SAFETY: RDTSC has no preconditions on x86_64.
let tsc = || unsafe { core::arch::x86_64::_rdtsc() };
let (wall, start) = (Instant::now(), tsc());
std::thread::sleep(Duration::from_millis(20));
(tsc() - start) as f64 / wall.elapsed().as_nanos() as f64
});
let mut cycles = 0;
// SAFETY: GetCurrentThread's pseudo handle needs no closing; a valid out pointer.
if unsafe { QueryThreadCycleTime(GetCurrentThread(), &mut cycles) } == 0 {
return None;
}
Some(Duration::from_nanos((cycles as f64 / rate) as u64))
}
#[cfg(not(all(windows, target_arch = "x86_64")))]
pub fn thread_cpu() -> Option<std::time::Duration> {
None
}
/// Set once as the game starts, before its helper threads do: they then go
/// behind the server thread and the render thread (`behind_server`).
static HELPERS_BEHIND: AtomicBool = AtomicBool::new(false);