Repository navigation
perf(libc): unprotect the rest of the 64 KiB block after a sequential write fault on macOS - #13783
Merged
adriadam10 merged 9 commits intoNov 1, 2026
Merged
Conversation
…macOS A guest thread that writes a large watched range in order takes one write-watch fault per page. Under Rosetta 2 each fault is a Mach exception round trip. When the two pages before the faulting one were written since the last collection, the handler now unprotects up to 32 pages from it that have the same protection and records them as written. APS5_WRITE_WATCH_FAULT_AHEAD sets the count; 0 or 1 keeps one page per fault. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… KiB block libSceAgcDriver decides per 64 KiB block whether a pending storage image keeps the CPU's bytes. A window that ran into the next block marked it CPU-written and dropped the GPU results there (agc_storage_write_back, agc_idle_publish_wait, array layer and depth plane tests). 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.
|
…ch-fault-ahead Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ch-fault-ahead Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
|
Re-checked against current |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
On macOS the write watch write-protects watched pages and takes one fault per page on the first write. A guest thread that writes a large watched range in order therefore takes one fault for every 4 KiB. Under Rosetta 2 each of those faults is a Mach exception round trip plus an
mprotect.In PPSA21564 (ASTRO BOT 01.007.000), shortly after the intro, one guest thread writes about 1.4 GB this way (0x512570000 to 0x5699df000, in order). A 3 s System Trace (
xctrace --template 'System Trace',syscalltable) during that phase counted 65,900sigreturnand 66,799mprotect(…, 0x1000, PROT_READ | PROT_WRITE)on that thread alone: about 22,000 faults per second, so about 90 MB/s of guest writes. The driver's queues wait on that thread, and the frame goes from about 20 ms to several hundred ms.With this change, when a write fault lands on a page whose two predecessors were written since the last collection and are writable again, the handler unprotects the rest of the faulting page's 64 KiB block in one
mprotectand records those pages as written. It stops earlier at the first page that is not write-protected by the watch, has a different protection, or ends the range. Two predecessors are required so that a single write across a page boundary still reports exactly its two pages. With 4 KiB pages (Rosetta 2) a sequential writer takes one fault per 64 KiB block instead of 16.APS5_WRITE_WATCH_FAULT_AHEADcaps the pages per fault (32 by default); 0 or 1 keeps one page per fault.The window stays inside one 64 KiB block on purpose. Marking a page written that the guest did not write makes libSceAgcDriver's write-back keep the CPU's bytes for that page's 64 KiB block, dropping a pending GPU result there (the keep rule in
writeBackLayers). Inside the faulting page's own block that changes nothing, because the block already counts as CPU-written once one of its pages is. A first version opened 32 pages regardless of blocks; it made nine driver tests fail (agc_storage_write_back,agc_storage_write_back_scratch,agc_driver_recorder,agc_idle_publish_wait,agc_driver_array_layer_target,agc_driver_array_layer_target_cpuand the threeagc_driver_depth_plane_partial_storeentries), for example "a CPU store into a unit with results pending was not seen by the refresh": the window ran from the CPU-written first block of a surface into the next one and dropped the GPU results there. They pass with the block limit. The debt entry for the macOS write watch says that a collection after a sequential write can report pages nobody wrote, inside a block one of whose pages was written.#13442 adds read traps to the same handler. Whichever of the two lands second has to stop the window at a read-trapped page as well; I'll add that to this PR or to #13442, depending on which merges first.
Performance
Apple M5 Pro, macOS 27.2, MoltenVK 1.4.2, 3840x2160. PPSA21564 01.007.000 from boot, warm shader cache, 100 s per run, the same tree (current
mainplus the open macOS port PRs) with and without this change, alternated. Traces cover presents 1100 to 1140 (APS5_TRACE_FRAMES=1100:40), in the slow phase after the intro.The intro, where the guest does not write this way, runs at the same rate (the 33.4 in round 2 was load on the machine during that run). Seconds to the 19th dumped frame (
APS5_DUMP_FRAMES_EVERY=60): 55, 55, 55, 58, 53 and more than 100 without; 64, 45, 47, 41, 47 and 53 with, so round 1 was slower with the change. This phase varies a lot from run to run, which is why I compare alternated pairs only.Frames dumped at the same present differ between the two builds where the scene animates on real time: at present 1080 the slower build already shows the logo breaking into particles, the faster one still shows the whole logo. Two runs of the same build differ as much where their speed differs; I saw no missing or corrupted content in frames 14 to 21.
Paired summary lines (rounds 4 and 6)
Round 4, without:
{"commit":"64613f54b0cb","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_DUMP_FRAMES":"200","APS5_DUMP_FRAMES_EVERY":"60","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_FRAMES":"1100:40"},"gpu":"Apple M5 Pro","driver":"MoltenVK 1.4.2","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:40","first_present":1100,"ms":10644.134},"presents":40,"frame_ms":{"p50":276.066,"p90":332.402,"p99":374.701,"max":374.701},"gpu_busy_ms_per_present":126.669,"worker_busy_pct":{"queue worker 0x0":99.739,"queue worker 0x40":97.380,"queue worker 0x48":94.470,"queue worker 0x3a":0.000},"top_phases_ms_per_frame":{"Driver.Packet":775.607,"Driver.Packet.memory_wait":507.061,"Graphics.Draw":109.419,"Driver.Packet.prepare_memory":104.529,"Driver.Packet.draw":70.208,"Driver.Draw":70.149,"Graphics.Draw.draw_and_resource_release":53.883,"Driver.Draw.draw_and_resource_release":52.452},"trace_dropped":false,"trace":"aps5-trace-64613f54b0cb-68798.json.gz"}Round 4, with this change:
{"commit":"a20b7f91dbe4","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_DUMP_FRAMES":"200","APS5_DUMP_FRAMES_EVERY":"60","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_FRAMES":"1100:40"},"gpu":"Apple M5 Pro","driver":"MoltenVK 1.4.2","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:40","first_present":1100,"ms":2262.041},"presents":40,"frame_ms":{"p50":50.158,"p90":52.252,"p99":193.395,"max":193.395},"gpu_busy_ms_per_present":31.022,"worker_busy_pct":{"queue worker 0x0":99.024,"queue worker 0x40":91.748,"queue worker 0x48":84.939,"queue worker 0x3a":0.000},"top_phases_ms_per_frame":{"Driver.Packet":155.512,"Driver.Packet.memory_wait":97.318,"Driver.Packet.draw":19.129,"Driver.Draw":19.078,"Driver.Packet.prepare_memory":18.016,"Graphics.Draw":16.329,"Driver.Packet.dispatch":10.193,"Graphics.Draw.draw_and_resource_release":9.549},"trace_dropped":false,"trace":"aps5-trace-a20b7f91dbe4-69863.json.gz"}Round 6, without:
{"commit":"64613f54b0cb","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_DUMP_FRAMES":"200","APS5_DUMP_FRAMES_EVERY":"60","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_FRAMES":"1100:40"},"gpu":"Apple M5 Pro","driver":"MoltenVK 1.4.2","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:40","first_present":1100,"ms":24761.846},"presents":40,"frame_ms":{"p50":608.361,"p90":778.038,"p99":923.626,"max":923.626},"gpu_busy_ms_per_present":156.031,"worker_busy_pct":{"queue worker 0x0":99.893,"queue worker 0x40":97.405,"queue worker 0x48":98.809,"queue worker 0x3a":0.000},"top_phases_ms_per_frame":{"Driver.Packet":1832.532,"Driver.Packet.memory_wait":1212.578,"Graphics.Draw":205.918,"Driver.Packet.prepare_memory":175.897,"Recorder.Submit":145.094,"Graphics.Draw.draw_and_resource_release":142.562,"Driver.Packet.draw":81.874,"Driver.Draw":81.815},"trace_dropped":false,"trace":"aps5-trace-64613f54b0cb-70737.json.gz"}Round 6, with this change:
{"commit":"a20b7f91dbe4","dirty":null,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_DUMP_FRAMES":"200","APS5_DUMP_FRAMES_EVERY":"60","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_FRAMES":"1100:40"},"gpu":"Apple M5 Pro","driver":"MoltenVK 1.4.2","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:40","first_present":1100,"ms":8825.433},"presents":40,"frame_ms":{"p50":216.885,"p90":281.136,"p99":383.870,"max":383.870},"gpu_busy_ms_per_present":89.919,"worker_busy_pct":{"queue worker 0x0":99.668,"queue worker 0x40":99.975,"queue worker 0x48":98.456,"queue worker 0x3a":0.000},"top_phases_ms_per_frame":{"Driver.Packet":656.029,"Driver.Packet.memory_wait":434.839,"Graphics.Draw":80.893,"Driver.Packet.prepare_memory":66.735,"Driver.Packet.draw":51.568,"Driver.Draw":51.518,"Graphics.Draw.draw_and_resource_release":44.736,"Recorder.Submit":43.246},"trace_dropped":false,"trace":"aps5-trace-a20b7f91dbe4-71468.json.gz"}Traces:
main: round4-main.json.gzmain: round6-main.json.gzTested
Apple M5 Pro, macOS 27.2.
write_watch_testsbuilt withclang++ -std=c++20 -O2 -Wall -Wextrafrom this branch, for x86-64 (Rosetta 2) and arm64: every mode passes on both, including the new ones. Configuringcore/libs/tests/write_watchwith CMake onmainfails on macOS because two test names are registered twice; #13220 fixes that, so the new CTest entries need it too.New modes:
fault-ahead, on an 80-page watched range:fault-ahead-off, the same sequence withAPS5_WRITE_WATCH_FAULT_AHEAD=0: each sequential write reports only its page.Negative controls, each of which fails a mode:
In the tree above, the
agc_*,guest_*andwrite_watch*CTest entries (2,163) give 49 failures with this change and 48 withAPS5_WRITE_WATCH_FAULT_AHEAD=0. The extra one,agc_driver_graphics, also fails without the change on reruns ("two ranges under a budget for two were not imported", 1 of 2); every other failure is the same on both sides.Not tested on Linux or Windows, where nothing changes.
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