perf(host): NVN recordings' copies: the sweep's ticket read at retirement, segment size hints, frees outside the copy lock (HOS_NVN_REC_OLD=1 as before) #1812
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
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1812
Loading…
Reference in a new issue
No description provided.
Delete branch "GummiNaomi/Longshot:perf/nvn-recording-copies"
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
Longshot copies each command recording the game makes and frees those copies later. This PR changes the bookkeeping around those copies, and fixes a race that main has too.
The race: the sweep of overwritten copies read its ticket (the submits so far, held ones included) before it read the copies' marks. A producer could submit a copy at N + 1 and then overwrite its mark in between, so the sweep let the copy go at N, and the pool could hand its block out again while the replay still read it. main reads the same ticket under the copy lock, but still before the slice reads its marks, so the window there is smaller but present. The ticket is now
retire_ticket(), read once the copy is seen dead: fences, then the held count, then pushed.The bookkeeping, on top of that:
seg_hints), instead of 64 KiB every frame;copy_lockis let go (frees/frees_run);sweep_slice), not inside its hold;handle_prefetch, incheck_handle, CallCommands and CopyCommands).HOS_NVN_REC_OLD=1restores the old bookkeeping (64 KiB first segments, frees under the lock, the slice inside EndRecording, no prefetch) and keeps the ticket fix.Unchanged: the copies themselves are main's, byte for byte (24-byte prefix, pool class, bucket nodes:
copy_prefix,rec_node,flatten, BeginRecording), the bytes the replay reads, the handles and the API. What the game sees is the same.The result: measured at this head against its base on 4 and 16 cores, frame rate, frame tails, memory and the game threads' CPU are unchanged, within run-to-run spread. The ticket fix closes a use-after-free window present on main. Whether the bookkeeping without a measured gain is worth carrying is the reviewer's call.
The review of
b8d2204b(futondestroyer) had two blocking points, both fixed: the sweep's ticket (above), and the copy's prefix, which had grown to 48 bytes (the bucket node inside it) even withHOS_NVN_REC_OLD=1, so a 65,504-byte recording's copy crossed into the 128 KiB class; the prefix and nodes are main's again, both ways.#1780 (snaildev99) hands a half-full segment over as the handle (
HOS_NVN_REC_HANDOFF): the same mechanism and rule as this PR's formerend_copy, and only one can be on. This PR's hand-over is removed, since #1780's came first, is that one change behind its own switch, is measured at its own head, and ours never showed a gain. The remaining overlap with #1780 is text next to BeginRecording, EndRecording andflattenonly; #1780'srec_endwrites main's 24-byte prefix, which is the prefix here again, so either can merge first. #1811's submit accounting: this PR no longer changes what is submitted or when, only when the ticket is read.Read first:
retire_ticket,release_copiesandsweep_bucketsinhost/nvn_native.c, thenseg_hints,frees_runandsweep_slice, andtests/test_nvn_recording_copies.py. See #1780, #1811, #1553, #766.Testing
Tested: Windows 11 Pro 26200, RTX 4070 SUPER (NVIDIA 617.42), Ryzen 9 9950X3D with SMT off, 32 GB, at this head
2763ba17: 12 stands in Castle Town's market (Measurements), 4 and 16 cores, with and withoutHOS_NVN_REC_OLD=1. Nonot a live recordingline, no FATAL, no frame of 50 ms or more, and the picture as on main.Checks, at
2763ba17:tests/test_nvn_recording_copies.pypasses on Windows (clang) and in WSL Ubuntu 24.04 (gcc 13.3,--cc gcc), both ways: the real recording code over stand-ins (eight frames of draw contexts calling cached model recordings, lifetimes, the sweep, six threads, nothing left at the end).sizes()checks a 65,504-byte recording's copy is 65,528 bytes in the 64 KiB class.tickets()pauses a sweep after its ticket while a producer submits the copy at N + 1 and overwrites its mark, and flushes a held submit between the ticket's two reads; neither copy may be freed with the replay at N. 7 broken variants are each caught: no hint, a hint that never shrinks, an early free, a batch from the wrong bucket, the ticket before the marks (main's order), pushed read first, a 48-byte prefix.tickets()andsizes()fail both ways. The fixture does not build against main'shost/nvn_native.c(it cuts out functions this PR adds); main's ordering is theticket-earlyvariant, which it catches.tests/test_hos_ab.py(the wired switch;sizes,ticketsand six threads with it flipping every 16 reads: 127,016 reads, half of them old),tests/test_nvn_host_tables.py,tests/test_recording_log_handoff.pyandtests/test_switch_table.pypass on Windows.tests/run_all.py --changed 1780fbe0in WSL Ubuntu 24.04: 30 pass, 10 fail, 6 skipped (no clang there). The 10 fail on main there too:test_crash_dialog,test_dev_ab,test_dev_packs,test_frame_overlap,test_hold_gpu,test_host_guest_writes,test_linux_pressure,test_present_queue_default,test_split_before_draw,test_vk_occlusion_queries.test_nvn_recording_copiesandtest_hos_ab, which run_all skips there, pass with--cc gcc.-Werrorin WSL).Still to test:
HOS_AB=HOS_NVN_REC_OLD=1,3,60 RX_VK_FRAME_TRACE=1, thenscripts/perf.py ab, expecting side 1 (the old bookkeeping) no faster and nonot a live recordingin the log (@Beanstalk steamdeck, Castle Town's market 3 minutes with HOS_AB=HOS_NVN_REC_OLD=1,3,60 and RX_VK_FRAME_TRACE=1)not a live recording, no FATAL and the picture as on main (@Beanstalk linux, Castle Town's market 3 minutes, the log and a screenshot)not a live recordingand the frame rate as main's (@Beanstalk windows, Castle Town's market 3 minutes on this PR merged with #1780)Measurements
Rebased: this head,
45159102, is2763ba17rebased onto main89c3d21ewith no conflicts; the numbers below are from2763ba17on its old base. At45159102,tests/test_nvn_recording_copies.py,test_hos_ab.py,test_nvn_host_tables.py,test_recording_log_handoff.pyandtest_switch_table.pypass on Windows (clang), andhost/nvn_native.ccompiles syntax-only with the builder's flags.Verdict: no change. Main, this PR and this PR with
HOS_NVN_REC_OLD=1are within each other's run-to-run spread at 4 and 16 cores, in mean frame rate, frame tails, process memory, the copies' memory and the game threads' CPU.Setup: Windows 11 Pro 26200, RTX 4070 SUPER 12 GB (NVIDIA 617.42), Ryzen 9 9950X3D with SMT off, 32 GB. Castle Town's market (child), standing after a debug teleport from the
kokiri-startsave, 160 s; the control tool'stestprofile (1920x1080, headless); uncapped (HOS_FPS_MAX=1000);HOS_THREAD_TIMES=5; otherwise defaults.RX_VK_FRAME_TRACE=1; frames from 300 after the arrival to the end; fps from the summed frame times; process private bytes sampled just before the stop; the copies' MB from the last "recordings made" line; the game threads' CPU from the last 2 minutes ofHOS_THREAD_TIMES. The game's affinity 0xF00 (4 cores) or 0xFFFF (16). Order main, on, off, off, on, main at each affinity (2 runs a side), all on 20 Nov 10:09-10:53. main @1780fbe0is this branch's base, built 20 Nov; this PR @2763ba17is one build for on and off ("off":HOS_NVN_REC_OLD=1).1780fbe02763ba17HOS_NVN_REC_OLD=1: this PR @2763ba17The earlier start-to-start numbers on
54a42715(62.4 to 58.8 and 60.1 to 64.5 fps at 2560x1440 with other experimental switches) were within their own spread and are replaced by these. Not measured: the copy lock's wait time itself (Tracy was not used), Linux, the Steam Deck.HOS_NVN_REC_OLD(new, a kill switch, its row ingpu/vk/switches.def; read throughhos_ab_value, soHOS_ABcan flip it) ships unset: the new bookkeeping. The measurements above find no difference either way, so unset is not chosen for speed;=1is for a recording problem, and keeps the ticket fix.Risks
What could break: a copy freed too early would show as a wrong or missing draw or a crash in the replay. The ticket is now read later than on main, never earlier, so a copy is freed later if anything. The test covers the race deterministically and runs six threads both ways and under flips. The frees and the sweep run in a different place and order than on main. Not run: Linux or the Steam Deck in the game, this PR together with #1780 or #1811. Turning it off:
HOS_NVN_REC_OLD=1(the ticket fix stays); a revert is enough. After updating: nothing changes for saves,longshot_options.inior the caches; no cost measured (Measurements).Checklist
Ready for review
main; the title is a Conventional Commit. (Rebased onto main89c3d21eas45159102, no conflicts.)mise run testpasses (any failure that also fails onmainis named). (tests/run_all.py --changed 1780fbe0in WSL, its 10 failures main's own there, named in Testing; the PR's own checks on Windows.)main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here. (tickets()intests/test_nvn_recording_copies.py: main's order, the ticket read before the marks, is itsticket-earlyvariant, caught; it fails both ways on this PR's first headb8d2204band passes here.)gpu/vk/switches.def. (HOS_NVN_REC_OLD, unset by default; the measurements found no difference either way.)tests/that reads changed source is updated, not weakened.2763ba17, the measured build. On Linux only checks:tests/test_nvn_recording_copies.pyandtests/test_hos_ab.pycompile the changedhost/nvn_native.ccode with gcc 13.3-Werrorin WSL Ubuntu 24.04 and pass, and the--changedsuite there. No Linux build was done.)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
Recommendation: Request changes. Copy retirement can use a completion watermark older than a submit that still references the recording.
Does it help: Unproven. GummiNaomi's earlier four-core headless comparisons on
54a42715with companion changes went in opposite directions, 62.4 to 58.8 and 60.1 to 64.5 FPS. The exact published head is reported unbuilt.Other systems: The new default runs across OSes and changes recording ownership and allocation size. Linux/Deck and exact-head build/game evidence are missing.
Switches: HOS_NVN_REC_OLD=1 restores portions of the previous path. It leaves the larger prefix and intrusive bookkeeping in use.
Reviewed:
df5d7aedagainst0de25c3e, with exact Graphify maps and source verification. Read copy allocation, submit accounting, control marks, retirement, and tests. Nothing run.Fixed in this review: Nothing.
Checked and fine: The retirement pool has a completion gate. Its input must cover every submit that still owns the copy.
Blocking
host/nvn_native.c:1753–1756,1618: the sliced sweep takes the submitted-plus-held watermark before acquiring the copy lock. While it waits, a producer can publish/submit a handle at N+1 and overwrite its control mark. The sweep sees the overwritten mark but retires it at N; the pool can reuse that copy while replay still needs it. Tie the retire ticket to an ordering point at actual retirement that covers preceding submits and held work. Add a deterministic paused-sweep, later-submit and overwritten-mark test.host/nvn_native.c:1365–1372,1483,1792: the copy prefix grows from 24 to 48 bytes even with the rollback enabled. A 65,504-byte recording crosses the 64 KiB pool class. Label the rollback as partial and compare exact main/off/on before making this the default, including allocation/RAM, contention and frame tails.Testing: Rebase, build the exact candidate and replace the historical mixed comparison with matched Windows and Linux evidence. The head's description and Guide screenshots checks are red, and source checks were skipped.
Overlaps and merge order: Coordinate first with #1780, which also changes EndRecording ownership. Choose one handoff and rebase the remaining independent changes. Reconcile submit accounting with #1811 before testing them together.
@ -1349,0 +1362,4 @@exclusively (~98 recordings a frame in the Market, 4 cores, 18 Nov). A command buffer's segment keeps room for it at itsstart (REC_HEAD), so that the segment itself can become the copy (end_copy). */#define COPY_MAGIC 0x59504f43444d43ull /* "CMDCOPY" */typedef struct copy_prefix {The larger prefix remains in the old-switch arm and can cross a pool class. Compare against exact main as well as both candidate settings.
@ -1675,0 +1750,4 @@size_t swept = 0;for (unsigned s = 0; s < SWEEP_SLICE; s += SWEEP_BATCH) {const unsigned count = SWEEP_SLICE - s < SWEEP_BATCH ? SWEEP_SLICE - s : SWEEP_BATCH;const uint64_t after = rx_nvn_pushed() + held_count();The watermark predates copy_lock acquisition and can miss a later submit of a now-retired handle. Tie retirement to a watermark covering actual retired handles and held submits.
df5d7aed1eb8d2204b4a@Beanstalk
@HumanBean the PR got a new push (
b8d2204) after your comment. Review it and approve again.perf(host): NVN command buffers' segments and recordings' copies made cheaper (HOS_NVN_REC_OLD=1 as before)to perf(host): NVN recordings' copies: the sweep's ticket read at retirement, segment size hints, frees outside the copy lock (HOS_NVN_REC_OLD=1 as before)Thanks, both points were right. New head:
2763ba17(66fb7c1cis the fix and2763ba17makes the test build with gcc).1. The sweep's ticket was read too early. This was real, and main has it too. main read
rx_nvn_pushed() + held_count()under the lock but before the slice read its marks, so the window was smaller but there. Inhost/nvn_native.cthe ticket is nowretire_ticket(), read where the copy is actually retired:sweep_bucketsreads every mark of its batch first, unlinks the copies whose marks were overwritten, and only then reads the ticket and lets them go.release_copiesreads the ticket inside its hold ofcopy_lock, after the AddControlMemory call that retires the copies.held_flushcounts its submits up in pushed before it clears the held count, so a flush between the two reads counts those submits once or twice, never zero times. The other order (and C's unspecified order of the olda() + b()) could miss them.Both ways (
HOS_NVN_REC_OLDunset and=1) get the fix.Test:
tests/test_nvn_recording_copies.py,tickets(). The stubsrx_nvn_pushedandheld_counthave a pause point, which makes two cases deterministic:In both cases the copy must still be on the dead list, with its bytes counted, when the replay is at N. It must be freed once the replay has passed its submit.
Before and after:
nvn_native.c, (a) fails both ways: the copy is freed with the replay at N. (I ran the new fixture with only the take_due/node helpers adapted to that head's names.)ticket-early(b8d2204b's expression before the marks) by (a), andpushed-firstby (b).2. The prefix grew from 24 to 48 bytes even with the rollback. Fixed at the root:
copy_prefixis main's 24 bytes again, and the bucket nodes are main'srec_node, both ways. So a copy's block is byte for byte main's, andHOS_NVN_REC_OLD=1is no longer partial.Test:
sizes()records a 65,504-byte recording and requires a 65,528-byte copy in the 64 KiB class, both ways. It fails onb8d2204bboth ways; theprefix-48broken variant is caught.#1780. I read its diff at
620f3067. Itsrec_begin/rec_endhand a half-full segment over as the handle, which is the same mechanism and the same rule as this PR'send_copy. Only one of them can be on. #1780's should win:HOS_NVN_REC_HANDOFF);a433db32);So
end_copyand theREC_HEADroom are gone from this PR. What is left is independent of it:copy_lockis let go;Either PR can merge first. The only overlap left is text next to BeginRecording, EndRecording and
flatten, with no shared semantics: #1780'srec_endwrites main's 24-byte prefix, and that is the prefix here again.#1811. This PR no longer changes what is submitted or when. It only moves the ticket read later, so it is reconciled with #1811 by construction. They have not been tested together.
Does it help? No. Measured at exact heads: base main
1780fbe0(this branch's base) and the PR at2763ba17, with on and off (HOS_NVN_REC_OLD=1) from the same build. Setup:kokiri-start, 160 s, headless at 1920x1080,HOS_FPS_MAX=1000.1780fbe0Every difference is inside the spread of the same arm's two runs, frame tails included. No
not a live recording, no FATAL and no frame of 50 ms or more.The game threads' CPU over the last 2 minutes (
HOS_THREAD_TIMES=5, threads over 5 %) is the same within about 2 % in every arm, so I can't show less contention either. The copy lock's wait was not measured directly.What this PR can claim now is the ticket fix, a race main has too. Otherwise it is bookkeeping with no measured effect. If you'd rather not carry that, I can split the ticket fix into its own small PR on main and close this one.
Checks at
2763ba17:test_nvn_recording_copies(both ways, 7 broken variants caught),test_hos_ab(including the flips run, which now also runssizesandtickets),test_nvn_host_tables,test_recording_log_handoffandtest_switch_tablepass.nvn_native.ccompiles syntax-only with the builder's flags (0 errors).run_all --changed 1780fbe0passed 30, failed 10. The 10 are the same 10 that fail on main there.test_nvn_recording_copiesandtest_hos_ab, skipped by run_all there for want of clang, pass when run with--cc gcc; they needed two fixture fixes for gcc, which are in2763ba17.@Beanstalk steamdeck: Castle Town's market (child), standing for 3 minutes after a teleport from kokiri-start, with
HOS_AB=HOS_NVN_REC_OLD=1,3,60 RX_VK_FRAME_TRACE=1, then scripts/ab_summary.py. Expecting side 1 (the old bookkeeping) no faster than side 0, and nonot a live recordingin the log. Also linux, any GPU: the same, to check the changed host code runs there.(Earlier runs on #1846 and #1847 today ended in
errorbefore building: the farm's checkout of main failedgit rev-parse --verify HEAD^{commit}in packs.py's cmd_base, so nothing was measured there.)Beanstalk test farm · an earlier run: commit
2763ba1(the newer results are in a comment below)Commit
2763ba1· tier reviewed · approved by GummiNaomi · dashboardWhat will be tested · AI plan (
gpt-6.1-terra)On Steam Deck and on a Linux PC, the plan loads kokiri-start, teleports to the child Market, and stands for three minutes on both main and the PR. It enables the requested in-game bookkeeping alternation and compares frame times, memory, and a Market screenshot; crashes, a “not a live recording” log message, or the specified regressions fail. The farm cannot enable the requested frame trace or run ab_summary.py, so this plan cannot judge whether side 1 is faster than side 0. Each target takes about 16 minutes of game runs, excluding builds.
kokiri-start(switchesHOS_AB=HOS_NVN_REC_OLD=1,3,60), then teleport to Market, take the picture market to compare with main, measure stand for 3 min (after 0 s warm-up); 1 run on each side.Started at 13:13 UTC.
Estimate: done in about 9 h 15 min, from the queue and the testers online now; some jobs can't start yet.
unknown, unknownMeasurements · linux
Verdict: running on linux (1/1 report).
Hardware: 1: linux+other
unknown, unknownAMD Ryzen 7 5700X3D 8-Core Processor, 128 GB RAMScene: child-market, from the repository's save kokiri-start (HOS_AB=HOS_NVN_REC_OLD=1,3,60); each cell's settings and switches as the table names them
Method: the Beanstalk test farm: main and this PR built and run on the same PC in one session, game starts alternating (ABBA); child-market: 1 game start a side, stand 180 s after 0 s warm-up; each number the median of the runs, then of 1 report
How to reproduce: the farm's plan above (
@Beanstalkon this PR runs it again)Nothing was measured: no metric came back.
Flags and settings
None: the plan varied no setting, switch or build option.
Markdown for the PR description
2763ba174b451591028dBeanstalk test farm
Commit
4515910· approved by nobody yet · dashboardA member of
BigBeanscan review this commit and comment@Beanstalkto test it.Closing: no change at 4 or 16 cores against main, in frame rate, frame tails or memory. The one part with value on its own is the fix for the sweep's ticket read (the ticket read at retirement, not at the sweep); I can send that as a small PR of its own if you want it.
Pull request closed