Files
AnyPS5/docs/dev/debt/changes/fix-libkernel-windows-cpu-clock.md
videogamer10293andClaude Opus 5.5 beb0afc706 fix(libkernel): read Windows thread CPU clocks from thread cycle time
GetThreadTimes only advances at the 15.6 ms scheduler tick, so on
Windows the thread CPU clock moved in 15.6 ms steps where clock_getres
reports 1 us, and could read ahead of the monotonic clock under load.
With an invariant TSC, read the calling thread's clock and other guest
threads' CPU clocks from QueryThreadCycleTime, converted with the
measured host TSC frequency.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 20:45:11 +00:00

959 B

libkernel

add Functional

  • Thread CPU time on Windows (clock_gettime with the thread CPU clock, and the CPU time of a guest thread) is measured only on one AMD CPU with an invariant TSC: QueryThreadCycleTime, for which Microsoft documents no time unit, divided by the TSC frequency libkernel measures. On a Ryzen 5 7600X it stayed within 0.25% of wall time without system calls; each read of the clock went about 6.5 ns uncounted, other system calls 1 ns or less. The first read, if the TSC frequency is not measured yet, pins the thread to one CPU and waits 20 ms. Without an invariant TSC these clocks read GetThreadTimes, which moves in 15.6 ms steps, as the process CPU clocks and getrusage still do; a thread clock and those can differ by about one step. Closes when: measured on an Intel CPU and on a CPU without an invariant TSC.