Skip to content

GPU: ask for written pages allocation by allocation when a range spans several - #515

Closed
desplmfao wants to merge 3 commits into
Marvisak:mainfrom
desplmfao:fix/write-watch-per-allocation
Closed

desplmfao wants to merge 3 commits into
Marvisak:mainfrom
desplmfao:fix/write-watch-per-allocation

Conversation

@desplmfao

@desplmfao desplmfao commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Status (2026-11-24): not needed if #185 lands as it is now. #185 was reworked after this PR was opened (481fb1a, 8bec415, 5aca0db; 481fb1a refers to "the wholly reserved case examined in #515"): it groups write-watch questions by reservation up front and handles what this PR handles; the two conflict in watch::writes. Measured (world frames, median): old #185 + this and #436, #462, #600 4.3 s per frame, new #185 alone 2.9 s. Without #185 this still helps main's ordinary refreshes. (Correction: the third commit fixes a case this PR's own first commit introduced, not one in main, whose single question resets nothing when it fails.) Details in the comments below; the rest of this description is as written before.

Problem

watch::writes asks GetWriteWatch about a whole range at once. The kernel refuses a range that spans more than one allocation, and the runtime reserves title memory allocation by allocation, so a buffer over several of them was taken as not write-watched: writes returned None and its range was copied whole on every refresh, although every part of it is watched.

In the world (with #185's direct SRV refreshes), a diagnostic counting the failed questions per buffer showed thousands per frame, on ranges of 133 KB to 3.9 MB on average, starting in allocations that are only partly committed (for example a 64 KiB allocation at 0x756000000 with its last 56 KiB reserved). They came to 1.6–5 GB of whole copies per frame.

Change

crates/victory-gpu/src/watch.rs only. When the question for the whole range fails, by_allocation asks it again allocation by allocation (VirtualQuery for the boundaries). Parts never committed cannot have been written and are not asked about. If a committed part is not write-watched, nothing changes: writes falls back to guarding or None as before. A range inside one allocation is not asked again.

Measured

RTX 5080, Linux (proton-cachyos, vkd3d-proton 3.2.0), Series X 1.0.2.1, diagnostics build with #185, #436, #462, #440, #493 (+#336), #259, #177, #151, Enter into story mode at about 35 s. Before: the same stack without this change and without #464; after: with this change and #464 (unmapped guarded view blocks, the same kind of waste for view memory), so the two effects are not separated here:

per world frame before with this and #464
failed write-watch questions 19,000–31,000 5,000–6,800
bytes copied whole (not watched) 2.7–5 GB 0.58–0.83 GB
copies in total 2–6 GB 0.5–0.85 GB
frame time 2.4–4.2 s (a 30 s window) 0.6–8 s over 10 min, mostly 3–5 s

Frame time does not follow the bytes: what is left of a frame is mostly CPU time per refreshed range (87,000–105,000 direct SRV ranges took 2.7–6.4 s), not bytes copied. Part of that may be this change asking VirtualQuery for allocation boundaries again on each refused range; caching them is a possible follow-up.

Of the questions still failing at first, 98 % were ranges inside one allocation reserved but not committed at all; the second commit answers those with no writes (nothing can have written there; once committed, the range is asked about normally).

Both commits, measured (same stack, with #514 in place of #464; #514 includes it), 7.5 min, about 6 min in the world, no GPU hang:

per world frame first commit only (+#464) both commits (+#514)
failed write-watch questions 141–6,800 2–120
bytes copied whole (not watched) 0.1–0.8 GB 1–81 MB
frame time, median over the world (middle half) 3.7 s (2.2–5.2 s) 3.0 s (1.0–5.0 s)

What remains of a frame is mostly the number of questions, not bytes: in late frames copies were 36–99 MB and still took 4.5–7.6 s. The whole-range question fails first for almost every refreshed range (they span several allocations), and by_allocation then took 0.4–0.9 s per frame (59,000–141,000 calls, 93,000–279,000 VirtualQuery calls). Remembering which ranges span several allocations, and their boundaries, is the next step.

Third commit: a fix

by_allocation asks with WRITE_WATCH_FLAG_RESET. When an allocation later in the range is committed but not write-watched, it returned None and dropped the pages the earlier allocations had reported, which the kernel had already cleared. The caller copied its own range whole, so it saw them, but the journal never did, so another host copy over the same pages would not copy those writes. The third commit hands those pages back and records them before falling back; writes_found_before_a_refusal_are_kept fails without it (the write is gone: [] instead of the page) and passes with it.

Tests

New a_range_never_committed_has_no_writes: a reservation not committed has no writes; committed and written afterwards, the write is found. New a_range_over_several_watched_allocations_is_watched: two adjacent write-watched allocations, written in each, asked about as one range. It fails on main (writes returns None) and passes with the change. Both test allocations are kept rather than freed, since the journal is shared and a later test given the same addresses would find their pages written. The watch tests pass, also merged with #464 (its unmapped_blocks_are_reported_once_until_mapped included). cargo clippy reports nothing new in watch.rs.

Related

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…s several

GetWriteWatch refuses a range that spans more than one allocation, and the
runtime reserves title memory allocation by allocation, so a buffer over
several of them was taken as not write-watched and copied whole on every
refresh, even where every part of it is watched.

When the question for the whole range fails, ask it again per allocation.
Parts never committed cannot have been written. If a committed part is not
write-watched, nothing changes: the range falls back as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Of the write-watch questions still refused in the world, almost all were
ranges inside one allocation reserved but not committed at all, copied
whole (as zeros) on every refresh. Nothing can have written there, so
answer them with no writes; once the title commits the memory, the range
is asked about normally.

Also keep the test allocations instead of freeing them: the journal keeps
their pages, and a later test given the same addresses found them written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
aycow added a commit to aycow/VIctory that referenced this pull request Nov 24, 2026
Plan only requested pages outside pending GPU writes. Use a verified
reservation extent for dense groups and share a fresh observation within
each group. Query journal chunks against the oldest valid shadow bytes.

Adapt the clean guarded-memory path from PR Marvisak#393 and the guarded view
lookup cache from PR Marvisak#490. Remap and allocation notifications invalidate
their proof. Preserve exact byte coverage and journal observations across
mixed allocations, including the wholly reserved case examined in Marvisak#515.

Co-authored-by: KaiserBloo <kaiserbloo@protonmail.com>
Co-authored-by: wrothgaril <211408787+wrothgaril@users.noreply.github.com>
@Raxstreams

Copy link
Copy Markdown

Tested the allocation-by-allocation query on Windows alongside #514. All nine watch tests passed. This included the reserved-then-committed range and the range spanning two write-watched allocations. This was an integrated run; I did not measure a standalone performance delta.

by_allocation asks allocation by allocation with WRITE_WATCH_FLAG_RESET.
When a later allocation in the range is committed but not write-watched,
it returned None and dropped the pages the earlier allocations had
reported, although the kernel had cleared them. The caller then copied
its own range whole, but the journal never learned of those writes, so
another host copy over the same pages would not copy them again.

Hand those pages back and record them in the journal before falling back.

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

desplmfao commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Update: #185 has since been reworked (commits 481fb1a, 8bec415, 5aca0db, after this PR was opened; 481fb1a refers to "the wholly reserved case examined in #515"). It now groups write-watch questions by reservation up front and handles ranges over several reservations, never-committed memory and failed private watches itself, so it covers what this PR does, and the two conflict in watch::writes.

Measured on the same machine and input (RTX 5080, Linux / proton-cachyos, world frames, median over the same window after world entry, with #493 in all three): old #185 6.1 s per frame; old #185 + #436, #462, #515, #600 4.3 s; new #185 alone 2.9 s, with 0.07 GB copied whole per frame against 1.3 GB.

If #185 lands, this PR is not needed. Without #185 it still helps main's ordinary refreshes of buffers spanning several allocations, and its third commit fixes a case in which writes found before a refused allocation were lost from the journal. Up to you whether that is worth keeping; I'm fine closing it in favour of #185.

AI-assisted (Claude Code).

@desplmfao

Copy link
Copy Markdown
Contributor Author

Correction to my comment above: the third commit does not fix a bug in main. In main, writes asks once over the whole range, and a refused question resets nothing, so no writes are lost there. The lost writes could only happen with this PR's own first commit (asking allocation by allocation with reset), which the third commit then fixes. #185's current version avoids the problem by design (it tests the span without resetting first).

AI-assisted (Claude Code).

aycow added a commit to aycow/VIctory that referenced this pull request Nov 24, 2026
Require the second query to identify the same reservation and cover the fallback classification directly in the synthetic test. Credit desp for the per-allocation and never-committed handling in Marvisak#515 that informed 481fb1a, and for the Wine compatibility implementation in 9e94c21.

Co-authored-by: desp <me@desp.lol>

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested d9cb908 on Windows 11, RTX 5070, MSVC: cargo test -p victory-gpu -- --test-threads=1 46 passed, including a_range_over_several_watched_allocations_is_watched. Clippy's 1 warning is the existing one in host.rs.

  • by_allocation walks VirtualQuery regions grouped by AllocationBase and asks GetWriteWatch once per committed allocation. A wholly reserved range correctly reports no writes, because nothing uncommitted can have been written.
  • The failure path matters most and is handled: the earlier allocations' GetWriteWatch calls already reset their bits in the kernel, so those pages go into lost and are stamped into the journal instead of being dropped.
  • As your status note says, this and #185's reworked watch::writes overlap. On mercuryVM/VIctory main (which doesn't have #185) this helps on its own. If #185 lands upstream, this should be closed rather than rebased.

AI-assisted (Claude Code).

@AgneyaK

AgneyaK commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Test report on a 6 GB / 16 GB laptop. On this machine this PR made write-watch time worse: 475 → 1,605 ms per world frame, alone on the base below.

Setup: RTX 3050 6 GB Laptop (NVIDIA, driver 32.0.16.2124), i7-13700H, 16 GB RAM, Windows 11 26300, diag build. Title 1.0.2.1, Series X profile, the story load. Base tree: main ffe310e + the PRs listed in #1232 + #431. Each PR was merged onto that base, and each row is one run of 300 s from launch. "World frame" is the average of the fps, ms/frame (…) lines under 10 fps with 20+ draws. Write-watch numbers are the last 8 copystat lines.

tree world frame preparing copies write-watch askings / frame their time / frame
base (+ #431) 3,966 ms 1,988 447 867 475 ms
+ #393 3,347 ms 1,579 100 585 13 ms
+ #440 + #393 3,221 ms 1,553 67 572 14 ms
+ #515 5,256 ms 3,228 1,223 884 1,605 ms
+ #440 + #515 + #393 4,090 ms 2,243 201 550 29 ms

My guess, not verified: the expensive part here is asking inside one huge allocation. #776 measured a 2.2 GB allocation at about 2.2 ms per asking. Splitting a range into per-allocation askings then multiplies the expensive ones. #393 avoids most askings, and with it the time stays low either way (29 ms with all three, 14 ms without this PR). One run per row, spread about ±10%.

AI use: Claude Code (Claude Opus 5.5) built the trees, ran the title on my machine and drafted this report; results are from the runs described.

edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…itives, capture), verified together

Merges #124 #119 #515 #934 #484 #143 #1191 #278 #553 #153 #270 #1158 #1204
#1220 #488 #1146 #631 onto main (ffe310e), each head unchanged, no conflicts,
no changes of mine. cargo clippy --workspace --all-targets --locked -- -D
warnings, with and without --all-features, is clean and cargo test
--workspace --locked passes (372 passed, 8 ignored; main has 342 passed,
6 ignored).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…ures, device, DXGI, objects, primitives, capture)

Kept from our side:
- watch.rs: the per-submission journal (Journal.round, new_round, covered,
  cover, writes_once) and the WatchKernelUs/WatchGuardUs/WatchPages
  counters; the guard timing now wraps the PR's by-allocation fallback,
  and the per-allocation kernel askings (#515) count into WatchKernelUs
  and WatchPages too. Both sets of watch tests kept.
- texture.rs: our version (CommittedRegions, RUNTIME_WRITES and the
  last.checked skip, changed-subresource detiling, mip drop, widened
  packed16) with the PR's footprint fix on top: upload footprints end at
  the last row's data bytes (GetCopyableFootprints row sizes), detiling
  goes through detile_plane, title_written_flags, overflow-checked
  committed() and refresh range, and the PR's tests. #143 was already
  merged; the SCRATCH -> UNWATCHED_SCRATCH rename is left out (no #472
  here).
- copystat.rs: our counters and lines, plus the PR's process memory line.
- primitive.rs: our position+color fast path, plus the PR's zero and
  overflowing stride guards in the generic path.
- #124, #119, device.rs and object.rs changes were already in our tree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
The tests #1418 brought (#515's per-allocation askings) reserve address
space, release it and allocate again at fixed addresses, while ours free
views and blocks and remap: run in parallel they took each other's
addresses (and their journal entries), and a remapping reset the guards
another test had just made, so 1 run in ~5 failed. The tests that take
ranges or remap now run one at a time, and the new ones forget the
journal's pages of their range first, as written_pages_... already did.
No assertion changed; the journal and per-allocation code are as merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
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.

5 participants