Skip to content

fix(libc): absorb macOS nanosleep lateness in PreciseSleep - #12127

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
iamthehimansh:split/macos-precise-sleep
Oct 29, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
iamthehimansh:split/macos-precise-sleep

Conversation

@iamthehimansh

@iamthehimansh iamthehimansh commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What

Next part of splitting #8658: precise_wait fails on macOS. PreciseSleepFromNanosWithSlack sleeps the whole wait minus a fixed 100 µs spin tail, but macOS coalesces timers, so nanosleep wakes late. PreciseSleepUs(1500) takes about 1.757 ms, past the test's 1.65 ms bound.

On macOS the sleep part now stops early by a margin of 30% of the wait, capped at 5 ms, and the existing spin covers the rest. The cap follows measured nanosleep lateness on macOS: about 25% up to about 5 ms, then about 2 to 5 ms (p90 under 5 ms) for longer waits. So a 100 ms wait spins about 0.6 ms instead of the 25 ms an uncapped margin cost. Only PreciseWait.hpp changes, by 14 added lines in a new #elif defined(__APPLE__) branch. Linux and Windows compile the same code as before.

I compared the added lines with all open PRs; none carries this code. @hamb3r's #8097 also edits this header, but only its Windows QPC path. The one line it shares is a }, and git merges both without a conflict.

Tested

Apple M2 Pro, macOS 26.6.2, x86_64 Release under Rosetta 2, idle machine.

nanosleep lateness on macOS (plain nanosleep, no spin):

requested runs late, median late, p90 late, max
0.5 ms 200 0.132 ms (26%) 0.139 ms 0.203 ms
1.5 ms 200 0.383 ms (26%) 0.398 ms 2.758 ms
5 ms 100 1.258 ms (25%) 1.284 ms 5.815 ms
16.7 ms 40 2.071 ms (12%) 4.193 ms 4.200 ms
50 ms 20 2.577 ms (5%) 4.740 ms 5.022 ms
100 ms 12 2.111 ms (2%) 3.874 ms 4.969 ms

PreciseSleepNanos from the real header, calling-thread CPU time from Mach thread_info, medians (overshoot is how long after the deadline it returned):

wait main: overshoot / CPU uncapped 30% (5a632cc71): overshoot / CPU this PR, 30% capped at 5 ms: overshoot / CPU
1.5 ms 0.260 / 0.007 ms 0.000 / 0.273 ms 0.000 / 0.269 ms
5 ms 1.133 / 0.008 ms 0.001 / 0.702 ms 0.001 / 0.684 ms
16.7 ms 3.265 / 0.016 ms 0.001 / 2.176 ms 0.001 / 2.433 ms
33.3 ms 3.690 / 0.028 ms 0.001 / 5.063 ms 0.001 / 1.934 ms
50 ms 3.639 / 0.027 ms 0.001 / 9.997 ms 0.001 / 1.021 ms
100 ms 2.618 / 0.025 ms 0.001 / 24.859 ms 0.001 / 0.580 ms

With the cap, the largest overshoot seen at any length was 2.0 ms (one 50 ms run), against 58.7 ms on main.

precise_wait, same base and the same test binary, with only PreciseWait.hpp swapped:

PreciseWait.hpp precise_wait
main fail, 7 of 8: "PreciseSleepUs(1500) median overslept target bound: 1756792 ns" (1,756,042 to 1,759,708 ns across the failures)
uncapped 30% (5a632cc71) pass, 18 of 18
this PR, capped pass, 10 of 10

The base is my local macOS test base: main at b27cc8416 plus the open macOS build PRs and local patches listed in #11303. precise_wait_tests is header-only on top of libc's test helpers. The CPU and lateness probe compiles each header on its own under Rosetta.

Outside the __APPLE__ block, the file's text equals main for both unifdef -U__APPLE__ -D__linux__ and -D_WIN32. python3 tools/check_conventions.py --base origin/main passes.

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
  • 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 (Cana)

macOS timer coalescing wakes a nanosleep about 25% late, so the fixed 100 us
spin tail overshoots: PreciseSleepUs(1500) takes about 1.76 ms. On macOS the
sleep now covers 70% of the wait before the spin tail, which leaves that
lateness inside the spin. Linux and Windows compile the same code as before.
@github-actions github-actions Bot added the libc label Oct 28, 2026
@tkcm01

tkcm01 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Ran precise_wait on an Apple M4 at head 5a632cc71a (merge base f759e165d9). The PR was merged onto upstream main 9fc181dde5 plus the macOS build PRs from #1 (#7631, #7804, #10552, #11249, #11447, #10669, #11554, #12076).

Environment: Apple M4, macOS 27.0.1 (26A434), Apple clang 21.0.0, x86_64 Release under Rosetta 2, idle machine.

With the PR: 40 of 40 passed: ctest -R '^precise_wait$' --repeat until-fail:20, then 20 separate runs.

Negative control (same tree without the PR, 20 separate runs): 17 of 20 failed with "PreciseSleepUs(1500) median overslept target bound", at 2,103,625–2,115,709 ns. The other 3 passed only through the "busy runner" skip, at 2.10–2.12 ms.

Not tested: parallel load, Linux, Windows.

AI-assisted: yes

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@iamthehimansh thanks; the change is confined to macOS and fixes the late wake-up. One concern: the sleep part is now 70% of the wait, so every wait longer than 100 µs spins for 30% of its length. PreciseSleep is used outside the test (libc threads, sockets, AIO, the PM4 path, audio and fonts), so on macOS a 16.7 ms wait would spin about 5 ms and a 100 ms wait about 30 ms on each calling thread. Could the spin be bounded? For example, keep the proportional margin only for short waits, or cap it at a few milliseconds. And could you show the CPU time of a long wait (50 to 100 ms) on main and with the change, alongside the precise_wait result?

@adriadam10 adriadam10 self-assigned this Oct 28, 2026

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the fix, and for the A/B runs (also confirmed by @tkcm01 on an M4). Same concern as @oneandonlydean's, still open:

  • core/libs/prx/libc/include/PreciseWait.hpp:174: sleepNanos = (nanos - 100000) * 7 / 10 makes the spin tail 30% of every wait longer than 100 µs, with no upper bound. On macOS a 16.7 ms wait spins about 5 ms and a 100 ms wait about 30 ms, on every thread that calls PreciseSleep (libc threads, sockets, AIO, PM4, audio). Anything kept busy this way needs a bound and a measurement.
  • Please measure how late nanosleep wakes on macOS as a function of duration (for example 0.5, 1.5, 5, 16.7, 50, 100 ms). If the lateness is roughly constant or capped, size the spin tail to that (a few ms at most); if it really grows with the duration, a spin cannot absorb it for long waits and those should keep the plain sleep.
  • Show the CPU time of a 50-100 ms PreciseSleepUs on main and with the change, next to the precise_wait result.

Everything else (macOS-only, Linux/Windows text unchanged) looks fine.

nanosleep on macOS wakes about 25% late up to roughly 5 ms, then a
roughly constant 2 to 5 ms late for longer waits. Keeping 30% of the wait
as margin made a 100 ms wait spin about 25 ms. The margin is now 30% of the
wait, capped at 5 ms, so long waits spin at most about 5 ms.
@iamthehimansh

Copy link
Copy Markdown
Contributor Author

@oneandonlydean @adriadam10 Thanks, you were right: the uncapped margin cost far too much CPU on long waits. 8c3c205e6 caps the margin at 5 ms (30% of the wait, at most 5 ms), and the branch is merged with current main.

How late nanosleep wakes on macOS (Apple M2 Pro, x86_64 under Rosetta 2, plain nanosleep):

requested late, median late, p90 late, max
0.5 ms 0.132 ms (26%) 0.139 ms 0.203 ms
1.5 ms 0.383 ms (26%) 0.398 ms 2.758 ms
5 ms 1.258 ms (25%) 1.284 ms 5.815 ms
16.7 ms 2.071 ms (12%) 4.193 ms 4.200 ms
50 ms 2.577 ms (5%) 4.740 ms 5.022 ms
100 ms 2.111 ms (2%) 3.874 ms 4.969 ms

So it's proportional up to about 5 ms, then roughly constant at 2 to 5 ms. Lateness stops growing past about 16 ms, which is why a bounded spin can absorb it.

PreciseSleepNanos built from each header, with calling-thread CPU from Mach thread_info (medians; overshoot = how long after the deadline it returned):

wait main overshoot / CPU uncapped 30% overshoot / CPU capped 5 ms overshoot / CPU
16.7 ms 3.265 / 0.016 ms 0.001 / 2.176 ms 0.001 / 2.433 ms
33.3 ms 3.690 / 0.028 ms 0.001 / 5.063 ms 0.001 / 1.934 ms
50 ms 3.639 / 0.027 ms 0.001 / 9.997 ms 0.001 / 1.021 ms
100 ms 2.618 / 0.025 ms 0.001 / 24.859 ms 0.001 / 0.580 ms

The spin no longer grows with the wait: at most 5 ms per wait, and well under that for 50 to 100 ms, since the sleep usually wakes before the margin starts. With the cap, the largest overshoot at any length was 2.0 ms (one 50 ms run), against 58.7 ms on main.

I tried smaller caps. 2 ms and 3 ms let the sleep overshoot again from 16.7 ms upward: 35 and 28 of 60 runs at 16.7 ms were more than 0.15 ms late, with medians up to 2.9 ms. So 5 ms is the smallest cap that held at every length.

precise_wait with the capped header: 10 of 10 pass (main: 7 of 8 fail). The full tables are in the description. Linux and Windows views of the file are still identical to main.

AI-assisted: yes (Cana)

@AdnanKhan-ak47 AdnanKhan-ak47 mentioned this pull request Oct 29, 2026
@adriadam10
adriadam10 merged commit d9f6887 into boykopovar:main Oct 29, 2026
18 of 19 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the cap against the earlier points: the margin is now min(30%, 5 ms) of the wait before the 100 µs spin tail (PreciseWait.hpp:176-177), the lateness table shows it stops growing past ~16 ms, and the CPU per wait drops from 24.9 ms to 0.58 ms at 100 ms. The Linux/Windows paths are untouched; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants