fix(libkernel): read Windows thread CPU clocks from thread cycle time #23238
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/23238"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by videogamer10293 on 2026-11-18T20:45:54Z.
What
On Windows, the thread CPU clocks came from
GetThreadTimes:clock_gettimewith clock 14, and the clockpthread_getcpuclockidgives for a guest thread. Those times advance in 15.62 ms steps, also with the timer resolution raised to 0.5 ms, whileclock_getresreports 1 µs for these clocks. They can also read ahead of wall time, by up to 6.45 ms in the probe below.With an invariant TSC (CPUID 0x80000007 EDX bit 8), both clocks now read
QueryThreadCycleTimeand divide it by the host TSC frequency that libkernel already measures forsceKernelGetTscFrequency(HostTscFrequency_nid_no_patch). Microsoft writes "Do not attempt to convert the CPU clock cycles returned by QueryThreadCycleTime to elapsed time" and documents no time unit, so the numbers below compare the result with wall time. Without an invariant TSC the clocks stay onGetThreadTimes.Unchanged: clocks 1 and 2 and
getrusagereport user and system time apart. A cycle count cannot split them. Clock 15 is a process-wide total, and #18471 changes it separately; its diff and this one touch different lines. The first read now measures the TSC frequency if nothing did before: that pins the calling thread to one CPU and waits 20 ms.The new debt entry records what is measured and what is not. The fragment
fix-nkwebkit-cputime.mdfrom merged #20139, not folded yet, said its clock comes fromGetThreadTimeson Windows; this PR updates that text in place, sincedebt_fold.pycannotchangean entry that is still pending.Test.
guest_thread_cpu_clockgetsThreadClocksAdvanceInSmallSteps. A thread burns 200 ms of its own CPU time, with a short busy loop between readings of its clock, once through clock 14 and once through itspthread_getcpuclockidclock. Readings must not go back. The clock may not advance more than the monotonic clock plus 2% and 2 ms, nor less than the process CPU clock (clock 2) minus 10% and 31.25 ms. At least 100 readings must differ; a clock that moves in 15.62 ms steps gives about 13. A Windows host without an invariant TSC skips that last check, since the clock keeps the steps there.Tested
Windows 10 Pro 10.0.19045, AMD Ryzen 5 7600X (invariant TSC bit set), NVIDIA GeForce RTX 5070 Ti for the GPU tests, WinLibs GCC 15.2.0 r7, Release. Tested on
main2a9d82dbf6with the branch at6abac1715f. This PR's commit differs from it only in the debt entry's wording and a rebase onto newermain, whose new commits change none of this PR's files; the tests were not run again after that. Both trees carry the same uncommitted_putenv_sguard inUploadCacheReadOnlyFlush.cpp, without whichmaindoes not build on Windows (#19899, #22373 and #22634 fix it).Through libkernel (
libclock.cppbelow), clock 14 on one thread for 1 s of wall time, 20 fresh processes per row. "Reads" does nothing but read the clock. "Work" spends 3.9 µs per step on a busy loop and two other system calls, and reads the clock once per millisecond.mainmainHostTscFrequency_nid_no_patchranged from 4691035024 to 4691079935 Hz over these and the loaded starts. With 12 busy threads running, the "work" thread gave most of its time away;main's clock 14 did not move once in 20 runs, and the branch's gave 84 to 127 readings.What the "reads" loop loses. Without any AnyPS5 library (
probe4.cpp), a thread's cycle time divided by the TSC frequency stayed within 0.03% of wall time with no system calls (within 0.25% over the five saved runs of this probe). With about 10 millionQueryThreadCycleTimecalls a second it read 6.50% low, about 6.5 ns per call;SwitchToThread,WaitForSingleObjectandGetThreadTimeslost 1 ns or less per call. So the clock seems to leave part of each of its own reads uncounted; the debt entry records this.GetThreadTimeswithout AnyPS5 (probe.cpp, 40 runs per row): a thread burns untilGetThreadTimeshas advanced by more than 0.1 s. Its largest step was 15.62 ms in all five rows: no load, 12 and 24 busy threads at the default timer resolution, and no load and 12 busy threads at 0.5 ms. In 5 of the 200 runs it got there before 0.1 s of wall time had passed, by up to 6.45 ms. The thread'sQueryThreadCycleTimenever read ahead.The new check.
guest_thread_cpu_clockpassed on the branch 5 of 5 alone and 20 of 20 with 12 busy threads running. The branch's test executable withmain's libraries aborted 5 of 5;main's own test executable passed 5 of 5 with the same libraries (exit code 0).Mutations, each built and run 3 times: each failed the test 3 of 3: clock 14 read with
GetThreadTimesagain, a guest thread's clock read withGetThreadTimesagain, the converted time doubled, the converted time halved, the conversion's remainder term dropped, and the invariant TSC check made to report none. The unchanged branch passed 3 of 3 before and after.tools/select_tests.pyselected the same 5400 tests on both sides, run with-j 8; 26 were skipped on both. Both sides failedagc_driver_stencil_plane_memory(#22427),agc_driver_msaa_dcc_unwatch,agc_driver_bda_device(#22773) andnkwebkit_wtfcputime.mainalso failedframe_capture_minimizeand the branchagc_driver_image_table_packed_guards. Run again 5 times alone and 5 times with 12 busy threads on each side, those two failed once each onmainand never on the branch.nkwebkit_wtfcputimereads clock 14. It passed 20 of 20 alone on each side. With 12 busy threads it failed 8 of 20 onmainand 13 of 20 on the branch; two earlier rounds of 20 onmain6286d36449gave 7 onmainand 9 onf0828c0734, then 11 onmainand 8 onb218c883c6. Every failure was at line 111, which checks that the main thread's own CPU time stays under 30 ms. With the precise clock on the branch, this suggests the thread really used more than 30 ms under load; why was not examined here.Not tested: Linux and macOS, whose clock code is unchanged but where the new check also runs; Intel CPUs; a CPU without an invariant TSC; titles.
tools/pr_overlap.pycould not compare 12 open PRs that conflict withmain; their diffs were compared by hand and none touches the changed functions.libclock.cpp (g++ -O2 -std=c++20 -static)
probe.cpp (probe_res.cpp adds NtSetTimerResolution(5000))
probe4.cpp
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-18T21:00:40Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
beb0afc706:tools/check_conventions.pypassed.guest_thread_cpu_clock_testsbuilt cleanly.guest_thread_cpu_clock):core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/prx/libkernel/Time/Time.cpp,core/libs/prx/libkernel/Time/include/Time.hppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me