Skip to content

Memory: size the coherent window by the GPU heaps alive at once - #3695

Open
Prime1997 wants to merge 2 commits into
xcl-dev:mainfrom
Prime1997:contrib/up-coherent-window-size
Open

Prime1997 wants to merge 2 commits into
xcl-dev:mainfrom
Prime1997:contrib/up-coherent-window-size

Conversation

@Prime1997

Copy link
Copy Markdown
Collaborator

Requested by Prime

The coherent window (committed, GPU-visible system memory that stays resident) is sized from the title's GPU heaps stored by its last run, plus XCL's 512 MB. kernelx never took a heap off that count when the title released it, so the stored size was the sum of every heap the title ever asked for. A title that makes and frees heaps (streaming, level changes) grew it run after run, and it was never lowered. An oversized window is resident RAM for nothing (estimated 0.3-1.5 GB); RAM was the limit in a run on a 32 GB machine (30.9 of 31.9 GB in use).

What is in it

  • Memory: size the coherent window by the GPU heaps alive at once: kernelx keeps the heaps counted for the window alive by address, in the window or in plain memory, and takes a heap off when it is released (VirtualFree of a unified-memory block or of plain memory). uma stores the most alive at once, the peak of note_coherent_need (hle/kernelx.rs, gpu/uma.rs).
  • Memory: count the coherent window's holes in its stored size: the stored size includes the holes freed heaps leave in the window, so the next run's window still fits the same heaps (gpu/uma.rs).

Tested

  • cargo check -p xcl --lib --tests --target x86_64-pc-windows-gnu on Linux: builds, no new warnings.
  • Run in Prime's batch builds since 30 Nov on an RTX 3060 Ti 8 GB, 12 threads, 32 GB, Windows 11, release, with a retail title and XCL_COHERENT_MB=512: the title reaches its world, and the window no longer grows between runs.

Regression run

  • No smoke blocks: this branch was not built and run on its own from main (Prime's call for our PRs; they merge after community testing).

Not tested

  • The full test suite on this exact branch, AMD GPUs, and a smoke comparison against main.

AI use

  • Claude Code wrote the code, the commit messages and this description; Prime ran the builds.

🤖 Generated with Claude Code

Prime1997 and others added 2 commits December 2, 2026 15:09
The coherent window is committed, GPU-visible system memory that stays
resident, made as large as the stored size of the title's GPU heaps from
its last run plus XCL's 512 MB. kernelx's ASKED was never decremented,
so the stored size was the sum of every heap the title ever asked for:
a title that makes and frees heaps (streaming, level changes) grew it run
after run, and with every heap in the window (the default route) it was
never lowered, a run that stayed in the menu being the reason. RAM was
the limit in the own10 run (1f5d4d5: 30.9 of 31.9 GB in use); an
oversized window is resident RAM for nothing (estimated 0.3-1.5 GB).

kernelx now keeps the heaps counted for the window alive by address (in
the window or, without room, in plain memory) and takes a heap off on
its release (VirtualFree of a unified-memory block or of plain memory);
uma stores the most alive at once (note_coherent_need's peak).
settled_need lowers the size stored for every heap in the window only
after a long run (600 s), and only when the run asked for at least the
stored size in all yet had less alive at once (it freed heaps), by half
at most and never under its largest heap; a menu-only run, which asked
for less, still leaves it. The sparse routes settle at 60 s as before.

Not done: an automatic cap at RAM/6 on 32 GB machines. 31.9 GB / 6 is
5.3 GB, within 4% of a measured retail title's real need (4582 MB of
heaps + 512 + 96 MB), and a cap under the real need sends heaps to plain
memory (no image); with the stored size now the peak alive, the cap would
only add that risk. The launcher sets XCL_COHERENT_MB=512, so the own10
configuration is unaffected by either.

Off: XCL_COHERENT_SHRINK=0 (the sum of all asked, never lowered with
every heap in the window); XCL_COHERENT_MB still sets the size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review fix for 86c5e52 (size the coherent window by the GPU heaps alive
at once). The stored size became the most heap bytes alive at once, but
the window is a first-fit allocator with no compaction (and 2 MB alignment
for large-page heaps): after heaps are freed and made again in other sizes
the same placement needs more than the bytes alive. Example: A (1 GB) and
B (1 GB) made, A freed, C (1.5 GB) made: 2.5 GB alive, but C needs 3.5 GB
of window. After a 600 s run lowered the stored size to 2.5 GB, the next
run's C fit in neither A's 1 GB hole nor the tail, went to plain memory,
and its draws had no image. It never healed: the fallback heap counts as
alive, so the peak alive equalled the stored size and note_coherent_need
never grew it (the old cumulative ASKED did).

With XCL_COHERENT_SHRINK on, each GPU heap the title makes now also notes
the bytes alive plus the window's holes: the free room below the highest
block in each committed window (Uma::holes, the pure holes() with a unit
test). After a heap placed in the window that is the window the same
placement needs; after one that did not fit it is more than the window
held (the fallback heap counts on top of the holes), so the next run's
window grows to take it, as ASKED did. The 600 s settle lowers towards
NOTED, the most of these, never under the run's fragmented need. Holes
XCL's own frees leave count too (conservative, at most its share of the
512 MB it keeps). The measured own10 setup (XCL_COHERENT_MB=512, no title
heaps) is not affected.

Off: XCL_COHERENT_SHRINK=0 (all asked for, never lowered, as before own11).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Prime1997 Prime1997 self-assigned this Dec 2, 2026
@github-actions github-actions Bot added area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) needs-pr-check Failing the PR description check (CONTRIBUTING.md) labels Dec 2, 2026

@gut0to gut0to 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.

AI-assisted review (model: GPT-6). I checked the coherent allocation rounding against the shrink accounting.

crate::gpu::uma::note_coherent_heap(size);
// (counted alive until it is released, `coherent_heap_released`; one Windows refuses below
// is taken off again)
let live = COHERENT_LIVE.fetch_add(size, Ordering::Relaxed) + size;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could this counter use the allocated footprint rather than the requested byte count? Uma::alloc_where rounds each request up to GRAIN before recording it in used (gpu/uma.rs:846), but COHERENT_LIVE adds the unrounded size here and subtracts that same amount on release. Two simultaneously live 64 KiB + 1 byte heaps therefore occupy 256 KiB while contributing only about 128 KiB to the high-water value used by the 10-minute shrink; holes() cannot account for this padding because it is part of used. That can persist a window too small for the same live heaps on the next run. Could the live accounting track the rounded UMA block length and include a regression test with multiple unaligned live heaps? AI-assisted review (model: GPT-6); I checked the allocation rounding against the shrink accounting in this diff.

@Edgaras0404

Copy link
Copy Markdown

test report (not a merge-check)
base: df0cfef (the last main before #3655; first parent of d71578f)
head: e665b42 (tested as cherry-picked onto df0cfef; git diff base..tree is the PR's own diff, 182+/24-)
merge: clean (also clean on current main 5eaadb5, which builds; that base's suite is the #3694 breakage, so the functional run is on df0cfef)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> base df0cfef: lib aborted (exit 0x80004005) after 1,380 ok / 6 failed / 25 ignored, no test result:; failing: gpu::dxil::compact_tests::gpu_compact_form_with_micro_blocks_and_literal_table_runs_as_the_plain_form, gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu, gpu::recomp::tests::gpu_f32_compares_with_nan, gpu::recomp::tests::gpu_float_compares_random, hle::d3d12x::alloc_pool::tests::a_reset_that_puts_the_allocator_aside_keeps_what_the_gpu_still_reads, hle::d3d12x::list_x::tests::a_put_off_call_whose_close_fails_is_not_submitted; integration 147 passed / 2 failed / 20 ignored (gpu_two_pass_cache_e2e 0/1 twice, lines 472 and 584); SKIPPED: heap_window_out_of_memory_falls_back_to_the_switch_form: no heap windows. Merge: lib aborted the same way after 1,384 ok / 4 failed / 25 ignored; the 4 are the compact-form test and the three f32 tests, all on base; the base's alloc_pool and list_x failures did not recur in either merge (list_x is the known parallel flake; alloc_pool was not run alone); integration 147/2/20 with the same two-pass failure; the three tests this PR adds/changes pass: gpu::uma::tests::holes_are_the_free_room_below_each_windows_highest_block, gpu::uma::tests::a_long_run_that_freed_heaps_lowers_the_stored_need_of_every_heap, gpu::uma::tests::only_the_sparse_heaps_settings_lower_the_stored_coherent_need (3 passed, 0 failed). Current main 5eaadb5: merges clean, build ok, but its suite is the #3694 breakage (lib aborted after 1,153 ok / 278 failed / 26 ignored; gpu_compute_e2e removed the device 0x887a0020) and was not used for functional evidence. rustc/cargo 1.100.1, debug.
review: 1) The change itself is untested: COHERENT_HEAPS/COHERENT_LIVE and coherent_heap_released have no test, and the new tests cover only holes() and settled_need(), so the claimed fix (a released heap stops counting) would regress silently. 2) XCL_COHERENT_SHRINK is a new user-facing switch, on by default, and is not in README.md; the default behaviour change (after 600 s a run may halve the stored coherent window down to its peak) should be documented. 3) The 600 s rule is a heuristic: a run that reaches 10 min but later loads a heavier level can leave the next run's window too small, and its heaps go to plain memory until the window grows back; the 60 s title run cannot measure this. The rest of the diff is small and follows the existing uma_coherent.txt pattern. No duplicate found: #632 (whole window in the first run), #3546 (commit planning), #3677 (watch ranges) and #3658 (no-buffer window address) are different changes; #3696 is stacked on this PR.
performance: 60 s smoke on df0cfef with the #3544 workaround XCL_DIRECT_DXIL=1 on both trees: the default (non-direct-DXIL) base run crashed at 48 s with 0xc0000005 in amdxc64.dll (the known #3544 driver crash), so the workaround isolates the memory change; the PRs do not touch shader translation. Base: 1,537 frames shown / 19,779 MB peak / 0 device removals / 0 crashes. Merge #3695: 1,584 frames / 19,550 MB / 0 removals / 0 crashes. Frames are 103.1% of base and peak 98.8% (gate: frames >=80%, peak <=125%), so the pair passes. The 600 s shrink is not reached by a 60 s run, so this checks no regression, not the lowering. Not tested: the default non-direct-DXIL path, which crashes on the base too (#3544).
verdict: test report only (not a merge-check): build and merge are clean; on df0cfef the merge's suite has four failures, all on the base (the base's other two did not recur), and no new one, and the three new tests pass; the 60 s pair (with the #3544 workaround) is 103.1% frames / 98.8% peak with 0 removals and 0 crashes. The missing test for the release accounting and the undocumented default-on switch are what I would ask for before merge.
hardware / tool and model: AMD Radeon RX 6700 XT 12 GB (RDNA2), driver 32.0.21045.29003; AMD Ryzen 5 5600X; Windows 10 build 19045 / OpenCode, DeepSeek V4.1 Flash 1017

Smoke blocks (60 s, XCL_DIRECT_DXIL=1, title 1778c2d9):

xcl-smoke v1
commit: df0cfef638306f325b208e5adae18d35ab4bf4ed
gpu: AMD Radeon RX 6700 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1537 (PresentX 1537)
presents per 2 s (last 3): 35 59 44
device removals: 0
crashes: 0
peak commit: 19779 MB
xcl-smoke v1
commit: 1e2533ea4281c6c0b549da697b1ec5cfc16fa76a
gpu: AMD Radeon RX 6700 XT
title: 1778c2d9
seconds: 60
exit: 0x0
frames shown: 1584 (PresentX 1584)
presents per 2 s (last 3): 48 39 61
device removals: 0
crashes: 0
peak commit: 19550 MB

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Test report (not a merge-check: PR description red)
base: 5eaadb5
head: e665b42
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2976 passed, 0 failed, 52 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read the whole diff (+182/-24). Before this PR, the coherent window's stored size was the sum of every GPU heap the title ever asked for, and on the default route it never went down. Now kernelx tracks the heaps alive at once (COHERENT_HEAPS/COHERENT_LIVE) and stores the peak alive bytes plus first-fit holes. After 600 s a run can lower the stored size by up to half. This is on by default (XCL_COHERENT_SHRINK unset = on). Findings: (1) Brings back a lowering that was removed on purpose, with no evidence. settled_need/coherent_shrink (uma.rs ~1152/~1166) lower the default route's stored size again. d75c373 stopped exactly that, after a too-small window sent a retail title's heaps to plain memory, where its shaders read zeros (comment at kernelx.rs ~1005). After a first lowering, the asked >= before guard compares against the peak alive bytes. So a long run with less alive (a menu, a small level) can lower the size again, and the next run that loads a bigger level loses its image. The only reported run used XCL_COHERENT_MB=512, which returns early in coherent_hint_for (uma.rs ~1089) and never touches the stored size. There are no suite counts and no smoke blocks. (2) The second commit is a fixup of the first. e665b42 says it is a "Review fix for 86c5e52", and that f4140e5 alone stores a size below what first-fit placement needs, so f4140e5 on its own is a regression and the history is not bisect-safe. Squash them. The commit bodies also cite private batch names. (3) A heap that does not fit raises the stored size less reliably than before. The size grows only when alive + holes exceeds it, but the fit check (kernelx.rs ~1041) also counts XCL's own blocks and the 96 MB KEEP. When XCL's own blocks take most of the share, a heap can be refused without raising the stored size, and it then goes to plain memory on every run. (4) Gaps and races in the live bookkeeping. On the plain-memory path, coherent_heap_released runs after Windows has freed the address (kernelx.rs ~1335), so another thread can get the same address and insert over the entry. VirtualFreeEx on fallback memory (~1369) is never counted. Both errors overcount, which is the safe side, but they defeat the shrink. COHERENT_LIVE also adds unrounded sizes while alloc_where rounds each heap up to 64 KiB (uma.rs ~846). (5) Tests cover only the pure functions. settled_need and holes() are tested. Nothing tests the kernelx accounting: create/free/reuse of an address, the fallback path, VirtualFreeEx, or that a refused heap raises the need. (6) README. XCL_COHERENT_SHRINK is not in it. Fine: holes() counts free room below each window's highest block, 2 MB alignment padding included. The sparse routes still settle at 60 s. XCL_COHERENT_SHRINK=0 keeps the old cumulative behaviour. A UMA block's release is counted before u.free.
performance: 60 s smoke of a retail title, main 5eaadb5 vs this merge (e665b42): 1422 -> 1664 frames shown (117 %; last presents per 2 s: 61 60 60), peak commit 17184 -> 16726 MB, 0 device removals, 0 crashes.
assessment: merges, builds and passes the whole suite on current main, and the smoke run is clean; the blocking point is in the review: On by default it lowers the default route's coherent size again, which d75c373 removed on purpose (a heap that no longer fits loses its image); the only run used XCL_COHERENT_MB=512, which bypasses the stored size, and there are no smoke blocks or suite counts.
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@throny

throny commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Review of e665b42c4 (read against main, not a merge check; our suite, smoke and world runs of a trial merge are queued and follow as a check).

  1. The default-on shrink can cost a run its picture (gpu/uma.rs ~1155 settled_need, the 600 s settle ~1194; XCL_COHERENT_SHRINK unset = on). The stored size is now the peak of live heaps plus holes, and a run can lower it by up to half. If a later run places its heaps differently, or reaches an area with a higher peak than the run that set the size, a heap misses the window and goes to plain memory, where shaders read zeros. It heals from the next run (the fallback note grows the file), so it costs one broken run per new peak. On main that can't happen, because the window is sized by everything ever asked. A margin on the stored size, or keeping the shrink opt-in, would close it.
  2. Bookkeeping slips that over-count (the safe direction):
    • COHERENT_HEAPS.insert ignores an old entry at a reused address (hle/kernelx.rs ~1035/1052).
    • On a plain-memory release the entry is removed after Windows frees the range (~1331), so a concurrent allocation at that address can lose its entry instead.
    • A heap that took the plain mem::VirtualAlloc fallback and is freed through VirtualFreeEx is never released.
  3. Description: the PR-description check wants the two xcl-smoke blocks. I'll post ours (main and this head, RTX 4080) here when they're done, if they help.

Fine: sparse heaps are not counted; no stored file and =0 behave as before; Series S naming is unchanged; the lock is dropped before holes().

AI use: Claude Code (Claude Opus 5.5) reviewed the diff; posted at throny's request.

@throny

throny commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: c53e969
head: e665b42
merge: trial merge 721997d52 into main c53e96960: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace -- --test-threads=4 -> 3006 passed, 0 failed, 52 ignored; 25 SKIPPED (no wave64 and similar); failing: none
title run: retail title, RTX 4080, 300 s in the world (run 493), no crash, no device removal, 2.79 presents/s
review: the review comment above (whole diff read)
performance: two 900 s warm runs each, main c53e969 vs this trial merge: 3.00 / (495 hung, a stall seen on main too) vs 2.82 / 2.80 presents/s; video memory peak 18.9 vs 19.1-19.2 GB; peak commit 42.1 vs 42.3-42.4 GB
verdict: not yet: the default-on shrink can cost a run its picture (review above, finding 1). Also, on this machine it showed no effect: the stored window (4.9 GB) stayed the same in two 900 s runs, as on main. Our runs set XCL_UMA_DRIVER=mixed:1536:1792, so this setup may not reach the sizing. The frame rate was 2.80 and 2.82 presents/s against main's 3.00 in the same pair of runs (2 runs each, so it may be noise).
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300 / Claude Code with Claude Opus 5.5 (builds, suite, world runs, review)

Smoke blocks for the description (60 s each, retail title):

main:

xcl-smoke v1
commit: c53e96960265a82477a9c98677afb0402e98d486
gpu: NVIDIA GeForce RTX 4080
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1563 (PresentX 1563)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 17578 MB

this PR's head:

xcl-smoke v1
commit: e665b42c4bf3fe0afa9fe170da0e22f55f73338c
gpu: NVIDIA GeForce RTX 4080
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1538 (PresentX 1538)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 15974 MB

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

Labels

area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) needs-pr-check Failing the PR description check (CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants