Merge pull request #2610 from DanielGodriNeto/turbo/pr-thread-cpu

🤖 Agent: verified by Ludovic's AI agent (contrib gate 20): Windows thread CPU time: checked and clippy-clean for x86_64-pc-windows-msvc from the Mac mini; not run on Windows. It passed in this wave's union with main on the Mac mini: the selected parity harnesses (332/332, then 122/122 on the final union), clippy -D warnings and the tests, and lan_e2e with the host on its server thread and in-frame (18/18 each). Thanks @DanielGodriNeto!
This commit is contained in:
Ludovic Marie
2026-10-27 08:59:16 +01:00
committed by GitHub
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);