GPU: write tracking guards sparse pages in 4 KiB blocks (VICTORY_WATCH_ADAPTIVE=1) #3879

Open
luciano-repetti wants to merge 2 commits from upstream-pr/3879 into main
luciano-repetti commented 2026-11-30 09:58:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by luciano-repetti on 2026-11-30T09:58:07Z.


Correction, 2026-11-30. The first version of this description gave the first of three benchmark runs as their median, and said they ran on main plus this branch; they ran on a tree with 35 other commits. The table below is new: three runs of this commit. The rule and the list of related PRs are also stated more exactly.

The problem

watch.rs guards title memory in 64 KiB pages: the first write to a page faults once, and the whole page is then refreshed. 64 KiB is right for sequential writers (few faults), wrong for scattered ones: a few bytes written in a page cost a 64 KiB copy. 4 KiB guards are the reverse: about a tenth of the bytes in the sparse benchmark below (81.6 MB to 7.8 MB a frame), but many more faults under sequential ones. Neither fixed size wins everywhere.

The fix

Per page, adaptive, opt-in with VICTORY_WATCH_ADAPTIVE=1:

  • A page starts guarded whole. After 2 takes that find it changed outside a run of 16 or more changed pages, it is tried in 16 blocks of 4 KiB (the system page), and only the written blocks are reported. Pages with aliases stay whole.
  • When 4 or more blocks are written in 2 intervals in a row, it goes back to whole, and waits 8 takes, doubling each time up to a limit, before blocks are tried again.
  • The per-page state is bounded (16 384 pages) and forgotten when no tracker holds the page.
  • VICTORY_WATCH_ADAPTIVE=fine keeps every page in blocks; it exists for the benchmark's fixed 4 KiB baseline.
  • Off by default: nothing changes without the variable.

Relation to other work

Several open PRs touch watch.rs (#3229, #3343, #3455, #3615, #3720, #3754, #3772). From a search of their diffs, not a full review: #3615 also works at 4 KiB, with GetWriteWatch on write-watched memory instead of guards, and I found no other that changes the size of a guard. This will need a rebase after whichever lands first, and I will do it.

Measurements (synthetic, no game)

New tests/watch_page_bench.rs at e9f9723 (this branch), release build, ms per frame (writes with their faults + takes + copies), median of 3, Core Ultra 9 185H (CPU-only bench):

Pattern 64 KiB (today) 4 KiB fixed Adaptive
Sparse 17.3 8.9 8.8
Dense 2.5 5.4 2.5
Mixed 12.2 10.5 7.7

Adaptive stays near the best fixed size in each pattern and wins on the mixed one.

Needs the game; not run here. An in-game run with the variable on and off would tell whether real frames look like the mixed pattern.

Tests

  • New: the_adaptive_policy_tries_blocks_and_backs_off, the_adaptive_state_is_bounded, copies_stay_equal_to_memory_through_blocks_and_partial_takes, blocks_are_listed_as_runs_cut_to_the_range, kernel_reads_into_guarded_blocks_after_opening.
  • writes_mark_every_tracker_of_their_page still checks the exact whole page when adaptive is off; with it on, it checks that the reported blocks cover the writes.
  • cargo test -p victory-gpu, also with VICTORY_WATCH_ADAPTIVE=1: 236 passed, 2 failed, 1 ignored, with the variable 0 and 1. The 2 (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views) fail the same way on main 63bcca4 on this machine.
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean. cargo run -p xtask -- policy: 192 files ok.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5562991319 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3879). Originally posted by [luciano-repetti](https://github.com/luciano-repetti) on 2026-11-30T09:58:07Z. --- > **Correction, 2026-11-30.** The first version of this description gave the first of three benchmark runs as their median, and said they ran on main plus this branch; they ran on a tree with 35 other commits. The table below is new: three runs of this commit. The rule and the list of related PRs are also stated more exactly. ## The problem `watch.rs` guards title memory in 64 KiB pages: the first write to a page faults once, and the whole page is then refreshed. 64 KiB is right for sequential writers (few faults), wrong for scattered ones: a few bytes written in a page cost a 64 KiB copy. 4 KiB guards are the reverse: about a tenth of the bytes in the sparse benchmark below (81.6 MB to 7.8 MB a frame), but many more faults under sequential ones. Neither fixed size wins everywhere. ## The fix Per page, adaptive, opt-in with `VICTORY_WATCH_ADAPTIVE=1`: - A page starts guarded whole. After 2 takes that find it changed outside a run of 16 or more changed pages, it is tried in 16 blocks of 4 KiB (the system page), and only the written blocks are reported. Pages with aliases stay whole. - When 4 or more blocks are written in 2 intervals in a row, it goes back to whole, and waits 8 takes, doubling each time up to a limit, before blocks are tried again. - The per-page state is bounded (16 384 pages) and forgotten when no tracker holds the page. - `VICTORY_WATCH_ADAPTIVE=fine` keeps every page in blocks; it exists for the benchmark's fixed 4 KiB baseline. - Off by default: nothing changes without the variable. ## Relation to other work Several open PRs touch `watch.rs` (#3229, #3343, #3455, #3615, #3720, #3754, #3772). From a search of their diffs, not a full review: #3615 also works at 4 KiB, with `GetWriteWatch` on write-watched memory instead of guards, and I found no other that changes the size of a guard. This will need a rebase after whichever lands first, and I will do it. ## Measurements (synthetic, no game) New `tests/watch_page_bench.rs` at e9f9723 (this branch), release build, ms per frame (writes with their faults + takes + copies), median of 3, Core Ultra 9 185H (CPU-only bench): | Pattern | 64 KiB (today) | 4 KiB fixed | Adaptive | |---|---|---|---| | Sparse | 17.3 | 8.9 | 8.8 | | Dense | 2.5 | 5.4 | 2.5 | | Mixed | 12.2 | 10.5 | **7.7** | Adaptive stays near the best fixed size in each pattern and wins on the mixed one. **Needs the game; not run here.** An in-game run with the variable on and off would tell whether real frames look like the mixed pattern. ## Tests - New: `the_adaptive_policy_tries_blocks_and_backs_off`, `the_adaptive_state_is_bounded`, `copies_stay_equal_to_memory_through_blocks_and_partial_takes`, `blocks_are_listed_as_runs_cut_to_the_range`, `kernel_reads_into_guarded_blocks_after_opening`. - `writes_mark_every_tracker_of_their_page` still checks the exact whole page when adaptive is off; with it on, it checks that the reported blocks cover the writes. - `cargo test -p victory-gpu`, also with `VICTORY_WATCH_ADAPTIVE=1`: 236 passed, 2 failed, 1 ignored, with the variable 0 and 1. The 2 (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`) fail the same way on main 63bcca4 on this machine. - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean. `cargo run -p xtask -- policy`: 192 files ok.
cobrajet302gt commented 2026-11-30 12:02:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cobrajet302gt on 2026-11-30T12:02:29Z.


data point from our box for this lane, non-prescriptive: we single-factor-reverted a free-tracking watch change (#1849, watch.rs only, +12/-46, md5 7664575b -> 778fad8b) and armed exceptions:page_fault_user (filter: title-arena window, error_code==4) before launch. In-window read-AVs went from six-at-one-instant to zero across two boots (Pool 98.8% at death, 34 and 74 min alive vs a 504-924 s band). Kernel fault counts before/after a guard-size change are cheap ground truth; the revert pair + trace recipe would work as a regression harness for adaptive granularity. Full data: #1633 comment 7277859599. (agent-assisted)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7277861289 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3879#issuecomment-7277861289). Originally posted by [cobrajet302gt](https://github.com/cobrajet302gt) on 2026-11-30T12:02:29Z. --- data point from our box for this lane, non-prescriptive: we single-factor-reverted a free-tracking watch change (#1849, watch.rs only, +12/-46, md5 7664575b -> 778fad8b) and armed `exceptions:page_fault_user` (filter: title-arena window, error_code==4) before launch. In-window read-AVs went from six-at-one-instant to **zero across two boots** (Pool 98.8% at death, 34 and 74 min alive vs a 504-924 s band). Kernel fault counts before/after a guard-size change are cheap ground truth; the revert pair + trace recipe would work as a regression harness for adaptive granularity. Full data: #1633 comment 7277859599. (agent-assisted)
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/3879:upstream-pr/3879'
git switch 'upstream-pr/3879'

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/3879'
git switch 'upstream-pr/3879'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3879'
git switch 'upstream-pr/3879'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3879'
git switch 'main'
git merge --squash 'upstream-pr/3879'
git switch 'main'
git merge --ff-only 'upstream-pr/3879'
git switch 'main'
git merge 'upstream-pr/3879'
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
Marvisak/VIctory!3879
No description provided.