test: bench the guard lock between title write faults and askings #594

Open
KainanP wants to merge 1 commit from upstream-pr/594 into main
KainanP commented 2026-11-29 11:06:35 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-29T11:06:35Z.


What

A test-only, ignored benchmark in the VIctory copy: watch::many_askers_tests::guard_contention (third_party/victory/crates/victory-gpu/src/watch.rs). No behaviour change.

The VIctory copy's view memory is asked about range by range, the way a submission's refreshes guard what they take. Meanwhile title threads write the same memory, so each first write to a guarded block is a fault that opens it. The bench prints how long the askings take, how much time the writers spend in their writes (the fault handler, including its wait for the guards' lock), how many writes were slow, and how many faults went under the shared lock. It runs once with renderer port #6 off and once with it on (port_guard).

cargo test -p victory-gpu --release --lib guard_contention -- --ignored --nocapture

Knobs: BENCH_PAGES, BENCH_SPAN, BENCH_DIRTY, BENCH_WRITERS, BENCH_GAP_US, BENCH_ROUNDS, BENCH_ASKERS, BENCH_REST_US. It ends with one guard_contention: key=value line per port, for scripts.

Why

The plan was to move the fault handler's own VirtualProtect out of the GUARDS lock: on the VIctory and fork bases, that lock showed 50-76 ms of title-thread wait. On this base, renderer port #6 (VICTORY_PORT_GUARD=1, off by default) already does that. Faults take GUARDS shared plus their block's claim (BUSY), so the fault's VirtualProtect runs outside the exclusive lock, and the no_write_*_with_port_6 tests cover it. A simpler split for the default path is not safe without those claims. If the lock is released before VirtualProtect, a refresh can take it in between and guard the block. The fault's late PAGE_READWRITE then leaves the block writable while protected says it is clean, and writes are lost. So I did not open a fix PR. This bench is what decided it.

Measured (CPU only, quiet laptop, 3 runs each, medians of 15 rounds)

VirtualProtect alone: 1/2/4/8 threads, each on its own 64 KiB blocks, give 647/663/552/446 RW↔RO pairs per ms in aggregate. Windows serialises it inside a process.

guard_contention, BENCH_PAGES=4096 BENCH_DIRTY=1400, port #6 off → on:

Setup Askings (ms) Writers' time in writes per asking ms Per write (µs) Fault wait for the lock (port 6)
1 asker, 4 writers every 20 µs 5.4-8.0 → 6.0-7.5 2.04-2.26 → 1.95-2.05 22-28 → 19-25 0.08 µs
4 askers, 4 writers every 20 µs 5.2-5.4 → 7.7-8.0 2.74-2.89 → 2.30-2.35 49-56 → 29-30 0.07 µs
4 askers, 8 writers every 5 µs 9.9-11.7 → 18.0-20.4 7.11-7.22 → 6.84-7.07 48-53 → 31-32 0.07 µs

With the fault's VirtualProtect outside the exclusive lock, the fault's lock wait drops to almost nothing. The writers' total time per ms of asking stays the same, though, and the askings get 45-75 % slower once several threads ask at once. The wait moves into the kernel. Only an in-game A/B of VICTORY_PORT_GUARD=1 could show more. The bench keeps the question answerable on later changes to watch.rs.

Tests

  • cargo test -p victory-gpu --release --lib watch: 52 passed (2 ignored, this bench among them).
  • cargo xtask policy: ok.
  • ANYXBOXX_REQUIRE_RUNTIME=1 python -m pytest -n auto: 1983 passed.
  • No pytest was added, so the README count is unchanged.

AI use: written with Claude Code (Claude Opus 5.5), which also ran the measurements on my machine.

🤖 Generated with Claude Code

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5550101175 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/594). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-29T11:06:35Z. --- ## What A test-only, ignored benchmark in the VIctory copy: `watch::many_askers_tests::guard_contention` (`third_party/victory/crates/victory-gpu/src/watch.rs`). No behaviour change. The VIctory copy's view memory is asked about range by range, the way a submission's refreshes guard what they take. Meanwhile title threads write the same memory, so each first write to a guarded block is a fault that opens it. The bench prints how long the askings take, how much time the writers spend in their writes (the fault handler, including its wait for the guards' lock), how many writes were slow, and how many faults went under the shared lock. It runs once with renderer port #6 off and once with it on (`port_guard`). ``` cargo test -p victory-gpu --release --lib guard_contention -- --ignored --nocapture ``` Knobs: `BENCH_PAGES`, `BENCH_SPAN`, `BENCH_DIRTY`, `BENCH_WRITERS`, `BENCH_GAP_US`, `BENCH_ROUNDS`, `BENCH_ASKERS`, `BENCH_REST_US`. It ends with one `guard_contention:` key=value line per port, for scripts. ## Why The plan was to move the fault handler's own `VirtualProtect` out of the `GUARDS` lock: on the VIctory and fork bases, that lock showed 50-76 ms of title-thread wait. On this base, renderer port #6 (`VICTORY_PORT_GUARD=1`, off by default) already does that. Faults take `GUARDS` shared plus their block's claim (`BUSY`), so the fault's `VirtualProtect` runs outside the exclusive lock, and the `no_write_*_with_port_6` tests cover it. A simpler split for the default path is not safe without those claims. If the lock is released before `VirtualProtect`, a refresh can take it in between and guard the block. The fault's late `PAGE_READWRITE` then leaves the block writable while `protected` says it is clean, and writes are lost. So I did not open a fix PR. This bench is what decided it. ## Measured (CPU only, quiet laptop, 3 runs each, medians of 15 rounds) `VirtualProtect` alone: 1/2/4/8 threads, each on its own 64 KiB blocks, give 647/663/552/446 RW↔RO pairs per ms in aggregate. Windows serialises it inside a process. `guard_contention`, `BENCH_PAGES=4096 BENCH_DIRTY=1400`, port #6 off → on: | Setup | Askings (ms) | Writers' time in writes per asking ms | Per write (µs) | Fault wait for the lock (port 6) | |---|---|---|---|---| | 1 asker, 4 writers every 20 µs | 5.4-8.0 → 6.0-7.5 | 2.04-2.26 → 1.95-2.05 | 22-28 → 19-25 | 0.08 µs | | 4 askers, 4 writers every 20 µs | 5.2-5.4 → 7.7-8.0 | 2.74-2.89 → 2.30-2.35 | 49-56 → 29-30 | 0.07 µs | | 4 askers, 8 writers every 5 µs | 9.9-11.7 → 18.0-20.4 | 7.11-7.22 → 6.84-7.07 | 48-53 → 31-32 | 0.07 µs | With the fault's `VirtualProtect` outside the exclusive lock, the fault's lock wait drops to almost nothing. The writers' total time per ms of asking stays the same, though, and the askings get 45-75 % slower once several threads ask at once. The wait moves into the kernel. Only an in-game A/B of `VICTORY_PORT_GUARD=1` could show more. The bench keeps the question answerable on later changes to `watch.rs`. ## Tests - `cargo test -p victory-gpu --release --lib watch`: 52 passed (2 ignored, this bench among them). - `cargo xtask policy`: ok. - `ANYXBOXX_REQUIRE_RUNTIME=1 python -m pytest -n auto`: 1983 passed. - No pytest was added, so the README count is unchanged. _AI use: written with Claude Code (Claude Opus 5.5), which also ran the measurements on my machine._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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/594:upstream-pr/594'
git switch 'upstream-pr/594'

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