Repository navigation
test(libkernel): keep the guest_sleep_wake waker running under load - #26347
Conversation
The waker must call KernelWakeSleepers from the moment a case starts and keep calling it. Under load it started 10 to 20 ms late and, since it paused with std::this_thread::yield, gave its CPU away on every pass, so a case could run out of early wakes or tries before a wake arrived. The constructor now waits for the first wake, the waker pauses in a busy loop, and on Windows it runs at THREAD_PRIORITY_HIGHEST. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 09762d30c7:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_sleep_wake_testsbuilt cleanly. - CTest (
guest_sleep_wake):1/1 Test #6001: guest_sleep_wake ................. Passed 0.09 sec 100% tests passed, 0 tests failed out of 1 - Code reading:
- Verified that std::atomic started{false} and stop{false} provide sequentially consistent memory ordering by default, preventing memory reordering between KernelWakeSleepers_nid_no_patch() and started.store(true).
- Verified Waker constructor handshake: thread initializes, optionally elevates priority on Windows, invokes KernelWakeSleepers_nid_no_patch(), and signals started.store(true) before the main thread unblocks from while (!started.load()).
- Verified Waker destructor lifetime: sets stop.store(true) and joins thread, ensuring no thread leakage or use-after-free of Waker member variables.
- Verified Spin(std::chrono::microseconds) busy-loop implementation against std::chrono::steady_clock, ensuring monotonic clock progression and absence of signed/unsigned timing bugs.
- Verified platform conditional compilation (#ifdef _WIN32 / #endif) and Windows API call SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST) with correct headers (<windows.h>).
Looks Good To Me
|
Reproduced the CI failure and the fix independently on a different CPU, using CI's own WinLibs GCC 15.2 toolchain. With the test and 8 busy loops pinned to 4 logical CPUs, the Existing runs: PrivacyOnion, Linux (Ubuntu 24.04, GCC 13.3): What this run adds. A second Windows machine (Zen 5, 32 logical CPUs) built end to end with the WinLibs GCC 15.2 toolchain the CI runners use. Each variant below is a separately built
Under load the median run took 1.71 s on
So on this host too, the priority is what separates the change from H+S under pinned load. In this condition the handshake made no difference. The single idle abort without the handshake is one run in 50, too few to read anything into. In coarse mode Code. Test-only change. Checked at the head:
No comments, no assertion changes, and Negative control and mutation.
Tests.
Host:
AI-assisted: yes |
catminou4
left a comment
There was a problem hiding this comment.
Verified locally on Linux (x86-64, GCC 11), building libkernel and the guest test from this branch:
guest_sleep_wake_testsbuilds clean and passes 3/3 consecutive runs (exit 0).
This is a test-only change: the started flag removes the startup race where the test could proceed before the waker thread was looping, and the busy-spin (plus THREAD_PRIORITY_HIGHEST on Windows) makes KernelWakeSleepers_nid_no_patch wake sleepers more responsively, which should reduce the timing flakiness this test is prone to. The _WIN32 block is correctly guarded. LGTM from my side.
What
Test-only change to
core/libs/tests/GuestSleepWake.cpp, +17 −2. TheWakerthread that callsKernelWakeSleepers_nid_no_patch()every 50 µs now:std::this_thread::yield()loop,THREAD_PRIORITY_HIGHESTon Windows. Linux and macOS keep the default priority.The sleep code (
SleepWake.cpp,TimedWait) and every assertion are unchanged.Related: #24619, #24755
Why
guest_sleep_wakeaborts (0xc0000409fromRequire, 0.32 to 0.71 s, no message) on Windows CI more often since #24755 replaced the waker'ssleep_for(50us)with ayieldloop. @andyohyeah, this builds on your #24755 and keeps its gain (see below).CI. Finished
windowsjobs of the Build workflow onmain, pushes and manual PR batches, 2026-11-18 00:00 to 2026-11-23 12:56 UTC; #24755 merged 2026-11-21 06:32. The batches merge unmerged PRs ontomainand are counted only for how often the test fails.windowsjobsguest_sleep_wakefailedFisher's exact test, two-sided: p = 0.010. All failures are
0xc0000409aborts on WinLibs GCC 15.2 runners (4 vCPUs,ctest --parallel 4).main: 44970429056, 45175534560 (its only failed test).SubmitStartsNewRun'sRequire(early). The WinLibs aborts could not be mapped to a line; test(libkernel): report sleep-wake assertion call sites #25705's call-site output would show which check fails on CI.What the test needs.
SleepWake::Sleeplets a wake end a sleep early at most 8 times per run.RunOfSleepsEndsEarlyAtMostEightTimes,LongSleepKeepsRunLimitandZeroSleepCountsAsSleepsleep 16 × 1 ms to use up those 8 early wakes and then require full sleeps, which holds only if at least 8 of the 16 sleeps saw a wake.WakeEndsShortSleep,SubmitStartsNewRunandGapStartsNewRunrequire one of 20 × 1 ms sleeps to end early. Both need the waker running from the start of the case and without long pauses: 16 − 8 sleeps leave about 9 ms without a wake, 20 tries about 20 ms.The start delay, made deterministic. An instrumented copy of the test with an injected delay before the waker's first wake, idle machine (Ryzen 7 5800X), 20 runs per value:
RunOfSleepsEndsEarlyAtMostEightTimes, full-sleep check, every timeWakeEndsShortSleepin 5WakeEndsShortSleep, except oneLongSleepKeepsRunLimitat 21 msA pause of the waker after its first wake gives the same thresholds: 4 to 9 ms pass 20 of 20, 10 to 19 ms and 20 to 22 ms fail 20 of 20 (30 ms passes: no wake reaches the case at all). The run rule stays covered: 500 or 900 µs busy before every sleep pass 20 of 20, and 1010 µs, a gap that starts a new run before every sleep, fail the
early <= 8check 20 of 20.Measured waker delays under load
Under load the delays are in that range. With no free CPU (16 busy-loop processes on 16 logical CPUs, or the test and 4 busy loops pinned to 4 logical CPUs), in failing runs of
mainthe waker's first wake comes 10.6 ms after construction (median over runs of the slowest of a run's six wakers; up to 75 ms seen), against 0.70 ms idle. Its longest pause between two wakes is 9.1 ms median, against 0.14 to 0.20 ms idle. In 292 of 337 recorded failures a sleep that had to be full was woken and fewer than 8 of the 16 preceding sleeps had seen a wake call. In 44 none of 20 tries ended early. In 1 neither held, and in 0 was there a gap of 1 ms or more before the failing sleep.Why #24755's
yieldmade it worse under load. Before #24755 the waker paused withsleep_for(50us). With WinLibs'libwinpthread, which CI uses, that returns at once, so the old waker never left its CPU: 3.5 million wake calls per run. With MSYS2'slibwinpthreadthe same waker failed even alone (#24619; 27 of 60 passes here). #24755'syieldloop makes the period independent of the runtime, butyieldhands the CPU to any ready thread on every pass. On a full machine the waker then gets about 1% of a CPU: 0.18 to 0.22 wake calls per ms of sleeping, against 13.5 idle. Same machine, WinLibs runtime, instrumented copies of both wakers:sleep_for)main(yield)This change keeps #24755's independence from the runtime (d passes 100 of 100 alone on each runtime) and does not give the CPU away.
Each part, measured.
Block 1: all seven variants
Variants run in the same block. A runner rotates through all of them in chunks of 25 runs, under the same busy-loop processes and on both runtimes, so machine state hits every variant alike. Absolute pass rates drift with machine state between blocks:
mainpassed 396 of 400 with 16 busy loops in the first block below, and 6 of 80 in the same condition today, so only columns of one block compare. "4 CPUs" pins the test and the busy loops to 4 logical CPUs; H = handshake, S = busy-loop pause, P =THREAD_PRIORITY_HIGHEST. Variants a to g are instrumented copies that also record the waker's timing.Block 1 (passes, both runtimes summed):
mainPass rates drift with machine state, so only variants in the same block compare. H = handshake, S = busy-loop pause, P =
THREAD_PRIORITY_HIGHEST.Block 2 (B and D are the uninstrumented sources; D is this PR):
yield: under load the waker's CPU time over its lifetime is 0.4 to 28% for a (yield) and 1 to 41% for e (H+P, stillyield), against 99 to 100% for d. e passes 1378 of 1400, d 1400 of 1400; with 32 busy loops 185 of 200 against 200 of 200. H alone withyield(b) does not help: 1118 of 1400.THREAD_PRIORITY_HIGHEST: without it, c (H+S) gets 54 to 97% of a CPU under load and passes 1954 of 2000 (B, its uninstrumented source, 1950), where d and D pass 2000 of 2000 in the same block.THREAD_PRIORITY_TIME_CRITICALmeasured no better.Side effects: cost and three full ctest runs per side
Launch mode. ctest and a direct start give the same rate:
main, 16 busy loops, alternating blocks of 20, 6 of 80 run directly and 6 of 80 throughctest -R ^guest_sleep_wake$.Cost. The waker already used a whole CPU while the test ran: the
yieldloop keeps the CPU whenever nothing else is ready. Instrumented, alone, the waker uses 80 ms of CPU per run in bothmainand d. Uninstrumented, alone, 20 alternating runs each, the whole process uses 102 ms of CPU formainand 109 ms for this change (medians; 117 and 118 ms wall). The waker exists only while a case runs;guest_sleep_waketakes 0.12 s alone and 0.15 to 0.29 s underctest -j16.Side effects on other tests. Full
ctest -j16 --timeout 120, 6802 tests, three times onmainand three times with this change, alternating, on one build tree that differs only inguest_sleep_wake_tests.exe:mainframe_capture_minimize, in one run)guest_sleep_wake,guest_sleep_wake_coarseEvery test that failed with this change also failed on
mainin these runs and onmainhere before:agc_process_sampler_stack(#24354),agc_storage_write_back_host_retile(#23751),agc_driver_store_over_untracked(#22764),guest_raise_short_sleepandguest_raise_short_sleep_without_wake(#24359),agc_driver_cpu_label_wake. Only onmain, once each:agc_driver_cached_image_multisample_guard,agc_driver_stencil_plane_memory, theframe_capture_minimizetimeout.Durations: the sum of per-test medians is 9521 s on
mainand 9679 s with this change (+1.7%). A whole run's median test took 0.91 to 1.04 times the other build's median formainruns and 1.01 to 1.06 for these runs, so the drift between runs is of the same size. For the tests that ran at the same time asguest_sleep_wake, the summed time above their six-run medians is +0.39, +1.37 and +0.64 s with this change, −1.37, +0.17 and +0.13 s onmain, and −0.50 to +0.84 s for windows of the same length 3 to 6 s earlier in the same runs. The +1.37 s isguest_raise_short_sleep(3.9 s; it ranges 0.2 to 3.9 s over the six runs); without it every window lies in the range of the shifted ones.Tested
Windows 11 Pro 26H2 (10.0.26300), Ryzen 7 5800X (16 threads), MSYS2 UCRT64 GCC 16.2.0, Release; the WinLibs
libwinpthreadfrom CI's GCC 15.2.0 r7 archive for the WinLibs rows.guest_sleep_wakeandguest_sleep_wake_coarsethrough ctest: 20 of 20 each (0.12 s and 0.04 s median).libwinpthreadbuilds.ctest -j16 --timeout 120, three times onmainand three times with this change, alternating, machine otherwise idle: no new failure, timeout or slowdown (above).tools/check_conventions.py: passed.tools/debt_fold.py --check: passed.tools/pr_overlap.py --branch: test(libkernel): report sleep-wake assertion call sites #25705 merges without conflict; test(libkernel): cover APS5_NO_SLEEP_WAKE presence switch in GuestSleepWake #25346 also adds includes after<thread>(lines 6-7), so whichever lands second rebases that block. Neither changes the waker.guest_sleep_wakewould be welcome.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)