Skip to content

Tiered memory for unified memory emulation - #3723

Open
fl4rion wants to merge 4 commits into
trustno1-coder:mainfrom
fl4rion:memory-tiers
Open

fl4rion wants to merge 4 commits into
trustno1-coder:mainfrom
fl4rion:memory-tiers

Conversation

@fl4rion

@fl4rion fl4rion commented Nov 30, 2026 •

Copy link
Copy Markdown

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:

  • on the card (fast for the GPU, but the card's budget is limited);
  • in system RAM the card can read over the bus (slow per read, and it uses commit);
  • in plain system RAM (CPU only);
  • nowhere: dropped, and made again from the console's memory the next time it is needed.

Every copy costs something wherever it lives:

  • card budget;
  • the system's commit charge;
  • bus traffic;
  • CPU time to refresh it when the game writes;
  • or, when it was dropped too early, the time to rebuild it.

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:

  • Usage statistics per copy: when it was last used, and in which of the last 44 seconds it was used, written by the CPU, or written by the GPU. They are kept in a few atomics on the copy itself, and noting a use costs a load and a compare.
  • A record of what was dropped, so a copy rebuilt soon after counts as a refault: the one number that says whether dropping it was a mistake.
  • One budget type for "how much may we hold": a limit, a line where we are over, hysteresis so it does not flap, and a cooling period after being over.
  • Rules built on these, starting with the window rule below.
  • A benchmark for memory rules that needs no game. It replays a workload, made up from a seed or recorded in play, against two or more rules. It prints what each rule rebuilt, copied and held. Every run gives the same numbers, every scenario has a control in which all rules must agree, and it runs the same rule code the emulator runs.

2. The window fix (victory-gpu). Under memory pressure, a window is no longer dropped just because it sat idle for 2–5 seconds:

  • a window used in at least 8 of the last 44 seconds waits at least 10 seconds;
  • a window that came back soon after being dropped waits 20 seconds the next time, then 40, then at most 60.

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:

  • Card memory (textures in 64 MiB blocks, the card's budget cut mid-run): ranking evictions by "used most often" sounds right but doubles pop-in when play moves between areas. Plain least-recently-used stays best.
  • Spare buffers under a tight commit limit: freeing the coldest spares across all pools, only as much as needed, makes about half as many buffers as trimming each pool on its own, with the same memory left at every second.

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

  • Under memory pressure: fewer rebuild stalls. In the stretch above, that was 11–15 rebuilds a second, each 1–3 ms plus a 6 MiB copy.
  • Memory held under pressure is a little higher: the windows kept are ones that were coming back anyway.
  • Frame rate in normal play: no change expected. This is about memory decisions and stutter under pressure, not the frame's critical path.

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.
  • 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_key fails the same way on untouched 41adb551;
    • watch::tests::a_late_fault_leaves_the_guard_taken_since and host::card_steps::tests::shared_scratch_eliminates_flight_settle_end_to_end each 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 warnings is clean. Nothing new in the victory-gpu lines touched. The workspace -D warnings run stops in victory-shader on 41adb551, 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):

    Windows built Rebuilt within 30 s of being dropped Time spent building Memory held under pressure
    Before 3,290 1,289 10.7 s 2.1 GB
    This PR 2,655 (−19 %) 791 (−39 %) 8.7 s (−19 %) 2.5 GB (+21 %)

    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.

  • 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 41adb551 for "tier", "idle window", "windows let go", "commit pressure", "spare", "memory bench", "unified memory", "eviction".

…pies let go to tell refaults, budgets with hysteresis, idle rules) and a deterministic benchmark replaying rules against made-up loads or traces (cargo run -p victory-tiers --release --example tiers_bench)
…or in 8 of the last 44 s waits 10 s before it is let go, and one made again within 30 s of its last let-go waits 20, 40, then 60 s, where the PC's idle time alone gave 2-5 s and windows in use were let go and made again seconds later; with memory to spare nothing changes
@github-actions github-actions Bot added the pr-performance Pull request: makes it faster or use less memory label Nov 30, 2026
@github-actions github-actions Bot changed the title [pr-performance] Tiered memory: decide where copies of console memory live by how they are used, and stop rebuilding windows that are still in use Tiered memory: decide where copies of console memory live by how they are used, and stop rebuilding windows that are still in use Nov 30, 2026
@fl4rion fl4rion changed the title Tiered memory: decide where copies of console memory live by how they are used, and stop rebuilding windows that are still in use [pr-performance] Tiered memory for unified memory emulation Nov 30, 2026
@github-actions github-actions Bot changed the title [pr-performance] Tiered memory for unified memory emulation Tiered memory for unified memory emulation Nov 30, 2026
…e line every thread of a hot copy writes; the policy module and IdleRule's floor described as they work; tests for a reader behind the newest note, notes from the past ignored, many threads on the same seconds, the cut keeping warm spares and freeing a tied group, limits not known, and block_covering in the crate itself
…ooks every second), the line's words as it counts; the windows' steps looked up under one hold of the lock a sweep; the books forget past their window when windows are made too, so they empty and take no lock again
moritz-eckhard-nickel added a commit to moritz-eckhard-nickel/VIctory-NVIDIA that referenced this pull request Nov 30, 2026
…ry emulation)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mia03ther

Copy link
Copy Markdown

Sharing a v2026.11.29.05 measurement from a 16 GB RAM / RTX 5070 Ti Laptop setup (#3909) in support of tiered/residency reductions like this PR:

  • Setup: Series S 540p, DLSS Performance, FG off, VICTORY_TEXTURE_ROOM_PERCENT=78, VICTORY_ROOT_CACHE=0, VICTORY_PROCESS_MEMORY=0.
  • Numbers: While GPU card time was only 24.8 ms (48% busy), preparing per frame spent 56.2–64.5 ms (lists' steps 29.8 ms, descriptor 13.5 ms with 13,253 raw twins/frame, card copies 8.2 ms), locking steady gameplay to ~19.8 fps (50.6 ms median). Meanwhile, 21.0 GB private commit (including ~3.3 GB buffer mirror span) could only keep 5.86 GB resident on 16 GB RAM (487 MB free), driving 694 / 10,933 frames over 100 ms (worst 4,295.9 ms). Lowering copy/commit footprint + submit work would be huge for 16 GB laptops.

@shurhundo

shurhundo commented Nov 30, 2026 •

Copy link
Copy Markdown

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=1 silently disables the refault/Holds half of the fix.

grown() in shadow.rs (around line 8570) makes a new window copy with made_window() but never calls crate::tiers::window_made(), unlike the plain path in loose() (line 8448) that does. grow() is checked first thing in loose() (line 8409), so once that flag is set, every window creation in the process goes through grown() and window_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, and Holds::made() never advances, so the 20/40/60 s escalation never engages. Only the "Hot" (used in N of 44 s) half of IdleRule still works under this flag, since the shared note() closure that updates seconds is called from grown() 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 with VICTORY_WINDOW_GROW=1. No test exercises this; grown_windows_hold_the_asks_and_what_was_there only checks grown_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::commit to compare #3381/#3411's low-water-mark spare trimming against Today and Ranked, 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 own look() was returning zero frees in the TIGHT scenario; the "win" was entirely the harness's own cap-eviction fallback in replay(), 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 calls rule.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 in victory-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 MiB BLOCK), which isn't something CardRule can 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::commit or sim::card table as settling one of those PRs though.

Tested: cargo test -p victory-tiers --locked (33 passed) and cargo 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request: makes it faster or use less memory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants