Guards: blocks made read-only again outside the guards' and journal's locks (VICTORY_GUARD_OUTSIDE, off; #1083) #1315
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/fxceless--victory-fork!1315
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@KainanP on GitHub · 2026-11-26 13:55 UTC · original
KainanP:perf/guard-lock→main· +979 −36 · 3 files · 1 commits · openFor #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. 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.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.Benchmark (
shadow::guard_bench::guard_contention,#[ignore]):VICTORY_COPY_PLAIN=1, quiet machine, three repetitions, on the i7-14650HX of the RTX 4060 Laptop machine.VICTORY_WIDE_REFRESH-style, helper threadsVICTORY_WIDE_REFRESH-style, helper threadsWith
VICTORY_LIGHT_REFRESH=1, and withoutCOPY_PLAIN, the results are the same within a few percent. With 8 writers, the wide flush went from 14.6 to 6.0 ms.So:
Correctness.
shadow::guard_bench::flushes_under_writes_lose_nothing(not ignored, about 0.7 s) runs 3 writer threads against 100 flushes, on one thread and on the helper threads, with the switch off and on, and with copies that overlap each other's edges. After each flush it checks every word:After the writers stop, one flush must make every copy byte-equal to title memory. It passed 10 of 10 runs, and also with
VICTORY_LIGHT_REFRESH=1/alt,VICTORY_ABSENT_ONCE=1 VICTORY_ABSENT_MS=toldandVICTORY_VIEW_KNOWN=1. (It found a self-deadlock in my first version of the light-bits step, which is fixed here.)cargo test -p victory-gpu: 214 passed, 8 ignored on the RTX 4060 Laptop and on the Intel UHD (8086:a78b), with the switch unset and withVICTORY_GUARD_OUTSIDE=1. On the Intel UHD only,inplace::tests::warp_a_title_buffer_in_place_shows_its_bytes_at_its_offsetfails now and then. It fails the same way onmainceb9e1f(2 of 6 runs there). 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.clippy -D warningspasses with and withoutdiagnostics. rustfmt was applied only to the lines this PR changes.Also new, always on, and 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. Thealtlog line prints both.Not measured: the game. I can't run it, so I don't know how much of your 29–46 ms
batch_flushis protection changes, or whether the title's threads waiting at their faults is on your critical path. On the submitting thread alone, the flush itself does not get shorter. The gain to look for there is in the title's threads. WithVICTORY_WIDE_REFRESH=1, the gain should show in the flush.@MKmajster, @CuteBooty: 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. A second run withoutVICTORY_WIDE_REFRESHwould show whether the title's threads gain anything on their own. Each switch line in the log also gives the guards' totals (lock held, protection changes, lock waits).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
@CuteBooty on GitHub · 2026-11-26 16:04 UTC · original
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.
@CarverXI on GitHub · 2026-11-26 17:10 UTC · original
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.
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.