GPU: reclaim unmapped host textures under host memory pressure #394

Merged
BlackBookOfficial merged 2 commits from upstream-pr/394 into main 2026-11-28 19:39:39 +00:00
BlackBookOfficial commented 2026-11-27 21:51:34 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by BlackBookOfficial on 2026-11-27T21:51:34Z.


Problem

Host copies of the title's placed textures are cached under the same policy as the card: with VRAM below its line, the existing sweep keeps title-filled copies idle for 600 s. Their backing allocations are system memory, and on a 64 GB host with ~14 GB commit headroom the layer reached 38.9 GB private (12.8 GB of host textures) and kept growing. Empty 64 MB texture-pool blocks are likewise retained (up to 64) while VRAM has room.

Root cause

resource::let_go_idle used only host::vram_keep_below for its idle threshold. The census counted placed, title-filled copies whose title memory was unmapped, but release was still governed by the card's 10-minute rule. Pool retention is governed by pool::room and SPARE_KEPT, again by VRAM only.

Change

  • residency.rs: host-pressure policy from commit/physical headroom: 5 s idle below min(RAM/4, 16 GiB), 1 s below 8 GiB. VICTORY_HOST_TEXTURE_SWEEP=0 restores the old card-only policy.
  • resource.rs::let_go_idle: under that policy only placed copies whose entire title-memory footprint is unmapped (texture::mapped_bytes == 0, two endpoint queries when both ends are uncommitted) are eligible. Existing safeguards are unchanged: title-filled, host never wrote, Arc::strong_count == 1, COM references == 1, no list or descriptor slot. Mapped/hot copies keep the 600 s cache.
  • pool.rs::room: under host pressure keep 2 empty 64 MB blocks instead of 64, returning backing to Windows after the existing rest period.
  • copystat.rs: counters for reclaimed copies and returned pool blocks.

Results (420 s diagnostic run, same start/profile)

Metric baseline this change
private at 420 s 35,970 MB 30,753 MB
private peak 36,642 MB 33,432 MB
host textures 12,775 MB 6,316 MB
pool blocks 10,944 MB 6,080 MB
commit headroom 14,600 MB 19,053 MB
median frame 113.45 ms 111.15 ms
p90 frame 202.3 ms 148.8 ms
median fps 8.8 9.0
device removals 0 0

Net process-private reduction is ~5.2 GB; the cumulative counters (54,322 copies / 9,289 MB; 62 blocks / 3,968 MB) are not net savings. No new GPU/device-removal errors; no FSR prepare failures. Rainbow-texture warnings are pre-existing (144 baseline, 113 here) and are not addressed.

Limitations

Guest-section commit retention is not addressed (XMemFreePhysicalPages removes page-table slices; section backing is released only with the section). Long-session stability beyond 420 s is not established. No FSR, DLSS, shader, or rendering changes.

Rollback

VICTORY_HOST_TEXTURE_SWEEP=0 restores the previous card-only caching and pool spare limit.

Refs #191, #260 (not closing them).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5533549280 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/394). Originally posted by [BlackBookOfficial](https://github.com/BlackBookOfficial) on 2026-11-27T21:51:34Z. --- ## Problem Host copies of the title's placed textures are cached under the same policy as the card: with VRAM below its line, the existing sweep keeps title-filled copies idle for 600 s. Their backing allocations are system memory, and on a 64 GB host with ~14 GB commit headroom the layer reached 38.9 GB private (12.8 GB of host textures) and kept growing. Empty 64 MB texture-pool blocks are likewise retained (up to 64) while VRAM has room. ## Root cause `resource::let_go_idle` used only `host::vram_keep_below` for its idle threshold. The census counted placed, title-filled copies whose title memory was unmapped, but release was still governed by the card's 10-minute rule. Pool retention is governed by `pool::room` and `SPARE_KEPT`, again by VRAM only. ## Change - `residency.rs`: host-pressure policy from commit/physical headroom: 5 s idle below `min(RAM/4, 16 GiB)`, 1 s below 8 GiB. `VICTORY_HOST_TEXTURE_SWEEP=0` restores the old card-only policy. - `resource.rs::let_go_idle`: under that policy only **placed** copies whose **entire** title-memory footprint is unmapped (`texture::mapped_bytes == 0`, two endpoint queries when both ends are uncommitted) are eligible. Existing safeguards are unchanged: title-filled, host never wrote, `Arc::strong_count == 1`, COM references == 1, no list or descriptor slot. Mapped/hot copies keep the 600 s cache. - `pool.rs::room`: under host pressure keep 2 empty 64 MB blocks instead of 64, returning backing to Windows after the existing rest period. - `copystat.rs`: counters for reclaimed copies and returned pool blocks. ## Results (420 s diagnostic run, same start/profile) | Metric | baseline | this change | |---|---:|---:| | private at 420 s | 35,970 MB | **30,753 MB** | | private peak | 36,642 MB | **33,432 MB** | | host textures | 12,775 MB | **6,316 MB** | | pool blocks | 10,944 MB | **6,080 MB** | | commit headroom | 14,600 MB | **19,053 MB** | | median frame | 113.45 ms | **111.15 ms** | | p90 frame | 202.3 ms | **148.8 ms** | | median fps | 8.8 | **9.0** | | device removals | 0 | **0** | Net process-private reduction is ~5.2 GB; the cumulative counters (54,322 copies / 9,289 MB; 62 blocks / 3,968 MB) are not net savings. No new GPU/device-removal errors; no FSR prepare failures. Rainbow-texture warnings are pre-existing (144 baseline, 113 here) and are not addressed. ## Limitations Guest-section commit retention is not addressed (`XMemFreePhysicalPages` removes page-table slices; section backing is released only with the section). Long-session stability beyond 420 s is not established. No FSR, DLSS, shader, or rendering changes. ## Rollback `VICTORY_HOST_TEXTURE_SWEEP=0` restores the previous card-only caching and pool spare limit. Refs #191, #260 (not closing them).
wildheathen commented 2026-11-27 22:54:55 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by wildheathen on 2026-11-27T22:54:55Z.


Thanks for this. I read the change and built it (diag, cargo test -p victory-gpu: the PR's new test passes; the two indirect::tests failures are the ones main has). I have not run it in the game yet. Three things I found reading it, the first one matters on PCs with 32 GB:

1. Under host pressure the card's rule is switched off, not added to (resource.rs, let_go_idle). When host_texture_idle_ms() returns Some, (idle_ms, most) comes from it alone and the vram_keep_below branch is not taken. Further down, a copy whose title memory is still mapped hits continue ("the card's rule still decides when it may go"), but in this branch no rule looks at it any more. So while the PC is short of memory, mapped textures are never let go, even with VRAM past its line.

On a 32 GB PC the line is min(RAM/4, 16 GiB) = about 7.8 GiB, which is below the 8 GiB of the 1 s tier, so the 5 s tier never applies; and in the world this machine (RTX 5080 16 GB, 32 GB) has about 1.2 GB available, so the host rule would be on for the whole session. I would expect shared GPU memory to go up there, not down. A fix that keeps your idea: run the card's rule as before, and take the unmapped placed copies under the host rule in addition.

2. The mapped check walks the whole texture when it is mapped. The comment says a mapped first page answers at once, but texture::mapped_bytes(..) == 0 queries every block until the end for a mapped texture. With the 1 s rule on, that is every mapped placed texture, every pass, on the submitting thread. An early-out "is any page mapped" would do.

3. Comment vs code: "Portions mapped between the ends make it skip" — with both ends uncommitted the copy is let go without looking in between (mapped_bytes takes the two-query path). Either the comment or the check should change.

For scale on this machine: the census line "host copies of placed textures whose title memory is not mapped any more" says 811 MB at 170 s, so that is the most the copies part could give back here; the 5.2 GB you measured probably needs the longer session and the pool-block part.

Happy to measure a revised version on the 5080 (3-minute diagnostics run, memory line and census).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7229721482 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/394#issuecomment-7229721482). Originally posted by [wildheathen](https://github.com/wildheathen) on 2026-11-27T22:54:55Z. --- Thanks for this. I read the change and built it (`diag`, `cargo test -p victory-gpu`: the PR's new test passes; the two `indirect::tests` failures are the ones `main` has). I have not run it in the game yet. Three things I found reading it, the first one matters on PCs with 32 GB: **1. Under host pressure the card's rule is switched off, not added to** (`resource.rs`, `let_go_idle`). When `host_texture_idle_ms()` returns `Some`, `(idle_ms, most)` comes from it alone and the `vram_keep_below` branch is not taken. Further down, a copy whose title memory is still mapped hits `continue` ("the card's rule still decides when it may go"), but in this branch no rule looks at it any more. So while the PC is short of memory, mapped textures are never let go, even with VRAM past its line. On a 32 GB PC the line is `min(RAM/4, 16 GiB)` = about 7.8 GiB, which is below the 8 GiB of the 1 s tier, so the 5 s tier never applies; and in the world this machine (RTX 5080 16 GB, 32 GB) has about 1.2 GB available, so the host rule would be on for the whole session. I would expect shared GPU memory to go up there, not down. A fix that keeps your idea: run the card's rule as before, and take the unmapped placed copies under the host rule in addition. **2. The mapped check walks the whole texture when it is mapped.** The comment says a mapped first page answers at once, but `texture::mapped_bytes(..) == 0` queries every block until the end for a mapped texture. With the 1 s rule on, that is every mapped placed texture, every pass, on the submitting thread. An early-out "is any page mapped" would do. **3. Comment vs code:** "Portions mapped between the ends make it skip" — with both ends uncommitted the copy is let go without looking in between (`mapped_bytes` takes the two-query path). Either the comment or the check should change. For scale on this machine: the census line "host copies of placed textures whose title memory is not mapped any more" says 811 MB at 170 s, so that is the most the copies part could give back here; the 5.2 GB you measured probably needs the longer session and the pool-block part. Happy to measure a revised version on the 5080 (3-minute diagnostics run, memory line and census).
hazben commented 2026-11-28 16:39:47 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by hazben on 2026-11-28T16:39:47Z.


A measurement of #394 on a smaller PC than the one it was written on, with one failure and a fix. PC: RTX 4070 SUPER (12 GB), 32 GB RAM, DLSS Quality, frame generation off. Main f4608f0c plus a local patch stack (not upstream) plus #394 at a60f2cc8 (#843 and #1041 are in too); "no #394" below is main 63b902fe with the same stack but without #394, #843 and #1041. The three plays start from successive saves of one playthrough, so the start points are close but not identical.

What the PR does here. On 32 GB both of its lines are 8 GiB (min(RAM/4, 16 GiB) = 8), so the 1 s idle applies, and the PC is under host pressure from the start: "limiting texture prefetch" is logged at 128 s with 3.05 GB of physical memory free, and RAM used stays at 28-31 GB of 32 for the whole play, so the rule applies throughout. In let_go_idle, with host pressure host_idle is Some, so (idle_ms, most) is (1 s, no cap) and the None arm, which calls vram_keep_below, is not evaluated; mapped copies continue with the comment that the card's rule still decides when they go, but in that call it is never run. So the card's room-making rule (idle time, byte cap, longest idle first) does not run under pressure, and the pass only takes copies whose whole title memory is unmapped.

What it did, 8-minute play with #394 as posted (against a play without #394 at the same point):

#394 as posted no #394
passes that made no host copy for lack of room 21,746 29
"brought up to date although the card has no room for new ones" 231,500 2,000
5-s tiles whose room test says "no room, new textures wait" 90 % 6 %
free room in texture blocks, median 376 MB 949 MB
texture queue at 5-7k / 7-9k / 9-13k draws 11.7k / 12.3k / 12.5k slots 2.4k / 6.9k / 10.5k
new textures uploaded in 480 s 5.1 GB 12.2 GB
copies let go (all rules) by 8 min 20.6k (2.6 GB) 60.4k (5.8 GB)
copies made again by 8 min 464 (54 MB) 10,604 (858 MB)
alive title-filled copies 43.8k (3.7 GB) 23.1k (3.0 GB)

The card was as full in both (9.05 and 8.95 GB in use of about 10.9). The player saw textures arrive late and pop in. It is not release-and-reload churn (only 464 copies were made again): the card stopped shedding idle copies, so there was no room for the new ones.

The change (resource.rs, residency.rs, pool.rs, copystat.rs; +97/-41): under host pressure the card's rule is computed and run exactly as before the PR (idle time, byte cap, longest idle first), and slot_evict gets the card's idle time; the PR's pass (1 s / 5 s, placed copies whose whole title memory is unmapped) runs beside it, as in the PR. VICTORY_HOST_SWEEP_KEEP_CARD=0 gives the PR as posted; the reclamation line gains "N by the pass beside the card's rule". The pool's cut of empty blocks to 2 under pressure is left as in the PR (VICTORY_HOST_SWEEP_SPARES=1 keeps them by the card's rule; not used below). I can send the diff.

Play with the fix, 44 minutes (same machine, the next save of the same playthrough):

  • passes with no room: 1,149 by 5 min and 4,026 over the 44 min (the play without #394: 29 at 8 min, 4,460 over 30 min); 7 % of 5-s tiles over the room line (6 % without #394); free room in blocks 892 MB median (949); queue 3.6k / 3.5k / 8.2k slots at 5-7k / 7-9k / 9-13k draws (2.4k / 6.9k / 10.5k); uploads 1,227 MB a minute over 60-480 s (1,236 without #394; 353 with #394 as posted), 1,822 MB a minute over the run.
  • the reclamation line: 19,459 copies (4,511 MB) let go, all by the pass beside the card's rule; the card's rule let go 377.9k copies (40 GB) by 30 min with 196.8k made again (the play without #394: 431.7k / 42.8 GB, 198.8k).
  • memory: process private 24.2 GB at 100 s to 25.4 GB at the end (+0.04 GB a minute), peak 26.12 GB (26.19 GB in the play without #394), RAM used peak 31.5 GB (31.2); alive copies flat at 2.5-3.0 GB. I cannot show #394's memory saving on this PC: private is the same with and without it. 114 empty pool blocks (7.3 GB) went back to Windows over the 44 min.
  • no crash, hang or device loss; the daylight part (152 s) has the same frame times as the play without #394 (17.5 against 17.7 fps, p99 113 against 112 ms); the world turned to night after that, so later frame numbers are not compared.

Not measured: the PR as posted and the fix on a fixed route (these are played sessions; the two #394 runs differ by the fix and by a local offload switch being off, and the run without #394 also lacks #843 and #1041); a 64 GB PC (where the pressure rule may rarely apply); VICTORY_HOST_SWEEP_SPARES=1; night frame times.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7242415485 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/394#issuecomment-7242415485). Originally posted by [hazben](https://github.com/hazben) on 2026-11-28T16:39:47Z. --- A measurement of #394 on a smaller PC than the one it was written on, with one failure and a fix. PC: RTX 4070 SUPER (12 GB), 32 GB RAM, DLSS Quality, frame generation off. Main f4608f0c plus a local patch stack (not upstream) plus #394 at a60f2cc8 (#843 and #1041 are in too); "no #394" below is main 63b902fe with the same stack but without #394, #843 and #1041. The three plays start from successive saves of one playthrough, so the start points are close but not identical. **What the PR does here.** On 32 GB both of its lines are 8 GiB (min(RAM/4, 16 GiB) = 8), so the 1 s idle applies, and the PC is under host pressure from the start: "limiting texture prefetch" is logged at 128 s with 3.05 GB of physical memory free, and RAM used stays at 28-31 GB of 32 for the whole play, so the rule applies throughout. In `let_go_idle`, with host pressure `host_idle` is `Some`, so `(idle_ms, most)` is `(1 s, no cap)` and the `None` arm, which calls `vram_keep_below`, is not evaluated; mapped copies `continue` with the comment that the card's rule still decides when they go, but in that call it is never run. So the card's room-making rule (idle time, byte cap, longest idle first) does not run under pressure, and the pass only takes copies whose whole title memory is unmapped. **What it did, 8-minute play with #394 as posted (against a play without #394 at the same point):** | | #394 as posted | no #394 | |---|---|---| | passes that made no host copy for lack of room | 21,746 | 29 | | "brought up to date although the card has no room for new ones" | 231,500 | 2,000 | | 5-s tiles whose room test says "no room, new textures wait" | 90 % | 6 % | | free room in texture blocks, median | 376 MB | 949 MB | | texture queue at 5-7k / 7-9k / 9-13k draws | 11.7k / 12.3k / 12.5k slots | 2.4k / 6.9k / 10.5k | | new textures uploaded in 480 s | 5.1 GB | 12.2 GB | | copies let go (all rules) by 8 min | 20.6k (2.6 GB) | 60.4k (5.8 GB) | | copies made again by 8 min | 464 (54 MB) | 10,604 (858 MB) | | alive title-filled copies | 43.8k (3.7 GB) | 23.1k (3.0 GB) | The card was as full in both (9.05 and 8.95 GB in use of about 10.9). The player saw textures arrive late and pop in. It is not release-and-reload churn (only 464 copies were made again): the card stopped shedding idle copies, so there was no room for the new ones. **The change** (`resource.rs`, `residency.rs`, `pool.rs`, `copystat.rs`; +97/-41): under host pressure the card's rule is computed and run exactly as before the PR (idle time, byte cap, longest idle first), and `slot_evict` gets the card's idle time; the PR's pass (1 s / 5 s, placed copies whose whole title memory is unmapped) runs beside it, as in the PR. `VICTORY_HOST_SWEEP_KEEP_CARD=0` gives the PR as posted; the reclamation line gains "N by the pass beside the card's rule". The pool's cut of empty blocks to 2 under pressure is left as in the PR (`VICTORY_HOST_SWEEP_SPARES=1` keeps them by the card's rule; not used below). I can send the diff. **Play with the fix, 44 minutes (same machine, the next save of the same playthrough):** - passes with no room: 1,149 by 5 min and 4,026 over the 44 min (the play without #394: 29 at 8 min, 4,460 over 30 min); 7 % of 5-s tiles over the room line (6 % without #394); free room in blocks 892 MB median (949); queue 3.6k / 3.5k / 8.2k slots at 5-7k / 7-9k / 9-13k draws (2.4k / 6.9k / 10.5k); uploads 1,227 MB a minute over 60-480 s (1,236 without #394; 353 with #394 as posted), 1,822 MB a minute over the run. - the reclamation line: 19,459 copies (4,511 MB) let go, all by the pass beside the card's rule; the card's rule let go 377.9k copies (40 GB) by 30 min with 196.8k made again (the play without #394: 431.7k / 42.8 GB, 198.8k). - memory: process private 24.2 GB at 100 s to 25.4 GB at the end (+0.04 GB a minute), peak 26.12 GB (26.19 GB in the play without #394), RAM used peak 31.5 GB (31.2); alive copies flat at 2.5-3.0 GB. I cannot show #394's memory saving on this PC: private is the same with and without it. 114 empty pool blocks (7.3 GB) went back to Windows over the 44 min. - no crash, hang or device loss; the daylight part (152 s) has the same frame times as the play without #394 (17.5 against 17.7 fps, p99 113 against 112 ms); the world turned to night after that, so later frame numbers are not compared. Not measured: the PR as posted and the fix on a fixed route (these are played sessions; the two #394 runs differ by the fix and by a local offload switch being off, and the run without #394 also lacks #843 and #1041); a 64 GB PC (where the pressure rule may rarely apply); `VICTORY_HOST_SWEEP_SPARES=1`; night frame times.
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
trustno1-coder/VIctory-NVIDIA!394
No description provided.