WIP: perf(nvn): AddControlMemory's release of old recordings waits for the next EndRecording's lock (HOS_NVN_COPY_LOCK_ONCE=1 to enable) #2025
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!2025
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/nvn-copy-lock-once"
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
The game adds control memory to a command buffer before every recording (
nvnCommandBufferAddControlMemory, as manycalls a frame as
EndRecording), and AddControlMemory released the copies of the recordings made in that memory in anexclusive
copy_locksection of its own: two exclusive sections a recording, which every recording thread and everyCallCommands miss waits for.
With this, AddControlMemory keeps the range in the command buffer's host table with the number of the last recording
made by then (
rec_born; each copy carries its recording's number), and the command buffer's next EndRecordingreleases it in the
copy_locksection it takes anyway (release_range, the dead copies freed first asrelease_copiesdoes). Only the recordings made up to that number go, so what another command buffer records in thatmemory meanwhile stays, as with the release made at the call. A range still waiting when memory is added again, or when
the command buffer is finalized or initialized again (
cmd_release), is released then. Old recordings live a littlelonger than before, never shorter, and their free still waits for the replay to pass the submits made before their
release. The result expected: one exclusive
copy_locksection a recording instead of two, when the switch is on.With the review (BeanBot 64275): EndRecording reads the deferred release's ticket under the lock, the held count before
the pushed one, as
release_copiesdoes since #2022's fix, andrec_bornis a plain increment by the lock holderstored atomically (no locked read-modify-write a recording).
The switch:
HOS_NVN_COPY_LOCK_ONCE(new, an attempt, bool, its row ingpu/vk/switches.def), off by default: ithas not run in the game, and it changes when every recording thread takes
copy_lock, so it ships off until a runmeasures it (the branch shipped it on). Read once, at the first AddControlMemory, and published atomically, so threads
that come to it first together keep one answer (
=1or any value but0,off,false,noor empty is on).Unset or off, AddControlMemory releases the copies at the call in a
copy_locksection of its own: main's releaseschedule, on top of #2022's flat buckets, not main's code; the comparison the switch gives is the deferred release
against the immediate one within this head.
Depends on #2022 (recorded copies in flat per-bucket arrays): this extends its bucket entries and walk. The branch is
on main @
fd7dc40dwith #2022's three commits (ae69cdbb,9f83a9a2,db31aa81) under its own; they show in this diff until#2022 merges, and this is rebased then.
From d1e33769 on
exp/x86-asm-sync. There the same switch also gave the dead copies back to the pool after the lock waslet go; that half is not here. Prior work: #1812 (GummiNaomi) freed the dead copies and bucket nodes outside
copy_lockand ran the sweep in slices outside EndRecording's hold; its author closed it unmerged, having measured nochange at 4 or 16 cores. This one does not move frees out of the lock: it folds AddControlMemory's own section into the
next EndRecording's.
Read first:
release_range,copy_lock_once,AddControlMemory,EndRecordingandcmd_releaseinhost/nvn_native.c, thentests/test_nvn_copy_lock_once.py.Testing
Tested: Windows 11, i7-9700, RTX 4060, the builder's llvm-mingw clang 23; head
762cac5aon #2022's headdb31aa81(on mainfd7dc40d), with the Vulkan headers (RX_CPM). Build jobsfb-2022-2,fb-2025-3andfb-2025-4of this PC's job queue, each check in a hidden console:tests/test_nvn_copy_lock_once.pyat the test commitac1f4ad9fails as expected (withHOS_NVN_COPY_LOCK_ONCE=1, a copy went back to the pool with 32 submits replayed when 34 were pushed or held as it was let go) and passes at the head, also with no--ccand noCC;tests/run_all.py --strict --exactwithtests/test_nvn_copy_lock_once.py,tests/test_nvn_host_tables.py,tests/test_nvn_rec_release.py,tests/test_recording_log_handoff.pyandtests/test_switch_table.py: 5 passed;tests/test_nvn_host_tables.pywith noCCpasses;host/nvn_native.ccompiles (-fsyntax-only) with the game build's own command.tests/run_all.py --strict --changed db31aa81(the checks for what this changes over #2022): 38 passed, 10 failed, 3 skipped, none for want of the Vulkan headers; the 9 other thantests/test_nvn_copy_lock_once.py(whose failure there was a fixture fixed since, passing at the head above) fail on mainfd7dc40dtoo, run from a main worktree:tests/test_crash_dialog.py,tests/test_dev_ab.py,tests/test_dev_packs.py,tests/test_frame_overlap.py,tests/test_host_guest_writes.py,tests/test_linux_pressure.py,tests/test_present_queue_default.py,tests/test_target_reuse_memory.py,tests/test_vk_occlusion_queries.py. The full suite ran on #2022's head (its description).It has NOT run in the game on this PC yet.
Still to test:
the Market (a heavy scene), main @
fd7dc40dwith #2022 merged or #2022's headdb31aa81, this PR unset (off) and thisPR with
HOS_NVN_COPY_LOCK_ONCE=1, each warmed a minute, then at least three interleaved three-minute runs of each(ABC ABC ABC), expecting no
not a live recordingline, no FATAL, the same picture, the log's live copies and thebuckets' room about the same, and average fps, 1% lows, median and p99 frame times of
=1at least those of theother two; a difference beyond the runs' spread is a gain or a loss, one within it is no difference measured; with a
Tracy build, the
copy_lockwaits (the "lock wait" zones) in EndRecording and outside it (by hand)and on this PR with
HOS_NVN_COPY_LOCK_ONCE=1, warmed and interleaved, expecting nonot a live recordingline, noFATAL and the same picture (@Beanstalk linux+nvidia, linux+amd, windows+amd, windows+intel, steamdeck: Kokiri
Forest three minutes, main, HOS_NVN_COPY_LOCK_ONCE unset and =1, warmed and interleaved, the log, frame times and a
screenshot)
python3 -I -B tests/test_nvn_copy_lock_once.py --cc gcc, expecting PASS (by hand)Measurements
Verdict: not measured: no micro-benchmark of this port and no run in the game on this PC yet; the in-game A/B is
in Still to test. The branch's reason was its CPU profile (
docs/cpu-profile.mdonexp/x86-asm-sync, taken of thebranch's code with the frees still inside the lock, not of this port), where
n_nvnCommandBufferAddControlMemoryshowedin every scene; no number of it is quoted here.
Risks
What could break: a copy released too early would show as a wrong or missing draw or a
not a live recordingstop inany scene; released too late, as memory held a little longer. The test runs 12,000 adds, recordings and finalizes over
six command buffers whose control memory overlaps, with the switch on and off: every copy released with it off is
released with it on no earlier, and at the latest at that command buffer's next EndRecording, AddControlMemory or
Finalize, and none other, and with held submits and flushes between a ticket's reads, a copy goes back to the pool
only after the replay passed every submit pushed or held when it was let go. With the switch on, old recordings live
until the command buffer's next EndRecording, AddControlMemory, Finalize or Initialize: an idle command buffer keeps
them until then, and the blocks freed in EndRecording's section go back to the pool after that recording's copy was
taken. Off unless
HOS_NVN_COPY_LOCK_ONCEis set; unset or=0is the release at the call. Host code: the runtime is rebuilt; nothingchanges for players' saves, options or 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.third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def.tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.The recordings' copies are kept by the 64 KiB block of their control memory in 65,536 buckets. Each bucket was a linked list of nodes malloc'd one a recording (EndRecording) and freed one a release, scattered through the heap: a release or a sweep walks its buckets whole, the copies that stay included, so a walk of k copies was k dependent cache misses, all of it with copy_lock held exclusively, which every recording thread waits for. A bucket is now a flat array of {ctl, copy, marked} entries, oldest first, walked newest first as the head-inserted lists were; the copies kept move back to the array's start in their order (rec_keep). So copies are released, wait dead and are freed in exactly the order they were; EndRecording mallocs nothing (copy_track). An array grows by doubling under copy_lock, as the dead list already does, and never shrinks. The three copies of "a copy is no handle any more" (release_copies, the sweep) are one function, copy_retire. No switch: the same releases and frees in the same order. Micro-benchmark on the branch (d4eb931f, i7-9700, with 32-byte entries that also held a recording number for HOS_NVN_COPY_LOCK_ONCE): one EndRecording copy_lock section with ~50,000 live copies (the game's logs: 46,000-51,000), k copies a bucket: k=1 444 -> 349 ns, k=4 708 -> 385, k=16 1379 -> 410, k=64 2423 -> 572, k=256 6073 -> 1230; with a submit's 155 dead copies waiting, k=1 650 -> 373 ns. Not measured in play. Test: tests/test_nvn_rec_release.py runs the flat code cut from the source and the linked lists as they were through the same 60,000 random operations (tracks, releases of chunks, blocks, unaligned and over-4-GiB ranges, sweeps with marks kept, overwritten and outside the window, dead copies freed as the replay moves), in three layouts (a few busy blocks, many, none); the handles leave the set and the blocks go back to the pool in the same order. tests/test_recording_log_handoff.py's stand-ins follow EndRecording's publication (copy_track instead of the node). #1812 (GummiNaomi) changed the same functions (release_copies, the sweep, EndRecording: frees outside the lock, the ticket read at retirement) but kept the list nodes; its author closed it unmerged (no change at 4 or 16 cores). Assisted-by: Claude CodeRecommendation: Request changes. The deferred release can return a recording to the pool while a held submit still needs it. It also leaves retired control-memory addresses visible to the sweep, and the player-facing performance gain is unmeasured.
The patch folds the usual AddControlMemory release into EndRecording’s existing lock section. It saves one lock acquisition in the common sequence, while adding per-recording bookkeeping, extending copy lifetimes, and moving pool reclamation after the next recording’s allocation.
@fixwill do (18)host/nvn_native.c:1814: The new retirement path can undercount held submissions. (more on the line)host/nvn_native.c:1686: Deferral leaves descriptor addresses in the sweep after AddControlMemory has returned. (more on the line)host/nvn_native.c:1650: release_copies reads rec_buckets without synchronization while the first EndRecording writes it under copy_lock. (more on the line)host/nvn_native.c:1609: The retirement helper carries forward the plain magic write that races with check_handle’s plain read. (more on the line)host/nvn_native.c:1819: The new 1 MiB bucket directory uses unchecked calloc. (more on the line)host/nvn_native.c:1618: Bucket capacity doubles in uint32_t. (more on the line)host/nvn_native.c:1729: Both paths form the range end from an unchecked guest address and length. (more on the line)host/nvn_native.c:1623: Every recording performs an atomic read-modify-write on rec_born while holding the sole writer lock, including with the switch off. (more on the line)tests/test_nvn_copy_lock_once.py:121: The fixture discards recording pool returns and hardcodes held_count to zero. (more on the line)tests/test_nvn_copy_lock_once.py:230: The test clears the new switch but inherits HOS_NVN_REC_SHIFT, which the extracted EndRecording reads. (more on the line)tests/test_nvn_copy_lock_once.py:123: The fixture uses ordered pointer comparisons between a separately allocated segment and the copy arena, and forms the end with arena[NCOPY][0]. (more on the line)tests/test_nvn_copy_lock_once.py:252: Both new compiler selectors pass CC as one executable name, so a setting such as CC='ccache gcc' fails before compilation. (more on the line)tests/test_nvn_host_tables.py:28: The changed host-table and log-handoff checks still force --cc gcc through run_all. On a Windows checkout with only the bundled llvm-mingw clang, strict mode reports a missing compiler before either changed fixture runs. Let both use CC and the configured llvm-mingw fallback, like the new fixtures.tests/test_nvn_rec_release.py:187: The stated outside-window sweep case is never marked. (more on the line)host/nvn_native.c:1800: Provide an eligible completed block before requesting a fresh allocation, or preserve the immediate-release path where pool reuse would otherwise be lost.host/nvn_native.c:1493: Report total bucket capacity and pending copy bytes separately from the live-copy count. Bound the lifetime of pending ranges and reclaim empty oversized buckets if the measurements show a cost.For the author (3) (a run, a measurement or a decision)
host/nvn_native.c:1800: The new sequence flattens the recording before reclaiming eligible dead copies. (more on the line)host/nvn_native.c:1493: Recordings kept in the buckets no longer tell you how much memory the table retains. (more on the line)Checks: Still running:
CI / Conventional Commit title (pull_request),CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request),CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request),CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request),CI / Workflow lint (actionlint) (pull_request),CI / Website build and checks (pull_request),Guide screenshots / Guide images and local UI source review (pull_request),Third-party parts / As their notes say (pull_request),Third-party parts / The same as upstream (pull_request).More: does it help, other systems, switches, what was read
Does it help: Unproven. The Windows fixture reportedly passed and checks two exclusive sections versus one in the common sequence. No game run or timing compares this head with its base. The commit’s estimate of 1.5% of one i7-9700 core comes from a different branch profile, and the flat-array microbenchmark comes from #2022’s prior work.
Other systems: Both submitting-thread and threaded Vulkan replay use these recording copies on Windows and Linux, across GPU vendors and Steam Deck. CallCommands, CopyCommands, held submits, and periodic sweeping share the lifetime changes. RADV batching can also make the added or shifted replay polls take the renderer’s present mutex under copy_lock. The diff changes no save, options, audio, input, shader, or renderer ABI code.
Switches: HOS_NVN_COPY_LOCK_ONCE is a new developer kill switch, on when unset. The OFF spellings use the shared boolean parser. Its sorted table row is present. Off restores immediate release scheduling on top of the flat buckets. The default has no hardware measurements behind it yet. No other switch changed.
Reviewed:
3b4edada27against75317e1f31: Read all four changed Python files, both commits, and the full diff for all six files. Read AGENTS.md, CONTRIBUTING.md, docs/dev/standards.md, and the Forgejo template in full. Read the changed host routines and their slot, pool, handle, submit, sweep, replay, and guest-memory dependencies. Skimmed unrelated host code and switch rows, the linked developer references, and other source-reading checks. The named run logs and branch profile were unavailable in the checkout. No local review-checklist file or sibling Tooling checkout was present.; nothing run (a code review).Checked and fine:
Testing: Read-only review. The author reports 39 of 48 selected Windows checks passed, including the new recording checks, with nine failures also present on the named baselines. CI was still running in the supplied snapshot. The PR opened without the in-game run required by repository rules. The full suite, Linux GCC and host build, in-game correctness, and Tier 1 performance remain unverified.
Overlaps and merge order: #2022 is the flat-array prerequisite. Merge it first, rebase #2025, and retest and measure against the new main. #1812 is closed and unmerged.
Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.
@ -1481,0 +1490,4 @@the same releases and frees, in the same order (tests/test_nvn_rec_release.py) *//* (born: the recording's number, rec_born as it was made: what a release put off to the next EndRecording goes by,HOS_NVN_COPY_LOCK_ONCE) */typedef struct { uint64_t ctl; uint8_t *copy; uint64_t born; int marked; } rec_ent;L16 (minor): Recordings kept in the buckets no longer tell you how much memory the table retains. The directory grows by 512 KiB on 64-bit hosts, bucket arrays keep their peak capacity, and this patch adds 8 bytes per capacity slot over #2022 plus 24 KiB per allocated command-buffer chunk. An idle command buffer with a pending range can also keep old copies live indefinitely. Measure peak and settled RAM after a scene transition and a long session.
@ -1739,3 +1798,3 @@o->recording = 0;const cmd_rec *r = cmd_host(o);cmd_rec *r = cmd_host(o);uint8_t *copy = flatten(r->seg, (size_t)(r->cur - r->seg));L15: The new sequence flattens the recording before reclaiming eligible dead copies. If the pool has no spare block in that size class but a dead block is ready, main returns the block before flatten and can reuse it. The new path can allocate a fresh block or enter the allocation retry while the reusable block is still held. Measure pool misses, peak commit and tail frame times through loads and steady play.
@ -1594,0 +1606,4 @@until the replay passed `after` */static void copy_retire(uint8_t *copy, uint64_t after) {copy_prefix *p = (copy_prefix *)copy - 1;p->magic = 0; /* (no submit names it any more) */L4: The retirement helper carries forward the plain magic write that races with check_handle’s plain read. A handle-set hit also provides no lifetime protection during inline_recording’s later memcpy or QueueSubmitCommands’ handoff. A reader that passes the check just before retirement can still use the copy after another cleanup returns it to the pool. Make validation and use share a lifetime guarantee, such as a pin spanning the copy or submit handoff. An atomic magic field alone leaves the lifetime race.
@ -1594,0 +1615,4 @@static void copy_track(uint64_t ctl, uint8_t *copy, int marked) {rec_list *l = &rec_buckets[rec_bucket(ctl)];if (l->n == l->cap) {l->cap = l->cap ? l->cap * 2 : 4;L6 (minor): Bucket capacity doubles in uint32_t. If a bucket reaches 2^31 entries, the next growth passes zero bytes to realloc and the subsequent append writes outside a valid allocation. This requires an unusually large process, but entries without control memory all use the same bucket. Check growth before doubling and check the byte count before reallocating.
@ -1594,0 +1620,4 @@}rec_ent *e = &l->e[l->n++];e->ctl = ctl, e->copy = copy, e->marked = marked;e->born = __atomic_add_fetch(&rec_born, 1, __ATOMIC_RELAXED);L8 (minor): Every recording performs an atomic read-modify-write on rec_born while holding the sole writer lock, including with the switch off. Reuse the recording serial already incremented under copy_lock and publish it with an atomic store. That avoids an extra locked instruction on the hot path and gives the off comparison less bookkeeping.
@ -1613,1 +1647,4 @@}}static void release_copies(uint64_t lo, uint64_t hi, uint64_t born) {if (hi <= lo || !rec_buckets) return;L3: release_copies reads rec_buckets without synchronization while the first EndRecording writes it under copy_lock. The new pending-release path can hit this when a command buffer is finalized as another thread publishes the first bucket table. The off path retains the same race from main. Put the check under copy_lock or publish a readiness flag atomically and recheck under the lock.
@ -1642,3 +1685,2 @@uint64_t mark[2];if (!n->marked) {pn = &n->next;void *at = e->marked ? hos_guest_range(e->ctl, sizeof mark) : NULL;L2: Deferral leaves descriptor addresses in the sweep after AddControlMemory has returned. If an added range is abandoned and its pages are returned before the next EndRecording, another recorder’s sweep can touch an old mark. On Windows the window check accepts a decommitted page, and the fault handler can recommit 64 KiB under copy_lock or fail under commit pressure. Immediate release had already removed that entry. Prevent the sweep from probing pending retirements, and use a guest-memory read that is safe against decommit for the marks it still probes.
@ -1675,0 +1726,4 @@released then. The old recordings live a little longer than before, never shorter */cmd_rec *r = cmd_host(o);if (r->rel_hi > r->rel_lo) release_copies(r->rel_lo, r->rel_hi, r->rel_born);r->rel_lo = U64(1), r->rel_hi = U64(1) + U64(2);L7 (minor): Both paths form the range end from an unchecked guest address and length. An overflowing length makes hi fall below lo, silently skipping retirement while still installing the new control-memory arguments. Validate the span once before changing the object and use the checked endpoints in both modes.
@ -1756,3 +1817,3 @@const char *e = getenv("HOS_NVN_REC_SHIFT");if (e && atoi(e) >= 12 && atoi(e) <= 24) rec_shift = (unsigned)atoi(e);rec_buckets = (rec_node **)calloc(REC_BUCKETS, sizeof *rec_buckets);rec_buckets = (rec_list *)calloc(REC_BUCKETS, sizeof *rec_buckets);L5: The new 1 MiB bucket directory uses unchecked calloc. If it fails on first recording, release_range or copy_track accesses a null table. Use hos_xcalloc so the allocation gets the host’s retry and out-of-memory handling.
@ -1754,0 +1811,4 @@/* (longshot, HOS_NVN_COPY_LOCK_ONCE) the control memory added before this recording lets its old recordings go inthis section (nvnCommandBufferAddControlMemory), before the new one goes in */const int waiting = r->rel_hi > r->rel_lo;const uint64_t after = waiting ? rx_nvn_pushed() + held_count() : 0; /* (held submits name them too) */L1: The new retirement path can undercount held submissions. With HOS_NVN_HOLD=1, a submit can already be held when AddControlMemory returns. If a flush transfers it to Vulkan between the pushed-count read and the held-count read at EndRecording, the sum can miss it and the next cleanup can recycle its copy before replay. The volatile nheld read also races with the flush. Take a consistent snapshot at retirement, using the held queue’s synchronization or a submission sequence that covers both states. Moving the two reads under copy_lock alone cannot synchronize the flush.
@ -0,0 +118,4 @@if (!p) die(what);return p;}static void pool_put(void *b, unsigned cls) {L9: The fixture discards recording pool returns and hardcodes held_count to zero. Changing EndRecording to retire every deferred copy with ticket zero can still pass because the assertions only observe handle removal and lock counts. Add a deterministic held-to-pushed handoff and record pool returns. Assert the copy stays out of the pool until replay reaches the transferred submit.
@ -0,0 +120,4 @@}static void pool_put(void *b, unsigned cls) {(void)cls;if ((uint8_t *)b >= &arena[0][0] && (uint8_t *)b < &arena[NCOPY][0]) return;L11 (minor): The fixture uses ordered pointer comparisons between a separately allocated segment and the copy arena, and forms the end with arena[NCOPY][0]. Both new fixtures also use declared byte arrays as copy_prefix objects without guaranteeing alignment or compatible storage. Use typed or suitably aligned allocated storage, valid one-past bounds, and integer addresses or explicit allocation tags to distinguish segments from copies.
@ -0,0 +227,4 @@def run(exe, value, once):env = {k: v for k, v in os.environ.items() if k != SWITCH}L10 (minor): The test clears the new switch but inherits HOS_NVN_REC_SHIFT, which the extracted EndRecording reads. A developer with shift 24 exercises a different bucket layout from CI. Clear it for the default case and explicitly set it in any alternate-layout cases.
@ -0,0 +249,4 @@def compiler(given):"""--cc, else $CC, else the configured llvm-mingw's clang (LONGSHOT_MINGW, Windows: run_all.py sets it to theclone's folder), else clang, gcc or cc on PATH"""for c in (given, os.environ.get("CC")):L12 (minor): Both new compiler selectors pass CC as one executable name, so a setting such as CC='ccache gcc' fails before compilation. Accept a compiler command as an argument vector while preserving explicit executable paths that contain spaces.
@ -0,0 +184,4 @@const uint64_t ctl = pick_ctl(shape);uint8_t *copy = new_copy();int marked = 0;if (ctl < WINDOW - 16 && rnd() % 8) {L14 (minor): The stated outside-window sweep case is never marked. Every address outside WINDOW enters the sweep with marked=0, so removing the range guard from marked entries would escape this case. Add a marked entry at or crossing the window boundary and assert the sweep handles it without reading the mark.
3b4edada27762cac5a63WIP: perf(nvn): AddControlMemory's release of old recordings waits for the next EndRecording's lock (HOS_NVN_COPY_LOCK_ONCE=0 to disable)to WIP: perf(nvn): AddControlMemory's release of old recordings waits for the next EndRecording's lock (HOS_NVN_COPY_LOCK_ONCE=1 to enable)Thanks for the review. The branch is rebased on #2022's new head
db31aa81(main @fd7dc40d; "depends on #2022" still holds), with a test commitac1f4ad9and a fix commit762cac5aon top; head762cac5a. The PR's own commitbdba502bchanged in the rebase only where #2022's fix forced it (git range-diff): EndRecording callsrec_buckets_makeinstead of making the table under the lock, AddControlMemory checks its range first,release_copieskeeps #2022's locked test and ticket, the walk keeps #2022's in-place keep, andtests/test_nvn_copy_lock_once.pycutsrec_buckets_make, has ahos_guest_rangestand-in and counts the first recording's twocopy_locksections (the table's publication).HOS_NVN_COPY_LOCK_ONCEis now off by default (762cac5a): the deferred release has not run in the game, so it ships off until a run measures it;=1turns it on. The checks below ran in build jobfb-2022-2andfb-2025-3of this PC's job queue (Windows 11, llvm-mingw clang 23):tests/test_nvn_copy_lock_once.pyfails at the test commitac1f4ad9(with=1, a copy back in the pool with 32 submits replayed when 34 were pushed or held as it was let go) and passes at the head; the five checks it touches or inherits pass undertests/run_all.py --strict --exact;nvn_native.ccompiles with the game build's command; of the checks--changed db31aa81picks, the 9 failures other than this fixture's fail on mainfd7dc40dtoo (Tested lists them). Nothing has run in the game yet.762cac5a, on #2022'sdb31aa81. EndRecording reads it inside itscopy_locksection, the held count first, then the pushed one, and the flushes empty the held list with a release store only after pushing it (#2022), so a flush between the reads is counted twice, never missed. The fixture now holds and pushes submits, flushes the held ones right after a read of the pushed count, moves the replay one submit at a time and logs each copy's return to the pool; a copy may go back only once the replay passed every submit pushed or held when it was let go. At the test commit it failed so with the switch on (32 submits replayed, 34 pushed or held); it passes at the head.rec_bucketswithout synchronization: fixed in #2022'sdb31aa81(tested in9f83a9a2):release_copies, which Finalize's pending release calls too, tests the pointer undercopy_lock.calloc: fixed in #2022'sdb31aa81:rec_buckets_makeuseshos_xcallocbefore the lock and publishes under it.db31aa81: stops past 2^31 entries, bytes insize_t,capset after the allocation.db31aa81: AddControlMemory checks the range is guest memory before either mode, so both take endpoints that do not wrap.rec_born: fixed in762cac5a.copy_track, its only writer, undercopy_lock, storesrec_born + 1with a relaxed atomic store for AddControlMemory's lockless read: no locked instruction a recording. It is a counter of its own beside EndRecording'smade(one each a recording), which stays a function-local count for the log line.ac1f4ad9, as in L1; at the end, with everything replayed, every copy let go must be back in the pool, once.HOS_NVN_REC_SHIFT: fixed inac1f4ad9: removed from every run's environment.ac1f4ad9(the copies' arena is allocated storage, aligned for any object, a row a copy; a copy is told from a segment by its address as an integer against the arena's range, and its number comes from the same) and in #2022's9f83a9a2fortest_nvn_rec_release.py.CCas one name: fixed: both fixtures take their compiler fromtests/cccmd.py(#2022's9f83a9a2):--ccorCCnaming an existing file is one program (spaces kept), any other value is split as a shell would (CC='ccache gcc').--cc gccthrough run_all: fixed:test_nvn_host_tables.pyinac1f4ad9andtest_recording_log_handoff.pyin #2022's9f83a9a2no longer declareRUN_ALL_ARGSand usetests/cccmd.py. Run with noCC, both pass at the head.9f83a9a2(copies marked past the window and across its end, in the run and in a directed sweep).db31aa81); copies waiting in a pending range are counted live until released. A pending range lasts until the command buffer's next EndRecording, AddControlMemory, Finalize or Initialize; Risks says an idle command buffer keeps its old copies until then. No reclaiming is added without a measurement; the RAM measurement after a scene change and a long session is pending.copy_lockwaits are Tracy "lock wait" zones (HT_WRAP_LOCKSinnvn_native.c), inside EndRecording's own zone when they are EndRecording's; Still to test asks for them with a Tracy build in Kokiri and the Market, with average FPS, 1% lows, median and p99 frame times, main against this PR unset and=1. Pending for the author.0(now the default) is the release at the call on top of #2022's flat buckets, a comparison within this head, not main's code; the description says the branch is on main @fd7dc40dwith #2022's commits in the diff until it merges.@Beanstalk This adds HOS_NVN_COPY_LOCK_ONCE, off by default: with HOS_NVN_COPY_LOCK_ONCE=1, AddControlMemory releases old recordings under the copy lock taken once (host/nvn_native.c); unset, they are released at the call as on main. It is on #2022, whose commit is in its diff. It has not run in the game on these PCs yet. Please play Hyrule Field for three minutes, the PR against main on the same PC, on the PR's side with HOS_NVN_COPY_LOCK_ONCE=1 and with it unset, with a picture compared with main's, on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture, no crash, no FATAL line and no "not a live recording" line; set, frame time p99 equal to unset or better; unset, as main's or better.
@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.