Skip to content

test(libkernel): keep the guest_sleep_wake waker running under load - #26347

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
einfachama:test/sleep-wake-waker-start
Nov 24, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
einfachama:test/sleep-wake-waker-start

Conversation

@einfachama

@einfachama einfachama commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What

Test-only change to core/libs/tests/GuestSleepWake.cpp, +17 −2. The Waker thread that calls KernelWakeSleepers_nid_no_patch() every 50 µs now:

  1. makes its first wake call before the constructor returns (start handshake),
  2. pauses in a busy loop instead of the std::this_thread::yield() loop,
  3. runs at THREAD_PRIORITY_HIGHEST on 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_wake aborts (0xc0000409 from Require, 0.32 to 0.71 s, no message) on Windows CI more often since #24755 replaced the waker's sleep_for(50us) with a yield loop. @andyohyeah, this builds on your #24755 and keeps its gain (see below).

CI. Finished windows jobs of the Build workflow on main, 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 onto main and are counted only for how often the test fails.

Finished windows jobs guest_sleep_wake failed
before #24755 453 1 (0.22%)
after #24755 625 12 (1.9%)

Fisher's exact test, two-sided: p = 0.010. All failures are 0xc0000409 aborts on WinLibs GCC 15.2 runners (4 vCPUs, ctest --parallel 4).

What the test needs. SleepWake::Sleep lets a wake end a sleep early at most 8 times per run. RunOfSleepsEndsEarlyAtMostEightTimes, LongSleepKeepsRunLimit and ZeroSleepCountsAsSleep sleep 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, SubmitStartsNewRun and GapStartsNewRun require 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:

Waker starts late by Result
0 to 9 ms 20 of 20 pass
10 to 19 ms 0 of 20: RunOfSleepsEndsEarlyAtMostEightTimes, full-sleep check, every time
20 ms 0 of 20: the same check in 15, WakeEndsShortSleep in 5
21, 22, 25, 40 ms 0 of 20: WakeEndsShortSleep, except one LongSleepKeepsRunLimit at 21 ms

A 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 <= 8 check 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 main the 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 yield made it worse under load. Before #24755 the waker paused with sleep_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's libwinpthread the same waker failed even alone (#24619; 27 of 60 passes here). #24755's yield loop makes the period independent of the runtime, but yield hands 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:

16 busy loops 4 CPUs, 4 busy loops
waker before #24755 (sleep_for) 148 of 150 150 of 150
main (yield) 7 of 150 9 of 150

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: main passed 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):

Condition a: main b: H c: H+S d: H+S+P e: H+P f: P g: S+P
alone 200/200 200/200 200/200 200/200 200/200 200/200 200/200
4 CPUs, 4 busy loops 397/400 400/400 400/400 400/400 400/400 400/400 400/400
15 busy loops 196/200 199/200 200/200 200/200 200/200 200/200 200/200
16 busy loops 396/400 387/400 400/400 400/400 393/400 394/400 400/400
4 CPUs, 8 busy loops 12/200 0/200 200/200 200/200 200/200 200/200 200/200
32 busy loops 135/200 132/200 200/200 200/200 185/200 173/200 200/200
loaded total 1136/1400 1118/1400 1400/1400 1400/1400 1378/1400 1367/1400 1400/1400

Pass 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):

Condition c: H+S d: H+S+P g: S+P B: H+S D: H+S+P
15 busy loops 591/600 600/600 364/600 596/600 600/600
16 busy loops 589/600 600/600 599/600 597/600 600/600
4 CPUs, 8 busy loops 574/600 600/600 600/600 557/600 600/600
4 CPUs, 16 busy loops 200/200 200/200 200/200 200/200 200/200
total 1954/2000 2000/2000 1763/2000 1950/2000 2000/2000
  • Handshake: the injected-delay table above. Without it, g (S+P) passes 364 of 600 with 15 busy loops, where d passes 600 of 600 in the same block.
  • Busy-loop pause instead of 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, still yield), 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 with yield (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_CRITICAL measured 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 through ctest -R ^guest_sleep_wake$.

Cost. The waker already used a whole CPU while the test ran: the yield loop keeps the CPU whenever nothing else is ready. Instrumented, alone, the waker uses 80 ms of CPU per run in both main and d. Uninstrumented, alone, 20 alternating runs each, the whole process uses 102 ms of CPU for main and 109 ms for this change (medians; 117 and 118 ms wall). The waker exists only while a case runs; guest_sleep_wake takes 0.12 s alone and 0.15 to 0.29 s under ctest -j16.

Side effects on other tests. Full ctest -j16 --timeout 120, 6802 tests, three times on main and three times with this change, alternating, on one build tree that differs only in guest_sleep_wake_tests.exe:

main this change
total time 645, 633, 682 s 629, 678, 683 s
failed tests 5, 5, 6 6, 6, 4
timeouts 1 (frame_capture_minimize, in one run) 0
guest_sleep_wake, guest_sleep_wake_coarse pass, 0.13 to 0.24 s pass, 0.15 to 0.29 s

Every test that failed with this change also failed on main in these runs and on main here before: agc_process_sampler_stack (#24354), agc_storage_write_back_host_retile (#23751), agc_driver_store_over_untracked (#22764), guest_raise_short_sleep and guest_raise_short_sleep_without_wake (#24359), agc_driver_cpu_label_wake. Only on main, once each: agc_driver_cached_image_multisample_guard, agc_driver_stencil_plane_memory, the frame_capture_minimize timeout.

Durations: the sum of per-test medians is 9521 s on main and 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 for main runs 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 as guest_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 on main, 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 is guest_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 libwinpthread from CI's GCC 15.2.0 r7 archive for the WinLibs rows.

  • guest_sleep_wake and guest_sleep_wake_coarse through ctest: 20 of 20 each (0.12 s and 0.04 s median).
  • Load runs and ablations above: the change passes 2000 of 2000 runs (uninstrumented) and 3400 of 3400 (instrumented) with no free CPU, on both libwinpthread builds.
  • Full ctest -j16 --timeout 120, three times on main and 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.
  • Linux and macOS were not run. There the waker gets the handshake and the busy-loop pause but no priority change. A Linux run of guest_sleep_wake would be welcome.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

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 PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 09762d30c7:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_sleep_wake_tests built 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

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 25346 core/libs/tests/GuestSleepWake.cpp L6-7

@Helldiverr

Copy link
Copy Markdown
Contributor

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 main test passes 4 of 200 runs (196 0xc0000409 aborts) and this change passes 200 of 200. The neighbouring sleep tests pass 61 of 61 on main and on each of 3 merge runs.

Existing runs: PrivacyOnion, Linux (Ubuntu 24.04, GCC 13.3): guest_sleep_wake 1/1. That run doesn't reach the _WIN32 priority branch. einfachama's runs are on a Ryzen 7 5800X with MSYS2 GCC 16.2 plus WinLibs' libwinpthread. Nobody else has posted a Windows run.

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 guest_sleep_wake_tests.exe run from the same build tree. A Python harness pins itself to logical CPUs 28-31 (mask 0xf0000000), and its children inherit that affinity. It rotates the variants in chunks of 25 runs, so machine drift hits every variant equally.

Condition main this PR (H+S+P) PR without priority (H+S) PR without handshake (S+P)
4 CPUs, no busy loops 50/50 50/50 50/50 49/50 (one 0xc0000409)
4 CPUs, 8 busy loops 4/200 (196 × 0xc0000409) 200/200 196/200 (4 × 0xc0000409) 200/200
4 CPUs, 8 busy loops, APS5_COARSE_TIMED_WAITS=1 100/100 100/100 not run not run

Under load the median run took 1.71 s on main (most runs abort early) and 2.27 s with the change; idle runs took 0.11 s for every variant. These results agree with the "4 CPUs, 8 busy loops" rows of the PR's ablation tables:

  • main: 12/200 there, 4/200 here.
  • D: 600/600 there, 200/200 here.
  • H+S without priority (B): 557/600 there, 196/200 here.
  • S+P without the handshake (g): 600/600 there, 200/200 here.

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 main already passes under load, so that mode doesn't exercise the waker regression.

Code. Test-only change. Checked at the head:

  • core/libs/tests/GuestSleepWake.cpp:8-10 includes <windows.h> under _WIN32 only.
  • :45-47 raises the waker to THREAD_PRIORITY_HIGHEST under _WIN32, so Linux and macOS are unchanged.
  • :49-51: the loop wakes, sets started, then busy-pauses 50 µs with Spin (:37-40) instead of yielding.
  • :54: the constructor waits until the first wake has happened.

No comments, no assertion changes, and SleepWake.cpp is untouched. tools/check_conventions.py --base bfcd717ebc passed, and tools/debt_fold.py --check passed. No debt fragment is needed, since there's no behaviour change outside the test.

Negative control and mutation.

  • Negative control: main's test under the same pinned load aborts in 196 of 200 runs, the failure mode the PR describes.
  • Mutations of the PR's waker under that load:
    • Without the SetThreadPriority line (:45-47): 4 aborts in 200.
    • Without the constructor wait (:54): 200 of 200.
    • Both mutants pass ctest -R '^guest_sleep_wake(_coarse)?$' on an idle machine, so only a loaded run tells the variants apart. That matches what the PR says about how often CI hits it.

Tests. ctest --test-dir build -R '^(guest_sleep_wake|guest_short_sleep|guest_relative_sleep|guest_usleep|guest_nanosleep|guest_kernel_clocks_nanosleep|guest_kernel_sys_nanosleep|guest_sleep_cancel|guest_raise_short_sleep|guest_raise_nested_sleep|guest_libcxx_sleep_for|guest_sync_wakeups)' -j 8 -V, 61 tests:

Run Result Total time guest_sleep_wake guest_sleep_wake_coarse
main 61/61 16.34 s 0.16 s 0.08 s
merge 1 61/61 14.99 s 0.17 s 0.03 s
merge 2 61/61 14.91 s 0.13 s 0.06 s
merge 3 61/61 14.95 s 0.12 s 0.03 s

Host:

  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits: head 09762d30c7, merged onto main bfcd717ebc as 1652750a5a

AI-assisted: yes

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally on Linux (x86-64, GCC 11), building libkernel and the guest test from this branch:

  • guest_sleep_wake_tests builds 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.

@oneandonlydean
oneandonlydean merged commit 9899653 into boykopovar:main Nov 24, 2026
13 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants