Skip to content

DXC memory gate: reserve only the running compiles' growth, no stampede after a minute - #139

Open
hexgeon1 wants to merge 3 commits into
ivall:mainfrom
hexgeon1:dxc-memory-gate
Open

hexgeon1 wants to merge 3 commits into
ivall:mainfrom
hexgeon1:dxc-memory-gate

Conversation

@hexgeon1

Copy link
Copy Markdown
Contributor

The memory gate from #113 throttles DXC hard on a normal desktop. With 8.1 GB of commit free, Minecraft Preview's 716 programs took 12.7–15.2 s on the default 15 threads; with this change, 6.35–6.71 s. The DXIL is identical.

What was wrong

wait_for_memory let a compile start while the system had 6 GB of commit left plus 1 GB for every compile already running.

  • Counted twice: the available commit already excludes what the running compiles use, so the 1 GB each was on top of their own use. With a browser and chat open this machine has ~8 GB of commit free, so about three compiles ran and the other workers polled every 50 ms.
  • Stampede after a minute: after a minute's wait, a compile went in regardless of commit. Every waiting worker did so at once, which happens exactly when commit is shortest – the case the gate exists for.
  • One reading, many starts: workers read the commit and incremented the running count in two steps, so several could start on the same reading.

Changes (crates/xcl/src/gpu/recomp/dxc.rs)

  • may_start: a compile goes in at once while commit covers 6 GB plus 300 MB per running compile. The 300 MB covers their growth still to come; Faster shader compiles: one window switch per four dwords, every core, --precompile builds the cache titles use #113 measured a typical retail compile's peak at 0.1–0.3 GB. With less, compiles go one at a time.
  • Escape: a compile that has waited a minute still goes in when commit is short, but only one per minute.
  • Count: the running count only rises from the value the decision saw (compare and swap).
  • Waking: waiters wake when a compile ends (condition variable), not on a 50 ms poll.
  • HANDOFF.md: the two places that describe the gate are updated.

The second commit adds dxc_bench_folder, an ignored test that times DXC for a folder of console shaders and prints HLSL and DXIL digests. It's how the numbers below were measured; drop it if you'd rather not have it.

Numbers

Ryzen 7 5700X (8 cores / 16 threads), RTX 3090, DXC 1.9.2602, Minecraft Preview's 716 programs, 4 memory windows, default 15 threads. Free commit was 8.1 GB before every run. Both builds use the same benchmark, run interleaved, 3 runs each:

wall time
main (484dd57) 12.88 s, 12.67 s, 15.23 s
this PR 6.71 s, 6.71 s, 6.35 s
no gate at all (reference only) 5.55 s, 5.33 s
  • DXIL: the digest of all 716 programs is the same on main and with this PR.
  • More commit free: with lots of free commit the old and new gates both let everything through. The difference shows when free commit is ~6–12 GB, which is common with other apps open or a game running.

Checked

Not covered

  • Driver pipeline compiles: the driver's PSO compiles on the xcl-pso workers are still not memory-gated.
  • Runaway compiles: a compile whose memory runs away (the 38 GB case) can't be stopped in-process. That would need DXC in a separate process with a memory limit per compile.

🤖 Generated with Claude Code

ivall added a commit that referenced this pull request Nov 23, 2026
… rect lists

A home in a sparse window is only reserved until its page is used there and gives its
memory back while the page is mapped elsewhere: the retail game's 5.7 GB of pages were
committed twice, with video-memory tiles (28.7 GB of commit, now 16-17 GB). chad.dll
(XDsp) connects and takes commands; the newer list layout's #139 is Reset; console rect
lists are drawn as triangles; a stream without a topology type is TRIANGLE.
XCL_TRACE_MEM=n logs the first n allocations.
@tronLives111

Copy link
Copy Markdown

The concurrency test can fail even when the gate admits every worker correctly. It computes allowed from one GlobalMemoryStatusEx snapshot, while wait_for_memory checks available commit again for each admission.

I checked a short schedule against the thresholds in 5d84b26: the test initially sees 6,144 MiB, so it asserts peak <= 2 (allowed + 1). The first worker enters at 6,144 MiB. If another process then releases 600 MiB, the second and third workers can both enter at 6,744 MiB while the first is still running. Their thresholds are 6,444 and 6,744 MiB, so all three admissions are valid, but the test rejects peak == 3. The initial probe also ignores the Windows API return, so a failed probe can supply a zero baseline while later probes succeed.

Could the admission loop take a test-only memory sampler so all workers in this test see a fixed budget? That would let the test assert the exact cap and leave a live-memory run as a smoke check. I verified the admission arithmetic with a deterministic schedule on macOS. I did not run the Windows concurrency test. AI-assisted review.

hexgeon1 and others added 2 commits November 23, 2026 19:33
…de after a minute

`wait_for_memory` (ivall#113) let a compile start with 6 GB of commit left plus
1 GB per compile already running. The available commit already excludes what
the running compiles use, so that counted them twice: on a desktop with
8.1 GB of commit free (a browser and chat open) about three compiles ran and
the other workers polled.

- `may_start`: in at once while commit covers 6 GB + 300 MB per running
  compile (their growth still to come; a typical retail compile peaks at
  0.1-0.3 GB per ivall#113's measurements); with less, one at a time.
- After a minute's wait a compile still goes in when commit is short, but one
  per minute; before, every waiting worker went in at once - when commit was
  shortest.
- The running count only rises from the value the decision saw (compare and
  swap), so workers deciding on the same reading cannot all go in.
- Waiters wake when a compile ends (condition variable) instead of polling
  every 50 ms.

Minecraft Preview's 716 programs, 4 memory windows, 15 threads (default),
Ryzen 7 5700X, 8.1 GB of commit free, main and this interleaved, 3 runs each:
12.7-15.2 s -> 6.35-6.71 s wall (5.3-5.6 s with no gate at all); DXIL
digest identical. Tests: `gate_tests` (the rule, short commit / one escape
per interval, 32 threads never exceeding what the commit allows);
`cargo test --workspace --release` green on an RTX 3090 with the Agility SDK
(0 GPU skips). Driver PSO compiles are still not gated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ignored test for measuring compile changes: every program of a folder of
console containers (`XCL_BENCH_DIR`) translated as a title does
(`XCL_BENCH_WINDOWS`, `XCL_BENCH_SPLIT`) or saved HLSL (`XCL_BENCH_HLSL=1`),
compiled on `XCL_BENCH_THREADS` threads with `XCL_DXC_ARGS`. Prints translate
time, HLSL and DXIL digests (an HLSL change that keeps the DXIL shows the same
DXIL digest), DXC wall / per-program times, cost per KB and peak commit;
`XCL_BENCH_TIMES=<file>` writes each program's time, `XCL_BENCH_SLICE=k/n`
splits a folder across processes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed commit

The test computed its limit from one `GlobalMemoryStatusEx` reading while the
gate reads the commit again at every decision: memory another process freed
mid-test let the gate rightly admit more than the test allowed (review on
ivall#139). It also ignored a failed reading, and other tests' compiles share the
real gate's count.

The gate is now a `Gate` (running count, lock with the last escape, wake-up)
and `admit(gate, commit)` takes the commit reading; `wait_for_memory` is
`admit(&GATE, available_commit)`. The test gives a gate of its own a fixed
commit with room for exactly three and asserts that three run at once
(the first ones in wait until the cap is full) and never four. A gate that
lets one more in fails it (peak 4); 30 runs of `gate_tests` in a row pass.
A second test sends 32 workers through the real gate and checks they all
finish.

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

hexgeon1 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks – you're right, the test could fail on a correct gate: its limit came from one reading while the gate re-reads commit per decision, and other tests' compiles share the real gate's count too. Fixed in 03e2efe: the gate is now a Gate value and admit(gate, commit) takes the commit reading, so the test runs on its own gate with a fixed budget (room for 3) and asserts the exact cap – 3 reached, 4 never (a gate letting one more in fails it). A separate test checks 32 workers get through the real gate. 30/30 runs pass; full suite green with the GPU on an RTX 3090. Would be glad to get feedback.

@gefydev

gefydev commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Measured on a machine with little commit: Ryzen 5 1600 (6C/12T), 16 GB RAM, 20.7 GB commit limit, GTX 1650, Windows 11 26300, DXC from SDK 10.0.26100, 11 DXC threads, debug build.

Synthetic bench, no game data: 200 different compute shaders through pso_async + DXC, no cache, no driver PSO. 3 runs each, interleaved, commit free before every run:

commit free all 200
main db5dc26 (this PR's base) 13.4 / 13.5 / 13.4 GB 7.42 / 7.38 / 7.41 s
this PR, 03e2efe 13.4 / 13.5 / 13.4 GB 6.37 / 6.37 / 6.35 s

So about 14 % less here. These shaders are small and alike, so this says little about memory growth of big programs. The 4 gate_tests pass, including the reworked concurrency one. I did not run the rest of the suite on this PR.

main is at 956a476 now and conflicts in dxc.rs: it added RESERVE_GB / set_memory_reserve (6 GB with a title, 2 GB for --precompile) on the line this PR changes. The 1 GB per running compile is still there, so the PR's point still stands by reading; I have not measured it on top of 956a476.

@RemingtonWilcox

Copy link
Copy Markdown
Contributor

Read: gpu/recomp/dxc.rs in this PR and on main (956a476), and shaders.rs on main.
Main now has RESERVE_GB / set_memory_reserve (dxc.rs:135-139, used at dxc.rs:166), and xcl shaders --precompile lowers it to 2 GB (shaders.rs:834). The gate here uses a fixed COMMIT_FLOOR = 6 << 30 (dxc.rs:171, :192).
When the conflict is resolved, the floor should come from RESERVE_GB, or precompile goes back to a 6 GB floor.

AI-assisted review (Gemini + Claude); code read only, not run.

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted review (Claude Code). Windows 11 (10.0.26200), Ryzen 9 9950X (32 threads), Rust 1.99.0 MSVC, debug build, Windows SDK DXC 1.7.2308. Synthetic programs only, no game files, no GPU.

Tested 03e2efe on its base db5dc26. It does not merge onto main (956a476, and still not onto current eaa36b6). dxc.rs conflicts, because main made the 6 GB floor RESERVE_GB / set_memory_reserve (2 GB for --precompile), as gefydev noted. A rebase should use RESERVE_GB for COMMIT_FLOOR. I did not test a rebased version.

The PR's gate_tests pass on the head (4/4).

Method: real concurrent DXC compiles through the gate, against a fixed commit budget.

  • A job commit cap (14 GiB) bounds the run. It can't drive the gate, though: GlobalMemoryStatusEx ignores a job's memory limit. Under a 9 GiB cap it still reported about 30 GB available.
  • System commit on this shared machine also swung between 18 and 26 GB from run to run.
  • So each worker goes through the PR's admit(gate, commit), on a gate of its own, exactly as a pool worker does, and then runs compile_here on a real translated program (xcl-pso's NVIDIA (RTX 5070): graphics pipelines never finish – nvgpucomp64 stalls on large translated shaders (+ 2 fixes: chad.dll XDsp, XMemMapPhysicalPagesScatter) #156 bench generator: wave32, 4 windows, 15 workers). The commit it sees is a budget minus the process's real private-commit growth since the start. That is the model in the PR: what the running compiles already use is out of available commit.
  • The "before" side is db5dc26's wait_for_memory loop, copied verbatim with the same commit source.
  • A sampler reads that commit every 5 ms.

One compile alone grows private commit by 160 MB for a 2000-round program (987 KB of HLSL) and by 460 MB for 4000 rounds (1.9 MB). A probe of 8000 rounds (3.8 MB, 65 s) grew 1.5 GB.

scenario gate compiles wall peak running min commit seen time below the 6 GB floor failed
S1: 8.5 GB budget, 2000 rounds base 45 49.3 s 6 7,881 MB 0 0
this PR 45 24.0 s 9 7,234 MB 0 0
S2: 8.5 GB budget, 4000 rounds base 30 125.7 s 15 2,887 MB 41.0 s 0
this PR 30 100.5 s 9 4,536 MB 38.8 s 0
S3: 6.05 GB budget (short), 2000 rounds base 40 89.5 s 15 3,913 MB 79.5 s 0
this PR 40 126.7 s 2 5,872 MB 113.2 s* 0

* In S3 the budget is below floor + one compile, so any running compile puts the commit under 6 GB. That is the "one at a time" case working as intended.

What this shows:

  • The stampede is real, and the PR removes it. In S2 and S3 the base gate lets every waiting worker in at the same instant at t = 60.0 s: 12 in S2 (3 running → 15), and 14 admissions after a minute's wait in S3. With the PR, S3 escapes exactly once per minute (t = 60.0 s and 120.1 s), and the peak is 2.
  • The race is real, and the PR removes it. In S1 the base rule allows 3 at most (8.5 GB ≥ 6 + 1 × running), yet 6 ran at once. At t = 41.2 s, jobs went in with 4 and 5 running before them, all from one reading of the count. The PR never exceeded its rule.
  • Throughput: S1 is 2× faster, consistent with the PR's Minecraft numbers.

Accounting finding: 300 MB per running compile is too little for bigger programs, and compiles that start together aren't counted at all.

  • In S2, all 9 compiles went in at t = 0. The budget was full then (8.7 GB ≥ 6 GB + 0.3 GB × 8), because none of them had grown yet. Each then grew to about 460 MB. Commit fell to 4.5 GB, 1.5 GB under the floor, and stayed below it for 39 s.
  • The rule reserves 300 MB of growth per running compile, so it holds only while each compile's whole growth is at most 300 MB. S1's 160 MB compiles stay above the floor; S2's 460 MB ones don't.
  • By extrapolation, for 1.5 GB compiles (the 8000-round probe) the same 9 would end about 10 GB under the floor. That is not measured.
  • Faster shader compiles: one window switch per four dwords, every core, --precompile builds the cache titles use #113's typical retail peak of 0.1–0.3 GB fits under 300 MB. Big compute and mesh programs (HANDOFF: "each can still grow to ~0.7 GB") don't.

A cheap improvement: scale the reservation with the source, e.g. reserve per compile by HLSL size, since growth here went roughly with size: 160 MB at 1 MB, 460 MB at 1.9 MB, 1.5 GB at 3.8 MB. Or keep the per-compile reservation until the compile ends instead of only counting it. Even in S2 the PR stays better than the base, whose stampede reached 2.9 GB.

Not tested:

  • the rebase onto main;
  • the real wait_for_memory path with system commit (deliberately not driven on a shared machine);
  • the full suite;
  • a retail pack;
  • runaway compiles (as the PR says, out of reach in-process).

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Linux / Wine / GNU run of 03e2efe on its own base db5dc26. It doesn't merge onto current main eaa36b6: gpu/recomp/dxc.rs conflicts, because main added RESERVE_GB / set_memory_reserve and a WORKING count where this PR introduces Gate. A rebase onto eaa36b6 is needed (as asked on #198), and the gate has to account for main's set_memory_reserve (xcl shaders --precompile with less reserve).

Setup: Ubuntu 24.04, cross-built for x86_64-pc-windows-gnu (release), under Wine 11.21 Staging. Under Wine, GlobalMemoryStatusEx's available commit is computed from Linux RAM and swap, so the_real_gate_lets_every_worker_through runs on a different commit source than on Windows.

recomp::dxc filter:

passed failed
base db5dc26 10 0
PR 03e2efe 14 0

All four new gate_tests pass: compiles_start_while_commit_covers_the_floor_and_the_running_ones_growth, short_commit_lets_one_compile_at_a_time_and_one_escape_per_interval, concurrent_admissions_reach_and_never_exceed_what_the_commit_allows and the_real_gate_lets_every_worker_through.

Not covered: the rebased PR; a run with little commit (this machine isn't short of it); dxc_bench_folder timing; no control with the old gate under the new tests (the tests need the Gate API).

AI-assisted test report (Claude); tests run locally as stated.

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