[GH-ISSUE #1702] Residency classes for title memory: keep one copy of GPU-read data in RAM or VRAM, not both #47

Open
opened 2026-11-26 20:16:02 +01:00 by 4ld0 · 2 comments
Owner

Originally created by @kawaii-shakal on GitHub (Nov 25, 2026).
Original GitHub issue: https://github.com/ubiquitarianism/leonida/issues/1702

Problem

On the console the title's assets live once, in 16 GB of memory the CPU and GPU share. On a PC the runtime keeps most GPU-read title data twice:

  • once in the title's own pages: pagefile-backed committed memory, much of it resident in RAM;
  • once again as a Vulkan copy (slot-70/slot-133 images, geometry views, title-memory windows, arena shadows).

Nothing ever releases the first copy after the second exists.

Measured on the retail boot (main c7e82e26e, RTX 5070 Ti 16 GB, 64 GB RAM, Windows 11 26300, LEONIDA_MEM_PROBE=10), at t=258 s, a few seconds before the int3 at +0x2474606 and the access violation:

process private bytes / working set 17.6 GB / 17.0 GB
system commit 67.1 of 75.3 GB, free RAM 0.8 GB
device-local Vulkan memory (heap 0, type 1) 1,849 allocations, 8,091 MiB
host-visible memory (heap 1, type 2) 4,103 MiB, almost all of it the arena (4,095 MiB, 1,319 MiB in use)
imported title memory 125 imports, 743 MiB
title memory 39 ranges, 63,714 MiB reserved/committed VA; 2,547 shadows

In the same log, title-memory imports that land below the arena fall back to an arena shadow (an import of title memory at ... landed below the arena ...: a shadow in the arena is used instead), so even data that could have been shared ends up copied.

Each run from #1537 onwards dies with commit and RAM exhausted. Raising LEONIDA_TITLE_WINDOWS_MB and LEONIDA_ARENA_MB (as in #1537) moves the point of failure, but the duplication stays.

Proposal: a residency class for every title range

Decide, per 64 KiB granule of title memory, where its one authoritative copy lives, and treat the other copy as a cache that can be dropped. This adds no new copy path; it only adds a rule for which existing copy may be released.

Signals the runtime already has

  • At allocation, in runtime/shims/xmem.cpp:
    • the PAGE_GRAPHICS_* bits (kPageGraphicsMask): the GPU reads the range;
    • a CPU protection of PAGE_NOACCESS: a GPU-only view (title_gpu_views);
    • PAGE_WRITECOMBINE: the CPU streams writes the GPU reads;
    • MEM_GRAPHICS on VirtualAlloc;
    • the XMemAlloc/XMemVirtualAlloc attributes, for example the 0x183, 0x283, 0x23 and 0x27 values the retail title passes for its large blocks.
  • At use, in the D3D12 layer: which object references the granule, whether a slot-70 texture, a slot-133 buffer or image, a geometry view, a window, or nothing.
  • Over time, from the write watch (title_write_watch / ShadowPages): whether the granule is rewritten every frame, was written once and then left alone, or is never written after its upload.

Classes and what each keeps

Class How it is recognised Authoritative copy The other copy
CPU-only no GPU reference, no PAGE_GRAPHICS_* title pages none (as today)
GPU-static (streamed textures, meshes, BVH inputs) GPU reference; no CPU write for K submissions after the upload Vulkan copy in VRAM title pages are trimmed from the working set: VirtualUnlock on a range that is not locked removes it from the working set (documented in the Win32 VirtualUnlock remarks). The OS writes it to the pagefile and pages it back in transparently if the CPU touches it, so no fault handler and no GPU readback are needed. A new CPU write sends the granule back to GPU-dynamic.
GPU-dynamic (constant rings, per-frame buffers) GPU reference; written in most submissions title pages no VRAM copy: import the pages (VK_EXT_external_memory_host, already used for slot 133) so the GPU reads them in place over PCIe, instead of refreshing an arena shadow before every submission
GPU-written (UAV targets, readbacks) written by GPU commands Vulkan copy unchanged; copies are one-way today

Changes, in order (each one measurable on its own)

  1. Accounting only. The memory probe reports bytes per class, and how many of them are both resident in RAM and copied to the GPU. This tells us how big the duplicate is before anything changes behaviour.
  2. Arena placement. In this run the 4 GiB arena sits in host memory (type 2), although the device has a ReBAR type (type 4, device-local, host-visible, coherent, heap 0). By section 3.3's "Memory type policy" it should prefer device-local when large BAR is available. Find out why it does not here. Placing it in device-local memory takes 4 GiB of host commit off the table.
  3. Imports that land below the arena. Let an import below the arena be reached at its device address minus heap_base, as the arena already allows for imports above it, wherever the 32-bit table-pointer rule permits. Otherwise reserve the arena low enough for imports to land above it. Either way, a GPU-dynamic buffer keeps one copy.
  4. Working-set trim for GPU-static granules. Use VirtualUnlock after K clean submissions, behind LEONIDA_TITLE_RESIDENCY (off by default until it has been measured). This frees RAM only, not commit; commit is helped by steps 2 and 3.

What this does not do

  • It does not change the VA layout, the console VAs the title sees, or the one-way direction of the copies.
  • Page faults stay the OS's: a trimmed page that the CPU reads is paged in from the pagefile, not reconstructed from VRAM. Re-tiling a slot-70 image back into console layout is not needed.
  • It does not cover physical-page sections, whose commit is charged when the section is created. For those, only the RAM side (step 4) helps.

How to check it

  • Steps 1–3: probe figures at the same point of a fresh-save retail boot, before and after, with commit, private bytes, working set, VRAM and import counts. Compare against the table above.
  • Step 4: the same, plus a unit test of the classifier fed synthetic allocation, reference and write-watch events (no game data). A boot reaching the point where boot14 died, with free RAM staying above zero.

Open question: what value of K separates a streamed asset from a per-frame buffer without trimming something that is about to be rewritten. Step 1's write-watch histogram should answer it.

Related: #1537 (title-window cap), section 3.3 of docs/d3d12_vulkan.md (arena, imports, write watch), runtime/shims/xmem.cpp (kPageGraphicsMask, title_gpu_views).

AI-assisted: yes (Claude Code, Claude Opus 5.5). The figures come from one retail boot on the hardware above, and the class rules are a proposal that has not been measured.

🤖 Generated with Claude Code

Originally created by @kawaii-shakal on GitHub (Nov 25, 2026). Original GitHub issue: https://github.com/ubiquitarianism/leonida/issues/1702 ### Problem On the console the title's assets live once, in 16 GB of memory the CPU and GPU share. On a PC the runtime keeps most GPU-read title data twice: - once in the title's own pages: pagefile-backed committed memory, much of it resident in RAM; - once again as a Vulkan copy (slot-70/slot-133 images, geometry views, title-memory windows, arena shadows). Nothing ever releases the first copy after the second exists. Measured on the retail boot (main c7e82e26e, RTX 5070 Ti 16 GB, 64 GB RAM, Windows 11 26300, `LEONIDA_MEM_PROBE=10`), at t=258 s, a few seconds before the int3 at `+0x2474606` and the access violation: | | | |---|---| | process private bytes / working set | 17.6 GB / 17.0 GB | | system commit | 67.1 of 75.3 GB, free RAM 0.8 GB | | device-local Vulkan memory (heap 0, type 1) | 1,849 allocations, 8,091 MiB | | host-visible memory (heap 1, type 2) | 4,103 MiB, almost all of it the arena (4,095 MiB, 1,319 MiB in use) | | imported title memory | 125 imports, 743 MiB | | title memory | 39 ranges, 63,714 MiB reserved/committed VA; 2,547 shadows | In the same log, title-memory imports that land below the arena fall back to an arena shadow (`an import of title memory at ... landed below the arena ...: a shadow in the arena is used instead`), so even data that could have been shared ends up copied. Each run from #1537 onwards dies with commit and RAM exhausted. Raising `LEONIDA_TITLE_WINDOWS_MB` and `LEONIDA_ARENA_MB` (as in #1537) moves the point of failure, but the duplication stays. ### Proposal: a residency class for every title range Decide, per 64 KiB granule of title memory, where its one authoritative copy lives, and treat the other copy as a cache that can be dropped. This adds no new copy path; it only adds a rule for which existing copy may be released. **Signals the runtime already has** - At allocation, in `runtime/shims/xmem.cpp`: - the `PAGE_GRAPHICS_*` bits (`kPageGraphicsMask`): the GPU reads the range; - a CPU protection of `PAGE_NOACCESS`: a GPU-only view (`title_gpu_views`); - `PAGE_WRITECOMBINE`: the CPU streams writes the GPU reads; - `MEM_GRAPHICS` on `VirtualAlloc`; - the `XMemAlloc`/`XMemVirtualAlloc` attributes, for example the 0x183, 0x283, 0x23 and 0x27 values the retail title passes for its large blocks. - At use, in the D3D12 layer: which object references the granule, whether a slot-70 texture, a slot-133 buffer or image, a geometry view, a window, or nothing. - Over time, from the write watch (`title_write_watch` / `ShadowPages`): whether the granule is rewritten every frame, was written once and then left alone, or is never written after its upload. **Classes and what each keeps** | Class | How it is recognised | Authoritative copy | The other copy | |---|---|---|---| | CPU-only | no GPU reference, no `PAGE_GRAPHICS_*` | title pages | none (as today) | | GPU-static (streamed textures, meshes, BVH inputs) | GPU reference; no CPU write for K submissions after the upload | Vulkan copy in VRAM | title pages are trimmed from the working set: `VirtualUnlock` on a range that is not locked removes it from the working set (documented in the Win32 `VirtualUnlock` remarks). The OS writes it to the pagefile and pages it back in transparently if the CPU touches it, so no fault handler and no GPU readback are needed. A new CPU write sends the granule back to GPU-dynamic. | | GPU-dynamic (constant rings, per-frame buffers) | GPU reference; written in most submissions | title pages | no VRAM copy: import the pages (`VK_EXT_external_memory_host`, already used for slot 133) so the GPU reads them in place over PCIe, instead of refreshing an arena shadow before every submission | | GPU-written (UAV targets, readbacks) | written by GPU commands | Vulkan copy | unchanged; copies are one-way today | **Changes, in order (each one measurable on its own)** 1. **Accounting only.** The memory probe reports bytes per class, and how many of them are both resident in RAM and copied to the GPU. This tells us how big the duplicate is before anything changes behaviour. 2. **Arena placement.** In this run the 4 GiB arena sits in host memory (type 2), although the device has a ReBAR type (type 4, device-local, host-visible, coherent, heap 0). By section 3.3's "Memory type policy" it should prefer device-local when large BAR is available. Find out why it does not here. Placing it in device-local memory takes 4 GiB of host commit off the table. 3. **Imports that land below the arena.** Let an import below the arena be reached at its device address minus `heap_base`, as the arena already allows for imports above it, wherever the 32-bit table-pointer rule permits. Otherwise reserve the arena low enough for imports to land above it. Either way, a GPU-dynamic buffer keeps one copy. 4. **Working-set trim for GPU-static granules.** Use `VirtualUnlock` after K clean submissions, behind `LEONIDA_TITLE_RESIDENCY` (off by default until it has been measured). This frees RAM only, not commit; commit is helped by steps 2 and 3. ### What this does not do - It does not change the VA layout, the console VAs the title sees, or the one-way direction of the copies. - Page faults stay the OS's: a trimmed page that the CPU reads is paged in from the pagefile, not reconstructed from VRAM. Re-tiling a slot-70 image back into console layout is not needed. - It does not cover physical-page sections, whose commit is charged when the section is created. For those, only the RAM side (step 4) helps. ### How to check it - Steps 1–3: probe figures at the same point of a fresh-save retail boot, before and after, with commit, private bytes, working set, VRAM and import counts. Compare against the table above. - Step 4: the same, plus a unit test of the classifier fed synthetic allocation, reference and write-watch events (no game data). A boot reaching the point where boot14 died, with free RAM staying above zero. Open question: what value of K separates a streamed asset from a per-frame buffer without trimming something that is about to be rewritten. Step 1's write-watch histogram should answer it. Related: #1537 (title-window cap), section 3.3 of `docs/d3d12_vulkan.md` (arena, imports, write watch), `runtime/shims/xmem.cpp` (`kPageGraphicsMask`, `title_gpu_views`). AI-assisted: yes (Claude Code, Claude Opus 5.5). The figures come from one retail boot on the hardware above, and the class rules are a proposal that has not been measured. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@kawaii-shakal commented on GitHub (Nov 25, 2026):

Progress on the steps above: step 1 (accounting) is #1734, step 2 (the arena out of ReBAR memory) is #1732. The cause of step 2 turned out to be the buffer's memoryTypeBits: NVIDIA leaves the ReBAR type out above about 4,091 MiB, so LEONIDA_ARENA_MB=4096 landed in system memory. Step 3 (imports below the arena): only 8 imports of up to 64 KiB fell back to shadows in that boot, so it is not worth doing for now.

<!-- gh-comment-id:7159346874 --> @kawaii-shakal commented on GitHub (Nov 25, 2026): Progress on the steps above: step 1 (accounting) is #1734, step 2 (the arena out of ReBAR memory) is #1732. The cause of step 2 turned out to be the buffer's `memoryTypeBits`: NVIDIA leaves the ReBAR type out above about 4,091 MiB, so `LEONIDA_ARENA_MB=4096` landed in system memory. Step 3 (imports below the arena): only 8 imports of up to 64 KiB fell back to shadows in that boot, so it is not worth doing for now.
Author
Owner

@Tyrannys commented on GitHub (Nov 26, 2026):

@kawaii-shakal is the residency-class step itself (the class table: per-granule classes and releasing the non-authoritative copy) free for someone to take? On a 12 GB card the world runs over the VRAM budget: on an RTX 4070 SUPER (NVIDIA 621.01, Windows 11, 64 GB RAM), the title's own device-local allocations come to about 9.3 GB plus our arena, 12-14.1 GB in use against a 10.6 GB budget at world entry, with the Series S device type already set. Shrinking LEONIDA_ARENA_MB/LEONIDA_TITLE_WINDOWS_MB doesn't get it under budget. The world then draws 1-7 frames a minute.

If it's free, I'd start with the VRAM half: GPU-dynamic granules (written in most submissions) keep the title pages as the authoritative copy and get no device-local copy, behind an env switch, measured with #1734's accounting on that card. Happy to leave the RAM half (trimming GPU-static title pages) to you or to do it after, whichever you prefer. Counts only.

AI-assisted (Claude Code).

<!-- gh-comment-id:7192310192 --> @Tyrannys commented on GitHub (Nov 26, 2026): @kawaii-shakal is the residency-class step itself (the class table: per-granule classes and releasing the non-authoritative copy) free for someone to take? On a 12 GB card the world runs over the VRAM budget: on an RTX 4070 SUPER (NVIDIA 621.01, Windows 11, 64 GB RAM), the title's own device-local allocations come to about 9.3 GB plus our arena, 12-14.1 GB in use against a 10.6 GB budget at world entry, with the Series S device type already set. Shrinking `LEONIDA_ARENA_MB`/`LEONIDA_TITLE_WINDOWS_MB` doesn't get it under budget. The world then draws 1-7 frames a minute. If it's free, I'd start with the VRAM half: GPU-dynamic granules (written in most submissions) keep the title pages as the authoritative copy and get no device-local copy, behind an env switch, measured with #1734's accounting on that card. Happy to leave the RAM half (trimming GPU-static title pages) to you or to do it after, whichever you prefer. Counts only. AI-assisted (Claude Code).
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#47
No description provided.