Skip to content

perf(libc): unprotect the rest of the 64 KiB block after a sequential write fault on macOS - #13783

Merged
adriadam10 merged 9 commits into
boykopovar:mainfrom
Gonic0:perf/libc-write-watch-fault-ahead
Nov 1, 2026
Merged

adriadam10 merged 9 commits into
boykopovar:mainfrom
Gonic0:perf/libc-write-watch-fault-ahead

Conversation

@Gonic0

@Gonic0 Gonic0 commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

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', syscall table) during that phase counted 65,900 sigreturn and 66,799 mprotect(…, 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 mprotect and 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_AHEAD caps 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_cpu and the three agc_driver_depth_plane_partial_store entries), 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 main plus 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.

round frame p50 / p90 (ms) without with GPU busy per present (ms) without → with intro FPS without / with
1 263.1 / 361.6 173.7 / 490.1 118.6 → 70.3 52.2 / 51.4
2 241.8 / 286.0 49.9 / 167.5 118.2 → 35.3 51.6 / 33.4
3 240.2 / 391.3 123.4 / 278.9 104.8 → 61.2 51.6 / 52.4
4 276.1 / 332.4 50.2 / 52.3 126.7 → 31.0 50.4 / 51.3
5 228.6 / 328.0 173.4 / 265.3 116.0 → 64.0 51.9 / 52.2
6 608.4 / 778.0 216.9 / 281.1 156.0 → 89.9 51.1 / 52.0

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:

Tested

Apple M5 Pro, macOS 27.2. write_watch_tests built with clang++ -std=c++20 -O2 -Wall -Wextra from this branch, for x86-64 (Rosetta 2) and arm64: every mode passes on both, including the new ones. Configuring core/libs/tests/write_watch with CMake on main fails 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:
    • writes to two adjacent pages report exactly those two;
    • three sequential writes report the third page and the rest of its 64 KiB block;
    • three sequential writes that end a block do not open the next block;
    • writes after a collection report exactly the written pages;
    • the window stops at a page that is already writable;
    • it stops at a read-only page, and at a writable page with another protection (an executable one, x86-64 only, since arm64 refuses a writable and executable mapping); that page keeps its protection;
    • it stops at the end of the watched range, and the bytes written near it are kept.
  • fault-ahead-off, the same sequence with APS5_WRITE_WATCH_FAULT_AHEAD=0: each sequential write reports only its page.

Negative controls, each of which fails a mode:

  • one written predecessor instead of two: "writes to two adjacent pages are not reported exactly";
  • no fault-ahead: "a third sequential write fault does not open the rest of its 64 KiB block";
  • no block limit (the first version): "a third sequential write fault does not open the rest of its 64 KiB block";
  • the window continuing over writable pages: "fault-ahead does not stop at a page that is already writable";
  • no protection check in the window: "fault-ahead does not stop at a writable page with another protection";
  • the environment variable ignored: "with fault-ahead off, sequential writes are not reported exactly";
  • only the faulting page recorded as written: "a third sequential write fault does not open the rest of its 64 KiB block".

In the tree above, the agc_*, guest_* and write_watch* CTest entries (2,163) give 49 failures with this change and 48 with APS5_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

  • 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: test(libc): register the macOS write watch fault tests once #13220 (the write-watch tests configure only with it)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M5 Pro (MoltenVK)
  • AI-assisted: yes

🤖 Generated with Claude Code

Gonic0 and others added 6 commits October 30, 2026 23:13
…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>
@github-actions github-actions Bot added perf Performance change libc build docs labels Oct 31, 2026
@github-actions

github-actions Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 1896 docs/dev/ENVIRONMENT.md
PR 4472 docs/dev/ENVIRONMENT.md
PR 5021 docs/dev/debt/libc.md
PR 6501 docs/dev/ENVIRONMENT.md
PR 6565 docs/dev/debt/libc.md
PR 6924 docs/dev/ENVIRONMENT.md
PR 7081 docs/dev/debt/libc.md
PR 7192 core/libs/prx/libc/src/GuestWriteWatch.cpp
PR 7386 docs/dev/debt/libc.md
PR 7447 docs/dev/debt/libc.md
PR 7449 docs/dev/ENVIRONMENT.md
PR 7457 docs/dev/ENVIRONMENT.md
PR 7514 docs/dev/ENVIRONMENT.md
PR 7604 docs/dev/ENVIRONMENT.md
PR 7715 docs/dev/debt/libc.md
PR 7922 docs/dev/ENVIRONMENT.md
PR 8004 docs/dev/ENVIRONMENT.md
PR 8150 docs/dev/debt/libc.md
PR 8484 docs/dev/ENVIRONMENT.md
PR 8542 docs/dev/ENVIRONMENT.md
PR 8965 docs/dev/debt/libc.md
PR 9012 docs/dev/ENVIRONMENT.md
PR 9176 docs/dev/ENVIRONMENT.md
PR 9248 docs/dev/ENVIRONMENT.md
PR 9287 docs/dev/ENVIRONMENT.md
PR 9289 docs/dev/ENVIRONMENT.md
PR 9340 docs/dev/debt/libc.md
PR 9345 docs/dev/ENVIRONMENT.md
PR 9356 docs/dev/ENVIRONMENT.md
PR 9371 docs/dev/debt/libc.md
PR 9387 docs/dev/ENVIRONMENT.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9712 docs/dev/ENVIRONMENT.md
PR 9852 docs/dev/ENVIRONMENT.md
PR 9863 docs/dev/debt/libc.md
PR 9898 docs/dev/ENVIRONMENT.md
PR 9929 docs/dev/debt/libc.md
PR 10026 docs/dev/ENVIRONMENT.md
PR 10069 docs/dev/ENVIRONMENT.md
PR 10113 docs/dev/debt/libc.md
PR 10193 docs/dev/ENVIRONMENT.md
PR 10324 docs/dev/debt/libc.md
PR 10349 docs/dev/debt/libc.md
PR 10382 docs/dev/ENVIRONMENT.md
PR 10405 docs/dev/debt/libc.md
PR 10442 docs/dev/ENVIRONMENT.md
PR 10547 docs/dev/ENVIRONMENT.md
PR 10729 docs/dev/debt/libc.md
PR 10757 docs/dev/ENVIRONMENT.md
PR 10989 docs/dev/debt/libc.md
PR 11262 docs/dev/debt/libc.md
PR 11266 docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11351 docs/dev/ENVIRONMENT.md
PR 11435 docs/dev/ENVIRONMENT.md
PR 11436 docs/dev/ENVIRONMENT.md
PR 11628 core/libs/tests/write_watch/CMakeLists.txt
PR 11661 core/libs/tests/write_watch/CMakeLists.txt
docs/dev/debt/libc.md
PR 11689 core/libs/tests/write_watch/CMakeLists.txt
docs/dev/debt/libc.md
PR 11698 core/libs/tests/write_watch/CMakeLists.txt
docs/dev/debt/libc.md
PR 11831 core/libs/tests/write_watch/CMakeLists.txt
docs/dev/debt/libc.md
PR 12120 docs/dev/ENVIRONMENT.md
PR 12323 docs/dev/ENVIRONMENT.md
PR 12324 docs/dev/ENVIRONMENT.md
PR 12544 docs/dev/ENVIRONMENT.md
PR 12560 docs/dev/debt/libc.md
PR 12586 docs/dev/debt/libc.md
PR 12682 docs/dev/debt/libc.md
PR 12688 docs/dev/debt/libc.md
PR 12700 docs/dev/debt/libc.md
PR 12746 docs/dev/ENVIRONMENT.md
PR 12863 docs/dev/ENVIRONMENT.md
PR 12965 docs/dev/debt/libc.md
PR 13793 docs/dev/ENVIRONMENT.md
docs/dev/debt/libc.md
PR 13809 docs/dev/ENVIRONMENT.md
docs/dev/debt/libc.md
PR 13863 docs/dev/ENVIRONMENT.md
PR 13920 docs/dev/ENVIRONMENT.md
PR 13928 docs/dev/ENVIRONMENT.md
PR 13929 docs/dev/ENVIRONMENT.md
PR 14051 docs/dev/ENVIRONMENT.md
PR 14212 docs/dev/debt/libc.md
PR 14235 docs/dev/ENVIRONMENT.md
docs/dev/debt/libc.md

@adriadam10 adriadam10 self-assigned this Oct 31, 2026
Gonic0 and others added 2 commits November 1, 2026 00:49
…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>
@adriadam10
adriadam10 merged commit 3ee2826 into boykopovar:main Nov 1, 2026
18 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Re-checked against current main with #13220 merged: the write-watch CMake list applies cleanly with one registration per mode, and the fault-ahead logic is unchanged since the reviewed head. Merged once the queue's full Build is green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants