watch: the journal's lock let go across the kernel's asking (VICTORY_JOURNAL_UNLOCKED=1), and what it waits for on the frame line #1099

Open
alexis27yt wants to merge 1 commit from upstream-pr/1102 into main
alexis27yt commented 2026-11-25 19:29:48 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by alexis27yt on 2026-11-25T19:29:48Z.


The write-watch journal (crate::watch) held its lock across the kernel's asking (GetWriteWatch with reset). An asking over big memory takes milliseconds, and every other answer of the journal waited for it, including those the journal gives alone (ranges asked ahead, asked). The queues prepare their submissions each on its own thread (HostQueue::submit locks per queue), so one queue's asking holds up another's.

Two parts

1. On the frame line, always on (no switch): journal: N askings X ms (Y with the lock held), Z ms waiting for the lock, W for other askings to land. The time waited is only measured when the lock is taken (a try_lock first, so the uncontended case adds nothing). This tells, from any run in the world, whether the wait matters in game, which I can't measure myself.

2. VICTORY_JOURNAL_UNLOCKED=1 (off by default): writes asks the kernel with the lock let go, holding a ticket instead.

  • An answer (of the journal alone, or after an asking of its own) waits for the tickets handed before it over any of its pages to land. An asking in flight may have reset the bits of pages written before the answer, which another asking then does not see, so the answer must have them. Askings over other memory are not waited for.
  • The numbers keep their meaning. An answer's is the journal's last, every page in it was found before the answer, and a page written after is found by an asking that lands after, with a higher one. With nothing in flight, the answer is exactly as before.
  • Asking ahead (ask_ahead, ask_once) and view memory (the guards) are as they were. Their kernel time shows on the line as "with the lock held", so the line also says whether those are worth the same treatment.

Tests (i5-12400F, Windows 11)

  • journal_tests::no_write_is_lost_with_the_lock_held_or_let_go: 2 writers, 5 askers over overlapping ranges (one asks ahead and answers from the journal), 1.5 s with the lock held and 1.5 s let go. A write that began after an asker's answer and ended before its next asking must show in that next answer at a higher number. Passes both ways, with the rest of watch::.
  • journal_tests::what_a_small_asking_waits_for_beside_a_big_one (ignored, a measurement): a 4-page asking every 200 µs while another thread asks about 1 GB with a tenth of it written. Two rounds:
lock held lock let go
big asking 1.35-1.55 ms 1.35-1.46 ms
small asking, median 14-388 µs 4.3-4.7 µs
p99 1.6-2.2 ms 0.13-0.21 ms
longest 3.0-6.1 ms 0.33-0.82 ms
all waited, over 3 s 1,467-1,602 ms 55-83 ms

This is synthetic. What it is worth in a frame depends on how often two queues' preparing overlaps, which is what part 1 shows. No frame-rate claim: I can't run the title.

🤖 Generated with Claude Code

<!-- greyfinch-sync:fxceless/victory-fork:issue:6612098509 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1102). Originally posted by [alexis27yt](https://github.com/alexis27yt) on 2026-11-25T19:29:48Z. --- The write-watch journal (`crate::watch`) held its lock across the kernel's asking (`GetWriteWatch` with reset). An asking over big memory takes milliseconds, and every other answer of the journal waited for it, including those the journal gives alone (ranges asked ahead, `asked`). The queues prepare their submissions each on its own thread (`HostQueue::submit` locks per queue), so one queue's asking holds up another's. ## Two parts **1. On the frame line, always on (no switch):** `journal: N askings X ms (Y with the lock held), Z ms waiting for the lock, W for other askings to land`. The time waited is only measured when the lock is taken (a `try_lock` first, so the uncontended case adds nothing). This tells, from any run in the world, whether the wait matters in game, which I can't measure myself. **2. `VICTORY_JOURNAL_UNLOCKED=1` (off by default):** `writes` asks the kernel with the lock let go, holding a ticket instead. - An answer (of the journal alone, or after an asking of its own) waits for the tickets handed before it **over any of its pages** to land. An asking in flight may have reset the bits of pages written before the answer, which another asking then does not see, so the answer must have them. Askings over other memory are not waited for. - The numbers keep their meaning. An answer's is the journal's last, every page in it was found before the answer, and a page written after is found by an asking that lands after, with a higher one. With nothing in flight, the answer is exactly as before. - Asking ahead (`ask_ahead`, `ask_once`) and view memory (the guards) are as they were. Their kernel time shows on the line as "with the lock held", so the line also says whether those are worth the same treatment. ## Tests (i5-12400F, Windows 11) - `journal_tests::no_write_is_lost_with_the_lock_held_or_let_go`: 2 writers, 5 askers over overlapping ranges (one asks ahead and answers from the journal), 1.5 s with the lock held and 1.5 s let go. A write that began after an asker's answer and ended before its next asking must show in that next answer at a higher number. Passes both ways, with the rest of `watch::`. - `journal_tests::what_a_small_asking_waits_for_beside_a_big_one` (ignored, a measurement): a 4-page asking every 200 µs while another thread asks about 1 GB with a tenth of it written. Two rounds: | | lock held | lock let go | |---|---|---| | big asking | 1.35-1.55 ms | 1.35-1.46 ms | | small asking, median | 14-388 µs | 4.3-4.7 µs | | p99 | 1.6-2.2 ms | 0.13-0.21 ms | | longest | 3.0-6.1 ms | 0.33-0.82 ms | | all waited, over 3 s | 1,467-1,602 ms | 55-83 ms | This is synthetic. What it is worth in a frame depends on how often two queues' preparing overlaps, which is what part 1 shows. No frame-rate claim: I can't run the title. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
rafaconvaitis commented 2026-11-25 23:09:57 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by rafaconvaitis on 2026-11-25T22:33:01Z.


Native RTX 4070 (616.64), Ryzen 9 5900X, Series S, pinned Flamingo 63d976b. I profiled the fixed view from the cell after a verified reload and camera inputs. Parallel shadow refresh stayed off. The 90-second window contains 664 presents at 7.709 effective FPS and 85 complete detail samples.

Median shadow updates cost 35.37 ms per frame. Within that work, calls to watch::writes took 17.23 ms, stale-range reads 14.41 ms, the oldest-stamp scan 0.78 ms, stale-page bookkeeping 0.63 ms, and pending-range collection 0.38 ms. Whole-range reads were zero in this scene. Ask-ahead outside shadow updates cost 0.21 ms, and GPU write registration 0.74 ms. The last two metrics measure their own work and exclude a pending refresh flushed ahead of them.

The 17.23 ms is inclusive call time. This trace does not attribute it to journal contention, kernel calls, or guard handling individually. It gives a native reference for the lock and kernel timing proposed here.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7176501670 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1102#issuecomment-7176501670). Originally posted by [rafaconvaitis](https://github.com/rafaconvaitis) on 2026-11-25T22:33:01Z. --- Native RTX 4070 (616.64), Ryzen 9 5900X, Series S, pinned Flamingo 63d976b. I profiled the fixed view from the cell after a verified reload and camera inputs. Parallel shadow refresh stayed off. The 90-second window contains 664 presents at 7.709 effective FPS and 85 complete detail samples. Median shadow updates cost 35.37 ms per frame. Within that work, calls to watch::writes took 17.23 ms, stale-range reads 14.41 ms, the oldest-stamp scan 0.78 ms, stale-page bookkeeping 0.63 ms, and pending-range collection 0.38 ms. Whole-range reads were zero in this scene. Ask-ahead outside shadow updates cost 0.21 ms, and GPU write registration 0.74 ms. The last two metrics measure their own work and exclude a pending refresh flushed ahead of them. The 17.23 ms is inclusive call time. This trace does not attribute it to journal contention, kernel calls, or guard handling individually. It gives a native reference for the lock and kernel timing proposed here.
rafaconvaitis commented 2026-11-25 23:10:00 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by rafaconvaitis on 2026-11-25T23:07:47Z.


I followed up with an opt-in instrumented native D3D12 Series S run on an RTX 4070, pinned to Flamingo 63d976b, from the same v3 prison checkpoint in a fixed view. The 90 s capture had 616 presents and 83 write-watch detail samples. These values are per-report medians, with the writes time included in the shadow update.

  • Shadow update: 33.57 ms. watch::writes: 16.24 ms.
  • Within writes: guard handling 9.55 ms, view lookup 2.33 ms, publication 2.75 ms, ahead answers 0.90 ms.
  • Journal lock contention, timed only on an actual try_lock WouldBlock: 0.00 ms (at displayed precision). Time in the direct GetWriteWatch call: 0.00 ms (same precision).

This is a single native VIEW_SET scene. The measurements point to guard handling for this scene and bound the benefit of unlocking only around the direct GetWriteWatch call here. I did not test the PR patch itself in this follow-up.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7177180400 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1102#issuecomment-7177180400). Originally posted by [rafaconvaitis](https://github.com/rafaconvaitis) on 2026-11-25T23:07:47Z. --- I followed up with an opt-in instrumented native D3D12 Series S run on an RTX 4070, pinned to Flamingo `63d976b`, from the same v3 prison checkpoint in a fixed view. The 90 s capture had 616 presents and 83 write-watch detail samples. These values are per-report medians, with the `writes` time included in the shadow update. - Shadow update: 33.57 ms. `watch::writes`: 16.24 ms. - Within `writes`: guard handling 9.55 ms, view lookup 2.33 ms, publication 2.75 ms, ahead answers 0.90 ms. - Journal lock contention, timed only on an actual `try_lock` `WouldBlock`: 0.00 ms (at displayed precision). Time in the direct `GetWriteWatch` call: 0.00 ms (same precision). This is a single native `VIEW_SET` scene. The measurements point to guard handling for this scene and bound the benefit of unlocking only around the direct `GetWriteWatch` call here. I did not test the PR patch itself in this follow-up.
slauko commented 2026-11-26 16:06:38 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by slauko on 2026-11-26T15:39:48Z.


A port to a Linux/Wine line (RTX 3080 Ti, vkd3d-proton ec20719, uiliannn's base), with a warning first: it hung the title here.

What I ported is your core, without the tickets: writes drops the journal's lock before GetWriteWatch, askings are made one at a time behind their own mutex (this line has one submit thread, no second queue), the answer goes into a thread-local buffer, and the journal is taken again to insert. Builds, clippy clean, and then m61: the title went into its shader translation and never came back — no frame line for three minutes, process and wineserver alive, log ending on mwait: thread 468 waited 0.6 ms for the value at 0x11e6950. Reverted; the tree is back on the locked version.

So this is a "works on my machine" data point in reverse — it may be my simplification (no tickets, so an asking and an insert are not atomic together), or the lock order with the guards' lock, which writes takes behind the journal's lock when a range has to be guarded. I have not investigated further; your version keeps the lock in the default path and I would not flip it on this stack without the tickets.

The numbers that say how much there is to win here, on a cell frame of ~5,000 draws:

per frame, one submit thread before after merging a copy's touching ranges into one pass
writes calls 34,282 (18,887 journal, 16,395 kernel) 8,965 (3,278, 5,687)
the asking, ms 40.6–41.2 5.9–6.4
the copies 166–173 MB / 17 ms 52–57 MB / 5.9 ms

The kernel's own time inside the calls is 11.5–15.9 ms of that 40; the rest was the journal's range query and its answer, per call. With ~35,000 calls a frame, the per-call cost dominates long before any lock wait does — so on a line with one submit thread the win of letting the lock go is bounded by 16 ms, and the merge above was worth 34.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7196579097 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1102#issuecomment-7196579097). Originally posted by [slauko](https://github.com/slauko) on 2026-11-26T15:39:48Z. --- A port to a Linux/Wine line (RTX 3080 Ti, vkd3d-proton `ec20719`, uiliannn's base), with a warning first: **it hung the title here.** What I ported is your core, without the tickets: `writes` drops the journal's lock before `GetWriteWatch`, askings are made one at a time behind their own mutex (this line has one submit thread, no second queue), the answer goes into a thread-local buffer, and the journal is taken again to insert. Builds, clippy clean, and then `m61`: the title went into its shader translation and never came back — no frame line for three minutes, process and wineserver alive, log ending on `mwait: thread 468 waited 0.6 ms for the value at 0x11e6950`. Reverted; the tree is back on the locked version. So this is a "works on my machine" data point in reverse — it may be my simplification (no tickets, so an asking and an insert are not atomic together), or the lock order with the guards' lock, which `writes` takes behind the journal's lock when a range has to be guarded. I have not investigated further; your version keeps the lock in the default path and I would not flip it on this stack without the tickets. The numbers that say how much there is to win here, on a cell frame of ~5,000 draws: | per frame, one submit thread | before | after merging a copy's touching ranges into one pass | |---|---:|---:| | `writes` calls | **34,282** (18,887 journal, 16,395 kernel) | **8,965** (3,278, 5,687) | | the asking, ms | 40.6–41.2 | **5.9–6.4** | | the copies | 166–173 MB / 17 ms | **52–57 MB / 5.9 ms** | The kernel's own time inside the calls is 11.5–15.9 ms of that 40; the rest was the journal's range query and its answer, per call. With ~35,000 calls a frame, the per-call cost dominates long before any lock wait does — so on a line with one submit thread the win of letting the lock go is bounded by 16 ms, and the merge above was worth 34.
slauko commented 2026-11-26 17:05:08 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by slauko on 2026-11-26T17:05:08Z.


A correction to my numbers above, before anyone ports the merge because of them: it holds the title on its prologue card.

  • 8,965 writes calls, 5.9-6.4 ms asking and preparing 57-63 ms were measured on a prologue frame, not in the cell.
  • With that change in, no run reached the cell any more (m66-m79): the title sits on the prologue's card at 3,000-3,100 draws, 121-137 ms, and never goes on.
  • Bisected: the pre-merge binary reaches the cell (m76: 4,976 draws, 209.8-214.8 ms); sorted, in recording order and capped at 4 MiB all do not (m77, m78), and VICTORY_REFRESH_SCAN=0 does not either (m79).
  • With the merge out and everything else kept, the cell is 205.1 ms median (m80, 201.4-205.1, 4,968-5,000 draws), the asking 34.3-34.6 ms at ~34,000 calls.

So the call count is still the lever (34,000 calls, 34 ms of asking), but merging the ranges changes something the intro's transition needs and is not a safe port as it stands. The other reading stands: the kernel's own time inside those calls is 11.5-15.9 ms, so letting the journal's lock go is bounded well below the count.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7198475411 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1102#issuecomment-7198475411). Originally posted by [slauko](https://github.com/slauko) on 2026-11-26T17:05:08Z. --- A correction to my numbers above, before anyone ports the merge because of them: **it holds the title on its prologue card.** - 8,965 `writes` calls, 5.9-6.4 ms asking and preparing 57-63 ms were measured on a **prologue frame**, not in the cell. - With that change in, no run reached the cell any more (`m66`-`m79`): the title sits on the prologue's card at 3,000-3,100 draws, 121-137 ms, and never goes on. - Bisected: the **pre-merge binary reaches the cell** (`m76`: 4,976 draws, 209.8-214.8 ms); sorted, in recording order and capped at 4 MiB all do not (`m77`, `m78`), and `VICTORY_REFRESH_SCAN=0` does not either (`m79`). - With the merge out and everything else kept, the cell is **205.1 ms** median (`m80`, 201.4-205.1, 4,968-5,000 draws), the asking 34.3-34.6 ms at ~34,000 calls. So the call count is still the lever (34,000 calls, 34 ms of asking), but merging the ranges changes something the intro's transition needs and is not a safe port as it stands. The other reading stands: the kernel's own time inside those calls is 11.5-15.9 ms, so letting the journal's lock go is bounded well below the count.
slauko commented 2026-11-26 18:03:28 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by slauko on 2026-11-26T18:03:28Z.


The follow-up I promised, and it settles the shape: taking the asking out of the per-range path works on the clock and breaks the picture.

What I built (Linux/Wine, RTX 3080 Ti, vkd3d-proton ec20719): one GetWriteWatch per copy for everything the submission refreshes of it, taken after the settle, and every range's update served from that answer — the order of the updates unchanged, the copies still per page.

cell, ~5,000 draws per-range asking (m80) one asking per copy per submission
frame, median 205.1 ms 193.7 ms (m87), 200.3 ms (m88)
the copies 55-60 ms for 170-180 MB 20-22 ms for 156-167 MB

m87 looked clean (no Xid, picture intact). m88, same build, is visibly broken: floating polygons, garbage geometry, black clipping on the character. That is the failure the shape invites — the answer is taken before the lists run, and pages the title writes after that asking are gone for this submission.

So: reverted (commit 0360aad5 on my line), and the asking stays per range here. Four shapes of this lever are now measured and rejected on this stack: merging touching ranges (34 ms of asking saved, keeps the title on its prologue card), one asking per flush (211.7 ms), one per copy per submission with a widened span (Xid in the window, inconclusive), and this one (fast, wrong picture). The 34 ms of asking are not recoverable by moving the asking out of the range.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7200003467 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1102#issuecomment-7200003467). Originally posted by [slauko](https://github.com/slauko) on 2026-11-26T18:03:28Z. --- The follow-up I promised, and it settles the shape: **taking the asking out of the per-range path works on the clock and breaks the picture.** What I built (Linux/Wine, RTX 3080 Ti, vkd3d-proton `ec20719`): one `GetWriteWatch` per copy for everything the submission refreshes of it, taken after the settle, and every range's `update` served from that answer — the order of the updates unchanged, the copies still per page. | cell, ~5,000 draws | per-range asking (`m80`) | one asking per copy per submission | |---|---:|---:| | frame, median | **205.1 ms** | **193.7 ms** (`m87`), **200.3 ms** (`m88`) | | the copies | 55-60 ms for 170-180 MB | **20-22 ms for 156-167 MB** | `m87` looked clean (no Xid, picture intact). `m88`, same build, is **visibly broken**: floating polygons, garbage geometry, black clipping on the character. That is the failure the shape invites — the answer is taken before the lists run, and pages the title writes after that asking are gone for this submission. So: reverted (commit `0360aad5` on my line), and the asking stays per range here. Four shapes of this lever are now measured and rejected on this stack: merging touching ranges (34 ms of asking saved, keeps the title on its prologue card), one asking per flush (211.7 ms), one per copy per submission with a widened span (Xid in the window, inconclusive), and this one (fast, wrong picture). The 34 ms of asking are not recoverable by moving the asking out of the range.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1102:upstream-pr/1102'
git switch 'upstream-pr/1102'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/1102'
git switch 'upstream-pr/1102'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1102'
git switch 'upstream-pr/1102'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1102'
git switch 'main'
git merge --squash 'upstream-pr/1102'
git switch 'main'
git merge --ff-only 'upstream-pr/1102'
git switch 'main'
git merge 'upstream-pr/1102'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
fxceless/victory-fork!1099
No description provided.