Guards: blocks made read-only again outside the guards' and journal's locks (VICTORY_GUARD_OUTSIDE, off; #1083) #1312
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1315"
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 fxceless/victory-fork. Originally posted by KainanP on 2026-11-26T13:55:52Z.
For #1083. MKmajster and CuteBooty found that spreading a submission's refreshes over more threads did not shorten the frame, because the threads queue on the guards' lock. This PR looks at what is done while that lock is held and moves the costly part out. It is off by default. Turn it on with
VICTORY_GUARD_OUTSIDE=1, or withaltto switch it on and off every 20 s; each switch is logged with the lock counters.What is serialized. For view memory,
Shadow::update→watch::writes→guard_atholds the journal's lock and then the guards' lock. While it holds them, it makes every block it found written read-only again: oneVirtualProtectper block, because the title's views are mapped block by block and a call cannot span two views.main(9a9091d) still does this under both locks. Three things wait for those locks:writing);VICTORY_WIDE_REFRESH), which line up at the journal's lock.Windows itself can run these protection changes in parallel. In
protect_scaling(below), 4,096 touched blocks take 48 ms on 1 thread, 21 ms on 4 and 15 ms on 8. So the serialization comes from our locks, not from the kernel.The change. With the switch,
guard_atonly records the blocks to re-arm while it holds the lock. It adds them toprotectedandclean, plus a newarmingset.writes_asstamps the journal, releases both locks, and then makes the blocks read-only (Arming::finish) before it returns, so before the caller copies them. The answers stay the same:writing,title_memory_written,views_mapped,title_memory_remapped) also opens arming blocks and records them asdisturbed.finishthen makes them writable again. A fault on such a block beforefinishis let through like any other opening.publishskips arming blocks.finishsets their bits afterwards, taking the journal's lock first and then the guards' lock, after releasing its own hold on the guards' lock.protectwould change only its own blocks: each page is shown at one view, or at more thanMANY_VIEWS(inplace::shown_alone, the same test asprotect's own loop). A run with a page shown at 2 toMANY_VIEWSviews is still guarded together with those views, under the lock, as before. A view mapped after that answer opens the blocks (views_mapped→open_here), so they end writable.protect_here.Armingis dropped without being finished, for example because of a panic, it is finished on drop, so no later asking can wait forever.The first version deferred a run only when
views_betweenfound no view at all. CuteBooty pointed out that this also finds the run's own view, and the runtime registers every view it maps. So in the game no run took the new path. The benchmark did not show it, because its views were never registered. Fixed in the second commit; the numbers below are with views registered as the runtime does it.Counters (always on, cheap).
watch::WAITScounts lock waits; the clock is read only when a lock is found taken.watch::GUARD_WORKcounts how long the guards' lock is held and the protection changes.watch::GUARD_LATERcounts runs made read-only outside the lock, and runs kept under it for other views.The
altlog line prints all three, so a run shows what share took the new path.Benchmark (
shadow::guard_bench::guard_contention,#[ignore]):BENCH_VIEWS=1registers the views throughtitle_memory_view, as the runtime does (VICTORY_VIEW_GUARDon, the default).BENCH_GAP_US=150: one write every 150 µs each).VICTORY_COPY_PLAIN=1, onmain9a9091d, on the i7-14650HX of the RTX 4060 Laptop machine. Cells are repetitions.Views registered (the game's case), 3 repetitions:
VICTORY_WIDE_REFRESH-style, helper threadsVICTORY_WIDE_REFRESH-style, helper threadsWith the first commit alone, registered views made no difference: the helper-thread flush took 10.6 / 10.9 / 12.4 ms with the switch on, against 10.6 / 10.3 / 13.4 off.
Views not registered, 2 repetitions:
VICTORY_WIDE_REFRESH-style, helper threadsVICTORY_WIDE_REFRESH-style, helper threadsSo:
Correctness.
shadow::guard_bench::flushes_under_writes_lose_nothing(not ignored, about 1.1 s) runs 3 writer threads against 100 flushes. It covers one thread and the helper threads, the switch off and on, and copies that overlap each other's edges. It runs twice: over unregistered views, and over registered views where 16 blocks' pages are also shown at a second view. After each flush it checks every word:After the writers stop, one flush must make every copy byte-equal to title memory. With the switch on, it also asserts that runs went outside the lock. Over registered views, it asserts that runs with a second view stayed under it. With the first commit's condition, it fails with "Told { aliased: 16 } wide false: no run made read-only outside the lock".
On
9a9091dit passed 10 of 10 runs in release and 10 of 10 in debug. It also passed withVICTORY_LIGHT_REFRESH=1andalt, withVICTORY_ABSENT_ONCE=1 VICTORY_ABSENT_MS=told, withVICTORY_VIEW_KNOWN=1, and withVICTORY_VIEW_GUARD=0. (An earlier version of the test found a self-deadlock in my first version of the light-bits step, which is fixed here.)Suite (
9a9091d).cargo test -p victory-gpu --lib, switch unset and withVICTORY_GUARD_OUTSIDE=1: 243 passed on the RTX 4060 Laptop.inplace::tests::warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset, or the parallel run crashes. Unmodifiedmaindoes both on this machine too (3 failures and 1 crash in 8 runs).watch::testsshare title addresses when run in parallel, and fail now and then on unmodifiedmaintoo (7 of 40 runs ofwatch::alone; 2 of 40 with this PR).--features diagnostics --test-threads=1, release): 260 passed. The 3profile::testsfail, and they fail onmaintoo.The GPU does not matter for this path: the benchmark's copies are host memory, and its D3D12 buffer is never used, so the timings are CPU-only.
Lints.
clippy -D warningspasses with and without--all-features. rustfmt was applied only to the lines this PR changes; the newguard_bench.rsis formatted whole.Rebase onto
9a9091d.main's new one-block fast path inopen_hereis kept for a block that is not being armed. A range holding an arming block takes the general path, which notes the block disturbed.Measured in the game by CarverXI (RTX 5090, first commit only, on
ceb9e1f): about +9% fps withVICTORY_WIDE_REFRESH=1 VICTORY_GUARD_OUTSIDE=alt. With the first commit, only runs over memory with no registered view could take the new path, so that gain came from those runs alone. With the second commit, runs over registered views can take it too.@MKmajster, @CuteBooty, @CarverXI: could one of you run an
altA/B in your scene, withVICTORY_WIDE_REFRESH=1 VICTORY_GUARD_OUTSIDE=altand the rest of your usual switches? Please comparebatch_flush/ the "copies" counter and the frame median between the ON and off phases. The switch lines in the log now also give the runs made read-only outside the lock and the runs kept under it.AI use: Claude Code (Claude Opus 5.5) analysed the code, wrote the change, the benchmark and the tests, and ran them on the machine above.
🤖 Generated with Claude Code
Imported from fxceless/victory-fork. Originally posted by CuteBooty on 2026-11-26T16:04:28Z.
Read of
33c2b7f3: with view guarding on, runs over registered view memory seem never to take the outside pathThe follow-up I promised on #1083. This is from reading the code (two independent reads), not from an in-game run: my tree's
watch.rshas diverged too far for the PR to apply, so I cannot give you the A/B from the 3070. Please correct me if I misread.later && (!view_guard() || views_between(from * BLOCK, block * BLOCK).is_empty())(watch.rs:1494-1506).views_betweenalso returns the run's own view (inplace.rs:349-363), so one registered view over the run keeps it under the locks.xmem::mapcallstitle_memory_view(xmem.rs:812-845), andVICTORY_VIEW_GUARDdefaults on. So with the default, only runs over memory with no registered view at all can defer.guard_benchmaps Windows views and callstitle_memory_remapped, which removes registry entries (guard_bench.rs:115-130, 274;watch.rs:2192), so every run there qualifies. The 11 -> 5.5 ms and 61-66 -> 3 ms would then describe that case.GUARD_WORK[2]count (watch.rs:1565, 1858). A separate count for deferred runs would let any tester say what share took the new path.What neither read measured is how many runs in a real frame are over unregistered memory; the share may not be zero.
One possible small change: defer a run when every block passes the existing alias-skip test (
shown.len() < 2 || shown.len() > MANY_VIEWS,watch.rs:1784-1789), with a new alias mapped during the deferred protection invalidating the pending guard.A related number from my tree, for what it is worth: cutting the guards' lock hold of the submit worker from about 34 to about 14 ms a frame did not change the frame time on this machine, so here the lock is not what limits the frame.
Imported from fxceless/victory-fork. Originally posted by CarverXI on 2026-11-26T17:10:20Z.
Measured in the game on an RTX 5090 (i9-14900K, 128 GB, Windows 11 26300, driver 621.01), this PR merged onto
ceb9e1f, Series X profile, the opening cutscene from one save (2,000 to 5,000 draws a frame), 60 s to 220 s after the world starts loading. The set is the one in #1253 plusVICTORY_SETTLE_ROOM=1 VICTORY_PARALLEL_REFRESH=1, soVICTORY_WIDE_REFRESH=1is on: the helper-thread case of your benchmark.The switch itself,
VICTORY_GUARD_OUTSIDE=alt, frames over time per phase, the first 4 s after each change dropped:About +9 %. The picture is as before and each run logs the same known lines as without it.
cargo test -p victory-gpu --libpasses here with the switch unset and set (232, one profiler test skipped that fails onmainon this machine).What it unblocks. A profile of this scene before the PR (100 s, the cutscene only) had the helper threads waiting 46 s for the journal's lock inside
watch::nothing_notedagainst 8 s running it, and 15 s waiting inwatch::writesagainst 10 s running; the title's recording threads waited 12 s for the guards' lock. Two switches ofmainthat put work beside the submitting thread were nulls here before this PR and are not on top of it:alt, with this PR onVICTORY_SYNC_BESIDEVICTORY_REAPER(
VICTORY_SYNC_BESIDEbefore: 10.80 / 10.61 / 10.67 fps without, 10.26 / 10.92 / 10.93 with, three launches a side.)Together, plain launches, two a side, alternating: 10.90 / 11.90 fps without any of the three, 12.87 / 13.09 with
VICTORY_GUARD_OUTSIDE=1 VICTORY_SYNC_BESIDE=1 VICTORY_REAPER=1(+14 %).Two things to hold against these numbers:
SYNC_BESIDE,REAPERand "together" launches a local change of ours was also on: the askings that only read the journal (nothing_noted, the asked-ahead and known-by-bits answers ofwrites_as) hold it shared, through anRwLock. I have not yet measured what it adds on top of this PR, so part of those three rows may be its. The first table is this PR alone.altphases start with the process and this scene is scripted: "with" falls on the same shots every launch. Swapping the halves and taking the mean takes the scene out. I'll send that small switch separately.A second profile with the journal shared still had readers queueing behind the askings that write it, which is what this PR shortens: the two look complementary rather than the same fix twice.
Imported from fxceless/victory-fork. Originally posted by KainanP on 2026-11-27T11:33:53Z.
Rebased on
main9a9091d(nowa17cdf2), plus a second commit for CuteBooty's finding.Is it still needed? Yes.
mainstill makes the written blocks read-only oneVirtualProtectat a time, under both the journal's lock and the guards' lock. The only conflict wasmain's new one-block fast path inopen_here. It is kept for blocks that are not being armed; a range holding an arming block takes the general path, which notes the block disturbed.@CuteBooty, your read was right.
views_betweenalso returns the run's own view, and the runtime registers every view it maps. So withVICTORY_VIEW_GUARDon, no run over registered memory ever took the outside path. The benchmark missed this because its views were never registered.The second commit changes the test. A run now goes outside the lock when
protectwould change only its own blocks: every page shown at one view, or at more thanMANY_VIEWS. That is the same test asprotect's own loop, now done under one take of the views' lock (inplace::shown_alone). If a view is mapped meanwhile, the blocks are opened, and they end writable. The newGUARD_LATERcounter, which thealtline prints, counts the runs made read-only outside the lock and the runs kept under it, as you suggested.Benchmark with the views registered (helper-thread flush, median of 11, 3 repetitions):
On the submitting thread alone, the flush time is unchanged, but a writer's faulting write takes about 5 µs instead of about 20.
Tests.
flushes_under_writes_lose_nothingnow also runs over registered views, with 16 blocks shown at a second view. With the first commit's condition, it fails.=1) and on WARP matchesmain, including the known Intelinplaceflake, the parallelwatch::testsflakes, and the 3 WARPprofilefailures.-D warningsis clean with and without--all-features.The description has the new tables. @CarverXI, your +9% was measured with the first commit, so it came only from runs over memory with no registered view. A rerun with this head would show what the registered views add.
AI use: Claude Code (Claude Opus 5.5).
Imported from fxceless/victory-fork. Originally posted by KainanP on 2026-11-27T15:35:24Z.
Closing this.
main5dbf5ce(the VIctory-NVIDIA tree) has a differentwatch.rs, and it already takes apart the lock chain this PR shortened:JOURNALis anRwLock.writesholds only its own range (Asking::hold) whileguardruns, so other refreshes no longer queue behind a guard at the journal. That queueing was most of what @CarverXI's profile showed (46 s waiting innothing_noted).VICTORY_FAULT_UNLOCKEDandopen_unlockedare off by default.guardalso releases the guards' lock between runs of blocks (here).What is not covered is the
VirtualProtectof each run. It still happens under the guards' lock (protect_here), so two refreshes guarding at once still take turns. This PR's code cannot be carried over: the fault paths (OPENING/TOLD/BESIDE) are different. If a profile on the new tree shows refreshes waiting on the guards' lock, I'll open a fresh, measured PR for that part. Thanks @CarverXI and @CuteBooty for the measurements and the careful read.AI use: Claude Code (Claude Opus 5.5).
Pull request closed