Write watch: the refreshes' checks read the journal without its lock #527
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/527"
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 readmet3xt-lab/foxhound. Originally posted by BrenoMazini on 2026-12-03T02:04:52Z.
Follow-up to #472, as @GValdezN suggested there. #472 lets unchanged copies skip the
JOURNALlock with a hashed bitmap. This PR makes all of the refreshes' journal reads lock-free:writes_into,writes_sinceand both nothing-new checks.What
Every refresh check took the write-watch journal's
RwLock, and that lock was heavily contended:writes_intocalls and ~8,000 checks of copies known to copy nothing;A thread noting what the kernel or the guards found waited ~70 µs for the readers to leave. Windows' SRW lock blocks new readers while a writer waits, so every reader then queued behind that writer: ~7 µs for each
writes_intoanswer.This PR adds
VICTORY_JOURNAL_MIRROR: on by default, and0restores the old behaviour.Lock-free page table. The pages' last askings are also kept in a table read without a lock:
AtomicU64per 64 KiB page;Notes go in through the journal's write guard (
JournalMut::note).Published asking ahead. The asking ahead (its ranges, its number, whether it stands) is published as an
Arcwhenever it changes. This happens when the write guard is dropped, after that guard's pages are noted. Each thread keeps the last copy it took until a generation counter says a new one exists.Readers.
writes_into,writes_since,nothing_new_inandnothing_new_minread from the table and the published asking. A batch'sJournalReadnow holds that snapshot instead of the lock. Writers still take the lock, so they only contend with each other.Semantics. A check sees each page either as it was before a concurrent noting or as it is after it, exactly as when it took the lock before or after that noting.
With #472
cargo test -p victory-gpu --libpasses: 622 tests, including #472'ssummary_tracks_max_asking_per_bucketand the two new tests here.Journal::note, so each stays exact.Shadow::nothing_newruns first. When it can't decide, the fallback no longer takes the lock either.With #478
VICTORY_JOURNAL_SHARDSsplits the journal in shards. Sharded notes do not go throughJournalMut::note, and the sharded path keeps its own asking ahead, so the mirror would miss both.VICTORY_JOURNAL_SHARDSis set and not0, read the same way #478 reads it. The journal is then read under its locks, whichever of the two lands first. The log says which way it is read.the_mirror_stands_aside_for_a_sharded_journal).watch.rs, because #478 reworks much of it, includingJournalRead.journal-mirror-on-478of my fork: #478 with this PR on top (the commit). There, this PR's reading helpers also read the shards, andcargo test --workspace --lockedpasses. If #478 lands first, this PR is rebased onto it with that resolution.Before and after
Setup: Ryzen 7 5700X, RTX 4070 SUPER, Series X profile, 3440x1440, DLSS performance. Same heavy scene in two play sessions: before, 7,027 indirect executions per frame; after, 7,448. Not a replay.
Version tested: builds of my fork based on v0.8.3.3 (main
7dbcdcb, the base of this PR).VICTORY_PORT_CONSTANTS=1, which is off by default upstream. Both also ran with my fork'sVICTORY_HOME=1, which keeps hot runs of the title's big buffer in the card's memory and is not upstream.writes_intoanswered from the asking ahead or the published bits (~900 calls, all threads)To be clear about the limit:
What this PR does give: the refreshes' journal reads no longer wait on writers, at no measured cost, with a switch to turn it off. It also includes the two diagnostics that showed where the submitting thread's time goes.
Diagnostics (off by default)
VICTORY_REFRESH_TOP=1: per frame, wherewrites_intospends its time; the split of the refresh flushes; and the copies whose refreshes cost most.VICTORY_GUARDS_TOP=1: the guards' lock, counted by the place it is taken and by thread (submitting, asking, others), with times taken, wait, hold and longest hold.kernel_writing. Those waits do not correlate with frame spikes (r ≈ −0.01 over 251 one-second lines).The three switches are added to the README's table of switches.
Checks
cargo clippy --workspace --all-targets --locked -- -D warnings: clean.--all-features: clean.cargo test --workspace --locked: all pass.cargo xtask policy: ok.🤖 Generated with Claude Code
Imported from readmet3xt-lab/foxhound. Originally posted by GValdezN on 2026-12-03T03:12:20Z.
Reviewed the full diff. The lock-free mirror design is clean — the semantics are correct, including the subtle factoring of
rangedintomirror_each'saboveparameter innothing_new_in(equivalent to the original.max(ranged)check). Memory ordering is right (Releasestores,Acquireloads), and the temporary false negatives from the group→page store order are benign and documented. TheJournalMutdrop publishing the ahead while the write lock is still held is the key invariant that makes the whole thing work — pages are in the mirror before the asking that found them is visible to readers. No deadlock risk since mirror readers never hold the journal lock.Two observations:
1. Integration with journal sharding (
VICTORY_JOURNAL_SHARDS)On our
perf-quick-winsbranch (commit1bb2252), we added journal sharding, which splitsJournalReadintoJournalReadInner::SingleandJournalReadInner::Sharded. This PR replacesJournalReadwithJournalReading::HeldandJournalReading::Mirror. If both land, the enum needs to handle three cases.More importantly: with sharding enabled, notes go through
sharded_note()→shard_write().note(), which does not callJournalMut::note— it writes directly to the per-shard journal. That meansmirror_notewould never be called for sharded notes, and the mirror would miss them. The mirror would need to hook into the sharded note path too, or the sharding would need to route throughJournalMut.This isn't a problem for merging this PR to main (sharding isn't there yet), but worth flagging for when both paths coexist.
2. Residual value of the summary bitmap (#472) with the mirror
Your measurements show the mirror handles ~8,135 nothing-new checks at ~0.5 µs each (~5.4 ms/frame total). The summary bitmap in #472 short-circuits most of these to ~0 µs when the copy hasn't changed — which is the common case at steady state. With both merged, the bitmap still saves most of that 5.4 ms/frame by skipping
mirror_eachentirely for unchanged copies. They're complementary fast paths at different levels: bitmap first (free), mirror second (cheap but not free).Overall this looks solid — no correctness issues found.
Imported from readmet3xt-lab/foxhound. Originally posted by BrenoMazini on 2026-12-03T03:51:10Z.
Thanks for the careful review.
Sharding. Confirmed in
1bb2252:sharded_note()goes straight toshard_write(..).note(), and the sharded path keeps its own asking ahead (GlobalAhead). With both switches on, the mirror would miss both the notes and the asking ahead, so a check could take a written page as unchanged.Rather than wait to see which lands first, I've added the guard here (
18447a5): the mirror stands aside wheneverVICTORY_JOURNAL_SHARDSis set and not0, read the same way #478 reads it, and the journal is read under its locks. The log line says which way it is read, and a new test covers the decision.The two still conflict as text in
watch.rs, since #478 reworks much of it. So I've also worked out how they fit once both are in, on the branchjournal-mirror-on-478of my fork: #478 with this PR on top, conflicts resolved (the commit).JournalReadgets three cases: held, mirror, sharded.cargo test --workspace --lockedpasses there, and clippy adds nothing beyond #478's own fourcollapsible_if.VICTORY_JOURNAL_SHARDS=1, two older tests fail (the_rest_of_the_one_asking_has_a_number_of_its_own,an_asking_begun_first_is_made_and_more_is_asked_for). They read the single journal directly and fail the same way on #478 alone.If #478 lands first, I'll rebase this PR onto it with that resolution. If this one lands first, #478 can take it from there. Routing the sharded notes through the mirror can come later, if both are wanted at once.
The bitmap. Fair point. I had written that the bitmap would save little next to the mirror, and I've corrected the description. With both in, the bitmap is the free first check for unchanged copies, and the mirror makes what is left cheap.
Imported from readmet3xt-lab/foxhound. Originally posted by tillo88 on 2026-12-04T00:47:11Z.
@BrenoMazini
Replay-bench measurement on v0.8.3.5: no measurable fps difference; CPU prep about 1 ms lower in both runs (base spread not known yet)
Measured: PR head
18447a5merged onto release v0.8.3.5 (5e44415), with the PR's defaults (VICTORY_JOURNAL_MIRRORon,VICTORY_JOURNAL_SHARDSnot set), 2 measured runs (after a warm-up run that is not counted), against 2 runs of v0.8.3.5 without the PR, same route (~6.5 min).Mean fps: release 36.46, PR 37.17 (+1.9%), below the bench's threshold for calling a run faster: no measurable difference in frame rate. CPU prep (about -1.1 ms) and the submitting thread's time (about -1.0 ms) were lower in both PR runs than in both release runs; frame-time percentiles and frames over 100 ms are within what the two release runs show. The v0.8.3.5 baseline has only 2 runs so far, so its run-to-run spread is not known yet: treat the prep difference as an observation, not a result.
Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost).
Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default
play-foxhound.ps1settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap.Update: the v0.8.3.5 baseline now has 5 runs. Three more release runs (37.23, 36.53, 37.10 fps) bring the release mean to 36.76 fps (range 36.45-37.23); against that the PR's 37.17 is +1.1%: no measurable difference in frame rate. CPU prep in the release runs: 18.49-19.38 ms (mean 18.98); submitting thread: 24.57-25.17 ms (mean 24.81). Both PR runs stayed below every release run on both (prep 18.27 / 18.04 ms, submitting thread 23.99 / 24.00 ms), so the roughly 0.8 ms lower CPU time holds against the wider baseline; it does not turn into frame rate on this machine.
Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.
Imported from readmet3xt-lab/foxhound. Originally posted by BrenoMazini on 2026-12-04T01:02:57Z.
Thank you for this, and for going back to widen the baseline to five runs before drawing anything from it.
Your reading matches what the PR is meant to do. It takes lock waits off the CPU side of the refreshes, and both of your PR runs stayed below every release run on prep and on the submitting thread, by about 0.8-1 ms. That this doesn't reach the frame rate on a 9800X3D makes sense to me: the CPU isn't what limits the frame there. On my 5700X the lock waits were larger (noting into the journal went from 4.8-7.3 ms to 1.7 ms a frame), but my before and after were two play sessions, not a replay, and with the port and my fork's home on. So your numbers are the cleaner evidence that the change is safe and consistent.
If a run on a CPU-bound setup would help, I can do a controlled set here: my 0.8.3.5-based build on the stock path (port and home off), same spot and scripted camera path, with
VICTORY_JOURNAL_MIRROR=0and1alternated.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.