fix(libkernel): prevent sub-millisecond timed wait busy-spinning on Windows #11725
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11725"
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 camezaro-intc on 2026-10-27T05:52:16Z.
What
Reduce
LEAD_NANOSinlibkernel::TimedWaitfrom 500,000 ns (0.5 ms) to 20,000 ns (20 µs), so sub-millisecond timed waits sleep on high-resolution waitable timers and only spin the last 20 µs withYieldProcessor(). Adds theguest_timed_wait_cpuregression test (500 µssceKernelUsleepandscePthreadCondTimedwait, bounds overshoot and, on Windows, calling-thread CPU time).Tested
Windows 11 Pro, Call of Duty: Modern Warfare III (PPSA24263). Verified that worker polling threads with sub-millisecond wait intervals (such as 500 µs via
sceKernelUsleep) sleep via high-resolution waitable timers instead of burning 100% CPU on spin-waits, reducing CPU usage during polling from 100% across multiple cores down to ~1.5%. Author's Windows 11 / Ryzen 5 3600X run of the revised test (ctest --test-dir <build> --repeat until-fail:20 --no-tests=error -R '^guest_timed_wait_cpu$' -V): 20/20 passed on this PR'sTimedWait.cpp; withTimedWait.cppfrommainthe same test fails at the 100 µs case (93.5% calling-thread CPU, limit 40%, abort0xc0000409). Worst over the 20 runs, P99 / max overshoot / CPU:sceKernelUsleep(100 us)1366 µs / 2641 µs / 3.1%;sceKernelUsleep(500 us)1259 µs / 2466 µs / 5.7%;scePthreadCondTimedwait(500 us)1552 µs / 2299 µs / 5.4%. Not rerun from the final pushed head3d4e1e0930.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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T05:58:57Z.
Review: APPROVED
Reviewed exact head
e7e3825aaeb71b030ea5028ab8ea024ac144c8aa. The wait-loop changes preserve the event/counter checks and deadline condition while reducing the precise-spin tail from 500 µs to 20 µs;SwitchToThread()gives other ready threads an opportunity to run before the processor pause. The CMake quoting change correctly keeps the forced-include path as one argument when it contains spaces.git diff --checkpassed. I syntax-checked the changed Windows translation unit with MinGW GCC 12 using a compatibility definition forCREATE_WAITABLE_TIMER_HIGH_RESOLUTION(absent from this old toolchain's headers); compilation passed. The author reports a Windows 11 title run showing polling CPU use fall from 100% across multiple cores to about 1.5%, and a successfulctest -R libkernel -V. I could not run a native Windows runtime test here; hosted product jobs are skipped by path policy. No blocking issue found in the reviewed change, so approving based on the supplied Windows run and local static check.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T09:01:37Z.
Review: CHANGES_REQUESTED
Thanks for the measurements. Before this can become the default for every Windows title:
TimedWait.cppis CMake), as @oneandonlydean asked. Add a registered guest test that waits ~500 µs throughsceKernelUsleeporscePthreadCondTimedwait, and bounds both the overshoot and the calling-thread CPU time (GetThreadTimes) so it fails onmain(100% spin) and passes here.mainto 735–1020 µs with this PR, and max up to ~1.5 ms. Please say whetherSwitchToThread()in the final spin causes that (it can give up the core for a whole quantum when another thread is ready) and whether a bounded number ofSwitchToThreadcalls, or onlyYieldProcessor()in the last 20 µs, keeps the tail down. The test above should pin the bound you accept.TimedWait.cpp(SleepUntil/SleepNanosUnlessChanged). Rebase on whichever lands first and list it under Depends on.-includequoting incore/libs/CMakeLists.txtis unrelated to timed waits; please move it to its own PR.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T03:08:13Z.
Review: DISMISSED
Reviewed — changes requested
The
LEAD_NANOSchange is small and merges cleanly, but the new Windows regression test can fail on an idle machine because of how it bounds CPU time and latency, which the author's own Windows runs of this head also show.What I ran
1cf6f482c03f3f2d033ecccc65931289d549bbc1main:3a2b91981651569163768854a1fcdc7193b6667bResults
maingit diff --checkcore/libs/tests/GuestTimedWaitCpu.cpptools/check_conventions.pylibsand the timing test targets (Linux)guest_cond_*,guest_nanosleep*,guest_sleep_*,guest_timed_*)-j4, repeated 5 timesguest_timed_wait_cpurepeated 50 timesctest excerpts
Negative control
I restored
core/libs/prx/libkernel/Time/TimedWait.cppfrommain(LEAD_NANOS = 500000), rebuilt, and ranguest_timed_wait_cpu20 times. It passed every run. This is expected:LEAD_NANOSis only read in the_WIN32paths (WaitEventUntil,SleepUntil,SleepNanosUnlessChanged), and the test's CPU assertion is under#ifdef _WIN32. On Linux and macOS the test only checks that a 500 µs wait takes between 500 µs and 3 ms, and that already holds onmain. I could not run the test on Windows, which is the only platform where it can fail onmain.Findings
Blocking
The CPU assertion can fail on an idle Windows machine (
core/libs/tests/GuestTimedWaitCpu.cpp:53,:92). Each API is measured over 50 waits of about 500–700 µs, so the window is about 25–35 ms (about 32 ms per API on this host).GetThreadTimesadvances in clock-tick increments (15.625 ms by default), and 40% of a 32 ms window is about 12.8 ms. One tick charged to the thread during the window is therefore enough to fail the test, even when the thread hardly ran. The author's comments describe the same thing: idle runs measured 45–55%. If this test lands as it is, it will fail intermittently on the Windows CI runs that follow every merge tomain.Fix: make the window long enough that one tick is small compared with the bound (the author's local 500-wait version would do), or measure with
QueryThreadCycleTimeinstead of tick-based times. Also fail whenGetThreadTimesfails, instead of returning 0 (:22).The per-wait
< 3000 µsbound fails under ordinary scheduling noise (:45,:77). WithLEAD_NANOS = 20000, the wait ends on a waitable-timer expiry and the thread then has to be scheduled again, so its tail depends on the host. The author reports that this exact head failed this assertion on the third Windows run, with a measured 5.664 ms wait. CPU load from other tests running at the same time (ctest -j) makes this more likely.Fix: bound a percentile and set a looser cap on the maximum, as the author proposes, and push that version. Then report repeated runs of the pushed head on Windows, and a run of the same test against
main, where it should fail on the CPU bound.Non-blocking
main, a Windows wait betweenYIELD_BELOW_NANOS(50 µs) and 500 µs spins to the deadline. With this change it sleeps on the timer until 20 µs before the deadline (TimedWait.cpp:25), and condition waits throughWaitEventUntildo this for anything over 20 µs. The author's measurements give a P95 of about 1 ms for 500 µs waits, so a 100 µs wait could plausibly overshoot by several times its length. Since this becomes the default for every title, please add a measurement or test point at a shorter wait (for example 100 µs), so the accepted overshoot is stated. IfCreateWaitableTimerExW(... HIGH_RESOLUTION)fails, the fallbackWholeMilliseconds/AlertableSleeppath is now taken for these short waits too, with a 1 ms minimum (or the system timer period forSleepEx).ctest -R libkernelpassed. That pattern does not select the timing tests (guest_nanosleep,guest_cond_timedwait,guest_timed_wait_cpu, …), as noted earlier. Please replace it with the exact Windows CTest names and counts for the pushed head, run with--no-tests=error.SleepUntilandSleepNanosUnlessChanged, which this change makes shorter. It merges cleanly with this PR, but CONTRIBUTING asks overlapping PRs to agree on an order and declare it on theDepends online, which is stillnone. The author's comments also say the PR is "on hold" or "in draft", but it is not marked as a draft. Please mark it as a draft until the test update is pushed.core/libs/tests/GuestTimedWaitCpu.cpp:101(git diff --check).Not verified
guest_timed_wait_cpuat this head, and whether it fails onmainthere.To get this to a good state
GetThreadTimesfails.ctest --repeat until-fail:20 --no-tests=error -R "^guest_timed_wait_cpu$"), and once against a checkout ofmainto show it fails there. Put both results in Tested.Depends online, and remove the trailing blank line.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-29T19:22:27Z.
Review: CHANGES_REQUESTED
Thanks for the investigation. Points still open on
1cf6f482c0:core/libs/tests/GuestTimedWaitCpu.cpp:53,:92: 50 waits are a ~32 ms window andGetThreadTimesadvances in 15.625 ms ticks, so one tick fails the 40% bound on an idle machine. Use a longer window (your 500-wait version) orQueryThreadCycleTime, and fail whenGetThreadTimesfails instead of returning 0 (:22).:45,:77: a hard< 3000 µsper wait fails under ordinary scheduling noise (you measured 5.664 ms). Bound a percentile and a looser maximum, as in your local revision. Please push it; the head still has the flaky version.mainon Windows and passing repeatedly here (ctest --repeat until-fail:20 --no-tests=error -R "^guest_timed_wait_cpu$"), and put the exact CTest names and results in Tested (-R libkerneldoes not select the timing tests). On Linux the test passes onmaintoo, since the CPU assertion is_WIN32only.TimedWait.cpp; agree the order with its author and record it inDepends on.GuestTimedWaitCpu.cpp:101.