perf(agc-driver): import adjacent registered ranges as one host-import span #3158
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3158"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:
ImportMappedRangesimports with the full registry, so it creates the same span.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):[guestmem] write-changed)Tests in
tests/BdaResources.cpp:importCrossingTestsnow expects the two ranges as one span.importSpanTestscovers: 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
BUILD_TESTING=ON.91b13413: 734/735; the one failure,agc_driver_dcc_keys_first_write, fails the same way on plain main91b13413. Conventions check passed.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)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 importsline 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
sceAgcCreateInterpolantMappingandsceAgcLinkShadersserialised every thread on one variant build — and the frames over 150 ms spent about 515 ms inShader.Registeragainst 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.
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
70d7aa58merged ontomaind31b57a4, on an RTX 5060 Laptop and the Arrow Lake iGPU.agc_driver_bda_deviceandagc_driver_bda_span_reads(theBdaResources.cpptests) pass on both GPUs. Butagc_driver_dcc_keys_first_writefails on both with this PR, and passes on both on plainmaind31b57a4. On Linux it already fails onmain(#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.That's
MapAliasinlibc/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
ctestwith this PR (in that batch) matchedmainon both GPUs.AI-assisted (Claude Code); I ran all of this on my machine.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T03:35:15Z.
On Linux (RTX 4090,
main77bd928a+ this PR) a title we test with stops 0.6-0.7 s in, on its first logo, in every run (4 of 4;mainalone runs fine):0x200230000(0x8cb0000-0xadc0000bytes) failsvkAllocateMemorywith-2(out of device memory);vkQueueSubmit/vkWaitForFencesreturns-4(device lost), with an Xid 109 on the host;[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.
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:
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:
f1ae427bplus this PR, plus local changes needed to get this far:_ammBasehint workaround from #3146;None of those changes touch
GuestBufferMemory.Mapping pattern:
sceKernelMapDirectMemoryof 0x4000 at consecutive addresses (0x126c400000,0x126c404000,0x126c408000, …), interleaved with GPU use.Without the PR (
APS5_TRACE_HOST_IMPORT=1):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:
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 newfromDriver::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.
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/importSpanTestscases inagc_driver_graphicsfail without the change toGuestBufferMemory.cppand pass with it. Reviewed that runs never overlap, any range change retires the span, a refused run is remembered by its base andImportMappedRangesbuilds the same span from the full registry. Not run here: the PPSA05684 numbers and the Windows path. Merging.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.