fix(libc): absorb macOS nanosleep lateness in PreciseSleep #12127
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12127"
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 iamthehimansh on 2026-10-27T22:15:09Z.
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)Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T10:53:35Z.
Review: CHANGES_REQUESTED
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.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.PreciseSleepUsonmainand with the change, next to theprecise_waitresult.Everything else (macOS-only, Linux/Windows text unchanged) looks fine.