perf(agc-driver): join host-import spans only over whole aligned windows #3425
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3425"
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 oneandonlydean on 2026-10-11T10:10:46Z.
What
Carries #3335 (@Tamalero, closed by the intake pause), rebased onto
main85c7cf9b, with one change: the window is fixed at 8 MiB instead of read fromAPS5_HOST_IMPORT_WINDOW_MIB.Since #3158, a title we test with loses the device on its first logo, 0.6-1.3 s after start, in 8 of 8 runs on NVIDIA; it ran at 60 fps before. A span is the whole run of adjacent registered ranges, and this title grows a heap next to its GPU memory in 64 KiB pieces, so every growth retires the span at
0x200230000and imports it again:with
NVRM: Xid ... 109 ... CTX SWITCH TIMEOUTon the host. 5 of the 8 runs have 268-402 refused imports, starting at a span of 119-123 MiB; the other 3 lose the device or segfault without one.With #3335, ranges are joined only where a run fills a whole aligned 8 MiB window, and a span never crosses a window edge. A range in a partly filled window is imported on its own, so a growth re-imports at most one window, once, when it completes. A buffer that crosses two imports is copied, as before #3158.
main77bd928a(before #3158)main85c7cf9bmain85c7cf9b+ #3335 as postedmain85c7cf9b+ this branchSecond commit:
VulkanDevice.cppsets the window to 8 MiB without the environment variable;SetHostImportWindowstays for the tests.Not measured here: #3158's own title (PPSA05684, 1.34 → 2.76 fps from importing buffers that cross its 1 MiB heap pieces). Those pieces fill whole 8 MiB windows only if the heap is mapped that far, and a buffer that crosses a window edge is copied again, so its gain may be smaller than with unbounded spans. #3335 reports Ratchet & Clank: Rift Apart at 11.0 GB imported for 9.4 GB live and no refusals, against about 1.5 TiB re-imported with #3158.
Tested
Linux (CachyOS, GCC 16, Release,
BUILD_TESTING=ON), RTX 4090, NVIDIA 610.57.04, onmain85c7cf9b:main(table);ctest: 802/810, with #3335'simportWindowTestspassing inagc_driver_graphics. The same 8 fail on plainmain85c7cf9b:agc_driver_dcc_keys_first_write(#3179),agc_driver_bda_device,agc_driver_f32_compare_constantandagc_driver_flat_apertures_0,_2,_3,_4,_5;tools/check_conventions.pypasses.Not tested on Windows or on AMD.
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 adriadam10 on 2026-10-11T10:35:55Z.
Checked on Linux: PR merged onto current main, libSceAgcDriver build,
agc_driver_graphicspasses, and the newimportWindowTestsdoesn't build without the change. Reviewed that a span only forms over a fully filled aligned window, otherwise the range is imported alone, and the test pins base/bytes, the window edge and that growth doesn't re-import another window. TechnicalDebt entry updated. Merging.Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-11T12:06:57Z.
A data point from the other side of #3158: Sword of the Sea (PPSA25344, Unreal) gets about 2.2x slower in its main menu with this change.
Setup. Windows 11, RTX 5070 Laptop (616.92). Our Sword of the Sea tree is
mainfrom 2026-10-10 (57f96fd2b) plus #3158 as 78e2408ee and a few title fixes. I cherry-picked both commits of this PR onto it; the only conflicts were context lines. Runs alternate between the two trees, 180 s each, same env:The picture is the same in all runs, and there are no host import failures or budget lines.
Why. The title binds a 368 MiB buffer that crosses two registered ranges, 320.75 MiB and 47.3 MiB next to it. Both are registered whole and early and never grow. With this PR, a range larger than the window is imported alone and never joined, so the buffer falls back to copies:
dispatch.q0_indirectbecomes the top cost at about 260 ms per frame.So the window fixes the heap that grows in 64 KiB pieces, but it also cuts the case #3158 was for: large ranges that are complete from the start. Could a span keep joining across window edges where every window it covers is already completely registered when it is first imported, and stop at a window that is still partly filled? A window that completes later would start a span of its own instead of extending an imported one. Then a growing heap never re-imports what is already imported, and ranges registered whole stay one import. I can run any variant on this title and post the numbers.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T22:54:52Z.
Yes, with one more condition: a span is made only from windows and large ranges that no import holds yet, and it is never extended afterwards.
up4/host-import-join-registered-windowson oneandonlydean/AnyPS5 (bcc81c52) does that; a window filled later gets an import of its own, so a growing heap still re-imports nothing. GPU memory is imported when it is mapped, so your two ranges join only if both are registered before the first one is imported, andAPS5_TRACE_HOST_IMPORT=1will show whether they come out as one 368 MiB import. Could you run it on PPSA25344?Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-11T22:56:18Z.
@oneandonlydean thanks, that's the shape I was hoping for. I'm building
up4/host-import-join-registered-windows(bcc81c52) on currentmainnow and will run it on PPSA25344 against plainmain, alternating, withAPS5_TRACE_HOST_IMPORT=1. I'll report whether the two ranges come out as one 368 MiB import, plus frame time and the 368 MiB upload / CPU-copy counts.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T01:47:37Z.
@oneandonlydean thanks for
up4/host-import-join-registered-windows(bcc81c52). I ran it on PPSA25344 against plainmain, alternating, withAPS5_TRACE_HOST_IMPORT=1. It does not join the two ranges, and nothing changes in the menu.Setup. Windows 11, RTX 5070 Laptop (616.92). Both trees are
maina11c8f7f7(2026-10-11, includes this PR) plus our local patches for this title; B adds your commit (bcc81c52, cherry-picked cleanly).APS5_WRITE_WATCH_IMPORTS=watch, 180 s rows, A B A B.mainmain1. The two ranges are still two imports in B. In B1 the trace has
host import of 0x30b1900000+0x140c0000(320.75 MiB) and, later,host import of 0x30c59c0000+0x2f50000(47.3 MiB). The second starts exactly where the first ends. B2 is the same (0x30b18c0000and0x30c5980000); in A1 the pair is0x30b1890000and0x30c5950000, also two imports. No 368 MiB import appears in any of the four runs, and the other large imports (576 MiB at0x306b480000, 128 MiB, 43.75 MiB, 42 MiB) are single ranges.2. Order. The trace has no timestamps and does not log registration, so I can only give the order of the import lines. The 320.75 MiB range is imported at stderr line 2826 (B1) and the 47.3 MiB range at line 3923: 1097 imports apart (live count 2342 → 3415). B2: lines 2828 and 3944. A1: 2816 and 4063. In all runs both come before the first 10 s
[present]report, so during start-up. So the second range is imported long after the first. I can't tell from this whether it was registered before the first import (and your join was refused, for example bycontinuesRuncomparingwritable/gpu) or only afterwards. From the code, when both ranges are leased and agree onreadable/writable/gpu,importRunshould join them: neither is held by an import, and each is awholerun because both cross a window edge. So I think the 47.3 MiB range was not registered yet at the first import. I'd like to confirm that with a registration trace.3. 368 MiB uploads and CPU copies, A vs B. The
[buffers]line (regions copied on the CPU outside imports) at the 9000th copied region:0x30b0000000: 453 regions, 166,732 MiB, i.e. 368.1 MiB each, in A1, B1 and B2 (A2: 504 / 185,504 MiB at the 10,000th). That is the buffer across the two ranges, copied as before.0x30c0000000: 1812 regions, 173,952 MiB, i.e. 96.0 MiB each, also identical in A1, B1 and B2. I haven't checked whether these cross the same boundary.So B copies exactly the same regions as A.
4. What would have to change. Only guesses, I have not tried any of them:
I can add a one-line trace for
GuestAllocationsregistrations (address, bytes, flags, tick) and a trace of the join decision inimportRun, and run A and B again, if that helps. I can also run the bind-time variant if you write it.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T05:16:24Z.
Thanks, that settles it: the first range is imported when it is mapped, so no rule at import time can join them.
up4/host-import-bind-joinon oneandonlydean/AnyPS5 (7787130) joins at bind time instead: a buffer bound across imports whose windows are completely registered gets one import over them, under the device lock, and the imports it replaces are retired then; growth beside an import still never re-imports it. Could you run it on PPSA25344 againstmainwithAPS5_TRACE_HOST_IMPORT=1? The lines to look for arehost import of ... joins N registered ranges for a bind of 0x30b...+0x17010000, anynot joined for a bind of ...: <reason>(the 96 MiB regions in bucket 0x30c0000000 will say why they are copied, if they still are), the[buffers] ... outside importstotals and the frame time.Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T05:17:41Z.
@oneandonlydean thanks, building
up4/host-import-bind-join(7787130) now. I'll run it againstmainon PPSA25344 withAPS5_TRACE_HOST_IMPORT=1and report the join/refusal lines, the[buffers]totals and the frame time.For context: a local bind-time join prototype of ours takes the menu from 289 to 134 ms/frame (3.5 → 7.5 FPS, Windows default env), with CPU copies outside imports going from ~422 GB to ~18 GB per run. On our tree it only joined after an extra change: there the direct memory under those ranges is mapped as shared views, and the read-write alias for the joined import has to span several view runs. I'll check whether yours hits the same limit and say so with the numbers.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T14:09:58Z.
@oneandonlydean thanks for
up4/host-import-bind-join(7787130). On PPSA25344 it finds the 368 MiB bind and tries the join, but onmainthe joined import is never made: the read-write alias it needs throws, so the buffer is still copied and the frame time doesn't move. #4367'sMapAliaschange covers that case. With the same mechanism, our own bind-time join took the menu from 289 to 134 ms per frame.Setup. Windows 11, RTX 5070 Laptop (616.92). The base is
maina11c8f7f7(2026-10-11) plus our local patches for this title. One of them keeps direct memory as shared views, as onmain(it undoes #2054's private pages, which turn this menu red). Your commit was cherry-picked without conflicts. Upstream default env (APS5_RESOURCE_CACHE_ENTRIES=8192,APS5_PROFILE_DRAW=1), 180 s rows, A B A B,APS5_TRACE_HOST_IMPORT=1on B.mainmain1. The join is tried once and never imported. B1 logs
host import of 0x30b18b0000+0x17010000 joins 2 registered ranges for a bind of 0x30b18b0000+0x17010000at stderr line 4454; B2 logs the same for0x30b1cb0000at line 4474. Neither run has ahost import of ... ok (...), joinedline after it, and the CPU copies don't change. At the 8000th copied region, A1, B1 and A2 match exactly: 5064 regions outside imports, 281,119 MiB. Of those, 366 × 368.1 MiB are in bucket0x30b0000000and 1464 × 96.0 MiB in bucket0x30c0000000. B2 at the same point has 400 × 368.1 MiB and 1600 × 96.0 MiB.2. Why. Both ranges are shared views here, so
createImportEntryneeds a read-write alias over the joined span.WindowsMappings::MapAliasonly aliases one contiguous run of views of one section. These two ranges are views of two sections (one per direct memory allocation), soGuestArenaMapAliasthrows. For a joined entry the catch then runsif (entry.joined) return refuse();, which adds the span tounjoinedwithout a trace line. Every later bind of the buffer returns beforeimportCoveringtraces anything. So the trace shows the attempt but not the refusal; a line inrefuse()for joined entries would make it visible. The 96 MiB regions in0x30c0000000log nonot joinedline either. With our own join (point 3) they went away together with the 368 MiB copies, so I think they are binds across the same pair that hit the sameunjoinedentry.3. With a multi-run
MapAlias. #4367 (Angel21PC's alias runs from #2254, carried by @MarcZX100) letsMapAliasalias a range made of several runs: it reserves a placeholder the size of the range and maps one view per run of a section's consecutive pages into it. We measured the same mechanism, with an alias change of our own that #4367 replaces, together with our own bind-time join, A/B in one build by an env switch:host import of 0x30b1880000+0x17010000 ok (...), joined at bind.Without the alias change our join is refused the same way: the tree before it logged
host import join of 0x30b1cc0000+0x17010000 (2 ranges) refused. I haven't run your join on top of #4367 yet; I can, and our join is up as #4515 (on #4385 and #4367) for comparison.4. The other
not joinedlines. B1 has 15 and B2 has 16, alla piece lies in a partly registered window. They are small binds: 2 MiB ones at0x305f...(bucket0x3050000000, 2386 × 2 MiB at the 8000th region) and 256 KiB and 3 MiB ones, mostly at0x31d7.... After our join, the 2 MiB bucket is the largest copy left: 7339 × 2 MiB per run.5. Retiring inside a bind. Your join retires the imports it replaces inside
UploadFinish's region loop and insideHostImportFor. Onmain,retireImportdestroys an import at once when the recorder is idle, because it only keeps the holder for a busy recorder. An earlier region of the same loop that already took one of those imports, or aHostImport*a caller took earlier under the same device lock, still names it. Theregion.directreuse is guarded by the epoch, but a region that already took its import is not. Commands recorded afterwards in the same hold then use a destroyed buffer. This is the mechanism behind the start-up device losses in #4385, which keeps a retired import for every batch its hold opens (Recorder::KeepThroughHold). With that fix, retiring inside the loop is safe. Our own join joins in a pass before any region of the build takes an import, and never insideHostImportFor. This part is from reading the code. No run showed a device loss from it, but B never made a joined import either.AI-assisted: yes (Claude Code)