d3d12: memory probe reports how much of the title memory behind filled images is in RAM (RT-MEM-PROBE-DUPLICATE) #1734

Open
github-bot wants to merge 5 commits from github-bot/gh-1734 into main AGit
Owner

@⁠kawaii-shakal on GitHub · 2026-11-25 10:11 UTC · original
kawaii-shakal:mem-probe-duplicates → main · +183 −5 · 5 files · 5 commits · open

Claim: RT-MEM-PROBE-DUPLICATE, #1608 (step 1 of #1702)

What and why

#1702 shows that the GPU-read title data is kept twice: once in the title's own pages (committed, much of it resident) and again as Vulkan copies. On the retail boot the slot-70 and slot-133 images alone came to about 7.4 GB of VRAM. Before anything trims or shares those pages, the probe has to say how big the duplicate is. Today it reports only the GPU side.

  • runtime/src/working_set.h (new, header-only): leonida::working_set::resident_bytes(base, size, step = 16). It estimates how much of a range is resident in RAM by sampling one 4 KiB page in step with one QueryWorkingSetEx call per range. Pages that are not committed or not mapped read as not resident. Off Windows it returns 0.
  • runtime/shims/d3d12_vulkan_device.cpp, log_title_census: with LEONIDA_MEM_PROBE, the existing registered image backing line now ends with title pages behind filled images X MiB, Y MiB of them resident in RAM. Only images filled from memory count (fills, a known layout, not gpu_written). The working set is queried only when the probe is on.
  • docs/d3d12_vulkan.md: the probe paragraph says this.

Test

  • working_set_test (new ctest working_set, Windows, psapi) checks these ranges:
    • 8 MiB written: all resident;
    • 8 MiB committed but never touched: 0;
    • reserved only: 0;
    • half written: half;
    • the written range after VirtualUnlock, which removes unlocked pages from the working set: under a quarter.
  • d3d12_existing_heap_test with LEONIDA_MEM_PROBE=1 on an RTX 5070 Ti (NVIDIA 617.42, Windows 11 Pro 26300) passes and prints the new fields.
  • gta6rt builds (MSVC 19.50). scripts/check_no_game_data.py is clean.

Not done here: a retail boot with this branch, so the size of the duplicate on 1.0.2.1 is not measured yet. That boot is the point of this PR. Title windows and arena shadows (buffers) are not counted; only images are.

AI-assisted: yes (Claude Code, Claude Opus 5.5).

🤖 Generated with Claude Code

**@⁠kawaii-shakal** on GitHub · 2026-11-25 10:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1734) `kawaii-shakal:mem-probe-duplicates` → `main` · +183 −5 · 5 files · 5 commits · open Claim: RT-MEM-PROBE-DUPLICATE, https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 (step 1 of #1702) ### What and why #1702 shows that the GPU-read title data is kept twice: once in the title's own pages (committed, much of it resident) and again as Vulkan copies. On the retail boot the slot-70 and slot-133 images alone came to about 7.4 GB of VRAM. Before anything trims or shares those pages, the probe has to say how big the duplicate is. Today it reports only the GPU side. - `runtime/src/working_set.h` (new, header-only): `leonida::working_set::resident_bytes(base, size, step = 16)`. It estimates how much of a range is resident in RAM by sampling one 4 KiB page in `step` with one `QueryWorkingSetEx` call per range. Pages that are not committed or not mapped read as not resident. Off Windows it returns 0. - `runtime/shims/d3d12_vulkan_device.cpp`, `log_title_census`: with `LEONIDA_MEM_PROBE`, the existing `registered image backing` line now ends with `title pages behind filled images X MiB, Y MiB of them resident in RAM`. Only images filled from memory count (`fills`, a known layout, not `gpu_written`). The working set is queried only when the probe is on. - `docs/d3d12_vulkan.md`: the probe paragraph says this. ### Test - `working_set_test` (new ctest `working_set`, Windows, psapi) checks these ranges: - 8 MiB written: all resident; - 8 MiB committed but never touched: 0; - reserved only: 0; - half written: half; - the written range after `VirtualUnlock`, which removes unlocked pages from the working set: under a quarter. - `d3d12_existing_heap_test` with `LEONIDA_MEM_PROBE=1` on an RTX 5070 Ti (NVIDIA 617.42, Windows 11 Pro 26300) passes and prints the new fields. - `gta6rt` builds (MSVC 19.50). `scripts/check_no_game_data.py` is clean. Not done here: a retail boot with this branch, so the size of the duplicate on 1.0.2.1 is not measured yet. That boot is the point of this PR. Title windows and arena shadows (buffers) are not counted; only images are. AI-assisted: yes (Claude Code, Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Step 1 of #1702: before any change that trims or shares those pages, the
probe has to show how big the duplicate is. With LEONIDA_MEM_PROBE the
registered-image line now adds the title pages behind images filled from
memory and how much of them is resident, sampled with QueryWorkingSetEx
(one page in 16, runtime/src/working_set.h). GPU-written images are left
out. working_set_test covers written, untouched, reserved, half-written
and trimmed ranges.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
AI-assisted by Codex.
Codex resolved the documentation overlap, retaining both the unique-memory diagnostic and the upstream registry snapshot notes.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:03:04 +00:00
Author
Owner

@⁠lixonik on GitHub · 2026-11-25 10:29 UTC · original · reviewed

Independent native Windows verification at c870341f: built working_set_test with MSVC and ran it five times. All passed. A second synthetic check exercised two paired title addresses that resolve to one CPU range. The per-image sum can count that range twice. Details inline.

AI-assisted. CPU only, no title workload or GPU run.

**@⁠lixonik** on GitHub · 2026-11-25 10:29 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1734#pullrequestreview-6055863377) · **reviewed** Independent native Windows verification at `c870341f`: built `working_set_test` with MSVC and ran it five times. All passed. A second synthetic check exercised two paired title addresses that resolve to one CPU range. The per-image sum can count that range twice. Details inline. AI-assisted. CPU only, no title workload or GPU run.
Author
Owner

@⁠lixonik on GitHub · 2026-11-25 10:29 UTC · original
On runtime/shims/d3d12_vulkan_device.cpp line 11213 (outdated):

@@ -11196,22 +11197,32 @@ class Device final
             refused += (refused.empty() ? "" : "; ") + why + " " + std::to_string(n);
         std::size_t owned = 0, slot133 = 0, mapped = 0, filled = 0;
         VkDeviceSize owned_bytes = 0, placed_bytes = 0, host_heap_bytes = 0;
+        // The title pages a filled image was made from, and how much of them is still in RAM: the copy the GPU
+        // image duplicates (#1702). Only with the memory probe on, since it queries the working set per texture.
+        std::uint64_t source_bytes = 0, resident_source_bytes = 0;
+        const bool probe = leonida::mem_probe::interval_seconds() != 0;
         for (const auto& [address, t] : state_.title_textures) {
             (t->owned ? owned : slot133) += 1;
             if (!t->owned && t->unbound_bytes) t->set_resource(t->resource);
             mapped += t->mapped;
             filled += t->fills > 0;
             (t->owned ? owned_bytes : placed_bytes) += t->backing_bytes;
             if (!t->device_local) host_heap_bytes += t->backing_bytes;
+            if (probe && t->fills && !t->gpu_written && t->layout_known && t->cpu && t->layout.layout.size) {
+                source_bytes += t->layout.layout.size;
+                resident_source_bytes += leonida::working_set::resident_bytes(t->cpu, t->layout.layout.size);

These totals sum per texture key, but two keys can refer to the same CPU pages. Registration keys the map by the original address and uses title_gpu_views::to_cpu for the backing. I paired two addresses using the real view mapper and pointed both synthetic entries at one touched 128 KiB range. Summing the helper results here produces 256 KiB resident, while the unique range is 128 KiB. If the diagnostic is meant to show the amount of distinct CPU memory behind the GPU copies, could it merge the CPU intervals before sampling? If the per-image sum is intentional, labeling it as such would make it clear that shared pages can be counted more than once.

**@⁠lixonik** on GitHub · 2026-11-25 10:29 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1734#discussion_r4693551629) On `runtime/shims/d3d12_vulkan_device.cpp` line 11213 (outdated): ```diff @@ -11196,22 +11197,32 @@ class Device final refused += (refused.empty() ? "" : "; ") + why + " " + std::to_string(n); std::size_t owned = 0, slot133 = 0, mapped = 0, filled = 0; VkDeviceSize owned_bytes = 0, placed_bytes = 0, host_heap_bytes = 0; + // The title pages a filled image was made from, and how much of them is still in RAM: the copy the GPU + // image duplicates (#1702). Only with the memory probe on, since it queries the working set per texture. + std::uint64_t source_bytes = 0, resident_source_bytes = 0; + const bool probe = leonida::mem_probe::interval_seconds() != 0; for (const auto& [address, t] : state_.title_textures) { (t->owned ? owned : slot133) += 1; if (!t->owned && t->unbound_bytes) t->set_resource(t->resource); mapped += t->mapped; filled += t->fills > 0; (t->owned ? owned_bytes : placed_bytes) += t->backing_bytes; if (!t->device_local) host_heap_bytes += t->backing_bytes; + if (probe && t->fills && !t->gpu_written && t->layout_known && t->cpu && t->layout.layout.size) { + source_bytes += t->layout.layout.size; + resident_source_bytes += leonida::working_set::resident_bytes(t->cpu, t->layout.layout.size); ``` These totals sum per texture key, but two keys can refer to the same CPU pages. Registration keys the map by the original address and uses `title_gpu_views::to_cpu` for the backing. I paired two addresses using the real view mapper and pointed both synthetic entries at one touched 128 KiB range. Summing the helper results here produces 256 KiB resident, while the unique range is 128 KiB. If the diagnostic is meant to show the amount of distinct CPU memory behind the GPU copies, could it merge the CPU intervals before sampling? If the per-image sum is intentional, labeling it as such would make it clear that shared pages can be counted more than once.
Author
Owner

@⁠kawaii-shakal on GitHub · 2026-11-25 16:37 UTC · original
On runtime/shims/d3d12_vulkan_device.cpp line 11213 (outdated):

@@ -11196,22 +11197,32 @@ class Device final
             refused += (refused.empty() ? "" : "; ") + why + " " + std::to_string(n);
         std::size_t owned = 0, slot133 = 0, mapped = 0, filled = 0;
         VkDeviceSize owned_bytes = 0, placed_bytes = 0, host_heap_bytes = 0;
+        // The title pages a filled image was made from, and how much of them is still in RAM: the copy the GPU
+        // image duplicates (#1702). Only with the memory probe on, since it queries the working set per texture.
+        std::uint64_t source_bytes = 0, resident_source_bytes = 0;
+        const bool probe = leonida::mem_probe::interval_seconds() != 0;
         for (const auto& [address, t] : state_.title_textures) {
             (t->owned ? owned : slot133) += 1;
             if (!t->owned && t->unbound_bytes) t->set_resource(t->resource);
             mapped += t->mapped;
             filled += t->fills > 0;
             (t->owned ? owned_bytes : placed_bytes) += t->backing_bytes;
             if (!t->device_local) host_heap_bytes += t->backing_bytes;
+            if (probe && t->fills && !t->gpu_written && t->layout_known && t->cpu && t->layout.layout.size) {
+                source_bytes += t->layout.layout.size;
+                resident_source_bytes += leonida::working_set::resident_bytes(t->cpu, t->layout.layout.size);

Thanks, you were right: two paired addresses that resolve to one CPU range were counted twice. Fixed in 355420e81 (in the current head, 516190908). The census now collects the CPU ranges of the filled images, merges overlapping and adjacent ones (working_set::merge_cpu_ranges), and samples residency once per merged range. The log line says "unique title memory behind filled images". working_set_test gained your case (two aliases of one 128 KiB range count as 128 KiB) plus nested, overlapping and adjacent ranges. The fix was made by Codex, who works on this with me; sorry for not saying so here earlier. AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠kawaii-shakal** on GitHub · 2026-11-25 16:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1734#discussion_r4697825553) On `runtime/shims/d3d12_vulkan_device.cpp` line 11213 (outdated): ```diff @@ -11196,22 +11197,32 @@ class Device final refused += (refused.empty() ? "" : "; ") + why + " " + std::to_string(n); std::size_t owned = 0, slot133 = 0, mapped = 0, filled = 0; VkDeviceSize owned_bytes = 0, placed_bytes = 0, host_heap_bytes = 0; + // The title pages a filled image was made from, and how much of them is still in RAM: the copy the GPU + // image duplicates (#1702). Only with the memory probe on, since it queries the working set per texture. + std::uint64_t source_bytes = 0, resident_source_bytes = 0; + const bool probe = leonida::mem_probe::interval_seconds() != 0; for (const auto& [address, t] : state_.title_textures) { (t->owned ? owned : slot133) += 1; if (!t->owned && t->unbound_bytes) t->set_resource(t->resource); mapped += t->mapped; filled += t->fills > 0; (t->owned ? owned_bytes : placed_bytes) += t->backing_bytes; if (!t->device_local) host_heap_bytes += t->backing_bytes; + if (probe && t->fills && !t->gpu_written && t->layout_known && t->cpu && t->layout.layout.size) { + source_bytes += t->layout.layout.size; + resident_source_bytes += leonida::working_set::resident_bytes(t->cpu, t->layout.layout.size); ``` Thanks, you were right: two paired addresses that resolve to one CPU range were counted twice. Fixed in `355420e81` (in the current head, `516190908`). The census now collects the CPU ranges of the filled images, merges overlapping and adjacent ones (`working_set::merge_cpu_ranges`), and samples residency once per merged range. The log line says "unique title memory behind filled images". `working_set_test` gained your case (two aliases of one 128 KiB range count as 128 KiB) plus nested, overlapping and adjacent ranges. The fix was made by Codex, who works on this with me; sorry for not saying so here earlier. AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠BadgersMC on GitHub · 2026-11-26 16:30 UTC · original

Native Windows check at 12767d1f. The PR's working_set_test passed three runs with MSVC and the target's compile definitions.

I also tested two distinct CPU addresses mapped to offset zero in the same 4 MiB section. A write through the first view was visible through the second at every page. The current range union and working-set estimate both reported 8 MiB in all three runs, including when sampling every page. This exercises a different case from the resolved review, which merged two references to the same CPU address range. Leonida's page mapping code can create different CPU addresses for the same backing.

Could the log label say CPU virtual bytes behind filled images and document that distinct views of one backing may count twice? The current probe is still useful with that interpretation. The effect in a title run remains unmeasured. Synthetic Windows test and AI-assisted interpretation reviewed with Claude Opus 5.5.

**@⁠BadgersMC** on GitHub · 2026-11-26 16:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1734#issuecomment-7197742885) Native Windows check at `12767d1f`. The PR's `working_set_test` passed three runs with MSVC and the target's compile definitions. I also tested two distinct CPU addresses mapped to offset zero in the same 4 MiB section. A write through the first view was visible through the second at every page. The current range union and working-set estimate both reported **8 MiB** in all three runs, including when sampling every page. This exercises a different case from the resolved review, which merged two references to the same CPU address range. Leonida's [page mapping code](https://github.com/ubiquitarianism/leonida/blob/2e2c51c0929cae8195366e52adfef17156a355d2/runtime/shims/xmem.cpp#L1993-L1998) can create different CPU addresses for the same backing. Could the log label say **CPU virtual bytes behind filled images** and document that distinct views of one backing may count twice? The current probe is still useful with that interpretation. The effect in a title run remains unmeasured. Synthetic Windows test and AI-assisted interpretation reviewed with Claude Opus 5.5.
Review of RT-MEM-PROBE-DUPLICATE (BadgersMC): two distinct CPU views of one
backing both reported 8 MiB. merge_cpu_ranges merges by virtual address, so
the same physical frames mapped at two addresses are counted in each, in the
total and in the resident part. The log line now reads "CPU virtual bytes
behind filled images ... (two views of one backing count twice)", and
working_set.h and docs/d3d12_vulkan.md say so.

Text and comments only; no behaviour change.

Changes made by Claude Code (AI-assisted).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JPqXc6svM4pbBagTsVEYA3
Author
Owner

@⁠kawaii-shakal on GitHub · 2026-11-26 18:26 UTC · original

@⁠BadgersMC thanks, you are right: merge_cpu_ranges merges by virtual address, so the same frames mapped at two
addresses count once per view, in the total and in the resident part. Claude Code (AI-assisted) changed this in
523b09fd, wording only, no behaviour change:

  • the census line now reads "CPU virtual bytes behind filled images ... (two views of one backing count twice)";
  • working_set.h and docs/d3d12_vulkan.md say why: the union is by virtual address, so distinct views of one
    backing are not merged.

Counting physical frames instead (QueryWorkingSetEx gives a frame number for resident pages) would need a frame
set per sample. I'd leave that for a follow-up if the duplicate views turn out to matter in real runs.

Not built here: the shim is Windows-only. working_set.h compiles on Linux.


Generated by Claude Code

**@⁠kawaii-shakal** on GitHub · 2026-11-26 18:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1734#issuecomment-7200462855) @⁠BadgersMC thanks, you are right: `merge_cpu_ranges` merges by virtual address, so the same frames mapped at two addresses count once per view, in the total and in the resident part. Claude Code (AI-assisted) changed this in 523b09fd, wording only, no behaviour change: - the census line now reads "CPU virtual bytes behind filled images ... (two views of one backing count twice)"; - `working_set.h` and `docs/d3d12_vulkan.md` say why: the union is by virtual address, so distinct views of one backing are not merged. Counting physical frames instead (`QueryWorkingSetEx` gives a frame number for resident pages) would need a frame set per sample. I'd leave that for a follow-up if the duplicate views turn out to matter in real runs. Not built here: the shim is Windows-only. `working_set.h` compiles on Linux. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1734'
git switch 'github-bot/gh-1734'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1734'
git switch 'github-bot/gh-1734'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1734'
git switch 'main'
git merge --squash 'github-bot/gh-1734'
git switch 'main'
git merge --ff-only 'github-bot/gh-1734'
git switch 'main'
git merge 'github-bot/gh-1734'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.