perf(libc): wait on a critical kernel timer in macOS PreciseSleep #15583
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15583"
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 Gonic0 on 2026-11-03T20:22:28Z.
What
PreciseSleepon macOS sleeps for 70 % of a wait withnanosleepand spins through the rest, capped at 5 ms (#12127), to absorb the wake latency ofnanosleep. A guest that sleeps in short steps keeps a core busy that way. In PPSA21564's gameplay about 1000 waits of 1.8 ms a second reachPreciseSleep(guestsceKernelUsleep/nanosleep), and they spin about 4.3 s of CPU every 10 s.The wait now arms a one-shot
EVFILT_TIMERwithNOTE_CRITICALand zero leeway on a per-threadkqueue, then spins only the last 30 us. A wait of 30 us or less spins as before, and thenanosleeppath stays as the fallback when thekqueuecannot be created. The timer lives insrc/PreciseWait.cpp, because<sys/event.h>in the header clashes with theEVFILT_*names inEqueue.hpp.Tested
macOS 27.2, Apple M5 Pro, Apple clang, x86-64 Release under Rosetta 2, on
main086c0686c8.precise_wait_testsgains a macOS check: the kernel timer waits until its wake time, and 20PreciseSleepUs(2000)calls take less than 3 ms of process CPU time (getrusage;CLOCK_THREAD_CPUTIME_IDdoes not count the spin under Rosetta 2). With this change: 3 of 3 runs pass, including the existing median bounds (PreciseSleepUs(1500)under 1650 us). With the timer disabled: the check fails in 2 of 3 runs (spun for 6786 us of thread CPU time over 20 sleeps); the third skipped the bound on a busy machine, as the existing checks do.extern "C":libc.prxexports only the C names of its_nid_no_patchfunctions, so with C++ linkagelibSceVideoOut.prxfailed to load (Symbol not found: __Z36PreciseKernelTimerSleep_nid_no_patchy). Every file that includesPreciseWait.hppcompiles on this branch.libkerneldoes not link on macOS on thismainwithout the open macOS port pull requests.main'sPreciseWait.hpp(4ef0c8c5d034) and once with this change (78f5788e6bcf), alternating, 140 s each, the same pad input, captureAPS5_TRACE_FRAMES=1100:300. Process CPU is sampled every 5 s withpsfrom 60 s on:4ef0c8c5d03478f5788e6bcf4ef0c8c5d03478f5788e6bcfAn earlier pair on that tree with the timer behind a switch counted the spin: 4.2 to 4.4 s every 10 s before, 0.11 to 0.12 s after; the mean lateness of a wait went from 1 to 2 us to about 4 us, with a maximum of 1.1 to 2.5 ms in both. Both traces below lost events of
queue worker 0x0after about 14.5 s (buffer full); the summary statistics cover the whole window.Summary lines of pair 2 (traces attached: base,
4ef0c8c5d034, this change,78f5788e6bcf):tools/trace_digest.py, base (4ef0c8c5d034)frames 1100:300 from present 1100: 300 presents, frame p50 81.9 / p90 98.8 / p99 157.3 / max 259.7 ms, GPU busy 63.1 ms per present
Apple M5 Pro, KosmicKrisp Mesa 27.0.0-devel (git-b5b73e5964), Darwin 27.2.0, 3840x2160, commit 4ef0c8c5d034, dropped: yes
Incomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 210.3, Driver.Packet.memory_wait 162.2, batch 63.2, Graphics.Draw 19.8, Driver.Packet.prepare_memory 19.0, Exp.FenceWait.idle 17.6, Graphics.Draw.draw_and_resource_release 14.6, Driver.Packet.draw 13.6, Driver.Draw 13.6, Driver.Packet.dispatch 9.2
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
tools/trace_digest.py, this change (78f5788e6bcf)frames 1100:300 from present 1100: 300 presents, frame p50 77.9 / p90 92.3 / p99 104.7 / max 131.1 ms, GPU busy 60.4 ms per present
Apple M5 Pro, KosmicKrisp Mesa 27.0.0-devel (git-b5b73e5964), Darwin 27.2.0, 3840x2160, commit 78f5788e6bcf, dropped: yes
Incomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 198.5, Driver.Packet.memory_wait 150.8, batch 60.4, Driver.Packet.prepare_memory 19.5, Exp.FenceWait.idle 18.3, Graphics.Draw 18.0, Driver.Packet.draw 13.9, Driver.Draw 13.9, Graphics.Draw.draw_and_resource_release 12.3, Driver.Packet.dispatch 8.1
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
__APPLE__blocks.tools/check_conventions.pypasses.tools/pr_overlap.pyfinds no overlapping pull request; the ones it could not compare (they conflict withmain) share only the source list incore/libs/prx/libc/CMakeLists.txt, and none of them touchesPreciseWait.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)🤖 Generated with Claude Code