perf(gpu/mem): add safe title-private working-set trimming via VICTORY_TRIM_BELOW_MB (#276) #384

Merged
Androtim merged 3 commits from upstream-pr/384 into main 2026-12-03 11:31:50 +00:00
Androtim commented 2026-12-03 10:43:16 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-03T10:43:16Z.


Summary

Adds safe, paced working-set trimming of cold title-private RAM pages when host physical RAM runs low (VICTORY_TRIM_BELOW_MB), targeting 16 GiB RAM systems without risking GPU device loss (0x887A0005).

Adapted from GPL-3.0 LinkoArts/VIctory commits 7839a18 and 2fa2113 (originally derived from tshadofr/Leila patches 0012 and 0055), with two clean refinements:

  • Computes in_place range subtraction once per region instead of twice.
  • Uses checked base.checked_add(len) bounds, half-open at..end removal on sub-decommits, and allocation_base(at) normalization in opened_whole.
  • Auto-enables at 1536 MiB free-RAM threshold on <= 20 GiB total RAM hosts when VICTORY_TRIM_BELOW_MB is unset, while remaining disabled (0) on > 20 GiB hosts unless explicitly set.

Safety Invariants

  • Tracks title allocations (watch::title_memory_allocated / title_memory_freed), D3D12 in-place host heaps (inplace::open), and write-combined ranges (shadow::write_combined_ranges).
  • Gentle mode (default, VICTORY_TRIM_GENTLE=1) trims strictly MEM_COMMIT + MEM_PRIVATE pages belonging to tracked title allocations after subtracting all in-place heaps and write-combined ranges, and never touches MEM_IMAGE, MEM_MAPPED, PAGE_WRITECOMBINE, PAGE_GUARD, PAGE_NOACCESS, runtime heaps, stacks, or driver allocations.
  • Rate-limited: checks every 2s, trims at most once per 30s, and requires +2048 MiB working-set growth since the previous settled trim.

Verification

  • cargo test -p victory-gpu --lib trim::tests (4 passed)
  • cargo clippy -p victory-gpu --all-targets -- -D warnings (clean)
  • Gate 1: GitHub CI check
  • Gate 2: Read-only Reasoning Audit (#282)
  • Gate 3: Live RTX 4060 Ti gameplay test with active trim trigger (VICTORY_TRIM_BELOW_MB=16000)

Related: #276

<!-- greyfinch-sync:Androtim/MiniVI:issue:5624866348 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/384). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-03T10:43:16Z. --- ## Summary Adds safe, paced working-set trimming of cold title-private RAM pages when host physical RAM runs low (`VICTORY_TRIM_BELOW_MB`), targeting 16 GiB RAM systems without risking GPU device loss (`0x887A0005`). Adapted from GPL-3.0 `LinkoArts/VIctory` commits `7839a18` and `2fa2113` (originally derived from `tshadofr/Leila` patches `0012` and `0055`), with two clean refinements: - Computes `in_place` range subtraction once per region instead of twice. - Uses checked `base.checked_add(len)` bounds, half-open `at..end` removal on sub-decommits, and `allocation_base(at)` normalization in `opened_whole`. - Auto-enables at `1536 MiB` free-RAM threshold on `<= 20 GiB` total RAM hosts when `VICTORY_TRIM_BELOW_MB` is unset, while remaining disabled (`0`) on `> 20 GiB` hosts unless explicitly set. ## Safety Invariants - Tracks title allocations (`watch::title_memory_allocated` / `title_memory_freed`), D3D12 in-place host heaps (`inplace::open`), and write-combined ranges (`shadow::write_combined_ranges`). - Gentle mode (default, `VICTORY_TRIM_GENTLE=1`) trims strictly `MEM_COMMIT` + `MEM_PRIVATE` pages belonging to tracked title allocations after subtracting all in-place heaps and write-combined ranges, and never touches `MEM_IMAGE`, `MEM_MAPPED`, `PAGE_WRITECOMBINE`, `PAGE_GUARD`, `PAGE_NOACCESS`, runtime heaps, stacks, or driver allocations. - Rate-limited: checks every `2s`, trims at most once per `30s`, and requires `+2048 MiB` working-set growth since the previous settled trim. ## Verification - [x] `cargo test -p victory-gpu --lib trim::tests` (`4 passed`) - [x] `cargo clippy -p victory-gpu --all-targets -- -D warnings` (clean) - [ ] Gate 1: GitHub CI `check` - [ ] Gate 2: Read-only Reasoning Audit (`#282`) - [ ] Gate 3: Live RTX 4060 Ti gameplay test with active trim trigger (`VICTORY_TRIM_BELOW_MB=16000`) Related: #276
Androtim commented 2026-12-03 11:05:25 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-03T11:05:25Z.


3-Gate Verification Summary (4c9bde6)

  • Gate 1 (GitHub CI check): PASS (4m06s)
  • Gate 2 (Deep Reasoning Audit, #282 Pass #12): VERIFIED CLEAN (0 P0, 0 P1) — [P1] scan-to-unlock TOCTOU race eliminated via OPEN_LOCK + STATE_GEN + per-chunk VirtualQuery re-verification (5fe71eb), and shadow::write_combined_regions contract preserved (4c9bde6).
  • Gate 3 (Live RTX 4060 Ti 1440p DLSS Quality gameplay with VICTORY_TRIM_BELOW_MB=24000): PASS — 3 active gentle VirtualUnlock trim passes mid-gameplay, 0 DRED, 0 0x887A0005, peak dedicated VRAM 5529 / 6736 MiB, crisp visuals.

Ready for merge!

<!-- greyfinch-sync:Androtim/MiniVI:comment:7376197994 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/384#issuecomment-7376197994). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-03T11:05:25Z. --- ### 3-Gate Verification Summary (`4c9bde6`) - **Gate 1 (GitHub CI `check`):** `PASS` (`4m06s`) - **Gate 2 (Deep Reasoning Audit, #282 Pass #12):** `VERIFIED CLEAN (0 P0, 0 P1)` — `[P1]` scan-to-unlock TOCTOU race eliminated via `OPEN_LOCK` + `STATE_GEN` + per-chunk `VirtualQuery` re-verification (`5fe71eb`), and `shadow::write_combined_regions` contract preserved (`4c9bde6`). - **Gate 3 (Live RTX 4060 Ti 1440p DLSS Quality gameplay with `VICTORY_TRIM_BELOW_MB=24000`):** `PASS` — 3 active gentle `VirtualUnlock` trim passes mid-gameplay, `0` DRED, `0` `0x887A0005`, peak dedicated VRAM `5529 / 6736 MiB`, crisp visuals. Ready for merge!
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
Androtim/MiniVI!384
No description provided.