Copies beside the guards open a block without the guards' lock, as a fault does (VICTORY_COPY_OPEN_UNLOCKED, on) #4027

Merged
mstrmoos merged 1 commit from upstream-pr/4027 into main 2026-12-01 18:21:39 +00:00
mstrmoos commented 2026-11-30 14:52:44 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mstrmoos on 2026-11-30T14:52:44Z.


[pr-performance]

What it changes

With VICTORY_COPY_BESIDE=1, the title's memcpy/memmove/memset into a guarded 64 KiB block is done through the layer's own view of the page, until the block has had its share (titlewrite::MOST). After that, watch::opened_for_writing opens the block, and it always took the guards' lock to do it. That's the same lock the refresh threads hold while they scan and re-guard ranges (guard, publish).

Write faults stopped taking that lock long ago (VICTORY_FAULT_UNLOCKED, open_unlocked plus settle). This makes the copies' open do the same thing:

  • Where faults take no lock, opened_for_writing calls open_unlocked(block) and returns.
  • If that can't open the block (another thread is opening it, or it's to be opened with every view of its page), the copy's write is left to fault. The handler then does exactly what it does for any other write. No write goes unseen. Only the lock is spared where it can be.
  • VICTORY_COPY_OPEN_UNLOCKED=0 brings back the locked path (toggled!).

How it was tested

  • GPU and profile: RTX 5080, 9800X3D, Series X profile, 2560x1440, DLSS Balanced, frame generation off, ray tracing off.
  • Windows virtualisation-based security was on for these runs (it makes concurrent protection changes slower; with it off the whole frame is about 5 ms faster, and the card becomes the limit).
  • Test setup: 150 s measured in the same save at the same time of day (clock held), diagnostics build, VICTORY_COPY_BESIDE=1, same shader cache.
  • I found the cause with the guards' lock diagnostics from #3604 (ported locally) and timeline spans. Over three frames of play, opened_for_writing was called 764 times on the title's threads and spent 346 ms waiting for the lock, up to 17 ms at a time. The change of protection itself took 0.2 ms in all.
  • Before (five runs of the same build without this change): frame 41.3 / 42.2 / 42.5 / 42.6 / 41.7 ms median. Waits for the guards' lock were 55-56 ms a frame summed over all threads.
  • After: frame 40.0 ms median. Waits for the guards' lock were 22 ms a frame, and title write faults waited 0.02 ms a frame for it. The middle and end screenshots show no visual difference.
  • cargo test --locked -p victory-gpu:
    • watch::tests::stores_and_copies_of_the_title_leave_the_guard and faults_that_take_no_lock_tell_the_same pass. The latter runs the copy tests with faults taking no lock, so it goes through this path.
    • ask_rest_falls_back_per_piece_when_touching_unwatched_neighbour fails now and then in the full parallel run, with or without this change (1 in 5 without it), and passes on its own.
  • Clippy on victory-gpu adds no warnings.

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: #3604 (the diagnostics that found it), #3626 (landing opens guards a run at a time, a different caller of the lock), #3945 (lock-free publish; it changes open_unlocked's internals and publish in the same file, not opened_for_writing, so the two should merge without conflict).
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5567640658 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4027). Originally posted by [mstrmoos](https://github.com/mstrmoos) on 2026-11-30T14:52:44Z. --- [pr-performance] ## What it changes With `VICTORY_COPY_BESIDE=1`, the title's `memcpy`/`memmove`/`memset` into a guarded 64 KiB block is done through the layer's own view of the page, until the block has had its share (`titlewrite::MOST`). After that, `watch::opened_for_writing` opens the block, and it always took the guards' lock to do it. That's the same lock the refresh threads hold while they scan and re-guard ranges (`guard`, `publish`). Write faults stopped taking that lock long ago (`VICTORY_FAULT_UNLOCKED`, `open_unlocked` plus `settle`). This makes the copies' open do the same thing: - Where faults take no lock, `opened_for_writing` calls `open_unlocked(block)` and returns. - If that can't open the block (another thread is opening it, or it's to be opened with every view of its page), the copy's write is left to fault. The handler then does exactly what it does for any other write. No write goes unseen. Only the lock is spared where it can be. - `VICTORY_COPY_OPEN_UNLOCKED=0` brings back the locked path (`toggled!`). ## How it was tested - GPU and profile: RTX 5080, 9800X3D, Series X profile, 2560x1440, DLSS Balanced, frame generation off, ray tracing off. - Windows virtualisation-based security was on for these runs (it makes concurrent protection changes slower; with it off the whole frame is about 5 ms faster, and the card becomes the limit). - Test setup: 150 s measured in the same save at the same time of day (clock held), diagnostics build, `VICTORY_COPY_BESIDE=1`, same shader cache. - I found the cause with the guards' lock diagnostics from #3604 (ported locally) and timeline spans. Over three frames of play, `opened_for_writing` was called 764 times on the title's threads and spent **346 ms waiting** for the lock, up to 17 ms at a time. The change of protection itself took 0.2 ms in all. - Before (five runs of the same build without this change): frame **41.3 / 42.2 / 42.5 / 42.6 / 41.7 ms** median. Waits for the guards' lock were **55-56 ms a frame** summed over all threads. - After: frame **40.0 ms** median. Waits for the guards' lock were **22 ms a frame**, and title write faults waited 0.02 ms a frame for it. The middle and end screenshots show no visual difference. - `cargo test --locked -p victory-gpu`: - `watch::tests::stores_and_copies_of_the_title_leave_the_guard` and `faults_that_take_no_lock_tell_the_same` pass. The latter runs the copy tests with faults taking no lock, so it goes through this path. - `ask_rest_falls_back_per_piece_when_touching_unwatched_neighbour` fails now and then in the full parallel run, with or without this change (1 in 5 without it), and passes on its own. - Clippy on `victory-gpu` adds no warnings. ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #3604 (the diagnostics that found it), #3626 (landing opens guards a run at a time, a different caller of the lock), #3945 (lock-free publish; it changes `open_unlocked`'s internals and `publish` in the same file, not `opened_for_writing`, so the two should merge without conflict).
upipibi commented 2026-12-01 02:03:58 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by upipibi on 2026-12-01T02:03:58Z.


Race review of #4027 (head e5cefb5f): it holds. I found no crash and no lost write, but the worst single wait got much longer in a stress test. That may be worth a look at the frame-time tail in your runs.

Why it holds. opened_for_writing now does what a write fault already does on main: open_unlocked, then settle by the next holder of the lock. If open_unlocked declines, the copy's write faults and the handler takes over. So no new protocol is added, only one more caller of the existing one.

Test. This is the stress test from my #3760 review (one 16 MiB view, four threads guarding it in a loop, writing() on another thread, 48 writer threads for 20 s), with one change. Before half of the writes, the writer calls super::opened_for_writing(at + page * PAGE) first, as crate::titlewrite does.

Results with this PR merged onto main 7d93ee07 (clean merge). Debug test build, Ryzen 9 5950X (16 cores, 32 threads), Windows 11, VICTORY_FAULT_UNLOCKED default:

VICTORY_COPY_OPEN_UNLOCKED runs crashed worst single title write worst single guarded ask
1 (default) 6 0 68–131 ms 151–270 ms
0 3 0 3–4 ms 77–97 ms

watch::: 69 passed, 0 failed, 3 runs.

The two ranges don't overlap. This load is artificial: random pages, 48 threads on 16 cores, and opened_for_writing far more often than a title calls it. It measures the worst single wait, while your numbers are the total lock wait per frame, so it doesn't contradict them. But it points one way: with the switch on, a single write or refresh can wait much longer, here up to about 0.1–0.3 s. I didn't trace where that time goes. The unlocked opens are settled in batches by whoever takes the lock next, and a write that finds a block another thread is opening waits for it, so either is a candidate.

Not tested: the game, release builds, and VICTORY_COPY_BESIDE=1 end to end (the test calls opened_for_writing directly).

(The ask_rest_falls_back_per_piece_when_touching_unwatched_neighbour flake you noted is a test-isolation bug on main, fixed by #4619.)

Tested with the help of Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7298194639 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4027#issuecomment-7298194639). Originally posted by [upipibi](https://github.com/upipibi) on 2026-12-01T02:03:58Z. --- Race review of #4027 (head e5cefb5f): **it holds. I found no crash and no lost write, but the worst single wait got much longer in a stress test.** That may be worth a look at the frame-time tail in your runs. **Why it holds.** `opened_for_writing` now does what a write fault already does on `main`: `open_unlocked`, then `settle` by the next holder of the lock. If `open_unlocked` declines, the copy's write faults and the handler takes over. So no new protocol is added, only one more caller of the existing one. **Test.** This is the stress test from my #3760 review (one 16 MiB view, four threads guarding it in a loop, `writing()` on another thread, 48 writer threads for 20 s), with one change. Before half of the writes, the writer calls `super::opened_for_writing(at + page * PAGE)` first, as `crate::titlewrite` does. Results with this PR merged onto `main` 7d93ee07 (clean merge). Debug test build, Ryzen 9 5950X (16 cores, 32 threads), Windows 11, `VICTORY_FAULT_UNLOCKED` default: | `VICTORY_COPY_OPEN_UNLOCKED` | runs | crashed | worst single title write | worst single guarded ask | |---|---|---|---|---| | `1` (default) | 6 | 0 | 68–131 ms | 151–270 ms | | `0` | 3 | 0 | 3–4 ms | 77–97 ms | `watch::`: 69 passed, 0 failed, 3 runs. The two ranges don't overlap. This load is artificial: random pages, 48 threads on 16 cores, and `opened_for_writing` far more often than a title calls it. It measures the worst single wait, while your numbers are the total lock wait per frame, so it doesn't contradict them. But it points one way: with the switch on, a single write or refresh can wait much longer, here up to about 0.1–0.3 s. I didn't trace where that time goes. The unlocked opens are settled in batches by whoever takes the lock next, and a write that finds a block another thread is opening waits for it, so either is a candidate. Not tested: the game, release builds, and `VICTORY_COPY_BESIDE=1` end to end (the test calls `opened_for_writing` directly). (The `ask_rest_falls_back_per_piece_when_touching_unwatched_neighbour` flake you noted is a test-isolation bug on `main`, fixed by #4619.) *Tested with the help of Claude Code (Claude Opus 5.5).*
mstrmoos commented 2026-12-01 15:46:31 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mstrmoos on 2026-12-01T15:46:31Z.


Thanks for the review, and for testing the worst single wait and not just the totals. Those ranges don't overlap, so it's worth checking in the game rather than arguing from the load. I keep this switch on in my play build, and a 30-minute run there today had 5 frames over 100 ms (longest 364 ms) out of 68,710. I'll run 10 minutes with VICTORY_COPY_OPEN_UNLOCKED=0 and 10 with =1 on the same scene and post the long-frame counts and the longest frame here. If =0 takes the tail away, the default should change.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7316484157 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4027#issuecomment-7316484157). Originally posted by [mstrmoos](https://github.com/mstrmoos) on 2026-12-01T15:46:31Z. --- Thanks for the review, and for testing the worst single wait and not just the totals. Those ranges don't overlap, so it's worth checking in the game rather than arguing from the load. I keep this switch on in my play build, and a 30-minute run there today had 5 frames over 100 ms (longest 364 ms) out of 68,710. I'll run 10 minutes with `VICTORY_COPY_OPEN_UNLOCKED=0` and 10 with `=1` on the same scene and post the long-frame counts and the longest frame here. If `=0` takes the tail away, the default should change.
mstrmoos commented 2026-12-01 16:15:01 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mstrmoos on 2026-12-01T16:15:01Z.


In-game result. In the game it goes the other way from the stress test.

RTX 5080, Ryzen 7 9800X3D. Series X, DLSS Balanced, frame generation 4x (Reflex low latency off, frameLimitUs 6,500). 10 minutes each standing on the pier after a warm-up, about 5,000-5,700 draws a frame. Same build and switches apart from this one:

frame (median) p95 p99 frames over 50 ms over 100 ms longest
VICTORY_COPY_OPEN_UNLOCKED=0 26.0 ms 26.1 26.7 10 3 1,069 ms
=1 26.0 ms 26.1 27.0 0 0 49.8 ms

That's one run of each, so it doesn't prove =1 is better at the tail, but there's no sign in the game of the waits you measured. My guess is that the stress test's density (48 writer threads, random pages, an open before half the writes) builds up a backlog of unsettled opens that the title never produces. I'm keeping the default as it is. If anyone sees a long-frame tail with it on, a VICTORY_COPY_OPEN_UNLOCKED=0 run beside it would show it quickly.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7317286689 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4027#issuecomment-7317286689). Originally posted by [mstrmoos](https://github.com/mstrmoos) on 2026-12-01T16:15:01Z. --- In-game result. In the game it goes the other way from the stress test. RTX 5080, Ryzen 7 9800X3D. Series X, DLSS Balanced, frame generation 4x (Reflex low latency off, `frameLimitUs` 6,500). 10 minutes each standing on the pier after a warm-up, about 5,000-5,700 draws a frame. Same build and switches apart from this one: | | frame (median) | p95 | p99 | frames over 50 ms | over 100 ms | longest | | --- | --- | --- | --- | --- | --- | --- | | `VICTORY_COPY_OPEN_UNLOCKED=0` | 26.0 ms | 26.1 | 26.7 | 10 | 3 | 1,069 ms | | `=1` | 26.0 ms | 26.1 | 27.0 | **0** | **0** | **49.8 ms** | That's one run of each, so it doesn't prove `=1` is better at the tail, but there's no sign in the game of the waits you measured. My guess is that the stress test's density (48 writer threads, random pages, an open before half the writes) builds up a backlog of unsettled opens that the title never produces. I'm keeping the default as it is. If anyone sees a long-frame tail with it on, a `VICTORY_COPY_OPEN_UNLOCKED=0` run beside it would show it quickly.
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
trustno1-coder/VIctory-NVIDIA!4027
No description provided.