WIP: perf(vk): a fence poll does not wait behind the replay's order lock (render thread off) #2523
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
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
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2523
Loading…
Reference in a new issue
No description provided.
Delete branch "neotoxic/Longshot:perf-vk-fence-poll"
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
With the render thread off (Threaded rendering Off, fewer than 4 cores, or Auto without pool mirrors: integrated GPUs,
the Steam Deck), every
nvnSyncWaitstarts with a poll,rx_nvn_wait(fence, 0)(host/nvn_native.c:1182), madebefore the game's thread gives its virtual core up (
:1190). Inwait_inner(gpu/vk/vk_replay.cpp) that poll tooksync_order_mufor the per-queue fence check, and that lock is held for a submission's whole replay (submit_q→run_submit) and for a present (present_tagged): the game's thread waited behind the replay with its core held, so noother guest thread could use that core either.
wait_rec_seq's flush also waited fordev.present_muwithout a limit,for polls too (the comment at
wait_rec_seqfixed that only for work already reached).The same pattern, fixed for readbacks in
sync_settle_met(the comment above it: a job thread blocked 24% of the time,3.9 ms a frame in Hyrule Field), was never fixed for this poll.
Now a poll (no timeout) tries the lock: taken, it answers as before. Busy (maybe by other work: a present, another
queue's replay), it looks its fence up in
value_whole, where a queue's submission thatrun_submitreplayed whole ispublished with its recording (
submit_recording, underrec_map_mu), and answers from the recordings' timeline(
wait_recorded_whole); not there (stopped, still being replayed, or with no queue), it falls through to thesubmissions' timeline check, also without the lock: the same answer the look under the lock gives. A flush with no
timeout (
queue_flush_for(.., 0): wait_inner's,wait_rec_seq's for a poll) takesdev.present_muonly if it isfree, settled parts or not. Waits with a timeout are unchanged.
Read first:
wait_innerandwait_rec_seqingpu/vk/vk_replay.cpp.Testing
Tested: Windows 11 Pro 26300, RTX 5080, Ryzen 9 7950X, NVIDIA:
dev.py ab --scene hyrule-field --set RX_VK_ASYNC=0(the render thread off, the path this changes), 3 interleaved runs each of main and this PR, 60 s each after the
warm-up: no hang, no black frame, no cold frames, the game played in Hyrule Field in every run. The default (render
thread on) never takes this path (
!async_on()).run_all.py --strict: 687 passed, 111 failed, 45 skipped; 110 failthe same on
mainon this Windows PC (Linux-only and POSIX checks), andtest_recording_admission.pyfails onmaintoo when run alone (4 of 4 here, its
parallelcase, 0xC0000374); it reads none of the changed files. That game runand
run_all.pywere atf578f837.The head (
d07d3d44) in the game, on 04/12 (moved from Still to test):dev.py ab --scene hyrule-field --set RX_VK_ASYNC=0 --runs 2, twice, againstmain@be663a08(this branch's base), on this PC (Windows 11 Pro 26300, RTX5080, driver 617.42, Ryzen 9 7950X): 4 interleaved runs a side, 60 s each, every log's
vk: switches setline readsRX_VK_ASYNC=0 (player); no hang, no black frame, no cold frames. Also on a Linux laptop (CachyOS, kernel 7.3.7,COSMIC Wayland, RTX 3060 Laptop, driver 620.85.02, 8 cores / 16 threads, about 69 fps there): 4 runs
a side, no hang, no black frame. Numbers in Measurements.
At
d07d3d44(game-free):tests/test_fence_poll_busy.py(new, 12 cases, each poll on its own thread while anotherholds
sync_order_muand/ordev.present_mu) passes, and fails withmain'svk_replay.cpp@be663a08(8 of 12) and f578f837's (5 of 12). Theother checks that read the changed functions pass unchanged: on this PC with g++
test_flush_nothing_held,test_hold_gpu,test_readback_early,test_readback_part,test_recorded_wait_flush,test_timeline_modes,test_value_rec_last_part; on Linux (g++, Vulkan-Headers v1.4.345), as they need POSIX or Vulkan headers,test_lost_wait_said(which cutswait_rec_seqfrom the source),test_readback_wait_boundedandtest_fence_poll_busy.Still to test:
main against this PR, expecting a better 1% low and no hang (@Beanstalk Steam Deck: Hyrule Field and Market, main
against this PR).
integrated GPU on Windows: Hyrule Field and Market, main against this PR).
Measurements
Verdict: on current
mainno gain on either machine. Withmain@ca830e89merged in (7f7f2b56), with the render thread off:main.mainagainst 50.6 / 45.2 here (two runs a side, so a lean, not a finding).The +1.2 % measured on this PC against
be663a08(below) is gone:mainhas since moved past that point. Neither machine is the target, though. The change aims at CPU-bound PCs with the render thread off (the Steam Deck, integrated GPUs). This stays a draft for the Test Farm runs in Still to test, and no gain is claimed.Not measured: the number the change targets (the time a poll waits for
sync_order_mu/dev.present_mu).Merged with main (
7f7f2b56againstca830e89), 04/12Setup:
dev.py ab --scene hyrule-field --set RX_VK_ASYNC=0 --runs 2 --seconds 60, interleaved A1 B1 A2 B2, with no fps cap (HOS_FPS_MAX=1000), Render resolution 67 % and V-Sync and frame generation off.RX_VK_ASYNC=0 (player)reads back in every run's log. On this PCdev.py abwrote no Measurements block, because handle numbers in the switches differ per run; the numbers are its per-run lines.ca830e897f7f2b56ca830e897f7f2b56This time the laptop's runs held one level each, with none of the 27.7 fps runs seen below.
At the head (
d07d3d44), 04/12Setup: Windows 11 Pro 26300, RTX 5080, driver 617.42, Ryzen 9 7950X;
dev.py ab --scene hyrule-field --set RX_VK_ASYNC=0 --runs 2 --seconds 60run twice (cap off, HOS_FPS_MAX=1000; Render resolution 67 %; V-Sync and framegeneration off), interleaved A1 B1 A2 B2, then A3 B3 A4 B4.
dev.py abwrote no Measurements block (the simulated-turfrows, which the game rewrites on an area load, and
HOS_CONTROL's pipe name differ between runs); the numbers are itsper-run lines.
be663a08d07d3d44The Linux laptop (CachyOS 7.3.7, COSMIC Wayland, RTX 3060 Laptop, driver 620.85.02, the same command twice, window
1440x900): fps
main68.5 / 57.9 / 68.7 / 27.7, this PR 27.7 / 69.3 / 69.6 / 72.8. A run settles within 30 s on alevel and stays on it (the log's
vi:lines every 5 s); the 27.7 fps level came once on each side, with the samewindow, the GPU at full clock and no limit but the display clock's (
nvidia-smievery 5 s), and the queue marked no runnoisy. Without those two runs, 68.5 / 57.9 / 68.7 against 69.3 / 69.6 / 72.8: too spread to call.
The first commit (
f578f837), 25/11Setup: Windows 11 Pro 26300, RTX 5080, Ryzen 9 7950X;
dev.py ab --scene hyrule-field --set RX_VK_ASYNC=0 --runs 3 --seconds 60(cap off, Render resolution 67%), interleaved A1 B1 A2 B2 A3 B3.be663a08f578f837(
dev.py abwrote no Measurements block: the runs' options files differ in the simulated-turf rows, which the gamerewrites on an area load; the numbers are its per-run lines.)
Risks
value_wholeanswers from the submissions' timeline, which cansay "not done" where the per-queue look might have said done (an older queue stopped): the game then waits for real
(core given up) and gets the answer there, a little later. It never says "done" early:
value_wholeholds onlysubmissions replayed whole (
value_recis not used, its recording can be a stopped submission's). Waits with atimeout are unchanged, so no wait can end early or hang that didn't before.
Checklist
Ready for review
main; the title is a Conventional Commit.main@ca830e89merged in (2a4d5433), plus7f7f2b56, which givestests/test_fence_poll_busy.pythesettings_startstub thatmain'swait_innernow reads.d07d3d44in the game on 04/12, this PC and a Linux laptop: Testing)mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure. (the head againstmain@be663a08, four interleaved pairs with p99: +1.2 % fps on this GPU-bound PC; the target hardware is the Test Farm's)third_party/untouched; nothing GPL-incompatible.When it applies
tests/that reads changed source is updated, not weakened. (none changed; the ones that read the changed functions pass atd07d3d44, as Testing lists, andtests/test_fence_poll_busy.pyis new)dev.py pack; Linux: CI's build)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
Tested in game: Windows 11 Pro 26300, RTX 5080, Ryzen 9 7950X: dev.py ab in Hyrule Field with RX_VK_ASYNC=0 (render thread off, the changed path), 3 interleaved 60 s runs each of main and this branch: played normally, no hang, no black frame.
The description: the cause, this PC's A/B (within the spread, GPU-bound), and what the Test Farm should measure.
@Beanstalk Steam Deck and an integrated GPU on Windows (Iris Xe or an AMD APU), default settings (Threaded rendering Auto, which is off there): Hyrule Field and the Market, fps, 1% low and median frame time, main against this PR, interleaved runs.
@neotoxic only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was forf578f83, and the PR has changed since (d07d3d4), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.@review
Recommendation: Request changes. A poll can report a completed per-queue fence as timed out, and the changed flush can still block behind a present. The reported performance runs are within the spread, so the claimed gain remains unproven.
The change tries to let a guest thread release its virtual core instead of waiting behind replay during a fence poll. It adds a false-timeout case and does not make the batch flush nonblocking in the common settled-recorder case.
Blocking (1): must change before merging.
gpu/vk/vk_replay.cpp:3278: A busy order lock does not establish that this fence is unfinished; it can belong to an unrelated submission or present. (more on the line) Do: Publish enough per-fence completion information to check the fence without waiting for the order lock, and add a game-free test with a completed per-queue fence and the lock held by unrelated work.Should fix (1): not blocking, but worth doing before merging.
gpu/vk/vk_replay.cpp:3203: Passing zero to queue_flush_for does not make its queue lock nonblocking when recorder parts are settled. (more on the line) Do: Make zero-timeout flushes use try_lock regardless of whether parts are settled, including the flush at the start of wait_inner; add a game-free contention test that fails when the lock is taken synchronously.Testing before merging (2) (runs on the systems it can change; it doesn't hold the recommendation)
@fixcan do L1 and L2.Checks: Still running: 9 checks.
More: does it help, other systems, switches, what was read
Does it help: Unproven. Three interleaved Windows RTX 5080 runs report 155.8–157.7 fps before and 157.6–158.3 fps after; the author describes these as within the spread. The CPU-bound target systems have not yet been measured.
Other systems: The order-lock change affects synchronous replay with per-queue fences; the recording flush is also shared by recording waits and readbacks. Windows NVIDIA was played successfully; Steam Deck and Windows integrated GPU runs are pending, as are affected Tier 1 AMD and Linux configurations.
Switches: None added, changed or removed. Existing RX_VK_ASYNC and per-queue fence settings determine which path runs.
Reviewed:
f578f8377fagainstbe663a08a3: Read the diff, commit, repository contribution rules, pull request template, relevant standards and synchronization code, including the guest caller, recorder settlement and submission/present locking. The 8,371-line vk_replay.cpp was not read in full; unrelated sections and architecture documentation were skimmed. docs/review-checklist.md and ../Tooling/AGENTS.md were absent. No generated files or recorded runs changed.; nothing run (a code review).Checked and fine:
Testing: No code or tests were run during review. The author reports three interleaved game runs per side on Windows and reports the listed check failures as also occurring on main; CI is still running.
Overlaps and merge order: Unknown; network access was unavailable.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -3202,1 +3201,3 @@queue_flush_for(FR_WAIT);// without a flush, and the job that waited held up the main thread, so nothing flushed it: the load hang on RADV. A poll// (no timeout) flushes what it can at once and does not wait for dev.present_mu behind a present)queue_flush_for(FR_WAIT, timeout_ns ? UINT64_MAX : 0);L2 (should fix): Passing zero to queue_flush_for does not make its queue lock nonblocking when recorder parts are settled.
When: With a batch held and no recorder parts outstanding, settle_parts returns true, so queue_flush_for calls lk.lock() on dev.present_mu. A poll can still wait behind a present while holding the guest core, including at the earlier flush in wait_inner.
Do: Make zero-timeout flushes use try_lock regardless of whether parts are settled, including the flush at the start of wait_inner; add a game-free contention test that fails when the lock is taken synchronously.
@ -3271,0 +3275,4 @@if (timeout_ns)lk.lock();else if (!lk.try_lock())return 0;L1 (blocking): A busy order lock does not establish that this fence is unfinished; it can belong to an unrelated submission or present.
When: An older stopped queue keeps the global timeline below f, while f's own queue has completed its recording. If another thread holds sync_order_mu, both calls made by nvnSyncWait with a zero guest timeout can return 0, so the guest sees TIMEOUT_EXPIRED for an already signaled fence instead of the per-queue result.
Do: Publish enough per-fence completion information to check the fence without waiting for the order lock, and add a game-free test with a completed per-queue fence and the lock held by unrelated work.
@fix
I didn't fix this pull request: the change I made would take out 5 of the 11 lines this pull request adds outside its tests, which undoes it rather than fixing it. So L1 and L2 are the author's now. Nothing was pushed.
Automated fix by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
Thanks, both were real. Fixed in
d07d3d44(a new commit on the branch).L1. A busy order lock no longer answers the poll. A queue's submission that run_submit replays whole (not stopped) is now published with its recording, in
value_wholeunderrec_map_mu, from the submit that finishes it (map_recording). A poll that findssync_order_mubusy looks its fence up there and answers from the recordings' timeline throughwait_recorded_whole. If the fence is not published (stopped, still being replayed, or a queue-less submission), the poll falls through to the submissions' timeline check, also without the lock. This gives the same answer as the look under the lock.value_recis not used for this, because a recording tagged with a stopped submission's value (or a later value of a burst) would give a false "signalled".L2.
queue_flush_forwithsettle_ns == 0now usestry_lockondev.present_mueven when the parts are settled. That covers the flush at the start ofwait_inner, the one before its final timeline check, and the one inwait_rec_seq. A wait with a timeout still takes the lock as before. Inwait_inner's stopped-work loop, the flush (which already passed 0) now tries the lock too. That wait flushes again with its timeout before its driver wait.Test.
tests/test_fence_poll_busy.pycompiles the realwait_inner,wait_recorded,wait_recorded_whole,wait_rec_seq,queue_flush_forandtime_leftagainst stand-ins. It runs each poll on its own thread while another thread holdssync_order_muand/ordev.present_mu. Each poll must come back within 2 s with the right answer. The cases:dev.present_mu: a poll's flush, a poll, and a poll whose recording waits in the batch (thewait_rec_seqflush) all come back at once without flushing.The test also checks in the source that
value_wholeis filled only from submissions run_submit finished whole.On the old code it fails:
f578f837: 5 of 12 cases fail (the busy order lock case, and the fourdev.present_mucases).value_recused in place ofvalue_whole: the stopped-submission case fails, which is the false-signalled guard.tests/run_all.py --changed mainon Windows: 106 passed, 31 skipped (no Vulkan headers here), and 3 failed (test_dev_packs, test_pad_speaker, test_bug_report). Those 3 fail the same way onf578f837without this change.dev.py vkcompiles and linkslongshot_vk.dll. Its export check did not run, because this PC has no llvm-objdump. Not re-run in the game for this commit, and L3/L4 (Steam Deck and Windows iGPU measurements) are still open.@review
Checked again at the head,
d07d3d44, against BeanBot's review off578f837:value_whole(submissions replayed whole, never a stopped one's recording), else from the submissions' timeline, never "not done" just because the lock is busy.tests/test_fence_poll_busy.pypasses here, and fails withmain'svk_replay.cpp@be663a08(8 of 12 cases) and with f578f837's (5 of 12).dev.present_mu): fixed, still holds.queue_flush_for(.., 0)takes the lock only if it is free, parts settled or not; the test's three present cases cover it.The other checks that read the changed functions pass unchanged at the head, including the two that need Linux (
test_lost_wait_said,test_readback_wait_bounded, run on a Linux PC). The description now matches the head. It says plainly that the A/B in Measurements was atf578f837, within the spread on a GPU-bound PC. The head still has to run in the game and be measured here (first Still to test item), so the PR stays WIP.ca830e89into perf-vk-fence-poll 2a4d5433cdb77c2f4b) into perf-vk-fence-poll 5abc5f9f3ce345b3cab) into perf-vk-fence-pollBrought up to current
main(e345b3ca) by a merge, nothing rewritten. Two test-only commits came with it:1cd5b71edeclaresrec_open_wholeandvalue_wholein two fixtures that cutvk_texture.cpp(main added them), and707803fdmarksflush_reason[[maybe_unused]]in the fence-poll fixture, which clang stops on (-Wunused-but-set-global). Windows,run_all.py --changed origin/main: 148 passed, 3 failed:test_dlssg,test_sync_watch_eventandtest_turf_budget, which fail onmainitself too (checked in a checkout ofmainon the same PC).18edd5097) into perf-vk-fence-pollBrought up to current
main(18edd509) by a merge,2fd2d6b4, nothing rewritten. No conflict. Windows,run_all.py --changed origin/main: 145 passed, 7 failed (test_dlssg, fourtest_present_*,test_sync_watch_event,test_turf_budget): all of them fail onmainat18edd509too, on this PC, run there with the same toolchain.3a0f7d8f7) into perf-vk-fence-pollView command line instructions
Checkout
From your project repository, check out a new branch and test the changes.