Repository navigation
perf(libc): wait on a critical kernel timer in macOS PreciseSleep - #15583
Conversation
PreciseSleep on macOS slept for 70% of a wait and spun through the rest, capped at 5 ms, to absorb nanosleep lateness. A guest that sleeps in short steps (about 1000 waits of 1.8 ms a second in PPSA21564) kept a core busy that way. The wait now arms a one-shot EVFILT_TIMER with NOTE_CRITICAL and zero leeway on a per-thread kqueue and spins only the last 30 us; the nanosleep path stays as the fallback when the kqueue cannot be created. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
This pull request is labeled |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
kevent64 can return a positive count whose event carries EV_ERROR (timer registration failed, errno in data). That was counted as a completed sleep and skipped the nanosleep fallback. Return false (fallback) on EV_ERROR, and retry on EINTR. Not built or run on macOS: Apple path only. Credited fix-up on Gonic0's PR boykopovar#15583.
|
Hi @Gonic0, thanks for the timer-sleep change. To keep it moving we pushed one small commit onto your branch (no force-push, tip 3232b43) for the open review point:
Your own code and test are otherwise untouched. If you push again, please pull the branch first so this commit isn't lost. |
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):{"commit":"4ef0c8c5d034","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_FRAMES":"1100:300"},"gpu":"Apple M5 Pro","driver":"KosmicKrisp Mesa 27.0.0-devel (git-b5b73e5964)","os":"Darwin 27.2.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":18,"memory_mib":49152,"title_id":"PPSA21564","title":"ASTRO BOT","content_version":"01.007.000","resolution":"3840x2160","window":{"spec":"frames 1100:300","first_present":1100,"ms":25369.456},"presents":300,"frame_ms":{"p50":81.852,"p90":98.830,"p99":157.339,"max":259.677},"gpu_busy_ms_per_present":63.090,"worker_busy_pct":{"queue worker 0x0":98.617,"queue worker 0x40":99.615,"queue worker 0x48":98.439,"queue worker 0x3a":0.000,"queue worker 0x46":0.546},"top_phases_ms_per_frame":{"Driver.Packet":250.781,"Driver.Packet.memory_wait":162.354,"Driver.Packet.prepare_memory":30.517,"Exp.FenceWait.idle":28.351,"Driver.Packet.draw":26.053,"Driver.Draw":25.983,"Graphics.Draw":22.531,"Graphics.Draw.draw_and_resource_release":15.349},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":14.367,"events_lost":251708,"totals_full":false}},"trace":"aps5-trace-4ef0c8c5d034-82982.json.gz"}{"commit":"78f5788e6bcf","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_FRAMES":"1100:300"},"gpu":"Apple M5 Pro","driver":"KosmicKrisp Mesa 27.0.0-devel (git-b5b73e5964)","os":"Darwin 27.2.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":18,"memory_mib":49152,"title_id":"PPSA21564","title":"ASTRO BOT","content_version":"01.007.000","resolution":"3840x2160","window":{"spec":"frames 1100:300","first_present":1100,"ms":23469.562},"presents":300,"frame_ms":{"p50":77.866,"p90":92.309,"p99":104.691,"max":131.074},"gpu_busy_ms_per_present":60.375,"worker_busy_pct":{"queue worker 0x0":99.177,"queue worker 0x40":99.640,"queue worker 0x48":98.758,"queue worker 0x3a":0.000},"top_phases_ms_per_frame":{"Driver.Packet":232.349,"Driver.Packet.memory_wait":150.957,"Driver.Packet.prepare_memory":29.948,"Exp.FenceWait.idle":28.117,"Driver.Packet.draw":24.260,"Driver.Draw":24.202,"Graphics.Draw":21.205,"Graphics.Draw.draw_and_resource_release":13.175},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":14.661,"events_lost":220656,"totals_full":false}},"trace":"aps5-trace-78f5788e6bcf-83849.json.gz"}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
{"commit":"4ef0c8c5d034","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_FRAMES":"1100:300"},"gpu":"Apple M5 Pro","driver":"KosmicKrisp Mesa 27.0.0-devel (git-b5b73e5964)","os":"Darwin 27.2.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":18,"memory_mib":49152,"title_id":"PPSA21564","title":"ASTRO BOT","content_version":"01.007.000","resolution":"3840x2160","window":{"spec":"frames 1100:300","first_present":1100,"ms":25369.456},"presents":300,"frame_ms":{"p50":81.852,"p90":98.83,"p99":157.339,"max":259.677},"gpu_busy_ms_per_present":63.09,"worker_busy_pct":{"queue worker 0x0":98.617,"queue worker 0x40":99.615,"queue worker 0x48":98.439,"queue worker 0x3a":0.0,"queue worker 0x46":0.546},"top_phases_ms_per_frame":{"Driver.Packet":250.781,"Driver.Packet.memory_wait":162.354,"Driver.Packet.prepare_memory":30.517,"Exp.FenceWait.idle":28.351,"Driver.Packet.draw":26.053,"Driver.Draw":25.983,"Graphics.Draw":22.531,"Graphics.Draw.draw_and_resource_release":15.349},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":14.367,"events_lost":251708,"totals_full":false}},"trace":"aps5-trace-4ef0c8c5d034-82982.json.gz"}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
{"commit":"78f5788e6bcf","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_FRAMES":"1100:300"},"gpu":"Apple M5 Pro","driver":"KosmicKrisp Mesa 27.0.0-devel (git-b5b73e5964)","os":"Darwin 27.2.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":18,"memory_mib":49152,"title_id":"PPSA21564","title":"ASTRO BOT","content_version":"01.007.000","resolution":"3840x2160","window":{"spec":"frames 1100:300","first_present":1100,"ms":23469.562},"presents":300,"frame_ms":{"p50":77.866,"p90":92.309,"p99":104.691,"max":131.074},"gpu_busy_ms_per_present":60.375,"worker_busy_pct":{"queue worker 0x0":99.177,"queue worker 0x40":99.64,"queue worker 0x48":98.758,"queue worker 0x3a":0.0},"top_phases_ms_per_frame":{"Driver.Packet":232.349,"Driver.Packet.memory_wait":150.957,"Driver.Packet.prepare_memory":29.948,"Exp.FenceWait.idle":28.117,"Driver.Packet.draw":24.26,"Driver.Draw":24.202,"Graphics.Draw":21.205,"Graphics.Draw.draw_and_resource_release":13.175},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":14.661,"events_lost":220656,"totals_full":false}},"trace":"aps5-trace-78f5788e6bcf-83849.json.gz"}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