perf(agc): keep a host import across a split of its guest range #6499
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6499"
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 parl0v on 2026-10-15T17:38:32Z.
What
A guest
mprotect/munmapover part of a registered range rebuilds every piece of that range as a newRange, the untouched ones included. A host import pinning the range is retired from the pin waiter during the guest call, and the next access imports the remaining tail again from scratch: a newVkDeviceMemoryimport, and on Windows since #6060 a new watched window (zero scan of every page, copy of the nonzero ones, write walks) and a settle. For a large range whose start moves up a little at a time, that is hundreds of milliseconds per step for pages that are still imported.Now an import whose head or tail is cut off is kept, and so is the larger side of an import cut strictly inside its range (the tail when both sides are equal; the smaller side is imported again). At the next refresh it is re-attached to the largest current range inside the surviving piece: a new
VkBufferis bound to the same imported memory at the new offset, so nothing is pinned, scanned, copied or settled again. To keep this exact,GuestAllocations::Rangecarries a backing serial: new ranges get a fresh one, the untouched pieces of a split keep it, and every span whose pages a mutation replaces (the spans that passRequireUnpinned: demote to a shared section,RemapFixedIntoRegistered, alias and resident mappings, protect and unmap) gets a fresh one. A trim is used only for a plain host-pointer import (no alias, dma-buf, chunked or joined import) whose surviving range has the same serial, does not overlap another import, is exactly what a new import would cover, and accepts a buffer at that offset; anything else is retired and re-imported as before. The original memory stays pinned until the trimmed import retires, andliveBytesand the eviction accounting count it. A trimmed import that is released instead of re-attached is held through the active recorder (KeepThroughHold), as a retired import is, so no batch in flight can still use its memory.One exception since the rebase onto current
main: an import that still has a deferred settle pending (pendingSettles, Windows) is retired as before instead of trimmed. That settle's read can still be in flight on the settle queue through the import's old buffer, and a trim moves the import's base, so the settle would no longer match it. Pending settles are short-lived (imports under 1 MiB until their read finishes, larger ones until the next poll), so the large re-imported ranges this is about still trim.Tested
Windows 10 19045, MinGW-w64 GCC 15.2, NVIDIA 617.42, RTX 4060 and RTX 3060.
Synthetic (new
agc_reimport_bench, RTX 4060): 1126 MiB of singly mapped direct memory, 1 page in 4 nonzero, imported throughHostImportFor; then 5 times the next 1440 KiB of the head is protected and[start + k·1440 KiB, end)is accessed again. Medians of 3 runs:main: guest protect / re-importOn
maineach re-import was ~170 ms import create, ~30 ms settle and ~400 ms inImportWatched(zero scan 75–150 ms, copy 73–122 ms, walks ~35 ms). The test also checks that after five trims the tail is still watched with no false writes, CPU stores at both ends are collected, and a GPU copy through the trimmed buffer (offset 0 and last page) equals guest memory.This is the pattern @Sevthered measured on Astro Bot (PPSA21567) on #6060: five re-imports of a ~1.1 GiB range ending at
0x37e000000with its start moving up, 295–348 ms each, as long frames in the logo sequence.Cuts at the tail and inside the range (
agc_reimport_bench, RTX 4060, NVIDIA 617.42): three patterns, each on a fresh 1126 MiB range, 5 cuts each, 3 runs. "Before" is this branch with the trim forced off, so every cut takesretireImport,main's path:The guest's protect drops from 18–22 ms to under 0.15 ms. A cut inside still imports the smaller side (256 MiB: 86–105 ms on both builds).
agc_trimmed_import(Windows): 32 MiB of singly mapped direct memory imported whole, then a 16 KiB no-access page near the start and near the end, an unmap of a 2 MiB run inside, an unmap of the first 2 MiB run, an unmap of the first 1440 KiB (which splits a 2 MiB resident run, so the kept side gets a new backing serial and is imported again), and an unmap of the whole kept side while the import waits trimmed. Every unmapped span is mapped again at the same address. Each case checks through a GPU copy that the import reads fresh CPU stores at both ends, that the write watch collects them and reports nothing else, and that pages mapped again are served by a new import. With the interior branch removed the first case fails; with the backing-serial check removed fromreattachTrimmedthe last case fails (the GPU reads the old pinned pages).Titles without that pattern, no change (same relinked executables, alternating,
APS5_PROFILE_DRAW=1): Minecraft Dungeons II (PPSA16064) title, both boxes, 2 × 240 s each:main6.58–6.69 FPS, this PR 6.61–6.79, no faults. Sword of the Sea (PPSA25344) title, RTX 3060, 3 boots each: 9.70 / 9.70 / 9.70 vs 9.70 / 9.65 / 9.60 FPS, no faults.ctest -R '^agc_'(RTX 4060): the same failures as onmain(agc_shader_disk_cache;agc_driver_gpu_indirect_arguments_misaligned_writer_aheaddoesn't compile with this toolchain onmaineither).agc_reimport_benchand every host-import test pass; 19 libc guest-memory tests (guest_memory, mmap/mprotect, heap, write-watch) pass.maind17f456ea(conflicts with the import retirement and lease-wait rework inGuestBufferMemory.cpp). On the rebased branch, RTX 4060:agc_reimport_benchfirst import 561 ms, the five re-imports 0.00–0.02 ms each; 53 host-import, settle, guest-memory and libc tests pass and 2 skip (agc_import_settle_first_use: the race it looks for did not happen in that run;agc_host_import_sparse: Linux only).agc_trimmed_import,agc_reimport_bench,agc_driver_host_import_lifetime(itspartialUnmapnow expects the kept side to stay imported, with 1 MiB parts so the cut trims on Windows as on Linux), 56 host-import, settle, guest-memory and libc tests and all 57agc_driver_recorder_testsentries pass, with the same two skips as before. The import tests also pass withAPS5_ASSERT_GPU_LOCK=1, under whichKeepThroughHoldaborts when it is reached withoutGpuMutex. The bench and the new test are registered on Windows only (on Linux the bench's skip line would not passcheck_skips.py).tools/check_conventions.py --base origin/mainpasses;git diff --checkclean.Performance capture (as the Performance report form describes): Minecraft Dungeons II (PPSA16064) title screen, RTX 4060 8 GB (i5-14400F), NVIDIA 617.42, Windows 10.0.19045, 3840x2160 output. Only
APS5_TRACE_SECONDS=120:12andAPS5_TEXTURE_CACHE_MIB=3072(8 GB card) set, caches warm (the title ran once on the build and was restarted), stderr not redirected.maind17f456eacagainst this PR's head 99581456c367:Frame and GPU times (ms):
Top phases (ms per frame):
Traces:
aps5-trace-d17f456eac16-21960.json.gz(main) andaps5-trace-99581456c367-13492.json.gz(this PR), attached below.trace_digest.py, main
seconds 120:12 from present 2073: 92 presents, frame p50 131.4 / p90 135.6 / p99 139.9 / max 139.9 ms, GPU busy 102.8 ms per present
NVIDIA GeForce RTX 4060, NVIDIA 617.42, Windows 10.0.19045, 3840x2160, commit
d17f456eac, dropped: noSlowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 253.9, batch 102.8, Driver.Packet.dispatch 88.0, Driver.Dispatch 87.1, Driver.Packet.memory_wait 71.9, Driver.Packet.device_lock 41.1, Vulkan.Dispatch 37.9, Vulkan.Dispatch.shader_resources 34.7, Driver.Packet.draw 24.8, Driver.Draw 24.6
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
trace_digest.py, this PR
seconds 120:12 from present 2085: 92 presents, frame p50 131.5 / p90 137.6 / p99 152.7 / max 152.7 ms, GPU busy 102.8 ms per present
NVIDIA GeForce RTX 4060, NVIDIA 617.42, Windows 10.0.19045, 3840x2160, commit 99581456c367, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 254.9, batch 102.9, Driver.Packet.dispatch 89.5, Driver.Dispatch 88.5, Driver.Packet.memory_wait 71.4, Driver.Packet.device_lock 39.8, Vulkan.Dispatch 39.3, Vulkan.Dispatch.shader_resources 36.2, Driver.Packet.prepare_memory 25.5, Driver.Packet.draw 25.2
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
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)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T18:27:06Z.
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.cppL1458-1459, L1484, L1673core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppL1458-1459, L1484, L1673core/libs/prx/libc/src/GuestAllocations.cppL40, L47, L382core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppL1367, L1445, L1458-1459, L1484, L1673core/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppL43Some 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/libc/include/GuestAllocations.hppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/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/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libc/include/GuestAllocations.hppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/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/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/Recorder.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libc/include/GuestAllocations.hppcore/libs/prx/libc/src/GuestAllocations.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hppcore/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppcore/libs/prx/libSceAgcDriver/tests/Recorder.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by frankea on 2026-10-15T20:25:52Z.
@parl0v the same three-round setup with this PR on top, on today's
main. Astro Bot (PPSA21564 v01.018.000), RTX 4090 (NVIDIA 616.92), Ryzen 7 5800X, 3840x2160, one executable relinked frommain44df21632with--windows --registry,libs/swapped betweenmain44df21632andmain+6af7d2cbe(merge6ed7f1c4d), alternating,APS5_TRACE_SECONDSthe only switch, no input. Two windows: the title intro (120:40, as yesterday) and the logo sequence (10:60, 110 s cap).main44df21632main44df21632main+ this PR (6ed7f1c4d)main+ this PR (6ed7f1c4d)main+ this PR (6ed7f1c4d)mainmainmainmainis at 20.0 to 20.3 FPS here, up from 15.1 for #6060's head6c6a6c85aona077a88dfyesterday and above #5851's 16.0, so the gap I measured is closed onmainitself (the 1 MiB settle floor and 400 other commits; I did not separate them). This PR on top: 20.0 to 20.1 FPS, the same 93 to 94 ms ofmemory_waitper frame.tools/trace_digest.pyon the four clean traces shows the same slow frames in both builds: one 460 to 520 ms frame at about 15.5 s (present ~1210, all three queue workers inDriver.Packet.memory_wait), a 130 ms frame right after it, and otherwise ~120 ms frames that carry a 100 ms GPU batch. I don't see the five 295 to 348 ms re-import frames @Sevthered reported, on either build; the one long memory wait here is the same size with and without this PR, so it looks like the first import of a large range rather than a re-import.mainlogos round 2 ran at 44.6 FPS with a third of the GPU time after a stray click advanced the title during it, and this PR's logos round 1 logged twoWAIT_REG_MEM_64 ... timed out after 1000mslines after its window had closed, in the stretch where the same click may have landed (its window stats match the other rounds, and its three other rounds have no timeouts). A fourth round of both replaced them.trace_dropped: true), the phase totals are complete. Summary lines and the logo traces are available.Summary lines (intro round 2, logos round 3, each build)
Tested with AI assistance (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-16T02:51:29Z.
@frankea thank you, that's a clean null: no change at the intro or the logos on your copy, and none of the five ~300 ms re-import frames on either build. One difference worth noting: @Sevthered's runs were on PPSA21567 v01.019.000, yours on PPSA21564 v01.018.000, so the re-import pattern (a ~1.1 GiB range ending at
0x37e000000re-imported with its start moving up) may depend on the version or region build. Your single 460–520 ms frame at ~15.5 s, the same in both builds, reads like a first import of a large range, which this PR doesn't touch.So the evidence for this PR stays the synthetic test (472–560 ms → <0.02 ms per re-import) until someone with the pattern runs it. @Sevthered, if you can, your v01.019 setup is the one that showed it. Rebased onto
main5d9dcc42d(the only conflict was a commentmainremoved next to the new line).AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by Sevthered on 2026-10-17T17:30:51Z.
@parl0v here is the v01.019 run. ASTRO BOT PPSA21567 v01.019.000 (my own copy, from a repack folder), Windows 11 build 26300, RTX 3080 Ti 12 GB on driver 616.56, Ryzen 7 5800X, 32 GB, WinLibs GCC 15.2.0, window 1152x648. I compared
main82f0c9aecagainst the samemainwith this PR's head1d3b33985merged locally (no conflicts). Both use one executable relinked with thatmain's relinker, the same run folder, save and warm shader cache, and the runs alternate between the builds.mainmain+ this PRSo no improvement here; the long frames are still there. The logo window with this PR is 0.30 to 0.34 s longer in each pair, but the probe runs below overlap (
main7.94 / 8.16 s, this PR 8.34 / 7.94 s), so I wouldn't call it slower. I don't have Crash Site numbers this time.With the import timing probe from #6060 on both builds (two logo runs each), the tail of the range ending at
0x37e000000is still imported fresh six times per run with this PR, as onmain: 190 to 315 ms each, 140 to 195 ms of it creating the import. The one exception is the last re-import of one run with this PR, which took 52 ms.To see why, I added prints to
retireOrTrim,reattachTrimmed,refreshImports,WaitForLeasesAndImportsand libc'sreplaceRangefor this address window (local only). Every step is the guest protecting a 16 KiB page to no access (a guard page) in the middle of the imported range, not at either end:The import is plain (no alias or dma-buf, not joined, one range, backing serial set), but the change is strictly inside it, so neither the head nor the tail case in
retireOrTrimmatches (first == last) and the whole import is retired. All 15retireOrTrimcalls in this address window ended inretire, none intrim, soreattachTrimmednever ran for it. The head and the ~1.15 GiB tail are then imported again. The next guard page lands inside the new tail, about 25 MiB above its start, and the later ones follow 1.44 MiB, 1.39 MiB, 288 KiB and 368 KiB apart (0x3347ec000,0x3360e0000,0x336250000,0x3363b4000,0x3363fc000,0x336458000), each inside the tail imported after the previous one. That is the "start moves up" pattern I described on #6060. Keeping the larger side of a split in the middle (or both sides) should cover this title; I haven't tried it.One more thing from that run, which may come from the prints: while the guest's protect at
0x336250000was pending, the tail0x3360e4000+0x47f1c000was imported seven times and retired after each one (each import 257 to 292 ms), and0x3363b8000+0x47c48000four times while the protect at0x3363fc000was pending. From the log order, the waiter retires the import, an import of the still unchanged range comes back before the guest's change goes through, and the guest waits again. That run's logo window took 11.9 s, with three frames of 1.25, 1.48 and 1.58 s; by count and order, its trace has seven 267 to 358 msVulkan.Dispatch.shader_resourcesspans in the first two of them and four in the third, matching the seven and four imports. I didn't see it in the four probe runs without these prints (one import per tail each), so the extra stderr writes may have changed the timing enough to expose it; I can't say whether it happens without them.No trace here dropped events (
trace_droppedis false in all eleven); frame counts and times come from the presents. Traces, the summary lines and the diagnostic patch are available if useful.AI-assisted: yes (Claude Code); the runs are from my machine.
Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-17T17:32:32Z.
@Sevthered thank you, that run settles it, and the probe pins down exactly why it's a null.
You're right about the cause:
retireOrTrimonly keeps an import when the change touches its head (begin <= entry.base) or its tail (end >= entryEnd). A guard page strictly inside the range leavesfirst == last, so the whole import is retired, and Astro Bot's 16 KiB no-access pages always land in the middle:0x3347ec000sits about 260 MiB into a 1.4 GiB import. My synthetic test moves the start of the range, which is the one case this title never hits.Next on this PR: for a change strictly inside a plain import, keep the larger side, which here is the ~1.15 GiB tail that is currently re-imported six times per run. Keeping both sides would need two buffers over one allocation, so I'll start with the larger side and see what is left. I'll add a test that protects a page in the middle of an import and expects the tail to be kept, not re-imported.
The repeated import/retire of the unchanged tail while the guest's protect is pending (seven and four times) is a separate path, the waiter in
WaitForLeasesAndImports. I'll look at it after the middle split, since keeping the tail may take away what that waiter keeps re-importing. Your diagnostic patch would help; if you can attach it here, I'll build on it rather than add my own prints.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-18T22:09:13Z.
Traces of the performance capture in the description (they open in ui.perfetto.dev):
maind17f456ea: aps5-trace-d17f456eac16-21960.json.gzImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T02:25:54Z.
Review: CHANGES_REQUESTED
Thanks for the detailed write-up. Points before this can merge:
Recorder.cpp:2308) moves the start of the range, but both Astro Bot runs in the thread (@frankea PPSA21564, @Sevthered PPSA21567 v01.019) show no change, and your own note says its guard pages land strictly inside the import, whichretireOrTrim(GuestBufferMemory.cpp:436) still retires whole. Please attach a trace/log from a real title where an import is re-imported after a head or tail cut (count of re-imports, ms before/after), or land the interior-split case this title actually hits.munmapof the head is untested. The bench only usessceKernelMprotect. After an unmap the trimmedVkDeviceMemorykeeps the host pages pinned while the guest VA is freed or remapped. Add a case that unmaps the head, remaps it, and checks the tail's bytes and write watch still match guest memory.releaseTrimmed(GuestBufferMemory.cpp:421) frees the memory immediately when the thread does not holdGpuMutex, while the old buffer, kept by the recorder, may still be in flight. Hold the memory with the recorder likeretireImportdoes (KeepThroughHold/Keep), or show that path is unreachable.trimmedstate and a backing serial; describe it under the import section.Happy to look again once the first point has data.
Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-19T03:19:57Z.
@adriadam10 thanks for the review. I pushed six commits on top of 99581456c (head 5126b4601) that cover the four points.
1. Cut inside the import. I took the second option.
retireOrTrimnow also handles a protect or unmap strictly inside a plain import: it keeps the larger side (the tail when both sides are equal) with the same trim as a head or tail cut, and imports only the smaller side again (GuestBufferMemory.cpp:447-451). That is the case @Sevthered's probe showed: a 16 KiB no-access page ~260 MiB into a 1.4 GiB import, with the next ones inside the kept tail. The bench now runs three patterns on a fresh 1126 MiB range, 5 cuts each, 3 runs (RTX 4060, NVIDIA 617.42). "Before" is the branch with the trim forced off, so every cut takesretireImport, which ismain's path:The guest's protect itself drops from 18–22 ms to under 0.15 ms. A cut inside still imports the smaller side (256 MiB: 86–105 ms on both builds). I don't have a title trace yet. @Sevthered, if you can rerun your import timing probe on the new head, I'd expect the six 190–315 ms tail re-imports per logo run to go away, with only the head side imported again.
2. munmap. New
agc_trimmed_import(Windows, 32 MiB of singly mapped direct memory imported whole):Every unmapped span is then mapped again at the same address. Each case checks through a GPU copy that the import reads fresh CPU stores at both ends, that the write watch collects them and reports nothing else, and that pages mapped again are served by a new import. With the backing-serial check removed from
reattachTrimmed, the last case fails because the GPU reads the old pinned pages; with the interior branch removed, the first case fails.3.
releaseTrimmed. It now hands the memory to the active recorder withKeepThroughHold, asretireImportdoes in the same callers, so a batch in flight can't lose it. I don't think the immediate free was reachable with a live recorder: the waiter syncs before and afterretireOrTrimwhile holdingGpuMutex;releaseTrimmedonly runs fromimportRunAtandrefreshImports; andImportMappedRangeswithout the lock refuses runs that overlap a trimmed import and only refreshes when it adopts a new device, which parks them. As a check, the import tests below also pass withAPS5_ASSERT_GPU_LOCK=1, under whichKeepThroughHoldaborts when it is reached withoutGpuMutex.4. ARCHITECTURE.md has a new paragraph after the host-import one, covering the trimmed state,
memoryBase/pinnedBytes, reattachment andRange::backingSerial.I also registered
agc_reimport_benchand the new test only on Windows, since on Linux the bench's skip line would not passcheck_skips.py. Tested on Windows 10 19045, MinGW GCC 15.2, NVIDIA 617.42, RTX 4060: both new tests, 56 host-import/settle/guest-memory/libc tests and all 57agc_driver_recorder_testsentries pass, with the same two skips as before. The description is updated.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T04:31:08Z.
Review: CHANGES_REQUESTED
The full Build (linux, run 39195696385) fails
agc_driver_host_import_lifetimewithAGC graphics: guest mutation reached host apply before vkFreeMemory completed;maindoes not fail it.core/libs/prx/libSceAgcDriver/tests/HostImportLifetime.cpp:517-518(partialUnmap): it unmaps the middle page of a 3-page import and requires that import to be freed (requireFreed(imported)) before the guest mutation is applied. With the cut inside the range,retireOrTrim(GuestBufferMemory.cpp:447) now keeps the larger side (the tail on equal sides) trimmed instead of retiring it, so novkFreeMemoryhappens at that point. Please update the test to the new behaviour: the kept side stays imported (assert it is not freed and still covers its range), the smaller side is imported again, and everything is freed onmutation.Remove(lines 527-534). Check the otherrequireFreedcallers that cut part of an import (protectRollback,partialUnmap) the same way.Re-run
ctest -R 'agc_driver_host_import_lifetime|agc_trimmed_import|agc_reimport_bench'on Linux (lavapipe) after the change.Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-19T04:38:17Z.
@adriadam10 thanks, you're right. Fixed in 68f1d5e5f.
partialUnmapnow expects the new behaviour: during the unmap the import's memory is not freed; afterwards the tail (the kept side, equal sides) is served by the sameVkDeviceMemoryand covers exactly its remaining range, the head is imported again with new memory, andmutation.Removefrees both. With the interior branch ofretireOrTrimdisabled, it fails with "an unmap inside a host import freed the memory of the side it keeps".Why it passed for me on Windows: the old test's 192 KiB import still had its deferred settle pending at the unmap, and
retireOrTrimretires an import with a pending settle instead of trimming it. On Linux there are no settles, so it trimmed. The parts are now 1 MiB each (no deferred settle for imports of that size), so the cut takes the trim path on both platforms and the result no longer depends on when the settle read lands.protectRollbackprotects the whole range, which neither trims nor changed, so it keepsrequireFreed. No otherrequireFreedcaller cuts part of an import.Tested on Windows 10, RTX 4060, NVIDIA 617.42:
agc_driver_host_import_lifetime,agc_driver_host_import_mutation_race,agc_trimmed_import,agc_reimport_bench, the host-import, settle, guest-memory and unit-shadow tests (28) and all 57agc_driver_recorder_testsentries pass, with the usual skips. I don't have a Linux machine with lavapipe set up, so I couldn't run the ctest you asked for there. Could you rerun the full Linux build?Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-19T08:04:04Z.
AMD Windows data for the new
agc_trimmed_import: it fails on our RX 6700 XT (gfx1031, driver 32.0.21045.5002, Windows 11 26200, WinLibs GCC 15.2.0 r7, Release).maind8d0222d0, unmodified: fails 10 of 10.main-based builds (one with an unrelatedTexture.cppchange, one with several open PRs that don't touch host imports): 9 of 10 each.In the logged failing runs, the first four cases pass. Those are the two inaccessible pages, and the unmaps of a 2 MiB run inside the import and of its first 2 MiB run, both checked again after the remap. The fifth fails:
So after the pages are unmapped and mapped again,
HostImportForover the whole range returns an import whosememoryequalsoriginal, the import made before the unmap. 2d90ce58a's message says the GPU must read the new pages there, not the old pages the trimmed memory still pins.In that case (
trimmed = false), the checks before the remap pass: the kept side is imported again, and the GPU and the write watch see the guest's bytes. Only the lookup of the whole range after the 1440 KiB are mapped again still findsoriginal. It passed 2 of the 20 runs on the other two builds, so timing seems to play a part. I haven't found the cause; a guess is that the original import is still registered when the remap lookup runs, for example before a deferred release. Happy to run a build with extra tracing on this GPU if that helps.Imported from boykopovar/AnyPS5. Originally posted by Sevthered on 2026-10-19T14:40:24Z.
@parl0v here is the import timing probe on the merged head, same machine and copy as my earlier report (PPSA21567 v01.019.000, Windows 11, Ryzen 7 5800X, 32 GB, RTX 3080 Ti, NVIDIA 616.56, WinLibs GCC 15.2.0 r7). Build:
main5eba12d6b(which has #6499 as64a9caa62), relinked with that commit's relinker, libraries from the same commit plus the probe patch from #6060 (it prints an import only when it took 10 ms or more). Warm shader cache, no input, 160 s from start (PlayStation logo, thentitle_controller_ship).One caveat first: on this
mainthe default host import budget on a 32 GB machine is 8172 MiB (#6472), and the title's four large imports (2172, 4608, 1434 and 3232 MiB, all before its first frame) no longer fit in it. The 1434 MiB import of the range this PR is about (0x324618000+0x599e8000in those runs) is the first one refused, so with default settings there is no import to split. All runs below therefore setAPS5_HOST_IMPORT_MIB=14336, about what the old formula gave on 32 GB. I have put the numbers for that on #6472.Before, from my 2026-10-17 runs (
main82f0c9aec+ this PR at1d3b33985, probe build,logos-Pp1in the archive): after the 1.40 GiB import at0x324618000(924 ms) and the 0.25 GiB head (126 ms), the tail ending at0x37e000000was imported again six times with a rising start (0x3347d8000,0x3360e0000,0x336254000,0x3363ac000,0x3363f4000,0x336444000), 270 to 293 ms each, of which create 150 to 172 ms. 49 imports in the run, import time summed 9.7 s.After, three runs on
5eba12d6b:r-Mp14-2(probe): the 1.40 GiB import takes 988 ms (create 191 ms), then the head0x324614000+0x101c8000is imported again in 99 ms, and nothing of 10 ms or more follows for that range. 39 imports in 160 s, import time summed 8.2 s.r-Mt14-1(APS5_TRACE_HOST_IMPORT=1instead of the probe, so every import is listed): the same head re-import, then only the smaller sides of the later cuts: 25 MiB at0x3347dc000, 1.4 MiB at0x3360e0000, 1.1 MiB at0x336254000, 272 KiB at0x336374000, 1.4 MiB at0x3363bc000, 16 KiB at0x336250000and0x3360dc000, and 7 MiB at0x3359e4000. 48 imports; live bytes go from 12.1 GiB after the fourth large import to 12.4 GiB at the end and never drop, since the trimmed memory keeps counting.r-Mp14-1(probe): head re-import 97 ms, but then the whole tail0x3347dc000+0x49824000(1.15 GiB) is imported three times with identical bounds, 320, 272 and 268 ms (create 154 ms each), followed by 25 MiB at0x3347dc000. 43 imports, import time summed 8.9 s. Same bounds each time, so that is a retire and re-import of an unchanged run rather than a trim; I did not trace which condition inretireOrTrimtook the retire path there (a pending settle would be my first guess), and the other two runs did not show it.So the six moving-start re-imports are gone, and in two of three runs only the small side of each cut is imported again, as you expected. The remaining case is the whole-tail retire in
r-Mp14-1. The archive has the err, out and samples logs of every run named above plus the 2026-10-17 probe log and the probe patch.AI-assisted: yes (Claude Code)