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

Merged
iamthehimansh merged 3 commits from upstream-pr/12127 into main 2026-10-29 21:18:39 +00:00
iamthehimansh commented 2026-10-27 22:15:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by iamthehimansh on 2026-10-27T22:15:09Z.


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)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5041928675 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12127). Originally posted by [iamthehimansh](https://github.com/iamthehimansh) on 2026-10-27T22:15:09Z. --- ### 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 - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes ([Cana](https://cana.build))
adriadam10 commented 2026-10-28 10:53:35 +00:00 (Migrated from github.com)

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 / 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5681474430 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12127#pullrequestreview-5681474430). Originally posted by [adriadam10](https://github.com/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 / 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12127
No description provided.