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

Closed
desplmfao wants to merge 3 commits from upstream-pr/515 into main
desplmfao commented 2026-11-24 04:22:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by desplmfao on 2026-11-24T04:22:22Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6574557180 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/515). Originally posted by [desplmfao](https://github.com/desplmfao) on 2026-11-24T04:22:22Z. --- **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)
Raxstreams commented 2026-11-24 06:55:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Raxstreams on 2026-11-24T05:00:33Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122538040 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/515#issuecomment-7122538040). Originally posted by [Raxstreams](https://github.com/Raxstreams) on 2026-11-24T05:00:33Z. --- 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.
desplmfao commented 2026-11-24 06:56:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by desplmfao on 2026-11-24T05:55:39Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123406463 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/515#issuecomment-7123406463). Originally posted by [desplmfao](https://github.com/desplmfao) on 2026-11-24T05:55:39Z. --- 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 commented 2026-11-24 06:56:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by desplmfao on 2026-11-24T05:47:44Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123281216 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/515#issuecomment-7123281216). Originally posted by [desplmfao](https://github.com/desplmfao) on 2026-11-24T05:47:44Z. --- 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).
mercuryVM commented 2026-11-24 12:03:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T12:01:52Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6038813849 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/515#pullrequestreview-6038813849). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T12:01:52Z. **Review:** APPROVED --- 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 commented 2026-11-24 19:04:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AgneyaK on 2026-11-24T18:24:51Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140610737 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/515#issuecomment-7140610737). Originally posted by [AgneyaK](https://github.com/AgneyaK) on 2026-11-24T18:24:51Z. --- 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.
upstream-sync closed this pull request 2026-11-24 23:04:14 +00:00

Pull request closed

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!515
No description provided.