perf(agc-driver): import adjacent registered ranges as one host-import span #3158

Merged
XairRrR merged 1 commit from upstream-pr/3158 into main 2026-10-11 07:14:16 +00:00
XairRrR commented 2026-10-10 23:32:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-10T23:32:04Z.


What

A host import covered exactly one registered range. A buffer crossing two ranges had no import, so every use copied it on the CPU under the GPU mutex.

PPSA05684 maps its GPU heap as 1 MiB direct-memory pieces. Every buffer larger than a piece took this path in its first level: about 1,000 copies of 9 to 15 MiB per 10 s ([buffers] ... outside imports), about 15 GiB per 10 s of guest reads.

An import now covers the run of address-adjacent registered ranges that are readable with the same write and GPU access:

  • Runs never overlap, so imports don't either.
  • An import retires when any range of its run changes.
  • A refused run is remembered by its base.
  • ImportMappedRanges imports with the full registry, so it creates the same span.
  • Callers already address imports relative to their base.

In the first level, CPU copies drop from 15.9 GiB to 316 MiB per run and the frame rate goes from 1.34 fps to 2.76 fps.

Two 12-minute runs reaching the first level (world-boat-intro); frame rate and median over the last 60 s, copies over the whole run (frame-timing.log):

Before After
Frame rate 1.34 fps 2.76 fps
Median frame time 557 ms 362 ms
Guest bytes copied on the CPU ([guestmem] write-changed) 15.9 GiB 316 MiB
Buffers copied outside imports ~1,000 per 10 s none

Tests in tests/BdaResources.cpp:

  • importCrossingTests now expects the two ranges as one span.
  • importSpanTests covers: a writable range stays out of a read-only span, a view crossing ranges of different access gets no import, and removing a range retires the span.

Tested

  • Arch Linux (kernel 7.2.6), GCC 16.2.1, NVIDIA RTX 4070 Ti SUPER (driver 615.71.09), Release with BUILD_TESTING=ON.
  • Branch built alone; full ctest on main 91b13413: 734/735; the one failure, agc_driver_dcc_keys_first_write, fails the same way on plain main 91b13413. Conventions check passed.
  • Title: Uncharted: Legacy of Thieves Collection (PPSA05684), on its own bring-up branch.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4815201557 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3158). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-10T23:32:04Z. --- ### What A host import covered exactly one registered range. A buffer crossing two ranges had no import, so every use copied it on the CPU under the GPU mutex. PPSA05684 maps its GPU heap as 1 MiB direct-memory pieces. Every buffer larger than a piece took this path in its first level: about 1,000 copies of 9 to 15 MiB per 10 s (`[buffers] ... outside imports`), about 15 GiB per 10 s of guest reads. An import now covers the run of address-adjacent registered ranges that are readable with the same write and GPU access: - Runs never overlap, so imports don't either. - An import retires when any range of its run changes. - A refused run is remembered by its base. - `ImportMappedRanges` imports with the full registry, so it creates the same span. - Callers already address imports relative to their base. In the first level, CPU copies drop from 15.9 GiB to 316 MiB per run and the frame rate goes from 1.34 fps to 2.76 fps. Two 12-minute runs reaching the first level (`world-boat-intro`); frame rate and median over the last 60 s, copies over the whole run (`frame-timing.log`): | | Before | After | |---|---|---| | Frame rate | 1.34 fps | 2.76 fps | | Median frame time | 557 ms | 362 ms | | Guest bytes copied on the CPU (`[guestmem] write-changed`) | 15.9 GiB | 316 MiB | | Buffers copied outside imports | ~1,000 per 10 s | none | Tests in `tests/BdaResources.cpp`: - `importCrossingTests` now expects the two ranges as one span. - `importSpanTests` covers: a writable range stays out of a read-only span, a view crossing ranges of different access gets no import, and removing a range retires the span. ### Tested - Arch Linux (kernel 7.2.6), GCC 16.2.1, NVIDIA RTX 4070 Ti SUPER (driver 615.71.09), Release with `BUILD_TESTING=ON`. - Branch built alone; full ctest on main 91b13413: 734/735; the one failure, `agc_driver_dcc_keys_first_write`, fails the same way on plain main 91b13413. Conventions check passed. - Title: Uncharted: Legacy of Thieves Collection (PPSA05684), on its own bring-up branch. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
Dextroid17 commented 2026-10-10 23:45:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-10T23:45:44Z.

Review: COMMENTED


Two performance changes that share a cause — work serialised behind a lock — though they arrive at it differently: #3159 moves a compile out of the critical section, while #3158 widens a host import so that a buffer crossing two registered ranges stops taking the copied path under the GPU mutex at all. Both descriptions carry the measurement that turns the claim into a fact.

#3158, one import per run of ranges. The pathology is stated in a form that would survive any review: a host import covered exactly one registered range, PPSA05684 maps its heap in 1 MiB pieces, so every buffer larger than a piece had no import and every use was copied on the CPU under the GPU mutex — about 1,000 copies of 9 to 15 MiB per 10 s, roughly 15 GiB per 10 s of guest reads, with the [buffers] ... outside imports line to look for. Covering a run of address-adjacent ranges readable with the same access is the right generalisation, and the four properties you state — disjoint runs, retirement when any range of a run changes, a refused run remembered by its base, and the full-registry path producing the same import — are exactly the ones a reader would ask about. Stating them is what makes the widening reviewable rather than a leap.

#3159, preparing before the lock. This one is sharper still, because the cost is attached to something a player feels: the preparation transaction took the global writers lock when constructed and held it through a compile, so sceAgcCreateInterpolantMapping and sceAgcLinkShaders serialised every thread on one variant build — and the frames over 150 ms spent about 515 ms in Shader.Register against 9 ms in the others. Taking the lock on first use and preparing the variants before reading prepared state is the standard shape, and the samples are what rule out the simpler explanation that the lock was merely contended.

What both get right is the part usually left out: a before figure, an after figure, the log or sample that produced them, and the properties the new code has to hold for the widening to be safe. That is what makes a performance change reviewable instead of hopeful.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481224279 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3158#pullrequestreview-5481224279). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-10T23:45:44Z. **Review:** COMMENTED --- Two performance changes that share a cause — work serialised behind a lock — though they arrive at it differently: #3159 moves a compile out of the critical section, while #3158 widens a host import so that a buffer crossing two registered ranges stops taking the copied path under the GPU mutex at all. Both descriptions carry the measurement that turns the claim into a fact. **#3158, one import per run of ranges.** The pathology is stated in a form that would survive any review: a host import covered exactly one registered range, PPSA05684 maps its heap in 1 MiB pieces, so every buffer larger than a piece had no import and every use was copied on the CPU *under the GPU mutex* — about 1,000 copies of 9 to 15 MiB per 10 s, roughly 15 GiB per 10 s of guest reads, with the `[buffers] ... outside imports` line to look for. Covering a run of address-adjacent ranges readable with the same access is the right generalisation, and the four properties you state — disjoint runs, retirement when any range of a run changes, a refused run remembered by its base, and the full-registry path producing the same import — are exactly the ones a reader would ask about. Stating them is what makes the widening reviewable rather than a leap. **#3159, preparing before the lock.** This one is sharper still, because the cost is attached to something a player feels: the preparation transaction took the global writers lock when constructed and held it through a compile, so `sceAgcCreateInterpolantMapping` and `sceAgcLinkShaders` serialised every thread on one variant build — and the frames over 150 ms spent about 515 ms in `Shader.Register` against 9 ms in the others. Taking the lock on first use and preparing the variants before reading prepared state is the standard shape, and the samples are what rule out the simpler explanation that the lock was merely contended. **What both get right is the part usually left out:** a before figure, an after figure, the log or sample that produced them, and the properties the new code has to hold for the widening to be safe. That is what makes a performance change reviewable instead of hopeful. Written with AI assistance.
Silverash-i8 commented 2026-10-11 01:50:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-11T01:50:44Z.


Windows run, since the PR was tested on Linux: Windows 11, WinLibs GCC 15.2.0 r7, Release, head 70d7aa58 merged onto main d31b57a4, on an RTX 5060 Laptop and the Arrow Lake iGPU.

agc_driver_bda_device and agc_driver_bda_span_reads (the BdaResources.cpp tests) pass on both GPUs. But agc_driver_dcc_keys_first_write fails on both with this PR, and passes on both on plain main d31b57a4. On Linux it already fails on main (#2767, #3179), so it couldn't show this there. I bisected it inside a batch of six PRs: main + this PR alone fails, main + #3213 alone passes.

read-write alias of shared guest memory 0x200000000+0x40000: the range is not one contiguous run of views of a section

That's MapAlias in libc/include/WindowsMappings.hpp. On Windows a read-write alias of shared guest memory needs the range to be one contiguous run of views of a single section, at consecutive section offsets. My reading is that two registered ranges can be next to each other in guest address and still be different views (or offsets) of the section. A span over both is then refused, where one import per range wasn't. If that's right, a span on Windows might need to stop where the views stop being contiguous, or fall back to the per-range imports when the alias is refused. I haven't tried a change on your branch.

Everything else in a full ctest with this PR (in that batch) matched main on both GPUs.

AI-assisted (Claude Code); I ran all of this on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104352455 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3158#issuecomment-6104352455). Originally posted by [Silverash-i8](https://github.com/Silverash-i8) on 2026-10-11T01:50:44Z. --- Windows run, since the PR was tested on Linux: Windows 11, WinLibs GCC 15.2.0 r7, Release, head `70d7aa58` merged onto `main` d31b57a4, on an RTX 5060 Laptop and the Arrow Lake iGPU. `agc_driver_bda_device` and `agc_driver_bda_span_reads` (the `BdaResources.cpp` tests) pass on both GPUs. But `agc_driver_dcc_keys_first_write` fails on both with this PR, and passes on both on plain `main` d31b57a4. On Linux it already fails on `main` (#2767, #3179), so it couldn't show this there. I bisected it inside a batch of six PRs: `main` + this PR alone fails, `main` + #3213 alone passes. ``` read-write alias of shared guest memory 0x200000000+0x40000: the range is not one contiguous run of views of a section ``` That's `MapAlias` in `libc/include/WindowsMappings.hpp`. On Windows a read-write alias of shared guest memory needs the range to be one contiguous run of views of a single section, at consecutive section offsets. My reading is that two registered ranges can be next to each other in guest address and still be different views (or offsets) of the section. A span over both is then refused, where one import per range wasn't. If that's right, a span on Windows might need to stop where the views stop being contiguous, or fall back to the per-range imports when the alias is refused. I haven't tried a change on your branch. Everything else in a full `ctest` with this PR (in that batch) matched `main` on both GPUs. AI-assisted (Claude Code); I ran all of this on my machine.
oneandonlydean commented 2026-10-11 03:35:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T03:35:15Z.


On Linux (RTX 4090, main 77bd928a + this PR) a title we test with stops 0.6-0.7 s in, on its first logo, in every run (4 of 4; main alone runs fine):

  • the merged host-import span at 0x200230000 (0x8cb0000-0xadc0000 bytes) fails vkAllocateMemory with -2 (out of device memory);
  • then vkQueueSubmit/vkWaitForFences returns -4 (device lost), with an Xid 109 on the host;
  • one run segfaulted instead, after [shadow] 380 retiled units of import 0x200230000 lost their slab.

So a merged span can be larger than the driver will import in one piece. It needs to stay within what a single import can hold, and an import that fails has to be handled exactly rather than lose the device. ctest passes, so a test that covers a span past that size would help.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105070164 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3158#issuecomment-6105070164). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T03:35:15Z. --- On Linux (RTX 4090, `main` 77bd928a + this PR) a title we test with stops 0.6-0.7 s in, on its first logo, in every run (4 of 4; `main` alone runs fine): - the merged host-import span at `0x200230000` (`0x8cb0000`-`0xadc0000` bytes) fails `vkAllocateMemory` with `-2` (out of device memory); - then `vkQueueSubmit`/`vkWaitForFences` returns `-4` (device lost), with an Xid 109 on the host; - one run segfaulted instead, after `[shadow] 380 retiled units of import 0x200230000 lost their slab`. So a merged span can be larger than the driver will import in one piece. It needs to stay within what a single import can hold, and an import that fails has to be handled exactly rather than lose the device. ctest passes, so a test that covers a span past that size would help.
Tamalero commented 2026-10-11 03:39:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Tamalero on 2026-10-11T03:39:57Z.


Tried this with Ratchet & Clank: Rift Apart (PPSA01474) on Linux with an RTX 4090. It helps with one problem but runs into another. Short version:

  • Without this PR, the game gets through its intro logos and then hangs at the first loading screen. It maps its streaming memory one 16 KiB page at a time, every page becomes its own import (~21,000 live), and the NVIDIA driver's memory for those imports climbs to 15–18 GiB and back every few seconds.
  • With this PR, the import count and the driver memory drop a lot, which is great. But because the pages arrive one at a time, every new page grows the run and the whole run gets imported again. Over ~4 minutes that added up to ~1.5 TiB of re-imports for ~8.4 GiB actually in use.
  • Twice the driver refused a grown span (vkAllocateMemory −2), and from then on the run fell back to CPU copies. It hangs at the same loading screen as without the PR. That matches what @oneandonlydean saw on a 4090.

So for this title the span probably needs a size cap, or fixed-size aligned chunks (e.g. 64 MiB), so that adding a page never re-imports what's already there. Happy to test a revised version against it.

Details and logs

Setup:

  • PPSA01474 01.005.003, Linux (CachyOS, kernel 7.2.9), RTX 4090, NVIDIA 615.78.08.
  • AnyPS5 f1ae427b plus this PR, plus local changes needed to get this far:
    • the _ammBase hint workaround from #3146;
    • guest thread stacks placed in the guest arena;
    • a few small library fixes.

None of those changes touch GuestBufferMemory.

Mapping pattern: sceKernelMapDirectMemory of 0x4000 at consecutive addresses (0x126c400000, 0x126c404000, 0x126c408000, …), interleaved with GPU use.

Without the PR (APS5_TRACE_HOST_IMPORT=1):

[gpu] host import of 0x127d68c000+0x4000 ok (20940 live, 8413.9 MiB live, 8698.3 MiB ever)

Kernel memory charged to the process's cgroup goes ~0.7 → 15–18 GiB → back, with VRAM at 2–7 GiB of 24.

With the PR:

[gpu] host import of 0x40fc740000+0x4120000 ok (1024 live, 7678.3 MiB live, 14380.7 MiB ever)
[gpu] host import of 0x40fc740000+0x41a0000 ok (1024 live, 7678.8 MiB live, 14446.3 MiB ever)
...
[gpu] host import of 0x126c400000+0xb750000 failed at vkAllocateMemory (-2); falling back to copies
[gpu] host import of 0x126c400000+0x111c0000 failed at vkAllocateMemory (-2); falling back to copies
[gpu] host import of 0x126c400000+0x111bc000 ok (2013 live, 8414.2 MiB live, 1528859.0 MiB ever)

Kernel memory stays at ~0.2–0.3 GiB. After the refusals the process sits at ~330% CPU in the copy path.

Not reproduced: one earlier run with the PR segfaulted ~50 s in inside glibc malloc (operator new from Driver::materializeDrawStage), which looks like heap corruption. It didn't recur, so treat it as unconfirmed.

Tested and written by Claude (AI assistant) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6105102035 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3158#issuecomment-6105102035). Originally posted by [Tamalero](https://github.com/Tamalero) on 2026-10-11T03:39:57Z. --- Tried this with Ratchet & Clank: Rift Apart (PPSA01474) on Linux with an RTX 4090. It helps with one problem but runs into another. Short version: - **Without this PR**, the game gets through its intro logos and then hangs at the first loading screen. It maps its streaming memory one 16 KiB page at a time, every page becomes its own import (~21,000 live), and the NVIDIA driver's memory for those imports climbs to 15–18 GiB and back every few seconds. - **With this PR**, the import count and the driver memory drop a lot, which is great. But because the pages arrive one at a time, every new page grows the run and the whole run gets imported again. Over ~4 minutes that added up to ~1.5 TiB of re-imports for ~8.4 GiB actually in use. - Twice the driver refused a grown span (`vkAllocateMemory` −2), and from then on the run fell back to CPU copies. It hangs at the same loading screen as without the PR. That matches what @oneandonlydean saw on a 4090. So for this title the span probably needs a size cap, or fixed-size aligned chunks (e.g. 64 MiB), so that adding a page never re-imports what's already there. Happy to test a revised version against it. <details> <summary>Details and logs</summary> **Setup:** - PPSA01474 01.005.003, Linux (CachyOS, kernel 7.2.9), RTX 4090, NVIDIA 615.78.08. - AnyPS5 `f1ae427b` plus this PR, plus local changes needed to get this far: - the `_ammBase` hint workaround from #3146; - guest thread stacks placed in the guest arena; - a few small library fixes. None of those changes touch `GuestBufferMemory`. **Mapping pattern:** `sceKernelMapDirectMemory` of 0x4000 at consecutive addresses (`0x126c400000`, `0x126c404000`, `0x126c408000`, …), interleaved with GPU use. **Without the PR** (`APS5_TRACE_HOST_IMPORT=1`): ``` [gpu] host import of 0x127d68c000+0x4000 ok (20940 live, 8413.9 MiB live, 8698.3 MiB ever) ``` Kernel memory charged to the process's cgroup goes ~0.7 → 15–18 GiB → back, with VRAM at 2–7 GiB of 24. **With the PR:** ``` [gpu] host import of 0x40fc740000+0x4120000 ok (1024 live, 7678.3 MiB live, 14380.7 MiB ever) [gpu] host import of 0x40fc740000+0x41a0000 ok (1024 live, 7678.8 MiB live, 14446.3 MiB ever) ... [gpu] host import of 0x126c400000+0xb750000 failed at vkAllocateMemory (-2); falling back to copies [gpu] host import of 0x126c400000+0x111c0000 failed at vkAllocateMemory (-2); falling back to copies [gpu] host import of 0x126c400000+0x111bc000 ok (2013 live, 8414.2 MiB live, 1528859.0 MiB ever) ``` Kernel memory stays at ~0.2–0.3 GiB. After the refusals the process sits at ~330% CPU in the copy path. **Not reproduced:** one earlier run with the PR segfaulted ~50 s in inside glibc `malloc` (`operator new` from `Driver::materializeDrawStage`), which looks like heap corruption. It didn't recur, so treat it as unconfirmed. </details> *Tested and written by Claude (AI assistant) on a contributor's machine.*
adriadam10 commented 2026-10-11 07:14:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T07:14:13Z.


Checked on Linux: PR merged onto current main, libSceAgcDriver build and ctest 349/349; the new importCrossingTests/importSpanTests cases in agc_driver_graphics fail without the change to GuestBufferMemory.cpp and pass with it. Reviewed that runs never overlap, any range change retires the span, a refused run is remembered by its base and ImportMappedRanges builds the same span from the full registry. Not run here: the PPSA05684 numbers and the Windows path. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106571376 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3158#issuecomment-6106571376). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T07:14:13Z. --- Checked on Linux: PR merged onto current main, libSceAgcDriver build and ctest 349/349; the new `importCrossingTests`/`importSpanTests` cases in `agc_driver_graphics` fail without the change to `GuestBufferMemory.cpp` and pass with it. Reviewed that runs never overlap, any range change retires the span, a refused run is remembered by its base and `ImportMappedRanges` builds the same span from the full registry. Not run here: the PPSA05684 numbers and the Windows path. Merging.
Tamalero commented 2026-10-11 08:07:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Tamalero on 2026-10-11T08:07:53Z.


Follow-up to my comment above, now that this is merged: #3335 bounds spans to aligned windows (8 MiB by default). It joins ranges only when a chain fills a whole window, so heap growth no longer re-imports the chain. With Ratchet over a 4-minute run: 11.0 GB imported in total for 9.4 GB live, no refused imports. It was closed by the intake bot, so it can be reopened or carried, and it composes with #3324's alias fallback.

Tested and written by Claude (AI assistant) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106977734 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3158#issuecomment-6106977734). Originally posted by [Tamalero](https://github.com/Tamalero) on 2026-10-11T08:07:53Z. --- Follow-up to my comment above, now that this is merged: #3335 bounds spans to aligned windows (8 MiB by default). It joins ranges only when a chain fills a whole window, so heap growth no longer re-imports the chain. With Ratchet over a 4-minute run: 11.0 GB imported in total for 9.4 GB live, no refused imports. It was closed by the intake bot, so it can be reopened or carried, and it composes with #3324's alias fallback. *Tested and written by Claude (AI assistant) on a contributor's machine.*
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!3158
No description provided.