perf(agc): copy only nonzero pages before a watched import #5698

Merged
milescoviello merged 1 commit from upstream-pr/5698 into main 2026-10-14 14:15:08 +00:00
milescoviello commented 2026-10-14 07:16:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-14T07:16:43Z.


What

Ghost of Tsushima (PPSA02225) maps about 4.9 GB of direct memory with sceKernelBatchMap2 at start-up, and the driver imports it (GuestAllocationsEnd → VulkanDevice::ImportGuestMemory → ImportMappedRanges → ImportWatched). The import leaves the range watched, so since #3874 ImportWatched first copies the whole range, to compare each page the import resolves with its bytes when the window closes. heaptrack shows this copy as the largest allocation the title makes: 4.92 GB of its 5.30 GB peak heap.

Pages that are all zero are no longer copied. ImportWatched gives each page of the range a slot: the index of its copy, or none for a page that was all zero. WindowCopy::Holds compares a page without a copy with a zero page. Every page is compared with the same bytes as before, so the window stamps the same pages as on main. The copy reads each page once to check it for zero and copies only the others; before, it copied every page.

agc_write_tracking gains CheckImportWindowZeroPages. It runs an import window over a block with bytes, two zero blocks and two blocks with other bytes after them. Inside the window it stores the same bytes into the first block, zero into the first zero block, and new bytes into the second zero block and the last block. It then checks which blocks read as written. The case also passes with main's full copy. It fails, each time with its own message, for three broken versions: one that takes a page without a copy as unchanged, one that takes it as changed, and one that looks up a copied page by its offset in the range instead of its slot.

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on main d8888df9b:

  • All 508 tests in libSceAgcDriver pass on an RTX 4070 Ti (NVIDIA 580.142). agc_driver_stencil_ref_export is skipped, as on main, because the device lacks VK_EXT_shader_stencil_export. agc_write_tracking passes 3/3.

  • Ghost of Tsushima (PPSA02225), relinked with --to-intel and run on Linux for 150 s under heaptrack. The two runs differ only in libSceAgcDriver.prx and start from the same shader cache:

    heaptrack main this PR
    peak heap 5.30 GB 2.08 GB
    ImportWatched at the peak 4.92 GB not among the allocations live at the peak
    peak RSS (with heaptrack's own) 10.27 GB 9.72 GB

    The title logs the same messages in both runs: the same 28 shaders fail to prepare, as on main.

  • A MinGW-w64 GCC 13 -fsyntax-only compile of both changed files has no errors.

  • tools/check_conventions.py --base upstream/main passes, and tools/pr_overlap.py --branch finds no overlap.

Not checked on Windows or on a console.

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
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4854353028 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5698). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-14T07:16:43Z. --- ### What Ghost of Tsushima (PPSA02225) maps about 4.9 GB of direct memory with `sceKernelBatchMap2` at start-up, and the driver imports it (`GuestAllocationsEnd` → `VulkanDevice::ImportGuestMemory` → `ImportMappedRanges` → `ImportWatched`). The import leaves the range watched, so since #3874 `ImportWatched` first copies the whole range, to compare each page the import resolves with its bytes when the window closes. heaptrack shows this copy as the largest allocation the title makes: 4.92 GB of its 5.30 GB peak heap. Pages that are all zero are no longer copied. `ImportWatched` gives each page of the range a slot: the index of its copy, or none for a page that was all zero. `WindowCopy::Holds` compares a page without a copy with a zero page. Every page is compared with the same bytes as before, so the window stamps the same pages as on `main`. The copy reads each page once to check it for zero and copies only the others; before, it copied every page. `agc_write_tracking` gains `CheckImportWindowZeroPages`. It runs an import window over a block with bytes, two zero blocks and two blocks with other bytes after them. Inside the window it stores the same bytes into the first block, zero into the first zero block, and new bytes into the second zero block and the last block. It then checks which blocks read as written. The case also passes with `main`'s full copy. It fails, each time with its own message, for three broken versions: one that takes a page without a copy as unchanged, one that takes it as changed, and one that looks up a copied page by its offset in the range instead of its slot. ### Tested Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on `main` d8888df9b: - All 508 tests in `libSceAgcDriver` pass on an RTX 4070 Ti (NVIDIA 580.142). `agc_driver_stencil_ref_export` is skipped, as on `main`, because the device lacks `VK_EXT_shader_stencil_export`. `agc_write_tracking` passes 3/3. - Ghost of Tsushima (PPSA02225), relinked with `--to-intel` and run on Linux for 150 s under heaptrack. The two runs differ only in `libSceAgcDriver.prx` and start from the same shader cache: | heaptrack | `main` | this PR | |---|---|---| | peak heap | 5.30 GB | 2.08 GB | | `ImportWatched` at the peak | 4.92 GB | not among the allocations live at the peak | | peak RSS (with heaptrack's own) | 10.27 GB | 9.72 GB | The title logs the same messages in both runs: the same 28 shaders fail to prepare, as on `main`. - A MinGW-w64 GCC 13 `-fsyntax-only` compile of both changed files has no errors. - `tools/check_conventions.py --base upstream/main` passes, and `tools/pr_overlap.py --branch` finds no overlap. Not checked on Windows or on a console. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 - [x] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-14 13:41:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T13:41:26Z.


Checked the diff against main: slots per 4 KiB page, zero pages compared against a zero page, same bytes compared as before. agc_write_tracking passes; the new case passes without the change (as the description says) and fails when Holds treats a zero page as unchanged. Merging once the Windows build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6169749057 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5698#issuecomment-6169749057). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-14T13:41:26Z. --- Checked the diff against main: slots per 4 KiB page, zero pages compared against a zero page, same bytes compared as before. `agc_write_tracking` passes; the new case passes without the change (as the description says) and fails when `Holds` treats a zero page as unchanged. Merging once the Windows build is green.
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!5698
No description provided.