perf(vk): arrivals' captures: retired capture buffers destroyed on a thread of their own, and a late page's pool given its stamps at once #1848
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1848
Loading…
Reference in a new issue
No description provided.
Delete branch "GummiNaomi/Longshot:perf/arrival-capture"
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?
Summary
Arriving in an area, two parts of the capture of guest memory held up the render thread. On 16 cores the replay's take of a capture destroyed the capture buffers past the spares with the pools' lock held: takes of 25 to 49 ms were that loop alone. On 4 cores each late page in a 64 KB block that had no stamps yet made its capture wait for the GPU, again and again in the same few new pools: up to 211 ms of waiting an arrival. This destroys those buffers on a thread of their own (
RX_VK_CAP_FREE_ASYNC) and gives a late page's pool its stamp blocks at once (RX_VK_LATE_STAMPS_AHEAD). In two teleport tours a side on my PC: on 16 cores the biggest memory-sync frame of an arrival 42.3 -> 13.9 ms at most and takes of 10 ms or more 70 -> 2; on 4 cores the late-page wait 32.9 -> 10.3 ms an arrival (211 -> 41 at most). Both on by default;=0puts either back as it was.Why: at each submit the game's thread copies the guest memory the GPU will read into a capture buffer, and the replay takes it (
take_capture,gpu/vk/vk_memory.cpp). A buffer it lets go past the spares is destroyed there (cap_buf_free), withpools_muheld: freeing a 64 to 256 MB host-cached memory object is the driver unmapping and releasing every page of it. An arrival retires dozens (6,013 destroyed in a 4-minute tour). Separately, a capture page the GPU still writes back ("late") is fixed on the GPU by the late pages' fix (late_fix_build), which needs the 64 KB stamp block of the page; a page whose block isn't there yet makes the block and waits for the GPU, as before the fix (its log's "no stamps or shadow yet", the only reason in every 4-core arrival). Arriving, the game writes 100 to 190 pages a capture of a new 4 to 8 MB pool the GPU also writes, each capture's late pages in other blocks, so the waits came one block at a time (6 to 20 an arrival, 20 to 250 ms each). On 16 cores with the frame overlap on, the takes' late pages are left to their first use, so these waits don't show there.What changes:
RX_VK_CAP_FREE_ASYNC:cap_buf_freehands a buffer past the spares todestroy_buffer_later, a queue and one thread that destroys them; only buffers the GPU is done with get there, a spare is never destroyed, and at the process's exit (cap_shutting_down) what is queued is left.RX_VK_LATE_STAMPS_AHEAD: wherelate_fix_buildmakes a late page's missing block,stamp_blocks_aroundalso makes the blocks of the rest of its pool (all of a pool of 16 MB or less, else the 2 MB around the page), throughstamp_block_make, so within the 64 MB stamp budget as any block; that capture still waits, the next ones' late pages there are fixed. Unchanged: the spares, which pages are late, the fix's rules, the frame overlap's.Read first:
destroy_buffer_laterandstamp_blocks_aroundingpu/vk/vk_memory.cpp, thentests/test_cap_free_async.pyandtests/test_late_stamps_ahead.py.Testing
Tested: Windows 11, RTX 4070 SUPER (NVIDIA 617.42), Ryzen 9 9950X3D with SMT off: two teleport tours from the
kokiri-startsave a side, both switches off against on, on 16 cores and on 4 cores (14 arrivals a tour: Castle Town's market, Hyrule Field, Kakariko, Kokiri Forest, the Lost Woods, Zora's River, round again), and one 16-core tour withHOS_NVN_FRAME_OVERLAP=2(the overlap's guard) and both on, on a build of main @f505340bwith #1810-#1813's changes and this one; the arrivals' pictures alike both ways (no white frames); the late pages' fix counted the same both ways (about 21,000 fixed, 0 CPU reads waited); the guard: 0 reads changed.Checks:
tests/test_cap_free_async.py(every buffer destroyed once, in order, on one other thread; none at the exit) andtests/test_late_stamps_ahead.py(the blocks a pool gets: small, big, unaligned, smaller than a block) pass on this head on Windows (clang) and Linux (g++ 13.3, WSL Ubuntu 24.04,-Wall -Wextra -Werror);tests/test_capture_spare_reuse.pypasses with its two new stand-ins.Suite:
tests/run_all.pywith every check on main @068fb8ebin WSL Ubuntu 24.04 (740 pass, 61 fail, 69 skipped: no clang or spirv-val there), then this head's--changedchecks in WSL (85 pass, 11 fail, 1 skipped) and on Windows (92 run, 16 fail). Every failure also fails on main @068fb8ebon the same OS: in WSL and on Windowstest_capture_read_once,test_crash_dialog,test_dev_ab,test_frame_overlap,test_guest_imported,test_linux_pressure,test_pending_guest_import,test_present_queue_default,test_replay_write_order,test_texel_buffer_ownership,test_vk_occlusion_queries; on Windows onlytest_driver_env_choose,test_mesh_fallback_layout,test_tracy_funcs,test_udmabuf_limit_unreadable,test_vertex_bind. (On Windows the changed checks ran on the head before its last two fixes:stamp_blocks_aroundwritten inuint64_talone, which g++ needs on Linux, andtests/test_late_fix_build.pyrun withRX_VK_LATE_STAMPS_AHEAD=0; the five checks that read that code were run again on this head and passed, as in WSL.) (On Windows most of them are compile errors with this PC's compiler wrapper, on main as here.) These ran on the head as it was on main @068fb8eb. It has since been rebased onto main @1780fbe0; there its own checks pass on Windows and in WSL, and every one of its--changedfailures in WSL (79 pass, 15 fail) also fails on that main.Still to test:
RX_VK_LATE_STAMPS_AHEAD=0, and itsstamps forlines under 65536 KB (by hand)RX_VK_CAP_FREE_ASYNC=0(by hand)Measurements
Verdict: smoother arrivals in the capture's own columns on both core counts; whole arrival time within the spread. 16 cores: memory sync 167 -> 141 ms an arrival, its biggest frame 42.3 -> 13.9 ms at most, takes of 10 ms or more 70 -> 2, the retire's time 2,267 -> 98 ms a tour. 4 cores: the late-page wait 32.9 -> 10.3 ms an arrival on average and 211 -> 41 ms at most, memory sync 214 -> 186 ms an arrival, the worst arrival frame's median 113 -> 97 ms. No change in RAM; the stamp blocks reached 36 MB of VRAM (of 64).
Setup: Windows 11 Pro 26200, RTX 4070 SUPER 12 GB (NVIDIA 617.42), Ryzen 9 9950X3D with SMT off, 32 GB; teleport tours from
kokiri-start, each arrival's first 3 s, teleports only; 2560x1440, no window (HOS_HEADLESS=1), uncapped; 16 cores, and 4 cores (affinity 0xF00);RX_VK_LATE_FIX=1 RX_VK_TEX_MOVE=1 RX_VK_TEX_AHEAD=2(andHOS_NVN_FRAME_OVERLAP=1on 16 cores) and a temporary diagnostic log of the captures, the same in both arms;RX_VK_FRAME_TRACE=1for memory sync, the late wait and the arrival frames.Baseline: one build of main @
f505340b(before the tooling revamp068fb8eb, which changes none of this code) with #1810-#1813's changes and this one, the two switches off (main's behaviour; that build's default) against=1(this PR's default); one tour with them off per core count was the same build less this change. "Excess" is an arrival's time above the steady frame time; on 16 cores without the frames of 900 ms or more, which came in both arms (run to run, not the capture).f505340bf505340bRX_VK_CAP_FREE_ASYNCandRX_VK_LATE_STAMPS_AHEAD(new, kill switches) ship on, chosen by the table above: better in every capture row on both core counts, and the whole arrivals within the spread, not worse;=0puts each back as on main.Risks
What could break: the stamp blocks made ahead count toward the 64 MB stamp budget (
kStampMax); once it is full, a late page in a pool with no block yet gets none, and its captures wait for the GPU every time, as before the fix. The tours reached 36 MB; an area with more GPU-written pools could fill it sooner (RX_VK_LATE_STAMPS_AHEAD=0then makes only the late pages' own blocks, as before). A buffer destroyed on its own thread frees its memory a moment later than before. The 16-core frames of about 1 s came in both arms, so they are not this change. Turning it off:RX_VK_CAP_FREE_ASYNC=0,RX_VK_LATE_STAMPS_AHEAD=0. After updating, a player sees shorter hitches as an area loads in; nothing changes for saves,longshot_options.inior the caches.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure. (Two tours a side, on a build of main @f505340bwith four open PRs' changes, as Baseline says; more tours are Still to test's first item.)third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def.tests/that reads changed source is updated, not weakened.gpu/vk/vk_memory.cppwas built on Windows for the measured build, not yet on this head or on Linux; the tests compile the new code with clang on Windows and g++ 13.3 on WSL Ubuntu 24.04. On this head the changed files pass a syntax-only compile with the builder's own Windows flags.)Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
Possibly related, for context:
50e2098f0fbb1a1b1384WIP: perf(vk): arrivals' captures: retired capture buffers destroyed on a thread of their own, and a late page's pool given its stamps at onceto perf(vk): arrivals' captures: retired capture buffers destroyed on a thread of their own, and a late page's pool given its stamps at once