diff --git a/crates/horizon-client/src/spans.rs b/crates/horizon-client/src/spans.rs index ce69be8ce..33c4bb933 100644 --- a/crates/horizon-client/src/spans.rs +++ b/crates/horizon-client/src/spans.rs @@ -52,8 +52,9 @@ pub fn thread_cpu() -> Option { 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 { 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(); diff --git a/crates/horizon-threads/Cargo.toml b/crates/horizon-threads/Cargo.toml index d43b3e4fc..67b3c20da 100644 --- a/crates/horizon-threads/Cargo.toml +++ b/crates/horizon-threads/Cargo.toml @@ -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 diff --git a/crates/horizon-threads/src/lib.rs b/crates/horizon-threads/src/lib.rs index 917c3b502..85957b7e1 100644 --- a/crates/horizon-threads/src/lib.rs +++ b/crates/horizon-threads/src/lib.rs @@ -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 { + 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 = 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 { + 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);