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

Closed
github-bot wants to merge 3 commits from gh-pr/515/head into gh-pr/515/base
Owner

@⁠desplmfao on GitHub · 2026-11-24 04:22 UTC · original
desplmfao:fix/write-watch-per-allocation → main · +187 −3 · 1 files · 3 commits · closed without merging

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.

  • #464: the same kind of waste for physical-page views (unmapped guarded blocks re-copied every submission); no overlap in code, they merge cleanly.
  • #514: makes #464 skip a view block only when the uncommitted region covers all of it, and recheck it. The second commit here asks VirtualQuery about every region of the range, so one committed page anywhere in it is enough to ask the kernel normally; a_range_never_committed_has_no_writes commits and writes after the first question and checks the write is found.
  • #436, #462: the other copy changes measured together here.
  • #185: its direct SRV refreshes ask about the many ranges that hit this.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

**@⁠desplmfao** on GitHub · 2026-11-24 04:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/515) `desplmfao:fix/write-watch-per-allocation` → `main` · +187 −3 · 1 files · 3 commits · closed without merging **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 - #464: the same kind of waste for physical-page views (unmapped guarded blocks re-copied every submission); no overlap in code, they merge cleanly. - #514: makes #464 skip a view block only when the uncommitted region covers all of it, and recheck it. The second commit here asks `VirtualQuery` about every region of the range, so one committed page anywhere in it is enough to ask the kernel normally; `a_range_never_committed_has_no_writes` commits and writes after the first question and checks the write is found. - #436, #462: the other copy changes measured together here. - #185: its direct SRV refreshes ask about the many ranges that hit this. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:06:47 +00:00
github-bot closed this pull request 2026-11-26 04:06:47 +00:00
Author
Owner

@⁠Raxstreams on GitHub · 2026-11-24 05:00 UTC · original

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.

**@⁠Raxstreams** on GitHub · 2026-11-24 05:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/515#issuecomment-7122538040) 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.
Author
Owner

@⁠desplmfao on GitHub · 2026-11-24 05:55 UTC · original

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).

**@⁠desplmfao** on GitHub · 2026-11-24 05:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/515#issuecomment-7123406463) 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).
Author
Owner

@⁠desplmfao on GitHub · 2026-11-24 05:47 UTC · original · edited 2026-11-24 05:58 UTC

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** on GitHub · 2026-11-24 05:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/515#issuecomment-7123281216) · edited 2026-11-24 05:58 UTC 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).
Author
Owner

@⁠AgneyaK on GitHub · 2026-11-24 18:24 UTC · original

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.

**@⁠AgneyaK** on GitHub · 2026-11-24 18:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/515#issuecomment-7140610737) 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.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!515
No description provided.