Skip to content

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

Open
kawaii-shakal wants to merge 6 commits into
ProjectLeonida:mainfrom
kawaii-shakal:mem-probe-duplicates
Open

kawaii-shakal wants to merge 6 commits into
ProjectLeonida:mainfrom
kawaii-shakal:mem-probe-duplicates

Conversation

@kawaii-shakal

Copy link
Copy Markdown
Contributor

Claim: RT-MEM-PROBE-DUPLICATE, #1608 (comment) (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

…d images is in RAM (RT-MEM-PROBE-DUPLICATE)

Step 1 of ProjectLeonida#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 <noreply@anthropic.com>

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread runtime/shims/d3d12_vulkan_device.cpp Outdated
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 and others added 2 commits November 25, 2026 12:38
Codex resolved the documentation overlap, retaining both the unique-memory diagnostic and the upstream registry snapshot notes.
@BadgersMC

Copy link
Copy Markdown
Contributor

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.

…ws of one backing count twice

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JPqXc6svM4pbBagTsVEYA3
@kawaii-shakal

Copy link
Copy Markdown
Contributor Author

@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
523b09f, 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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants