Repository navigation
fix(libc): absorb macOS nanosleep lateness in PreciseSleep - #12127
Conversation
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.
|
Ran 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: 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 |
|
@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. |
adriadam10
left a comment
There was a problem hiding this comment.
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 / 10makes 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 callsPreciseSleep(libc threads, sockets, AIO, PM4, audio). Anything kept busy this way needs a bound and a measurement.- Please measure how late
nanosleepwakes 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
PreciseSleepUsonmainand with the change, next to theprecise_waitresult.
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.
|
@oneandonlydean @adriadam10 Thanks, you were right: the uncapped margin cost far too much CPU on long waits. How late
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.
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 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.
AI-assisted: yes (Cana) |
|
Checked the cap against the earlier points: the margin is now |
What
Next part of splitting #8658:
precise_waitfails on macOS.PreciseSleepFromNanosWithSlacksleeps the whole wait minus a fixed 100 µs spin tail, but macOS coalesces timers, sonanosleepwakes 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
nanosleeplateness 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. OnlyPreciseWait.hppchanges, 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.
nanosleeplateness on macOS (plainnanosleep, no spin):PreciseSleepNanosfrom the real header, calling-thread CPU time from Machthread_info, medians (overshoot is how long after the deadline it returned):main: overshoot / CPU5a632cc71): overshoot / CPUWith 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 onlyPreciseWait.hppswapped:PreciseWait.hppprecise_waitmain5a632cc71)The base is my local macOS test base:
mainatb27cc8416plus the open macOS build PRs and local patches listed in #11303.precise_wait_testsis 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 equalsmainfor bothunifdef -U__APPLE__ -D__linux__and-D_WIN32.python3 tools/check_conventions.py --base origin/mainpasses.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)