Skip to content

GPU: takes make pages read-only outside the guard lock (VICTORY_GUARD_OUTSIDE, opt-in, draft for A/B) - #3516

Draft
KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:perf/guard-outside
Draft

KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:perf/guard-outside

Conversation

@KainanP

@KainanP KainanP commented Nov 27, 2026

Copy link
Copy Markdown

Refs #3425.

What this does

Tracked::take and Tracked::taken hold watch::GUARDS while protect_noting calls VirtualProtect on every run they take. The write-fault handler needs the same lock. #3425 measures title threads waiting about 52 ms/frame in the handler behind it (RTX 5090/Wine), and borosf's Arc B580 numbers there show 76 ms/frame. I found no open PR that moves the protection change itself out of the lock. #3343 and #2849 batch the unprotect/protect calls, #3035 moves the saved-bytes read, and #3455 turns GUARDS into an RwLock (the protect still runs under the write lock).

With VICTORY_GUARD_OUTSIDE=1, a take notes its runs as guarded under the lock (Guards::arming). It changes their protection after releasing the lock (Arming::finish) and before its caller copies them. Off by default: without the variable the code path is the same as before, apart from an is_empty() check.

This ports the idea from our fork's fxceless/victory-fork#1315. The code there doesn't map onto this one (the fork guards 64 KiB blocks with a journal, while VIctory has busy and alias_of), so this is a rewrite, not a cherry-pick.

Why the answers stay the same

  • Concurrent takes wait. A take that reaches a page another take is still arming waits on a condvar (ARMED). Otherwise it could count the page as guarded, copy it, and miss a write that lands before the guard is in place.
  • Pages with another mapping stay under the lock. Runs that include a page shown at another mapping (alias_of) are guarded under the lock along with their other mappings, as before. title_memory_aliased waits for armed pages before linking them.
  • Pages opened while armed end up writable. If a fault, writing or a remap opens a page while it is being armed, the page is marked changed as before and noted as disturbed. finish then makes it writable again. A stale fault on such a page (the thread faulted before the opening and retries after finish's change) is let through by the handler.
  • Pages that can't be made read-only end as they did under the lock. They are taken as zeros if nothing is mapped. Otherwise they stay changed and keep the bytes their tracker had saved.

Tests (CPU only, watch.rs)

  • refreshes_under_writes_lose_nothing: five copies (two overlapping copies of a write-watched pool, plus copies of both mappings of twin views) are refreshed together on separate threads while 4 writers write. It checks that every word written before a refresh is in every copy covering it, and that a quiet refresh afterwards leaves each copy equal to title memory. A test-only spin before the out-of-lock protect widens the window a preempted take would leave.
    • I checked that it fails if the condvar wait is removed (5/5 runs, by round 2) or if the alias_of check is removed (5/5 runs, by round 7, with "copy 2 lost a write").
    • Without the handler's disturbed branch the process crashes with an access violation.
  • armed_pages_opened_or_unmapped_end_as_under_the_lock
  • guard_contention (#[ignore], benchmark): cargo test -p victory-gpu --release --lib guard_contention -- --ignored --nocapture, with sizes set by BENCH_* variables (see its doc).
  • The full victory-gpu --lib suite also passes with VICTORY_GUARD_OUTSIDE=1: WARP with --features diagnostics, 248/248. On an RTX 4060 Laptop, the only failures are the two that also fail on main there (the 1M view-heap cap: dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views), plus a timing flake in descriptor::host that shows up in both modes.
  • clippy -D warnings passes both default and --all-features, and xtask policy passes.

Measurements (honest version: no win in the synthetic)

guard_contention runs on a 256 MB write-watched pool split into 32-page copies. Before each refresh 1,400 pages are written, then writers keep writing random words every 20 µs during the refresh. Off and on alternate within one process, 31 rounds, medians. Machine: 24 threads, about 11–40 % foreign load (another build running).

config refresh off → on writers' time in their writes off → on writers' wait for GUARDS off → on
1 writer 21.5 → 23.8 ms 7.48 → 7.37 ms 3.76 → 0.05 ms
4 writers 34.0 → 40.4 ms 39.9 → 44.9 ms 25.5 → 19.4 ms
4 writers, refresh on 4 threads 10.3 → 13.5 ms 32.9 → 38.3 ms 30.3 → 26.8 ms
4 writers, twin views (all runs stay under the lock: A/A control) 32.2 → 32.6 ms 59.8 → 59.1 ms 41.8 → 41.0 ms
  • The lock wait behind takes goes away. With one writer, the guard-lock wait drops from 3.76 to 0.05 ms.
  • But the faulting write doesn't get faster. A faulting write still costs about 20–30 µs, and the total time writers spend in writes stays the same. The time seems to move into the kernel's own protection changes, which most likely serialize there: the take's read-only change and the fault's read-write change both modify this process's mappings.
  • What's left with 4 writers is mostly writers waiting on each other's handlers, which call VirtualProtect (open) under the lock. This PR doesn't touch that path.
  • Refresh gets slower (+10–30 %), because each take now takes the lock twice and waits behind handlers the second time.

This matches what we saw in the fork. There, on a 5080, guard waits went from 23 to 0.5 ms with no change in frame time (fxceless/victory-fork#1379). The synthetic doesn't model the title's real write pattern, though, and #3425's profile puts about 49 ms/frame of GUARDS hold time on the takes. So the only real test is in-game, which is why this is a draft and off by default.

A/B request. @neonbyte1 (5090/Wine) and @borosf (Arc B580): could you run the prison-yard scene with and without VICTORY_GUARD_OUTSIDE=1, with the diagnostics build so held_times, GuardWaitUs, HandlerUs and ProtectUs are in the log? Frame time is what decides it. If it doesn't move, I'll close this.

AI use: written with Claude Code (Claude Opus 5.5): port design, implementation, tests, mutation checks and benchmark runs, all run on KainanP's machine.

🤖 Generated with Claude Code

…_OUTSIDE, off)

`Tracked::take`/`taken` hold `watch::GUARDS` while `protect_noting` runs
VirtualProtect over every run they take, and the title's write faults
queue behind that lock (Marvisak#3425). With VICTORY_GUARD_OUTSIDE=1 a take only
notes its runs guarded under the lock (`Guards::arming`) and changes their
protection after letting go of it (`Arming::finish`), before its caller
copies them. What a take answers stays the same:

- another take of a page being armed waits for it (condvar `ARMED`), or it
  could copy before the guard stands and miss a write landing after;
- runs with pages shown at another mapping (`alias_of`) stay under the
  lock, guarded with their other mappings; `title_memory_aliased` waits for
  armed pages before linking them;
- a page opened while armed (fault, `writing`, remap) is noted disturbed
  and made writable again once the take is done; the fault handler lets a
  write to such a page through;
- a page that cannot be made read-only ends as under the lock: taken as
  zeros if nothing is mapped, else still changed, with its saved bytes.

Off by default. Tests: `refreshes_under_writes_lose_nothing` (fails if the
wait or the alias check is removed), `armed_pages_opened_or_unmapped_end_as
_under_the_lock`, and the benchmark `guard_contention` (#[ignore]).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@neonbyte1

neonbyte1 commented Nov 27, 2026 •

Copy link
Copy Markdown

@KainanP I'll do once my agents are done, current queue ends in ~3 hours from now. Since the game needs a bunch of VRAM, I can't run the game twice for now :/

A bit off-topic but I've spawned 4 other agents for performance analysis regarding the frametime. We went from ~900ms to ~170ms this night, so chasing this is currently my focus. As I said, once the queue ends I'll start the A/B run.

@borosf

borosf commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

I took the Arc B580 half of the A/B. No verdict yet, and I would not want you to read a number from what I have.

What works now: I can drive the title into the prison yard unattended (a click on the PLAYER label plus Enter; its first screen is mouse-driven, which is what defeated my earlier menu-only captures). Tree: 63bcca4 + #2958 (without it the yard renders as a pink wash at ~4.7 s/frame, which would swamp a ~50 ms CPU-side effect) + your PR at a73feeb, --profile diag --features diagnostics, both arms the same binary toggled by VICTORY_GUARD_OUTSIDE=1, 240 s per arm, interleaved off/on/off/on.

What happened: three of four runs ended with the device removed (GetDeviceRemovedReason, count 4) partway through the yard — the pre-existing Arc failure tracked in #2671, unrelated to your change. Only one run sustained the yard (26 world frames, on), so I have one arm and no matched counterpart:

arm world frames frame GPU guards handler view-write wait protecting
off (3 runs) 1–3 — — 4.7–5.1 ms 0.0–0.7 ms 0.1–1.4 ms
on (1 run, 26 frames) 26 1,264 ms 879 ms 64.2 ms 111.1 ms 19.8 ms

The off rows are menu-interval lines, not yard lines — the runs died before steady state — so comparing them with the on row would be exactly the kind of false reading I have been trying to avoid in this repo. What I can say is that the one sustained on run sits in the same band as the Arc numbers in #3425 (76 ms handler), i.e. your change did not visibly collapse anything, but that is not a measurement of your change.

Why I am not just re-running until it works: each attempt costs 4 minutes and one in three dies on #2671's fault, so getting a matched pair is luck, and I have already burned several. Two options, your call: (a) I keep grinding until I land a paired off/on where both survive — honest but slow; (b) @neonbyte1's 5090/Wine setup is stable and already queued for this, and I contribute the harness (press-on-prompt.ps1 + run-world.sh) rather than the numbers. If you would rather have (a), say so and I will run it to completion before reporting again.

One thing from the same session that is worth your attention, since it is adjacent: your contention finding on #3356 (sync_heaps 9.6 → 13.4 ms, write-watched pools) reads correctly to me — VirtualQuery is exactly the cost my own code comment warns about in the title's big pools, and my timing test used a plain allocation, which has none of that structure. I am treating that as a probable regression in my PR, not a measurement dispute.

Synthetic tests only in the tree; the runs are my own dump. Machine: Windows 11 x64, Ryzen 5 5600, 32 GB, Arc B580 12 GB.

@KainanP

KainanP commented Nov 27, 2026

Copy link
Copy Markdown
Author

Thanks @borosf, and agreed: with the off arm dying before the yard there's no matched pair, so no number should be read from it. The one thing I'd take from your on-run is that the guards handler (64 ms) and view-write wait (111 ms) are still large with the switch on, which fits the synthetic result in the body: moving the take's re-protect out of the lock doesn't help while title writes queue behind each other's fault handler (still under the lock). That's why this stays a draft. @neonbyte1 no rush — a run on a card without the #2671 device loss would settle it.

AI use: Claude Code (Claude Opus 5.5).

@kilolimaVI

Copy link
Copy Markdown

Laptop numbers, head a73feeb: Windows 11, Ryzen 7 8845HS (8C/16T), toolchain 1.98.1.

Stability: refreshes_under_writes_lose_nothing and armed_pages_opened_or_unmapped_end_as_under_the_lock, 20 runs in a row: 0/20 failures.

guard_contention (--release --ignored --nocapture, defaults: 4096 pages, copies of 32, 1400 dirty, 4 writers every 20 us, 11 rounds, medians):

VICTORY_GUARD_OUTSIDE=0 =1
writers waiting for the guard lock 31.54 ms 22.88 ms
writers in their writes 47.83 ms 50.16 ms
writes over 5 us (mean) 1832 (25.4 us) 2252 (21.3 us)
longest write 192 us 162 us
refresh 28.13 ms (197.5 MB) 33.75 ms (196.5 MB)
refresh waited for the lock 0.26 ms 4.90 ms

9460 runs made read-only outside the lock, 0 kept under it. So here the lock wait drops by about a quarter and the worst writes get shorter, but total time in writes is slightly up, there are more slow writes, and the refresh is ~20% slower (it now waits ~5 ms). On this 8-core laptop the net effect looks neutral to slightly negative; one benchmark run, so treat it as one data point.

AI-assisted (Claude).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants