victory-gpu: reclaim idle ahead buffers by default under pressure, back off on sustained OOM/device removal, and report buffer OOMs #2015

Open
github-bot wants to merge 1 commit from github-bot/gh-2015 into main AGit
Owner

@⁠superhero-520 on GitHub · 2026-11-29 07:26 UTC · original
superhero-520:fix/ahead-reclaim-and-oom-backoff → main · +238 −23 · 2 files · 1 commits · open

Summary

Rebased cleanly onto v2026.11.29.05 (41adb551), reusing upstream first() (residency::scaled_default(..., 512, 4096)), trim_idle(), RetryGate, and uav_buffer_now(), while keeping the remaining valuable reliability fixes for low-RAM / low-VRAM hosts.

Root Cause

  1. Default-path idle ahead buffers never reclaimed once pools are full (crates/victory-gpu/src/ahead.rs):
    • Without opting into VICTORY_LOWSPEC_ADAPTIVE=1, fill() only paused refills under residency::pressured() || critical_commit() — and when both pools were already full (!more_shared && !more_card), it looped at the idle sleep without ever checking memory/VRAM pressure or trimming already-allocated idle buffers.
  2. No clean worker exit on D3D12 device removal (crates/victory-gpu/src/ahead.rs):
    • After DXGI_ERROR_DEVICE_REMOVED / DEVICE_HUNG, fill() kept attempting buffer allocations instead of checking GetDeviceRemovedReason(), releasing idle buffers, and exiting cleanly.
  3. Fixed 50 ms retry cadence under sustained OOM (crates/victory-gpu/src/ahead.rs):
    • Upstream RetryGate caps failure rate at a fixed 50 ms per lane (20 attempts/sec indefinitely during prolonged OOM).
  4. Missing OOM pressure feedback in buffer allocation helpers (crates/victory-gpu/src/host.rs):
    • read_back_buffer, shared_buffer_own, and uav_buffer_now did not notify crate::residency::allocation_error(&e) on E_OUTOFMEMORY.

Re-Evaluated Scope on v2026.11.29.05

  • Upstream 158d3177 lowered the default pre-allocation count on < 24 GiB RAM hosts from 4096 to 512 via residency::scaled_default(..., 512, 4096).
  • Consequently, on 16 GB RAM hosts, the maximum logical idle ahead pool size under default configuration is now 64 MiB (32 MiB shared + 32 MiB card), not the older 512 MiB ceiling (which still applies on >= 24 GiB RAM hosts with low VRAM or commit pressure).
  • Reclaimed logical buffer counts/MiB represent released D3D12 resource objects, not an equivalent guaranteed drop in OS physical RAM or physical VRAM usage.

Fix Design

  • crates/victory-gpu/src/ahead.rs:
    • On every loop iteration in the default path (before checking !more_shared && !more_card), check residency::pressured(), residency::critical_commit(), and DXGI local VRAM usage via next_card_vram_pressure() (enters at >= 85% of budget, clears only at <= 70%, matching card_has_room() to prevent reclaim/rebuild oscillation).
    • Reclaim idle buffers outside each pool mutex via trim_idle() (updated to drop removed resources outside the lock and return the freed count), cap post-reclaim want to KEPT, and log logical_shared_mb / logical_card_mb.
    • Check GetDeviceRemovedReason() on allocation failures, during pressure pauses, and periodically when idle; on removal, drop all idle buffers outside locks and exit fill() cleanly.
    • Extend RetryGate with RetryGate::exponential(50ms, 1000ms) per independent lane (shared_retry, card_retry): escalates 50 -> 100 -> 200 -> 400 -> 800 -> 1000 ms on consecutive failures and is never reset by pressured() / critical_commit() pauses (resolving earlier review feedback), while leaving RetryGate::default() at fixed 50 ms so upstream retry_tests stay 100% unchanged.
  • crates/victory-gpu/src/host.rs:
    • Call crate::residency::allocation_error(&e) in the Err(e) paths of read_back_buffer, shared_buffer_own, and uav_buffer_now (which triggers only on E_OUTOFMEMORY, ignoring device-removed errors).

Verification & Results

Automated & Real D3D12 Tests (RTX 3060 Laptop 6 GiB, Windows 11)

  • cargo test --locked -p victory-gpu --lib ahead:: -- --nocapture --test-threads=1: 12 passed, 0 failed (0.39s):
    • All 8 upstream ahead tests remain intact and passing.
    • 4 new tests: exponential_retry_gate_backs_off_to_cap_and_survives_pressure_pauses, default_card_vram_pressure_has_hysteresis_aligned_with_card_room, trim_idle_drops_resources_outside_pool_mutex, and d3d12_trim_idle_releases_real_shared_and_card_buffers_cleanly (uses .expect(...) on buffer creation so allocation failures fail the test; debug layer errors: None).
  • cargo clippy --locked -p victory-gpu --all-targets & --all-features: 0 warnings in ahead.rs / modified host.rs lines.
  • cargo check --profile diag --locked -p victory --features diagnostics, cargo test --locked -p victory, cargo build --release --locked -p victory, cargo run --locked -p xtask -- policy --staged: all passed.
  • Note on full victory-gpu --lib suite: 924 passed, 2 pre-existing unrelated failures already present on untouched 41adb551 (shared_root_signatures_load_from_library_and_execute_across_pso_switches and every_switch_of_the_translator_is_in_the_key).

Live Gameplay Validation (RTX 3060 Laptop 6 GiB + Ryzen 7 5800H + 16 GB RAM)

  • Config: Series S, internal 960×540 -> DLSS Performance (Model K) -> output 1920×1080, RT OFF, warm pipeline library (0 in-world slow loads).
  • Default-path reclaim observed live on every run containing PR #2015:
    • C1 (FG OFF): reclaimed 512 idle shared buffers (32 MiB logical, matching 16 GB scaled_default=512), 6.17 FPS (1% Low 1.87), 0 OOM / 0 device-removed.
    • E1 (combined with PR #2038, FG OFF): reclaimed 512 shared (32 MiB logical), 6.63 FPS (1% Low 2.10, P99 391.4 ms), 0 OOM / 0 device-removed.
    • G2 (combined with PR #2038 + perf fix, FG OFF): reclaimed 512 shared + 392 card = 904 idle buffers (56.5 MiB logical), 6.47 FPS, 0 OOM / 0 device-removed.
    • C-fsr2 / E-fsr2 (FSR 3 2X): FSR 3 active (0 failed prepare dispatches), 512 shared reclaimed, 0 OOM / 0 device-removed.

Limitations & Known Risks

  • Under default 16 GB RAM settings on v2026.11.29.05, the reclaimed logical volume is bounded by upstream's 512-buffer cap (<= 64 MiB total logical); physical RAM/VRAM savings are modest compared to pre-158d3177 builds, though preventing sustained OOM retry churn and exiting cleanly on device removal apply across all RAM sizes.
  • Short 72-second live benchmark runs with 0 OOM / 0 TDR do not prove long-session (30+ minute) OOM/TDR crashes are eliminated; extended open-world soak testing is in progress.

AI Assistance Disclosure

AI-Assisted: Rebased, refined, tested, and documented with AI assistance (Antigravity / Gemini), with all unit/D3D12 tests and live RTX 3060 hardware runs verified locally.

**@⁠superhero-520** on GitHub · 2026-11-29 07:26 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2015) `superhero-520:fix/ahead-reclaim-and-oom-backoff` → `main` · +238 −23 · 2 files · 1 commits · open ## Summary Rebased cleanly onto `v2026.11.29.05` (`41adb551`), reusing upstream `first()` (`residency::scaled_default(..., 512, 4096)`), `trim_idle()`, `RetryGate`, and `uav_buffer_now()`, while keeping the remaining valuable reliability fixes for low-RAM / low-VRAM hosts. ## Root Cause 1. **Default-path idle ahead buffers never reclaimed once pools are full (`crates/victory-gpu/src/ahead.rs`)**: - Without opting into `VICTORY_LOWSPEC_ADAPTIVE=1`, `fill()` only paused refills under `residency::pressured() || critical_commit()` — and when both pools were already full (`!more_shared && !more_card`), it looped at the idle sleep without ever checking memory/VRAM pressure or trimming already-allocated idle buffers. 2. **No clean worker exit on D3D12 device removal (`crates/victory-gpu/src/ahead.rs`)**: - After `DXGI_ERROR_DEVICE_REMOVED` / `DEVICE_HUNG`, `fill()` kept attempting buffer allocations instead of checking `GetDeviceRemovedReason()`, releasing idle buffers, and exiting cleanly. 3. **Fixed 50 ms retry cadence under sustained OOM (`crates/victory-gpu/src/ahead.rs`)**: - Upstream `RetryGate` caps failure rate at a fixed 50 ms per lane (20 attempts/sec indefinitely during prolonged OOM). 4. **Missing OOM pressure feedback in buffer allocation helpers (`crates/victory-gpu/src/host.rs`)**: - `read_back_buffer`, `shared_buffer_own`, and `uav_buffer_now` did not notify `crate::residency::allocation_error(&e)` on `E_OUTOFMEMORY`. ### Re-Evaluated Scope on `v2026.11.29.05` - Upstream `158d3177` lowered the default pre-allocation count on `< 24 GiB` RAM hosts from `4096` to `512` via `residency::scaled_default(..., 512, 4096)`. - Consequently, on 16 GB RAM hosts, the maximum **logical** idle ahead pool size under default configuration is now **64 MiB** (`32 MiB` shared + `32 MiB` card), not the older `512 MiB` ceiling (which still applies on `>= 24 GiB` RAM hosts with low VRAM or commit pressure). - Reclaimed logical buffer counts/MiB represent **released D3D12 resource objects**, **not** an equivalent guaranteed drop in OS physical RAM or physical VRAM usage. ## Fix Design - **`crates/victory-gpu/src/ahead.rs`**: - On every loop iteration in the default path (before checking `!more_shared && !more_card`), check `residency::pressured()`, `residency::critical_commit()`, and DXGI local VRAM usage via `next_card_vram_pressure()` (enters at `>= 85%` of budget, clears only at `<= 70%`, matching `card_has_room()` to prevent reclaim/rebuild oscillation). - Reclaim idle buffers outside each pool mutex via `trim_idle()` (updated to drop removed resources outside the lock and return the freed count), cap post-reclaim `want` to `KEPT`, and log `logical_shared_mb` / `logical_card_mb`. - Check `GetDeviceRemovedReason()` on allocation failures, during pressure pauses, and periodically when idle; on removal, drop all idle buffers outside locks and exit `fill()` cleanly. - Extend `RetryGate` with `RetryGate::exponential(50ms, 1000ms)` per independent lane (`shared_retry`, `card_retry`): escalates `50 -> 100 -> 200 -> 400 -> 800 -> 1000 ms` on consecutive failures and is **never reset by `pressured()` / `critical_commit()` pauses** (resolving earlier review feedback), while leaving `RetryGate::default()` at fixed 50 ms so upstream `retry_tests` stay 100% unchanged. - **`crates/victory-gpu/src/host.rs`**: - Call `crate::residency::allocation_error(&e)` in the `Err(e)` paths of `read_back_buffer`, `shared_buffer_own`, and `uav_buffer_now` (which triggers only on `E_OUTOFMEMORY`, ignoring device-removed errors). ## Verification & Results ### Automated & Real D3D12 Tests (RTX 3060 Laptop 6 GiB, Windows 11) - `cargo test --locked -p victory-gpu --lib ahead:: -- --nocapture --test-threads=1`: **12 passed, 0 failed** (`0.39s`): - All 8 upstream `ahead` tests remain intact and passing. - 4 new tests: `exponential_retry_gate_backs_off_to_cap_and_survives_pressure_pauses`, `default_card_vram_pressure_has_hysteresis_aligned_with_card_room`, `trim_idle_drops_resources_outside_pool_mutex`, and `d3d12_trim_idle_releases_real_shared_and_card_buffers_cleanly` (uses `.expect(...)` on buffer creation so allocation failures fail the test; debug layer errors: `None`). - `cargo clippy --locked -p victory-gpu --all-targets` & `--all-features`: **0 warnings in `ahead.rs` / modified `host.rs` lines**. - `cargo check --profile diag --locked -p victory --features diagnostics`, `cargo test --locked -p victory`, `cargo build --release --locked -p victory`, `cargo run --locked -p xtask -- policy --staged`: all passed. - Note on full `victory-gpu --lib` suite: 924 passed, 2 pre-existing unrelated failures already present on untouched `41adb551` (`shared_root_signatures_load_from_library_and_execute_across_pso_switches` and `every_switch_of_the_translator_is_in_the_key`). ### Live Gameplay Validation (RTX 3060 Laptop 6 GiB + Ryzen 7 5800H + 16 GB RAM) - Config: Series S, internal `960×540` -> DLSS Performance (Model K) -> output `1920×1080`, RT OFF, warm pipeline library (`0` in-world slow loads). - **Default-path reclaim observed live** on every run containing PR #2015: - `C1` (FG OFF): reclaimed `512` idle shared buffers (`32 MiB` logical, matching 16 GB `scaled_default=512`), `6.17 FPS` (`1% Low 1.87`), 0 OOM / 0 device-removed. - `E1` (combined with PR #2038, FG OFF): reclaimed `512` shared (`32 MiB` logical), `6.63 FPS` (`1% Low 2.10`, `P99 391.4 ms`), 0 OOM / 0 device-removed. - `G2` (combined with PR #2038 + perf fix, FG OFF): reclaimed `512` shared + `392` card = `904` idle buffers (`56.5 MiB` logical), `6.47 FPS`, 0 OOM / 0 device-removed. - `C-fsr2` / `E-fsr2` (FSR 3 2X): FSR 3 active (`0` failed prepare dispatches), `512` shared reclaimed, 0 OOM / 0 device-removed. ## Limitations & Known Risks - Under default 16 GB RAM settings on `v2026.11.29.05`, the reclaimed logical volume is bounded by upstream's `512`-buffer cap (`<= 64 MiB` total logical); physical RAM/VRAM savings are modest compared to pre-`158d3177` builds, though preventing sustained OOM retry churn and exiting cleanly on device removal apply across all RAM sizes. - Short 72-second live benchmark runs with 0 OOM / 0 TDR do **not** prove long-session (30+ minute) OOM/TDR crashes are eliminated; extended open-world soak testing is in progress. ## AI Assistance Disclosure AI-Assisted: Rebased, refined, tested, and documented with AI assistance (Antigravity / Gemini), with all unit/D3D12 tests and live RTX 3060 hardware runs verified locally.
On 6-8 GB GPUs and 16 GB RAM hosts, `victory-ahead` pre-allocates up to 4096 x 64 KiB
(256 MiB) of pinned L0 `SHARED` buffers and 4096 x 64 KiB (256 MiB) of default-heap
`CARD` VRAM buffers at startup, then keeps 1024 (64 MiB) of each:

1. Idle `CARD` buffers were never reclaimed when VRAM reached or exceeded budget, even
   after `shadow` stopped consuming `ahead::card()` past its budget cutoff, stranding
   up to 256 MiB of unused VRAM. Idle `SHARED` buffers were likewise never trimmed by
   `host::sweep()` under host memory pressure or critical commit.
2. When `host.shared_buffer_now(SIZE)` or `host.uav_buffer(SIZE)` returned `None`
   (`E_OUTOFMEMORY` or `DXGI_ERROR_DEVICE_REMOVED`), `ahead::fill()` looped immediately
   with zero sleep and never checked `GetDeviceRemovedReason()`, hammering
   `CreateCommittedResource` in a tight spin loop.
3. `Host::read_back_buffer`, `Host::shared_buffer_own`, and `Host::uav_buffer` did not
   call `residency::allocation_error(&e)` on `E_OUTOFMEMORY`, unlike
   `Host::upload_buffer_own`, leaving the 500 ms OOM emergency window inactive until an
   upload buffer failed.

Changes:
- `crates/victory-gpu/src/ahead.rs`:
  - Add `trim(memory)` (wired into `host::sweep()`) to reclaim idle `CARD` buffers at
    >= 85% VRAM budget or critical commit (preserving a 15% hysteresis band above the
    70% refill threshold) and trim idle `SHARED` buffers under host pressure (0 when
    physical RAM < 8 GiB or commit is critical; 256 / 16 MiB floor when >= 8 GiB free).
    All COM resources are dropped outside the store mutexes.
  - Add `drop_all()` (wired into F7 `host::drop_spare_chunks()` and device-removal exit).
  - Pause `fill()` under host pressure/critical commit, apply exponential backoff
    (100 ms -> 1000 ms) on allocation failure, and stop cleanly if the D3D12 device is
    removed.
  - Add 5 unit tests covering hysteresis, trim limits, backoff progression, lock-free
    dropping, and live D3D12 resource reclamation.
- `crates/victory-gpu/src/host.rs`:
  - Call `ahead::trim(memory)` in `sweep()` (reusing a single `video_memory()` query)
    and `ahead::drop_all()` in `drop_spare_chunks()`.
  - Call `residency::allocation_error(&e)` in `read_back_buffer`, `shared_buffer_own`,
    and `uav_buffer`.

AI-Assisted: Developed and verified with AI assistance (Antigravity / Gemini).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 07:26:49 +00:00
Address two review findings on the initial ahead-buffer reclaim commit:

1. Preserve `backoff_ms` during `pressured() || critical_commit()` pauses in
   `ahead::fill()`: because `residency::allocation_error(&e)` activates a 500 ms
   `critical_commit()` window on `E_OUTOFMEMORY`, zeroing `backoff_ms` on the
   pressure-pause branch prevented sustained OOM failures from escalating
   `100 -> 200 -> 400 -> 800 -> 1000 ms`. Introduce `FillEvent` / `step_backoff_ms`
   so `PressurePause` preserves `backoff_ms` and sleeps `PRESSURE_PAUSE_MS.max(backoff_ms)`,
   while only `Allocated` or normal `Idle` resets `backoff_ms` to `0`.
2. Harden `ahead::tests::trim_vec_drops_real_d3d12_buffers_cleanly`: once a D3D12
   host device exists, assert buffer allocations with `.expect(...)` instead of
   silently returning early on failure, and assert `host.log_messages() == None`.
3. Add `ahead::tests::sustained_oom_escalates_across_critical_commit_pauses_and_resets_on_recovery`.

AI-Assisted: Developed and verified with AI assistance (Antigravity / Gemini).
github-bot changed title from gpu: reclaim idle ahead buffers under pressure, back off on OOM/device removal, and report buffer OOMs to victory-gpu: reclaim idle ahead buffers by default under pressure, back off on sustained OOM/device removal, and report buffer OOMs 2026-11-30 05:13:17 +00:00
github-bot force-pushed github-bot/gh-2015 from 8c92331620 to 8a1abb0af7 2026-11-30 05:13:18 +00:00 Compare
Author
Owner

@⁠tyr0nsur on GitHub · 2026-11-30 12:58 UTC · original

I tested a follow-up for the DXGI non-local budget at 8a1abb0a. The ahead worker already checks physical RAM, commit and the local DXGI budget. On a discrete GPU, Windows can lower the non-local budget while those checks still leave room for speculative shared buffers (#1520).

The patch applies the existing 85% entry and 70% recovery thresholds to the non-local segment. The ahead worker pauses speculative shared refill and returns unused buffers. The block sweep shares the same pressure state, so it can release empty CPU-visible heaps after the normal rest even if releasing the ahead inventory first moved usage below 85%. Local VRAM has its own decision. Demand allocations retain their existing path.

With a simulated budget signal and real D3D12 allocations on an RTX 4060, DXGI non-local usage fell from 36 to 28 MiB after dropping 128 committed ahead buffers, and from 92 to 28 MiB in a separate test after a pooled shared heap became empty. A live view and its resting range kept that heap allocated. Both focused tests also passed on WARP, and the diagnostics suite passed 49 tests. These are standalone tests without a title.

Companion commit

**@⁠tyr0nsur** on GitHub · 2026-11-30 12:58 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2015#issuecomment-7279186932) I tested a follow-up for the DXGI non-local budget at `8a1abb0a`. The ahead worker already checks physical RAM, commit and the local DXGI budget. On a discrete GPU, Windows can lower the non-local budget while those checks still leave room for speculative shared buffers (#1520). The patch applies the existing 85% entry and 70% recovery thresholds to the non-local segment. The ahead worker pauses speculative shared refill and returns unused buffers. The block sweep shares the same pressure state, so it can release empty CPU-visible heaps after the normal rest even if releasing the ahead inventory first moved usage below 85%. Local VRAM has its own decision. Demand allocations retain their existing path. With a simulated budget signal and real D3D12 allocations on an RTX 4060, DXGI non-local usage fell from 36 to 28 MiB after dropping 128 committed ahead buffers, and from 92 to 28 MiB in a separate test after a pooled shared heap became empty. A live view and its resting range kept that heap allocated. Both focused tests also passed on WARP, and the diagnostics suite passed 49 tests. These are standalone tests without a title. [Companion commit](https://github.com/tyr0nsur/VIctory-NVIDIA/commit/45067991069a663c6aa19dfff69dabfa0bc03c3d)
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/ahead.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2015/head:github-bot/gh-2015'
git switch 'github-bot/gh-2015'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2015'
git switch 'github-bot/gh-2015'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2015'
git switch 'github-bot/gh-2015'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2015'
git switch 'main'
git merge --squash 'github-bot/gh-2015'
git switch 'main'
git merge --ff-only 'github-bot/gh-2015'
git switch 'main'
git merge 'github-bot/gh-2015'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!2015
No description provided.