perf(agc): reuse a cached address space's committed pages on a hit #6955
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6955"
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 alfalfa-star on 2026-10-16T21:04:28Z.
What
The read-only V# address-space cache (#5428) lets a draw reuse the address space built for an earlier draw that bound the same buffer. On each hit, every host import of the space was stamped with a new use count, a loop over all of the space's imports under the imports mutex. Kena: Bridge of Spirits (PPSA01802) hits this cache on almost every address-based build (about 6,100 per 10 s), so the loop cost about 58 us per build.
The space is now stamped once per hit: an import remembers the serial of the cached space that stamped it, and its last use is the later of its own stamp and that space's last hit (
effectiveUse). Only the current cached space can hit, so the space that stamped an import last is the one whose hits keep it alive. The last hits of the 64 most recently hit spaces are kept; when one more is added, the space hit longest ago is dropped and its last hit is written back into its imports, soeffectiveUsealways equals the per-hit stamp it replaces.APS5_NO_SPACE_STAMP_ONCE=1restores the per-import stamp.Tested
AMD Radeon RX 9070 XT (RADV GFX1201), Mesa 26.2.4, Arch Linux (kernel 7.2.9), Kena: Bridge of Spirits (PPSA01802).
mainf8b22edfb, no input,APS5_TRACE_SECONDS=100:30, three alternating rounds:main216 / 325* / 254 presents, this PR 315 / 271 / 303 (* that run had 48 ms GPU busy per present instead of about 80, a lighter stretch of the scene). WithAPS5_PROFILE_DRAW=1, the[address]line's lease time per address-based build is 58-59 us onmainand 0 us with this PR. Summary lines in the review reply.agc_driver_graphicspasses, including a new case that commits pages of a registered sparse range between two hits and requires the next builds to read them.ctest -R '^agc_'(on the previous revision, which also had a committed-page memo): 619 of 620 pass;agc_driver_tessellationsegfaults onmaintoo.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T21:27:27Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppL241core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppL241core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppL241, L1354-1355core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppL1348docs/dev/ENVIRONMENT.mdL294Some 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.
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppdocs/dev/ENVIRONMENT.mdcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppdocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/ENVIRONMENT.mdcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppdocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/ENVIRONMENT.mdcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/ENVIRONMENT.mdcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/ENVIRONMENT.mdcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppdocs/dev/ENVIRONMENT.mddocs/dev/ENVIRONMENT.mdcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppImported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-16T21:55:10Z.
Review: COMMENTED
Two correctness questions on the on-by-default part, and no measurement yet.
HostImport::spaceSerialholds one serial, but aHostImportis shared by every cached space that binds the same buffer.region.direct->spaceSerial = current->serialis written only on the first hit of a space (spaceUse == 0). If space A is stamped, then space B stamps the same import and overwrites the serial, later hits on A updatespaceUses[A]only, soeffectiveUsefor that import follows B. Once B stops hitting,pointerHeldis false for an import that A's draws still read, andevictablecan retire it after 256 uses. Please say why that cannot happen, or keep the stamp per import (the old loop) and only skip the ones already at the current stamp.spaceUsesis trimmed witherase(begin())above 64 entries, i.e. the lowest serial, which is the oldest space and not the least recently hit one. A long-lived hot space is the first to lose its protection.copiedCommittedis computed once per space. These ranges are reserved heaps committed on demand ("committed pages only" inaddCopiedRange), so the committed set can grow after the space was built. What invalidates the memo when pages are committed or decommitted? If nothing does, a hit serves the old page list, which is the stale-data case the per-hitDescribeCommittedcall was there to prevent.std::mutexlocked on every hit, held across theaddCopiedRangeloop. A hit is the hot path the PR is about; a lock-free read of an immutable vector published once would do.The description gives no numbers: "about 24 fps in our local test stack" is not a measurement of this change. Per the Performance report form, please add the summary lines of main and this PR from the same scene in PPSA01802, without any
APS5_PROFILE_*switch, with the per-draw time this removes.Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-17T00:05:15Z.
Data point from a CPU-bound scene on Windows: RTX 3060 12 GB (NVIDIA 617.42), i5-12400F, Windows 10 19045, MinGW GCC Release. Minecraft Dungeons II (PPSA16064 01.100.100), title screen, no input, 200 s rows,
APS5_PROFILE_DRAW=1 APS5_PROFILE_GPU=1;mainaf0fff762vsmain+ this PR, interleaved with five other PRs in one batch, two rounds. FPS is the mean of the last 10[present]windows, GPU busy and idle gaps per frame over the last 8. This scene is CPU-bound on this machine (44-47 ms of idle gaps per frame).mainWithin the run-to-run spread (
mainitself moved 6.61 -> 6.76 between rounds), no regression, 0 fatal.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-17T10:36:31Z.
Thanks. Changes in
5b01fb212and6877d954c(pushed on top, no rebase):Shared
spaceSerial. The scenario needs space A to hit again after B has stamped the shared import. That can't happen: the cache holds one space (Spaces().current), and only that one is looked up and stamped. Once B replaces A, A is never hit again; a build that still holds A doesn't stamp. While B is current, the imports it shares with A carry B's serial, and theireffectiveUsefollows B's hits. The imports only A used keep A's serial and A's last hit, which is when they were last used through a space. SoeffectiveUseequals what the old per-hit stamp would have written.Trimming
spaceUses. You're right that erasing the lowest serial isn't LRU. Serials are taken beforespaces.current.store, so two concurrent builds can publish out of order. It now drops the entry with the oldest last hit, and writes that hit back into thelastUseof the imports still carrying that serial (clearing it). Trimming never loses protection, and the current space always has the newest hit, so it's never the one dropped.copiedCommitted. I checked what invalidates it. Every page-state change of registered memory goes through aGuestAllocationsmutation (map, mprotect, munmap, reserve) orGuestAllocationsInvalidate; both advance the generation a hit checks, and a mutation over a leased range first drops the cached space through the pin waiter. The one exception wasGuestArenaCommiton Windows outside a mutation. But measuring the parts separately showed the memo saved nothing in Kena: its hits have no copied ranges ("imports pass" is 0 us per build with and without it). So I removed it in6877d954c, together with the lock it needed. That leaves this PR with one on-by-default change. The test I wrote for it stays:spaceCommittedPagesTestscommits pages of a registered sparse range between two hits and requires the next first build and hit to read them.Mutex on every hit. It went with the memo.
Measurement. Kena (PPSA01802), forest title scene, no input.
mainf8b22edfbwith and without this PR, warm cache,APS5_TRACE_SECONDS=100:30and no other switch. Three rounds, rotated with another build. Linux 7.2.9, RX 9070 XT, RADV Mesa 26.2.4, Release.main* That run reached a lighter stretch of the scene (48 ms GPU busy instead of about 80), so it isn't comparable. Per-draw time removed:
APS5_PROFILE_DRAW=1runs of the same scene give, in the[address]line, a lease time per address-based build of 58–59 us onmainand 0 us with this PR, at about 6,100 builds per 10 s. An earlier batch (same scene, with the memo still in) hadmain165 / 175 / 181 against 208 / 298 / 218.Summary lines
The traces are kept locally; I can attach any of them.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-18T12:32:22Z.
Rebased on
main0d078f75d(f192d8564). The only conflict wasHostImportinGuestBufferMemory.hpp: main addedleadnext to ourspaceSerial, and both fields are kept. Main's new env-reference check also needed a row forAPS5_NO_SPACE_STAMP_ONCEindocs/dev/ENVIRONMENT.md(021ed0a2a).Tested: Release build plus
ninja libs;ctest -j8: 1437 of 1438 pass. The one failure,agc_driver_tessellation, segfaults inside RADV 26.2.4 inLinkPipelineFromLibraries. It fails the same way withAPS5_NO_GPL=1and on a build of the older main without this PR, so it isn't from this change. Conventions check passes.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T12:26:07Z.
This pull request is labeled
perf, so it needs the summary line of a capture onmainand one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste bothaps5-summary.jsonllines, attach the two traces and add the output oftools/trace_digest.py.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T15:33:34Z.
Checked the net diff against
main: stamping the cached space once per hit gives the sameeffectiveUseas the per-import stamp (only the current space can hit, shared imports move to the new space's serial with a newerlastUse, trimming writes the last hit back into the imports), the otherlastUsewriters only ever set a newer value, and the memo/mutex the earlier review questioned are gone. Rebased onmain(only conflict:spaceSerialnext to the newHostImportfields); net diff unchanged. Note thatspaceCommittedPagesTestsalso passes without this change, so nothing pins the once-per-hit stamping yet; not blocking. Merged.