d3d12: memory nobody touches leaves the working set, a little at a time (LEONIDA_ROLL_TRIM, off by default) #3498

Code

Clone this repository

Checkout

From your project repository, check out a new branch and test the changes.

git fetch -u  "d3d12-rolling-trim:Krashgamer-d3d12-rolling-trim"
git switch "Krashgamer-d3d12-rolling-trim"
Open
leonida/mandy
Krashgamer wants to merge 6 commits from Krashgamer/mandy:d3d12-rolling-trim into main

This branch is out-of-date with the base branch

Conversation
Reviewers 0 approvals
  • No reviewers
Details
This pull request is blocked because it's outdated.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'd3d12-rolling-trim:Krashgamer-d3d12-rolling-trim'
git switch 'Krashgamer-d3d12-rolling-trim'
First-time contributor

Replaces #3332, which came over from GitHub under the name Ghost, so I can no longer push to it. Same code as #3332 (head 808c446cc, the commit the test reports there name), plus one commit that answers Kal0s-design's review there (its inline notes did not come over): a turn is a day at most, a clippy fix in a test, and the text says what locked pages and a minimum working set mean for the trim. The 12 rolltrim tests pass, clippy is clean. The discussion and test results so far are on #3332.

Claimed in the coordination issue as: D3D12-ROLLING-TRIM (https://git.leonida.now/leonida/mandy/issues/2570)

Depends on: none. Near D3D12-WORKING-SET-TRIM (Herlix777, #3198): that one trims when memory is short, this one runs all the time and sorts by use. I asked there; no answer yet.

What and why

Windows takes pages nobody touches only when memory is short, and then at once. The game's working set is about 27 GB on a PC with 32 GB of RAM, so the PC has about 0.8 GB left, and the game pages in gigabytes.

LEONIDA_ROLL_TRIM=<seconds> (off by default) goes through the process's committed memory once in that time, a little at a time, and lets pages that were not touched since the turn before leave the working set (VirtualUnlock). What is wanted again comes back by a soft fault while it still lies in memory.

  • A step (at most 4 MB, cut at fixed addresses) of which half was back at the next turn is in use, and left alone for four turns.
  • Steps with locked memory are left alone; a lock a call took away all the same is put back.
  • It runs on one below-normal thread of the host's sweep, and sleeps between bundles of steps.
  • Nothing is freed: the commit stays as it is, only the working set shrinks.

Tested

main d9b618f10 alone, RTX 3080 10 GB, 32 GB of RAM, Series X, 2560x1440, game gate 300 s in the world, the same route in every run. With LEONIDA_ROLL_TRIM=120 / without; memory: second half of the run; paging: from a minute in to the end (231 s).

game's working set, MB PC's free memory, MB frames a second, median (p5) stutters of 250 ms, of 1 s and more paged out, in, MB
-HardwareRT 12,100 / 27,708 6,838 / 799 19.48 (11.2) / 19.63 (10.4) 24, 0 / 41, 5 101, 3,508 / 67, 6,192
-HardwareRT 11,903 / 26,884 6,765 / 901 19.32 (11.0) / 19.82 (10.9) 22, 1 / 35, 2 76, 3,433 / 971, 4,886
-HardwareRT, the form in this pull request 12,081 / 27,728 6,721 / 644 19.19 (10.9) / 20.17 (10.9) 23, 0 / 31, 1 72, 3,806 / 0, 4,668
no ray tracing, the form in this pull request 11,360 / 26,275 7,286 / 935 24.23 (15.0) / 24.14 (14.2) 2, 0 / 0, 0 63, 4,322 / 1,135, 5,772
  • Speed: with -HardwareRT 19.33 against 19.87 frames a second in the mean of three pairs (-2.7 %; single pairs -0.8 to -4.9 %). No ray tracing, one pair: no difference. The number in my claim (-0.5 %) was of an older form.
  • Texture loading's memory brake: while the PC is short of memory, textures picked by index may take 64 MB a frame in place of 192 MB (residency::prefetch_limit). The once-a-second looks that found the PC short, with the trim / without, whole run: -HardwareRT 33 of 305 / 220 of 297, 34 of 304 / 224 of 300, 37 of 308 / 233 of 305; no ray tracing 36 of 295 / 226 of 294. No more textures were alive at the end because of it on this route.
  • Not seen: card memory and textures alive are the same in all pairs; no error in any run, no crash, no device loss.
  • An earlier form, 15 minutes in the world, one run: no errors.
  • The first two pairs are of the form before the locked-memory rule, the last row and the last -HardwareRT row are of this one.
  • The calls take 1.4 to 5.9 s of a turn of 120 s.

Unit tests (12 in rolltrim.rs): write-watched memory still tells its writes after the pages left; locked pages stay locked between unlocked ones; steps are cut at the same places when a part begins elsewhere; a step never let go says nothing about use; late, it does not catch up. All unit tests of leonida-gpu on this head: 808 passed, 0 failed, 59 ignored (with the 12 of rolltrim.rs again after the last review fix). (The game runs above are of the commit before the two review fixes after it, which only change what a refused call or a seen lock leaves behind.)

Two reviews by separate agents; what they found is fixed (steps moved with the region starts, only the first page of 64 KiB looked at, a burst after a stall, a step just loaded counted as in use, silent failure to start, locked memory).

Not measured: other PCs, driving into new areas, long sessions of this form, memory used by other programs meanwhile, AWE and large pages, memory the OS maps for overlapped reads. One PC, one route. Off by default for these reasons.

GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 610.88, Windows 11 Pro

AI-assisted: yes (Claude Code, Claude Opus 5.5)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator rule.)

🤖 Generated with Claude Code

Replaces #3332, which came over from GitHub under the name Ghost, so I can no longer push to it. Same code as #3332 (head `808c446cc`, the commit the test reports there name), plus one commit that answers Kal0s-design's review there (its inline notes did not come over): a turn is a day at most, a clippy fix in a test, and the text says what locked pages and a minimum working set mean for the trim. The 12 `rolltrim` tests pass, clippy is clean. The discussion and test results so far are on #3332. Claimed in the coordination issue as: D3D12-ROLLING-TRIM (https://git.leonida.now/leonida/mandy/issues/2570) Depends on: none. Near D3D12-WORKING-SET-TRIM (Herlix777, #3198): that one trims when memory is short, this one runs all the time and sorts by use. I asked there; no answer yet. ### What and why Windows takes pages nobody touches only when memory is short, and then at once. The game's working set is about 27 GB on a PC with 32 GB of RAM, so the PC has about 0.8 GB left, and the game pages in gigabytes. `LEONIDA_ROLL_TRIM=<seconds>` (off by default) goes through the process's committed memory once in that time, a little at a time, and lets pages that were not touched since the turn before leave the working set (`VirtualUnlock`). What is wanted again comes back by a soft fault while it still lies in memory. - A step (at most 4 MB, cut at fixed addresses) of which half was back at the next turn is in use, and left alone for four turns. - Steps with locked memory are left alone; a lock a call took away all the same is put back. - It runs on one below-normal thread of the host's sweep, and sleeps between bundles of steps. - Nothing is freed: the commit stays as it is, only the working set shrinks. ### Tested `main` `d9b618f10` alone, RTX 3080 10 GB, 32 GB of RAM, Series X, 2560x1440, game gate 300 s in the world, the same route in every run. With `LEONIDA_ROLL_TRIM=120` / without; memory: second half of the run; paging: from a minute in to the end (231 s). | | game's working set, MB | PC's free memory, MB | frames a second, median (p5) | stutters of 250 ms, of 1 s and more | paged out, in, MB | |---|---|---|---|---|---| | `-HardwareRT` | 12,100 / 27,708 | 6,838 / 799 | 19.48 (11.2) / 19.63 (10.4) | 24, 0 / 41, 5 | 101, 3,508 / 67, 6,192 | | `-HardwareRT` | 11,903 / 26,884 | 6,765 / 901 | 19.32 (11.0) / 19.82 (10.9) | 22, 1 / 35, 2 | 76, 3,433 / 971, 4,886 | | `-HardwareRT`, the form in this pull request | 12,081 / 27,728 | 6,721 / 644 | 19.19 (10.9) / 20.17 (10.9) | 23, 0 / 31, 1 | 72, 3,806 / 0, 4,668 | | no ray tracing, the form in this pull request | 11,360 / 26,275 | 7,286 / 935 | 24.23 (15.0) / 24.14 (14.2) | 2, 0 / 0, 0 | 63, 4,322 / 1,135, 5,772 | - **Speed:** with `-HardwareRT` 19.33 against 19.87 frames a second in the mean of three pairs (-2.7 %; single pairs -0.8 to -4.9 %). No ray tracing, one pair: no difference. The number in my claim (-0.5 %) was of an older form. - **Texture loading's memory brake:** while the PC is short of memory, textures picked by index may take 64 MB a frame in place of 192 MB (`residency::prefetch_limit`). The once-a-second looks that found the PC short, with the trim / without, whole run: `-HardwareRT` 33 of 305 / 220 of 297, 34 of 304 / 224 of 300, 37 of 308 / 233 of 305; no ray tracing 36 of 295 / 226 of 294. No more textures were alive at the end because of it on this route. - **Not seen:** card memory and textures alive are the same in all pairs; no error in any run, no crash, no device loss. - **An earlier form, 15 minutes in the world, one run:** no errors. - **The first two pairs** are of the form before the locked-memory rule, the last row and the last `-HardwareRT` row are of this one. - The calls take 1.4 to 5.9 s of a turn of 120 s. Unit tests (12 in `rolltrim.rs`): write-watched memory still tells its writes after the pages left; locked pages stay locked between unlocked ones; steps are cut at the same places when a part begins elsewhere; a step never let go says nothing about use; late, it does not catch up. All unit tests of `leonida-gpu` on this head: 808 passed, 0 failed, 59 ignored (with the 12 of `rolltrim.rs` again after the last review fix). (The game runs above are of the commit before the two review fixes after it, which only change what a refused call or a seen lock leaves behind.) Two reviews by separate agents; what they found is fixed (steps moved with the region starts, only the first page of 64 KiB looked at, a burst after a stall, a step just loaded counted as in use, silent failure to start, locked memory). **Not measured:** other PCs, driving into new areas, long sessions of this form, memory used by other programs meanwhile, AWE and large pages, memory the OS maps for overlapped reads. One PC, one route. Off by default for these reasons. GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 610.88, Windows 11 Pro AI-assisted: yes (Claude Code, Claude Opus 5.5) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (No translator rule.) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
First-time contributor

Independent local verification of this change (I changed no code).

  • Head 689d9307 (Merge main), merge base 0036195a. Both new tests are absent on the base: git grep -F off_unless_a_turn_is_given 0036195a -- d3d12/crates/leonida-gpu/src -> exit 1 (same for what_is_in_use_is_left_alone).
  • On the PR head, private CARGO_TARGET_DIR, -- --exact: rolltrim::tests::off_unless_a_turn_is_given -> 1 passed / 861 filtered; rolltrim::tests::what_is_in_use_is_left_alone -> 1 passed / 861 filtered. No is_none() device gate in the added lines.
  • git diff --check 0036195a 689d9307 -> clean. python -I scripts/check_no_game_data.py on host.rs, rolltrim.rs and docs/switches.md -> clean.

GPUs tested: none (the tests drive the trim counter on synthetic values; no card and no working-set query needed). Driver: n/a. No game data, assets or extracted shaders were used or attached.

AI-assisted: yes, Hy4 flash (verification only). No approval implied; this is a local test report.

Independent local verification of this change (I changed no code). - Head `689d9307` (`Merge main`), merge base `0036195a`. Both new tests are absent on the base: `git grep -F off_unless_a_turn_is_given 0036195a -- d3d12/crates/leonida-gpu/src` -> exit 1 (same for `what_is_in_use_is_left_alone`). - On the PR head, private `CARGO_TARGET_DIR`, `-- --exact`: `rolltrim::tests::off_unless_a_turn_is_given` -> 1 passed / 861 filtered; `rolltrim::tests::what_is_in_use_is_left_alone` -> 1 passed / 861 filtered. No `is_none()` device gate in the added lines. - `git diff --check 0036195a 689d9307` -> clean. `python -I scripts/check_no_game_data.py` on host.rs, rolltrim.rs and docs/switches.md -> clean. GPUs tested: none (the tests drive the trim counter on synthetic values; no card and no working-set query needed). Driver: n/a. No game data, assets or extracted shaders were used or attached. AI-assisted: yes, Hy4 flash (verification only). No approval implied; this is a local test report.
First-time contributor

A data point from a 16 GB PC, the case this pull request was not measured on | AI-assisted: Claude Code (Claude Opus 5.5)

The PR head 689d9307d, diag build, on a Ryzen 7 5700X, RTX 3070 8 GB, 16 GB of RAM (pagefile 24 GB), Windows 11, driver 621.47. Game gate, 600 s in the world, -SeriesS -Dlss quality -NoFrameGen, one save with the player standing still. Four runs, one executable, in the order A B B A: A without the switch, B with LEONIDA_ROLL_TRIM=120. All four PASS. The screenshots show the same scenes in both arms of each pair. The steady scene is the same in all four runs. In the scene changes the second pair ran about 5 s behind the first, so the cuts are compared within each pair (A1 with B1, B2 with A2). Windows counters came from typeperf once a second. Time windows are in log seconds: "cuts" means 300-460 s, the stretch with the scene changes, and "steady" means 480-660 s, one scene.

My prediction, written before the runs: frames over 100 ms in the cuts fall by 20 % or more and their p99 falls, and the steady median is at most 1 % slower. It would be refuted if neither the long frames nor the pages read in during the cuts fell, or if the steady median were more than 2 % slower. Both happened, so the runs refuted it. The p99 part did hold (see below).

A1 B1 B2 A2
cuts: frames over 100 ms 212 210 227 206
cuts: frame p99, ms 256 224 245 284
cuts: pages in per second, whole PC (mean) 20,321 20,912 21,807 21,269
steady: frame median / p90, ms 40.3 / 43.5 41.4 / 45.6 41.2 / 45.2 40.2 / 43.5
steady: game's working set, MB 6,088 4,710 4,316 5,880
steady: PC's available memory, MB 966 1,480 1,127 1,194
steady: the game's page faults per second 4,510 6,728 7,245 4,584
steady: transition faults per second (whole PC) 3,093 4,758 7,182 2,899
steady: pages in per second (whole PC) 941 2,028 1,732 1,032
  • Cuts. On this PC the long frames come with paging bursts. In earlier runs, seconds with a frame over 100 ms read in 21,000-29,000 pages a second, against 800-1,600 in seconds with frames up to 60 ms. The trim does not change that. Frames over 100 ms did not fall (212 -> 210, 206 -> 227), and pages read in were about 3 % higher with the trim in both pairs. p99 was lower with the trim in both pairs (256 -> 224 and 284 -> 245 ms), but two pairs cannot tell that apart from noise.
  • Steady. With the trim, the steady median is 2.5-2.7 % slower (about 1 ms) and p90 is 3.7-4.8 % slower. The trim takes the game's working set down by about 1.4-1.6 GB, and in the same scene the game's page faults rise by about half. Pages read in from disk across the PC rise 1.7 to 2.2 times. This fits the idea that, on 16 GB, part of what the trim lets go of is wanted again soon and faulting it back costs frame time, but I did not measure that link directly. The trim's own calls (1.4-2.4 s per turn) may also play a part.
  • One trim run (B1) also had 15 frames over 100 ms in the steady scene, the longest 0.9 s. Several of them came in the seconds before a trim turn ended. The other three runs had none in the steady scene.
  • The trim's own line is printed once per 120 s turn, five turns a run. In the last turn of each B run it went through about 31,600-31,700 MB of committed memory, 1.5-1.8 s of it in the calls, and let go of 15,000-19,000 of about 99,000 steps. Turns 2-5 range from 27,800 to 31,800 MB and from 1.4 to 2.4 s.
  • A thread-state census, sampled 4 times a second, shows the list and submitting threads equally busy in both arms in the steady scene.

So in these four runs the switch costs a little in the steady scene, and in the scene changes it does not reduce the long frames or the paging. I did not try other turn lengths. The PR keeps the switch off by default, and this is no objection to that. It is one more row for the description's "Tested" table, from a smaller PC.

GPUs tested: NVIDIA RTX 3070 8 GB, driver 621.47. No game data, assets or screenshots attached.

A data point from a 16 GB PC, the case this pull request was not measured on | AI-assisted: Claude Code (Claude Opus 5.5) The PR head `689d9307d`, diag build, on a Ryzen 7 5700X, RTX 3070 8 GB, **16 GB of RAM** (pagefile 24 GB), Windows 11, driver 621.47. Game gate, 600 s in the world, `-SeriesS -Dlss quality -NoFrameGen`, one save with the player standing still. Four runs, one executable, in the order A B B A: A without the switch, B with `LEONIDA_ROLL_TRIM=120`. All four PASS. The screenshots show the same scenes in both arms of each pair. The steady scene is the same in all four runs. In the scene changes the second pair ran about 5 s behind the first, so the cuts are compared within each pair (A1 with B1, B2 with A2). Windows counters came from `typeperf` once a second. Time windows are in log seconds: "cuts" means 300-460 s, the stretch with the scene changes, and "steady" means 480-660 s, one scene. My prediction, written before the runs: frames over 100 ms in the cuts fall by 20 % or more and their p99 falls, and the steady median is at most 1 % slower. It would be refuted if neither the long frames nor the pages read in during the cuts fell, or if the steady median were more than 2 % slower. **Both happened, so the runs refuted it.** The p99 part did hold (see below). | | A1 | B1 | B2 | A2 | |---|---|---|---|---| | cuts: frames over 100 ms | 212 | 210 | 227 | 206 | | cuts: frame p99, ms | 256 | 224 | 245 | 284 | | cuts: pages in per second, whole PC (mean) | 20,321 | 20,912 | 21,807 | 21,269 | | steady: frame median / p90, ms | 40.3 / 43.5 | 41.4 / 45.6 | 41.2 / 45.2 | 40.2 / 43.5 | | steady: game's working set, MB | 6,088 | 4,710 | 4,316 | 5,880 | | steady: PC's available memory, MB | 966 | 1,480 | 1,127 | 1,194 | | steady: the game's page faults per second | 4,510 | 6,728 | 7,245 | 4,584 | | steady: transition faults per second (whole PC) | 3,093 | 4,758 | 7,182 | 2,899 | | steady: pages in per second (whole PC) | 941 | 2,028 | 1,732 | 1,032 | - **Cuts.** On this PC the long frames come with paging bursts. In earlier runs, seconds with a frame over 100 ms read in 21,000-29,000 pages a second, against 800-1,600 in seconds with frames up to 60 ms. The trim does not change that. Frames over 100 ms did not fall (212 -> 210, 206 -> 227), and pages read in were about 3 % higher with the trim in both pairs. p99 was lower with the trim in both pairs (256 -> 224 and 284 -> 245 ms), but two pairs cannot tell that apart from noise. - **Steady.** With the trim, the steady median is 2.5-2.7 % slower (about 1 ms) and p90 is 3.7-4.8 % slower. The trim takes the game's working set down by about 1.4-1.6 GB, and in the same scene the game's page faults rise by about half. Pages read in from disk across the PC rise 1.7 to 2.2 times. This fits the idea that, on 16 GB, part of what the trim lets go of is wanted again soon and faulting it back costs frame time, but I did not measure that link directly. The trim's own calls (1.4-2.4 s per turn) may also play a part. - One trim run (B1) also had 15 frames over 100 ms in the steady scene, the longest 0.9 s. Several of them came in the seconds before a trim turn ended. The other three runs had none in the steady scene. - **The trim's own line** is printed once per 120 s turn, five turns a run. In the last turn of each B run it went through about 31,600-31,700 MB of committed memory, 1.5-1.8 s of it in the calls, and let go of 15,000-19,000 of about 99,000 steps. Turns 2-5 range from 27,800 to 31,800 MB and from 1.4 to 2.4 s. - A thread-state census, sampled 4 times a second, shows the list and submitting threads equally busy in both arms in the steady scene. So in these four runs the switch costs a little in the steady scene, and in the scene changes it does not reduce the long frames or the paging. I did not try other turn lengths. The PR keeps the switch off by default, and this is no objection to that. It is one more row for the description's "Tested" table, from a smaller PC. GPUs tested: NVIDIA RTX 3070 8 GB, driver 621.47. No game data, assets or screenshots attached.
Sign in to join this conversation.
No description provided.