perf(libc,agc): re-land skipping never-written guest pages in Windows heap mirror zero scans #18144

Merged
sktharunbalaji13-cmd merged 3 commits from upstream-pr/18144 into main 2026-11-11 00:36:49 +00:00
sktharunbalaji13-cmd commented 2026-11-10 16:21:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by sktharunbalaji13-cmd on 2026-11-10T16:21:46Z.


What

Re-lands #15967, which #18129 reverted because it hid a guest store of zero over a never-written page from the write tracker. Main failed agc_driver_msaa_dcc_clear_2/4/8 and agc_driver_native_depth_publication on real Windows GPUs (#18113, #18114); the Windows CI job has no GPU and skipped them.

Commits:

  • the two original #15967 commits, unchanged apart from a rebase: the Windows guest arena keeps a bitmap of fresh committed pages nothing has written, and the heap mirror zero scans (possiblyNonzero, GuestBytesZero) skip them instead of reading every page of a large, mostly empty heap. Review of the rebase: MarkPossiblyWritten(address, bytes, order) was the one overload without ForgetKnownZero; it now has it.
  • the fix: only the known-zero bookkeeping (harvestZero, resetWrites) may drop a watched page that still reads as zero. watchWrites takes dropZero, default off, so collectOwnedWrites, which feeds Collect, CollectFresh and CollectArmed, reports every watched page and clears its known-zero bit. A first read of a never-written page also sets its write-watch bit, so such a page is reported as a write again; that is the behaviour before #15967.

Why in the collector and not in each consumer: every residency check (StoredOver, UnchangedSince*, CpuWroteSince, UnchangedCommittedSince, the MSAA DCC clear and the native depth publication) compares against a generation that collectWindowsWrites or walkHints stamps from these collects, so they see a zero store without a per-caller GuestMemory::ForgetKnownZero. Survey of the paths that still filter:

  • harvestZero (inside KnownZero): a flagged page that still reads as zero keeps its known-zero bit. Its bytes are zero, so the heap mirror skipping it is correct; the flag is not reset.
  • resetWrites: only settles pages of a Collection::Fresh run, which Collect already reports as fresh (report = fresh), so consumers treat them as changed regardless of the watch.
  • The owned-block checks and the resident-run dirty check no longer filter.
  • Non-Windows builds are untouched (ForgetKnownZero is a no-op there).

@angrysandhill's 2bf167c210 takes the other route, ForgetKnownZero calls in MultisampleTarget::initialDccClear and prepareDepthSampling/prepareUniformPlaneSampling. It passes the same tests; I went with the collector because it also covers any consumer not listed. Thank you for the quick reproduction and the bisect.

Generic behaviour: nothing depends on a title, address or size.

Tested

Windows 11 26200, RTX 4060 Laptop GPU (8 GB), NVIDIA driver 617.42, MinGW GCC (winlibs) Release, BUILD_TESTING=ON, on main c5488026ab plus the commits of this PR, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation:

Test without the fix commit this PR
agc_driver_msaa_dcc_clear_2 / _4 / _8 failed ("a same-value CPU color store did not invalidate multisampled DCC residency") passed
agc_driver_native_depth_publication failed ("a native depth publication overwrote a byte the guest stored") passed
windows_write_watch failed ("a zero store into a never-written page was not collected as a write") passed
windows_write_watch_verify_direct, agc_driver_resident_heap_mirror passed passed

CheckKnownZeroFirstReads in core/libs/tests/WindowsWriteWatch.cpp is the test that needs no GPU: it stores zero into a never-written page and requires Collect to report it, and requires a real store to be reported too. It failed on the pre-fix code, so GitHub's GPU-less Windows job would have caught this regression.

Wider run, 90 tests with -R "host_import|guest_memory|guest_owned_memory|committed_read_mapping|copied_writer|linear_target|private_collect|write_watch|storage_write_back|read_only_lease_writer|partially_resident_snapshot|copy_guest_bytes|direct_write_completion|write_tracking|heap_mirror|msaa_dcc|native_depth_publication": 90 of 90 pass, 4 are skipped by design (agc_driver_msaa_dcc_compared_import_2/4/8 "Linux comparison mode only", agc_host_import_sparse). No VUID- in the log and no was not found but was requested by env var VK_INSTANCE_LAYERS.

Performance capture, main against this head, same title, scene and machine (Ghost of Tsushima Director's Cut PPSA02225, boot to the PlayStation Studios intro, RTX 4060 Laptop, driver 617.42, Windows 11 26200, 16 GB RAM, freshly restarted, APS5_TRACE_SECONDS=0:40 APS5_SKIP_REFUSED_DRAWS=1 APS5_PROFILE_DRAW=1 APS5_SOFT_DEPTH_CHECKS=1). main is c5488026ab (after the revert), the head is d372128ba1, each built as a full library set from the same tree, three runs each alternating, 80 to 100 s per run. Frame and GPU numbers are the 40 s tools/trace_digest.py window starting at present 0:

run free RAM at start presents in window frame p50 / p90 / p99 / max (ms) GPU busy per present peak private
main 1 10382 MiB 398 17.8 / 274 / 1157 / 12627 12.1 ms 25.5 GB
main 2 11752 MiB 688 17.8 / 20 / 391 / 11841 8.2 ms 23.6 GB
main 3 11673 MiB 367 17.4 / 275 / 951 / 12981 11.7 ms 24.9 GB
head 1 10234 MiB 111 16.7 / 57 / 6506 / 27365 12.9 ms 20.5 GB
head 2 11474 MiB 357 18.7 / 273 / 1780 / 7589 16.9 ms 26.0 GB
head 3 11742 MiB 355 18.4 / 280 / 1609 / 6823 16.7 ms 28.2 GB

A first main run froze after 160 presents (the trace window never closed, so it has no summary line) and was repeated; it is not in the table. The blue PlayStation logo appeared in every run of both builds except head 1.

Result: no speed or memory difference in this scene at 10 to 12 GB free RAM; the run-to-run spread (111 to 688 presents) is larger than any difference between the two builds. #15967 measured its gain at about 5 GB free RAM, where the old scans exhausted memory, and I did not reproduce a gain here. This PR therefore claims correctness only: the zero-scan skip returns, now without hiding zero stores.

Traces and aps5-summary.jsonl lines for all six runs: main summaries, head summaries, and the six traces next to them in that folder. The older #15967 captures at about 5 GB free are here. The zero-scan skipping that saves the RAM is unchanged from #15967, whose measurements are in that PR; only which write reports reach the tracker changed. 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 (re-lands #15967, reverted by #18129)
  • Fixes: none
  • Unblocks: PPSA02225 #4461
  • GPUs tested: NVIDIA GeForce RTX 4060 Laptop GPU, driver 617.42, Windows 11 26200 (the four tests above, agc_driver_resident_heap_mirror and the wider run)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5244821565 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18144). Originally posted by [sktharunbalaji13-cmd](https://github.com/sktharunbalaji13-cmd) on 2026-11-10T16:21:46Z. --- ### What Re-lands #15967, which #18129 reverted because it hid a guest store of zero over a never-written page from the write tracker. Main failed `agc_driver_msaa_dcc_clear_2/4/8` and `agc_driver_native_depth_publication` on real Windows GPUs (#18113, #18114); the Windows CI job has no GPU and skipped them. Commits: - the two original #15967 commits, unchanged apart from a rebase: the Windows guest arena keeps a bitmap of fresh committed pages nothing has written, and the heap mirror zero scans (`possiblyNonzero`, `GuestBytesZero`) skip them instead of reading every page of a large, mostly empty heap. Review of the rebase: `MarkPossiblyWritten(address, bytes, order)` was the one overload without `ForgetKnownZero`; it now has it. - the fix: only the known-zero bookkeeping (`harvestZero`, `resetWrites`) may drop a watched page that still reads as zero. `watchWrites` takes `dropZero`, default off, so `collectOwnedWrites`, which feeds `Collect`, `CollectFresh` and `CollectArmed`, reports every watched page and clears its known-zero bit. A first read of a never-written page also sets its write-watch bit, so such a page is reported as a write again; that is the behaviour before #15967. Why in the collector and not in each consumer: every residency check (`StoredOver`, `UnchangedSince*`, `CpuWroteSince`, `UnchangedCommittedSince`, the MSAA DCC clear and the native depth publication) compares against a generation that `collectWindowsWrites` or `walkHints` stamps from these collects, so they see a zero store without a per-caller `GuestMemory::ForgetKnownZero`. Survey of the paths that still filter: - `harvestZero` (inside `KnownZero`): a flagged page that still reads as zero keeps its known-zero bit. Its bytes are zero, so the heap mirror skipping it is correct; the flag is not reset. - `resetWrites`: only settles pages of a `Collection::Fresh` run, which `Collect` already reports as fresh (`report = fresh`), so consumers treat them as changed regardless of the watch. - The owned-block checks and the resident-run dirty check no longer filter. - Non-Windows builds are untouched (`ForgetKnownZero` is a no-op there). @angrysandhill's `2bf167c210` takes the other route, `ForgetKnownZero` calls in `MultisampleTarget::initialDccClear` and `prepareDepthSampling`/`prepareUniformPlaneSampling`. It passes the same tests; I went with the collector because it also covers any consumer not listed. Thank you for the quick reproduction and the bisect. Generic behaviour: nothing depends on a title, address or size. ### Tested Windows 11 26200, RTX 4060 Laptop GPU (8 GB), NVIDIA driver 617.42, MinGW GCC (winlibs) Release, `BUILD_TESTING=ON`, on `main` c5488026ab plus the commits of this PR, `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`: | Test | without the fix commit | this PR | |---|---|---| | `agc_driver_msaa_dcc_clear_2` / `_4` / `_8` | failed ("a same-value CPU color store did not invalidate multisampled DCC residency") | passed | | `agc_driver_native_depth_publication` | failed ("a native depth publication overwrote a byte the guest stored") | passed | | `windows_write_watch` | failed ("a zero store into a never-written page was not collected as a write") | passed | | `windows_write_watch_verify_direct`, `agc_driver_resident_heap_mirror` | passed | passed | `CheckKnownZeroFirstReads` in `core/libs/tests/WindowsWriteWatch.cpp` is the test that needs no GPU: it stores zero into a never-written page and requires `Collect` to report it, and requires a real store to be reported too. It failed on the pre-fix code, so GitHub's GPU-less Windows job would have caught this regression. Wider run, 90 tests with `-R "host_import|guest_memory|guest_owned_memory|committed_read_mapping|copied_writer|linear_target|private_collect|write_watch|storage_write_back|read_only_lease_writer|partially_resident_snapshot|copy_guest_bytes|direct_write_completion|write_tracking|heap_mirror|msaa_dcc|native_depth_publication"`: 90 of 90 pass, 4 are skipped by design (`agc_driver_msaa_dcc_compared_import_2/4/8` "Linux comparison mode only", `agc_host_import_sparse`). No `VUID-` in the log and no `was not found but was requested by env var VK_INSTANCE_LAYERS`. Performance capture, `main` against this head, same title, scene and machine (Ghost of Tsushima Director's Cut PPSA02225, boot to the PlayStation Studios intro, RTX 4060 Laptop, driver 617.42, Windows 11 26200, 16 GB RAM, freshly restarted, `APS5_TRACE_SECONDS=0:40 APS5_SKIP_REFUSED_DRAWS=1 APS5_PROFILE_DRAW=1 APS5_SOFT_DEPTH_CHECKS=1`). `main` is c5488026ab (after the revert), the head is d372128ba1, each built as a full library set from the same tree, three runs each alternating, 80 to 100 s per run. Frame and GPU numbers are the 40 s `tools/trace_digest.py` window starting at present 0: | run | free RAM at start | presents in window | frame p50 / p90 / p99 / max (ms) | GPU busy per present | peak private | |---|---|---|---|---|---| | main 1 | 10382 MiB | 398 | 17.8 / 274 / 1157 / 12627 | 12.1 ms | 25.5 GB | | main 2 | 11752 MiB | 688 | 17.8 / 20 / 391 / 11841 | 8.2 ms | 23.6 GB | | main 3 | 11673 MiB | 367 | 17.4 / 275 / 951 / 12981 | 11.7 ms | 24.9 GB | | head 1 | 10234 MiB | 111 | 16.7 / 57 / 6506 / 27365 | 12.9 ms | 20.5 GB | | head 2 | 11474 MiB | 357 | 18.7 / 273 / 1780 / 7589 | 16.9 ms | 26.0 GB | | head 3 | 11742 MiB | 355 | 18.4 / 280 / 1609 / 6823 | 16.7 ms | 28.2 GB | A first `main` run froze after 160 presents (the trace window never closed, so it has no summary line) and was repeated; it is not in the table. The blue PlayStation logo appeared in every run of both builds except head 1. Result: no speed or memory difference in this scene at 10 to 12 GB free RAM; the run-to-run spread (111 to 688 presents) is larger than any difference between the two builds. #15967 measured its gain at about 5 GB free RAM, where the old scans exhausted memory, and I did not reproduce a gain here. This PR therefore claims correctness only: the zero-scan skip returns, now without hiding zero stores. Traces and `aps5-summary.jsonl` lines for all six runs: [`main` summaries](https://github.com/sktharunbalaji13-cmd/AnyPS5/blob/evidence/got-ram-logo/ghost-of-tsushima/zero-scan-reland-2026-11-10/traces/main-aps5-summary.jsonl), [head summaries](https://github.com/sktharunbalaji13-cmd/AnyPS5/blob/evidence/got-ram-logo/ghost-of-tsushima/zero-scan-reland-2026-11-10/traces/head-aps5-summary.jsonl), and the six traces next to them in [that folder](https://github.com/sktharunbalaji13-cmd/AnyPS5/tree/evidence/got-ram-logo/ghost-of-tsushima/zero-scan-reland-2026-11-10/traces). The older #15967 captures at about 5 GB free are [here](https://github.com/sktharunbalaji13-cmd/AnyPS5/tree/evidence/got-ram-logo/ghost-of-tsushima/lowmem-2026-11-05/traces). The zero-scan skipping that saves the RAM is unchanged from #15967, whose measurements are in that PR; only which write reports reach the tracker changed. `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 (re-lands #15967, reverted by #18129) - [x] Fixes: none - [x] Unblocks: PPSA02225 #4461 - [x] GPUs tested: NVIDIA GeForce RTX 4060 Laptop GPU, driver 617.42, Windows 11 26200 (the four tests above, `agc_driver_resident_heap_mirror` and the wider run) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!18144
No description provided.