perf(libc): keep the guest allocation lease snapshot until the registry changes #6917
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6917"
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 milescoviello on 2026-10-16T18:15:09Z.
What
GuestAllocationsAcquire(andGuestAllocationsAcquireAll) shares one snapshot of the registry between the leases taken while it is unchanged. The registry kept that snapshot only through aweak_ptr, though, so it lived only as long as some lease held it. Most leases are short: the driver takes one per address-based build, import refresh and range check. So the first lease after the previous one ended rebuilt the snapshot from scratch, walking the whole registry map and taking a reference to every readable range.In MXGP 26 (PPSA30839) on Linux that rebuild is a large part of the queue workers' time. The run uses the PRs the title needs to get this far (#6814, #6094, #6160) plus #6308, #6184 and #6455, and both workers are at about 99% busy.
perf recordon the process putsstd::_Rb_tree_incrementat 9.9% of all samples,GuestAllocationsAcquire_nid_postfixat 9.7% and the snapshot vector'sshared_ptrcopies at 3.1%.The registry now keeps the snapshot (
shared_ptr) until the next registry change, where it is dropped exactly as before:GuestAllocationsBegin,GuestAllocationsEndand the pin wait reset it, under the registry mutex, as they already reset the weak pointer.use_count() != 1), so a kept snapshot never counts as a lease.The test that required "a snapshot outlived its leases" to be false now states the new contract instead. After the leases end, the snapshot is still the registry's, and the next lease reuses it without a rebuild. Any registry change, even an empty mutation, drops it.
Tested
Ubuntu 24.04, GCC 13.3, Release, RTX 4070 Ti (NVIDIA 595.99.02).
agc_driver_graphics(leaseSnapshotTestsandRunGuestLeaseWaitTests): withmain'sGuestAllocations.cppit fails (a lease taken after the others ended, without a registry change, rebuilt the snapshot), and with the change it passes 3 of 3. Since 602b464af9,leaseSnapshotTestsalso makes a range unreadable throughMutation::Protectwhile both snapshots are kept: the call must not wait on them, the replaced range must expire, and the next readable lease must leave the range out while the next lease of all ranges shows it unreadable. Passes 3 of 3.BdaResources' failed-import check now expects the kept snapshot's reference along with the registry's (use_count() == 2instead of 1). Its other checks are unchanged, including that a removed owner expires. All 391guest_*and libc tests pass, and the 501 tests matching^agc_driver_|^agc_vertex_barrier|recompiler|shader_cache|request_serializer|spirv|^agc_shader|bda|journalpass exceptagc_driver_store_over_untracked, which fails onmaintoo.APS5_TRACE_SECONDS=100:10(and the trace directory and pad input file) was set: noAPS5_PROFILE_*switch.maindoes not reach this scene, so the base is the stack above. The commits are local test merges:7b386bafd265is the stack (with #6896's one-line compute pipeline trace timer), and5f469824dc57adds this PR'sGuestAllocations.cppchange (the same library diff as this branch).7b386bafd2655f469824dc577b386bafd2655f469824dc57tools/trace_digest.py:Summary lines (
aps5-summary.jsonl, base 1, PR 1, base 2, PR 2):Traces: https://gist.github.com/milescoviello/336bdc50227a65c6ea50f86e84ffbe1d, files
stack-7b386bafd265-run{1,2}.json.gz(base) andlease-5f469824dc57-run{1,2}.json.gz(this PR). They open in ui.perfetto.dev.APS5_PROFILE_DRAW=1(which costs time, so they are not the speed claim): 90 s, interleaved, four runs each. Presents per 10 s:In separate 130 s runs with
APS5_TRACE_SECONDS=100:10(also withAPS5_PROFILE_DRAW=1), the frame p50 is 342 and 345 ms without the change (2 runs) and 240 ms with it (1 run; the other is the crash below). A perf profile of the same phase with the change no longer has_Rb_tree_increment,GuestAllocationsAcquire_nid_postfixor the vector copies among its top entries; the largest is nowPageSpan::runEnd(13.8%).@simonbronk ran it on Windows with Onimusha: Way of the Sword (PPSA27836) at its language menu (RTX 5070, 617.42, 3840x2160, 3 runs against 6 base runs). Frame p50 stays within the base's run-to-run range (84.4, 82.5 and 84.4 ms against 77.9–84.5 ms), so that scene shows no measurable change.
Not checked: macOS, AMD/Intel.
#6565 changes the same two lines in
GuestAllocationsAcquire/GuestAllocationsAcquireAll, to leave retiring ranges out of new leases and skip storing the snapshot while a range retires. #6499 adds aRegistryfield right after the two snapshot members that this PR changes, sotools/pr_overlap.pyreports an adjacent-line conflict there. #6951 changes the cache-hit and the finalreturnlines ofGuestAllocationsAcquireto pass the generation into the lease. The changes compose; whichever of them merges later needs a small rebase.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-16T18:44:50Z.
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/libc/src/GuestAllocations.cppL39-40core/libs/prx/libc/src/GuestAllocations.cppL39-40Some 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/tests/GuestAllocations.cppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libSceAgcDriver/tests/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/tests/BdaResources.cppImported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-16T22:09:59Z.
Review: COMMENTED
The cause is clear (the snapshot lived only as long as one lease, so the next lease rebuilt it by walking the whole registry;
_Rb_tree_increment9.9% andGuestAllocationsAcquire9.7% of samples in MXGP 26) and the change is small. Questions:shared_ptrto the snapshot, so everyRangein it stays alive until the next registry change, including ranges whose owner memory the title has already released through a path that doesn't mutate the registry. What destroys aRange's backing when its last lease ends? The test changeuse_count() == 2("a failed import pins its owner beyond the registry and its kept lease snapshot") shows that the kept snapshot now holds owners that nothing held before. Please state which owners can be pinned this way and for how long, and that none of them are waited on by the pin wait (use_count() != 1is the check).readablefor a range withoutBegin/Endhas to reset the snapshot, or a lease gets the old readability.Mutation::Protectis used in the #6921 test; add a case here where a range turns read-only and the next lease must not return the old snapshot.APS5_PROFILE_*) and theperfshares after the change, instead of the shares before it.Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-16T22:13:58Z.
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;mainbc3c0fc13vsmain+ 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 (45-47 ms of idle gaps per frame).mainWithin the run-to-run spread (about ±1.5 % here), no regression, 0 fatal.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-16T22:26:15Z.
Independent CPU-side checks for the first two review questions, on exact head
e3590fa2ceb62a81a4f5cc57dfec22b5842eb610(Ubuntu 24.04 x86-64, GCC 13.3). I compiled the complete, unchanged productionGuestAllocations.cppwith a small standalone probe, real anonymousmmapallocations and realmprotectcallbacks. No GPU/runtime stand-in was involved.Ownership detail:
Rangecontains addresses, sizes and flags, with no backing-storage owner or destructor. Dropping its lastshared_ptrdestroys that metadata; it does not itself free or unmap the address. The readable cache retains the nonempty readable ranges; the all-ranges cache retains every nonempty range. Both are cleared byBegin,End, and everyRequireUnpinnedscan. An independently held lease can retain the old metadata longer. Retaining a snapshot therefore does not make a free path that bypasses registry mutation safe; this test does not audit all such paths.Observed in five fresh-process runs:
Negative controls discriminate the reset paths: deleting only the two resets inside the pin-wait loop makes the reentrant callback case fail as still pinned; deleting only the
Begin/Endresets makes the empty-mutation check fail with retained cached references. The parent passes the protection and pin-wait compatibility cases, and fails the intended new snapshot-retention check.This supplies registry-level protection/pinning evidence only. It does not establish concurrent GPU lifetime safety, resolve the previously reported unretained-mode title crash, or supply the requested unprofiled PPSA30839 FPS/after-change
perfcomparison.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-16T22:57:06Z.
Thanks for the review, and thanks @simonbbby for the standalone probe. It answers the first two points independently and includes the negative controls.
Which owners the kept snapshots hold, and for how long. A
Rangeis the registry's record of one piece of a guest allocation: address, size, access and allocation. It owns no memory, so dropping its last reference frees only the record. The guest pages are unmapped by theapplycallback ofUnmap/Remove, inside aMutation, afterGuestAllocationsRequireUnpinned.Every registry writer is a
Mutationmember (Add,Remove,Protect,Unmap,RegisterMainImage,RegisterImage,UnregisterImage), and nothing calls them outside one.GuestAllocationsBeginresets both kept snapshots under the registry lock, andGuestAllocationsEndresets them again before it releases the lock. So between two mutations:Rangeonce;Rangeonce;They never hold a record the registry has already dropped. Memory the title releases without a registry change keeps its record in the registry anyway, so the kept snapshot adds no lifetime there. A lease someone still holds keeps its snapshot alive, as it did before. In
failedImportRemapTests,use_count() == 2counts the registry's own record plus the kept readable snapshot.The pin wait.
GuestAllocationsRequireUnpinnedresets both snapshots at the start of every round, before it countsuse_count() != 1. That includes the rounds after it takes the lock back following a wait, when a driver worker may have rebuilt a snapshot in the meantime. Only leases that are still held count as pins.Protect. 602b464af9 adds the case to
leaseSnapshotTests:use_count() == 3.Mutation::Protect. The call has to complete without the kept snapshots counting as a pin; otherwise it would waitAPS5_PIN_WAIT_MSand throw.readableandwritablecleared.agc_driver_graphicspasses 3/3 on the branch.Before/after.
maindoes not reach this scene.mainat1b1f8483c(today, 94 commits before the current head) exits at 103 s, 3 s into the window, so it writes no summary. It hitsdeferred write-back failed: BDA access failed: address=0x1227d10251 ... bytes=4(the unaligned 32-bit atomic that #6094 handles), thencomputed/data-dependent s_swappc_b64 call target ... is not statically resolvable(#6160). So the base stays the stack:mainplus #6814, #6094, #6160, #6308, #6184 and #6455. A second session today, same scene, interleaved, noAPS5_PROFILE_*switch,perf record -F 499over 100–112 s of each run:_Rb_tree_incrementGuestAllocationsAcquire7b386bafd2655f469824dc577b386bafd2655f469824dc57tools/trace_digest.py:After the change, the largest share is
PageSpan::runEnd, at 14.2% and 14.5% (#6921). The summary lines (session2-summary.jsonl) and traces are in the same gist as before, undersession2-*: https://gist.github.com/milescoviello/336bdc50227a65c6ea50f86e84ffbe1d. The first session's numbers in the description (29/30 vs 44/43 presents) agree with these.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T06:08:51Z.
A second title, for scale. Metal Gear Solid 4 (PPSA18194), Linux 6.8, RTX 4070 Ti (595.99.02), Release, pad input. Four interleaved rounds of 300 s, no
APS5_PROFILE_*. Base ismain0dc15620d2plus what MGS4 needs here (#6814, #6842, #6237, local merges); the other build is the same plus this PR. The numbers are presents per 10 s interval, aligned on the dip between the launcher and the game.That's 8% higher on average, but at four runs it's within the run-to-run spread (Welch t ≈ 1.5). So I'd read it as no regression on a second title, not as a measured gain.
The costs this PR targets dropped in every run (share of samples in a 12 s
perf recordat 225 s):GuestAllocationsAcquire_nid_postfix: 0.80, 0.70, 0.87, 0.89% → 0.24, 0.43, 0.33, 0.42%_Rb_tree_increment: 1.69, 1.93, 2.12, 1.82% → 1.10, 1.46, 1.68, 1.28%They make up only 1–3% of MGS4's samples to begin with, against about 22% in MXGP, which fits the small difference in presents.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T21:16:49Z.
A third title: Forspoken (PPSA03984). Linux 6.8, RTX 4070 Ti (595.99.02), Release, warm shader and pipeline caches, scripted pad input.
mainis922dc8999e; the other build ismainplus this PR's head8f0bd51c5d(merge 0b6640f568eb).Where the time goes. An 8 s
perf record50 s in, one run per build:mainGuestAllocationsAcquire_nid_postfix, selfstd::_Rb_tree_increment, selfWith the PR, the largest new share in that window is shader registration (
PublishRegisteredShaderat 15% inclusive). I'm opening a separate PR for it.Summary lines, no
APS5_PROFILE_*switch,APS5_TRACE_SECONDS=0:60:One window went the other way. With
APS5_TRACE_SECONDS=40:30,mainhad 56 presents (p50 398.1, p90 988.3 ms) and this PR 24 (p50 689.2, p90 6295.5 ms). In that run the PR's queue workers were busy only 15 to 30% of the window, against 97 to 99% onmain. Its trace has 7,422Shader.PrepareArtifactscopes and 80 s ofDriver.WorkerWait, so the workers spent that window waiting on shader preparation. I read it as the two runs being at different points of the title (the caches were warmed bymainruns), not as a slowdown, but it is a single run.Whole runs, 120 s with
APS5_PROFILE_DRAW=1, two interleaved rounds:main: 202 and 203 presents, 29,500 and 31,300 compute dispatches, both to the limit.vkAllocateMemory storage texture: Vulkan result -2, a device memory exhaustion thatmaindoesn't get far enough to hit within 120 s. With #6174 added, two 150 s runs reached the limit with 480 and 488 presents (details). I don't think this PR causes the exhaustion, but I haven't proved that.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-17T22:08:23Z.
Tested on Onimusha's menu: no measurable change in this scene.
Setup: Onimusha: Way of the Sword (PPSA27836), boot to the language menu with no input,
APS5_TRACE_SECONDS=60:30, warm cache. Windows 11 26200, RTX 5070 (617.42), 3840x2160. Basemain84b0d2f24+ #6557 #6727 #6762 (5c521efea), 6 runs (3 before and 3 after the PR runs). The PR is merged on top of it (clean merge), 3 runs.This scene may simply not exercise the path this PR speeds up. Summary lines and traces available on request. AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T18:55:57Z.
Thanks @simonbronk, and sorry for the slow reply. A clear "no measurable change" in a menu scene is useful, and it's in the description now. This PR saves the work of rebuilding the lease snapshot when the allocation registry hasn't changed, so it shows mainly in scenes with many guest-memory checks per frame. A menu may not do many.
AI-assisted: yes (Claude Code)