Tiered memory for unified memory emulation #3723
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3723"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by fl4rion on 2026-11-30T04:52:45Z.
The problem
The Xbox Series X has one pool of memory that the CPU and the GPU share. A game writes a buffer with the CPU and the GPU reads it a moment later, from the same bytes, at no cost.
A PC has two pools: system RAM, and the graphics card's own memory. Its GPU reads its own memory fast and system RAM slowly, over PCIe. So VIctory keeps copies of the console's memory, and each copy lives in one of several places:
Every copy costs something wherever it lives:
Today, every kind of copy decides this on its own. Textures, buffer copies, windows of memory, staging, send buffers and scratch each have a separate rule: their own idea of "idle", their own thresholds, their own reaction to memory pressure. We counted over a dozen independent idle rules and as many separate "are we under pressure?" switches. None of them knows how often a copy is actually used, and none can tell whether dropping a copy was a mistake.
One example of what that costs. VIctory copies memory outside buffers in "windows" (6 MiB each). When the PC runs short of memory, the window rule shortens its idle time to 2–5 seconds. A window that a game reads in bursts goes idle for a few seconds between them. It is dropped, then rebuilt moments later: a new buffer, a wait for it to become resident, and a full copy. In one play log with the system nearly out of commit, a third of all windows dropped came back within 30 seconds, and three in four did in the worst stretch, about 7.6 seconds later on average. The memory freed came straight back, and each rebuild was a small stall.
What this PR does
It gives VIctory a shared foundation for deciding where copies live, modelled on how storage systems tier data: keep what is used close to the compute, push out what is not, and decide from measured use rather than per-feature guesses. It then uses that foundation to fix the window problem above.
1. A small, reusable crate:
victory-tiers. It has no Direct3D, no game code and no OS calls, so other forks and other emulators of shared-memory consoles can use it. It provides:2. The window fix (
victory-gpu). Under memory pressure, a window is no longer dropped just because it sat idle for 2–5 seconds:With memory to spare nothing changes, and the idle time is never made shorter than today's. A log line next to the frame-rate line says how many windows were kept and how many still came back.
Why it matters beyond windows
The window fix is the first user. The point is that these decisions can now be made in one place, from the same measurements, and checked before they ship. The benchmark already shows this on its own scenarios:
Both are made-up loads, not play. They show the kind of question the benchmark answers in seconds, without a play session. This PR changes neither the card nor the spare pools.
What to expect
How it was tested
Unit tests:
cargo test -p victory-tiers --locked: 33 passed, including checks that replays are deterministic and that notes from many threads on the same seconds count once;cargo test -p victory-gpu --lib --locked -- tiers:: shadow::: 76 passed.Fixed after review: with
VICTORY_WINDOW_GROW=1, windows made bygrown()were never reported to the tiers' refault book, so the "came back soon" half of the rule did nothing (only "in use" worked).grown()now reports each window it makes, keyed by its 4 MiB cell, since a regrown window rarely has the extent of the one dropped. A test covers it: a grown window dropped and made again counts as a refault and its hold goes up.cargo test -p victory-gpu --lib --locked: all pass but tests outside this PR that also fail or flake without it:shader_cache::tests::every_switch_of_the_translator_is_in_the_keyfails the same way on untouched41adb551;watch::tests::a_late_fault_leaves_the_guard_taken_sinceandhost::card_steps::tests::shared_scratch_eliminates_flight_settle_end_to_endeach failed once in a full parallel run, and pass alone, with and without this PR.Clippy:
cargo clippy -p victory-tiers --all-targets --locked -- -D warningsis clean. Nothing new in the victory-gpu lines touched. The workspace-D warningsrun stops invictory-shaderon41adb551, before this change.Benchmark for the window rule (
cargo run -p victory-tiers --release --example tiers_bench, made-up load: 900 windows, memory pressure rising over 5 minutes):The same load with plenty of memory gives identical results for both. Almost all of the gain comes from the "came back soon" part; the "in use" part adds little.
Limit of the benchmark:
sim::commitlooks at each rule once a simulated second (replay()callslook()once per second, and its load is modelled per second). It can't fairly evaluate rules whose state changes within a second, such assend::trim's low-water mark: sampled once a second, the mark sits at the pool's low point and the rule frees nothing, so any "win" comes from the harness's own cap eviction. Comparing such rules needs a finer-grained load and replay, which this PR does not add.Not yet verified: a play session on this branch. The play figures in "The problem" come from a fork build running the same rule.
Checked for overlap
Searched open PRs at
41adb551for "tier", "idle window", "windows let go", "commit pressure", "spare", "memory bench", "unified memory", "eviction".memguard.host/membench.rs(existing) measures what the real GPU layer holds over synthetic frames.victory-tierscompares decision rules on modelled loads. They complement each other.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mia03ther on 2026-11-30T11:10:57Z.
Sharing a
v2026.11.29.05measurement from a 16 GB RAM / RTX 5070 Ti Laptop setup (#3909) in support of tiered/residency reductions like this PR:VICTORY_TEXTURE_ROOM_PERCENT=78,VICTORY_ROOT_CACHE=0,VICTORY_PROCESS_MEMORY=0.24.8 ms(48% busy),preparing per framespent56.2–64.5 ms(lists' steps 29.8 ms,descriptor 13.5 mswith13,253raw twins/frame,card copies 8.2 ms), locking steady gameplay to~19.8 fps(50.6 msmedian). Meanwhile,21.0 GBprivate commit (including~3.3 GBbuffer mirror span) could only keep5.86 GBresident on 16 GB RAM (487 MBfree), driving694 / 10,933frames over 100 ms (worst4,295.9 ms). Lowering copy/commit footprint + submit work would be huge for 16 GB laptops.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by shurhundo on 2026-11-30T15:31:24Z.
Ran the crate's own tooling independently (Linux, via WSL) and read through the diff. One bug, and a limitation of the benchmark worth knowing about before using it to settle the other open memory PRs.
VICTORY_WINDOW_GROW=1silently disables the refault/Holds half of the fix.grown()inshadow.rs(around line 8570) makes a new window copy withmade_window()but never callscrate::tiers::window_made(), unlike the plain path inloose()(line 8448) that does.grow()is checked first thing inloose()(line 8409), so once that flag is set, every window creation in the process goes throughgrown()andwindow_made()is never called at all.Consequence:
Ghosts::made()never runs, so a window remade soon after being let go is never counted as a refault, andHolds::made()never advances, so the 20/40/60 s escalation never engages. Only the "Hot" (used in N of 44 s) half ofIdleRulestill works under this flag, since the sharednote()closure that updatessecondsis called fromgrown()too. The "Back" half, which the benchmark table in the PR description credits with almost all of the measured gain, does nothing for anyone running withVICTORY_WINDOW_GROW=1. No test exercises this;grown_windows_hold_the_asks_and_what_was_thereonly checksgrown_extent's arithmetic, not the tiers integration.The benchmark can't fairly evaluate the other open memory PRs it names in "Checked for overlap."
Tried using
sim::committo compare #3381/#3411's low-water-mark spare trimming againstTodayandRanked, since the PR description says the benchmark's commit model could do this. First pass gave a clean table with the low-water rule beating both existing rules on every metric. Instrumented it to check why, and the rule's ownlook()was returning zero frees in the TIGHT scenario; the "win" was entirely the harness's own cap-eviction fallback inreplay(), not the rule being tested.The reason:
send::trim()'s low-water mark is updated on every take and give-back, many times a second.sim::commit::replay()only callsrule.look()once a second. At that sampling rate the mark collapses to the pool's near-instantaneous low point almost every look, which in a cycling workload is often 0, so the rule degenerates into a no-op. This isn't a bug invictory-tiers, it's a real gap in what the harness can evaluate: any rule that depends on sub-second state can't be ported to it without either changing the harness's time resolution or giving rules a way to see finer-grained events than one snapshot a second.The other two named PRs don't fit the rule-comparison abstraction either, for different reasons. #1562 changes block layout and placement (16/8 MiB size-segregated pools against
sim::card's hardcoded 64 MiBBLOCK), which isn't somethingCardRulecan express; it ranks eviction order among fixed-size-block candidates, it doesn't model allocation strategy. #3454 isn't a competing rule at all, it's a one-line fix to when an existing eviction trigger becomes reachable, and the PR's own A/B is explicit that the result was inconclusive.None of this is a problem with the PR as written, since it never claims to have run these comparisons itself, just that the tool could. Worth knowing before anyone takes a
sim::commitorsim::cardtable as settling one of those PRs though.Tested:
cargo test -p victory-tiers --locked(33 passed) andcargo clippy -p victory-tiers --all-targets --locked -- -D warnings(clean), both on Linux/x86_64 via WSL, matching the PR's own Windows results. That also confirms the crate's "no Direct3D, no game code, no OS calls" portability claim independently, for whatever that's worth to other forks.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by fl4rion on 2026-11-30T23:34:02Z.
Thanks for running it independently and for reading the diff this closely. You're right on both counts.
VICTORY_WINDOW_GROW=1: fixed infc7d3930.grown()now reports every window it makes to the tiers, except one that takes in the cell's existing window (a growth, not a remake, so it doesn't reset the hold). With growing on, the refault book keys a window by its 4 MiB cell: a regrown window rarely has the extent of the one dropped, and keyed by extent the remake still wouldn't have counted. There's a test that drops a grown window, makes a smaller one in the same cell, and checks that it counts as a refault and its hold goes up.The benchmark: agreed.
sim::commitlooks at each rule once a simulated second, and its load is per second too, so a rule likesend::trim's low-water mark degenerates and the harness's cap eviction gets the credit. A finer-grained replay would need a sub-second load model, which is more than this PR should take on. I've added that limit to the description and corrected "Checked for overlap": the commit model can't evaluate #3381/#3411 as they are, and the card model can't express #1562's block sizes or #3454's change to when eviction starts.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by fl4rion on 2026-11-30T23:34:06Z.
Thanks for the numbers. A 16 GB laptop with 21 GB of commit and under 6 GB resident is exactly the squeeze this is aimed at. To be clear about scope: this PR cuts the churn of rebuilding window copies under memory pressure, so it helps the stalls more than the frame rate. Your 50 ms frames are mostly CPU-side preparation (lists, descriptors, card copies), which this doesn't touch. The commit and copy footprint you mention is the next step we're working on.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by shurhundo on 2026-12-01T02:37:40Z.
Flagging for awareness: #2910 (open) flips
VICTORY_WINDOW_GROWon by default and reshapesgrown()/grow_reach()/made_window()/buffer_sides()for its own memory-ledger/pooling work. It doesn't referencecrate::tiersor this PR'sgrown_made()/tiers_key()fix (fc7d3930) at all. Once #2910's default flip lands, that fix here stops being opt-in-only coverage and becomes the default-path correctness for everyone, so the two will need a careful rebase against each other rather than landing independently.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by fl4rion on 2026-12-01T05:14:26Z.
@shurhundo Makes sense. fwiw, I'm in favor of not using flags for functionality that is objectively more performant/correct. Once your PR lands I can rebase mine and make sure it keeps working correctly.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:49:27Z.
Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.
Pull request closed