From c870341fa441e911e804c7f67e33bdb847cdeecc Mon Sep 17 00:00:00 2001 From: kawaii-shakal Date: Wed, 25 Nov 2026 15:10:46 +0500 Subject: [PATCH 1/3] d3d12: memory probe reports how much of the title memory behind filled images is in RAM (RT-MEM-PROBE-DUPLICATE) 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 --- docs/d3d12_vulkan.md | 5 ++- runtime/CMakeLists.txt | 8 +++++ runtime/shims/d3d12_vulkan_device.cpp | 15 ++++++-- runtime/src/working_set.h | 49 +++++++++++++++++++++++++++ runtime/test/working_set_test.cpp | 45 ++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 runtime/src/working_set.h create mode 100644 runtime/test/working_set_test.cpp diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index d988a45f14..ba749bcd85 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2803,7 +2803,10 @@ The console has two list topologies Vulkan lacks: rectangle list (`IASetPrimitiv slice can be reused. Large images, dedicated-only images, and failed pool allocations use individual memory allocations. `LEONIDA_TITLE_IMAGE_POOL=0` selects individual allocation for every image. With `LEONIDA_MEM_PROBE` enabled, the texture census also reports logical Vulkan backing bytes for the registered - slot-70 and slot-133 images and the portion on non-device-local types. + slot-70 and slot-133 images and the portion on non-device-local types. It also reports the title pages behind the images + it has filled from memory, and how much of them is resident in RAM, sampled with `QueryWorkingSetEx` (one page + in 16, `runtime/src/working_set.h`): the CPU-side copy the GPU image duplicates (#1702). Images the GPU writes + (render targets, depth, unordered access) are not filled from memory and are left out. - Sampled title images can use device-local memory even when Vulkan host image copies require a host-visible memory type. The runtime queries the same image with and without host-transfer usage. When only the latter permits plain device-local backing, it detiles the complete image into a reusable diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 14cc5fbf23..d72267c833 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -303,6 +303,14 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME) target_link_options(xmem_test PRIVATE /OPT:NOREF) target_link_libraries(xmem_test PRIVATE psapi) + # The memory probe's resident-bytes estimate (src/working_set.h): committed and touched pages read as resident, a + # reserved range and pages dropped from the working set do not. + add_executable(working_set_test test/working_set_test.cpp) + target_compile_definitions(working_set_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_compile_options(working_set_test PRIVATE /W4 /permissive- /utf-8) + target_link_libraries(working_set_test PRIVATE psapi) + add_test(NAME working_set COMMAND working_set_test) + # The title's CLZERO loops rewritten to stubs on a CPU without CLZERO (src/clzero_patch.h): both loop shapes are # found, patched and run, so the test runs on any x86-64 CPU. add_executable(clzero_patch_test test/clzero_patch_test.cpp) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 5dd6b42e47..afe22e67d0 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -34,6 +34,7 @@ #include "../src/uav_clear.h" #include "../src/vulkan_bindless.h" #include "../src/vulkan_memory.h" +#include "../src/working_set.h" #include "../src/vulkan_mesh_limits.h" #include "../src/resource_tiling.h" #include "../src/cached_pso.h" @@ -11196,6 +11197,10 @@ 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); @@ -11203,15 +11208,21 @@ class Device final 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); + } } if (leonida::mem_probe::interval_seconds()) log_title_image_uses(state_.title_textures, retired_title_textures_.size(), retired_backing_, state_.zero_image_bytes.load()); if (leonida::mem_probe::interval_seconds()) std::fprintf(stderr, "[rt] mem-probe: registered image backing: %.1f MiB from slot 70, %.1f MiB from slot " - "133, %.1f MiB total on non-device-local types; staged fills %llu, %.3f s, buffer %.1f MiB\n", + "133, %.1f MiB total on non-device-local types; staged fills %llu, %.3f s, buffer %.1f MiB; " + "title pages behind filled images %.1f MiB, %.1f MiB of them resident in RAM\n", double(owned_bytes) / 1048576.0, double(placed_bytes) / 1048576.0, double(host_heap_bytes) / 1048576.0, static_cast(s.staged_fills), - double(s.staged_ns) / 1e9, double(state_.title_upload.capacity()) / 1048576.0); + double(s.staged_ns) / 1e9, double(state_.title_upload.capacity()) / 1048576.0, + double(source_bytes) / 1048576.0, double(resident_source_bytes) / 1048576.0); std::fprintf(stderr, "[rt] d3d12: title textures (%s): %llu slot-70 texture views (%zu kinds), %llu of slot-133 textures, " "%llu of images made for them (%llu made, %llu over memory not readable yet, %llu retired), %llu " diff --git a/runtime/src/working_set.h b/runtime/src/working_set.h new file mode 100644 index 0000000000..504a90ffa4 --- /dev/null +++ b/runtime/src/working_set.h @@ -0,0 +1,49 @@ +#pragma once + +// How much of a range of this process's memory is resident in RAM, sampled with QueryWorkingSetEx (psapi): one 4 KiB +// page in every `step`, one call per range. Used by the memory probe to show how much of the title memory behind a +// GPU copy is also held in RAM, which is the duplicate docs/d3d12_vulkan.md and #1702 describe. An estimate, not an +// exact count: a range smaller than `step` pages is read from its first page only. Pages that are not committed, or +// not mapped at all, read as not resident. + +#include +#include + +#ifdef _WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#include +#endif + +namespace leonida::working_set { + +constexpr std::uint64_t kPage = 4096; + +// Estimated resident bytes of [base, base + size): the share of sampled pages that are valid in the working set, +// times size. 0 for an empty range or where the query is not available. +inline std::uint64_t resident_bytes(std::uint64_t base, std::uint64_t size, unsigned step = 16) { +#ifdef _WIN32 + if (!size || !step) return 0; + const std::uint64_t first = base & ~(kPage - 1); + const std::uint64_t pages = (base + size - first + kPage - 1) / kPage; + const std::uint64_t samples = (pages + step - 1) / step; + std::vector info(static_cast(samples)); + for (std::uint64_t i = 0; i < samples; ++i) + info[static_cast(i)].VirtualAddress = reinterpret_cast(first + i * step * kPage); + if (!QueryWorkingSetEx(GetCurrentProcess(), info.data(), + static_cast(info.size() * sizeof(PSAPI_WORKING_SET_EX_INFORMATION)))) + return 0; + std::uint64_t valid = 0; + for (const auto& e : info) valid += e.VirtualAttributes.Valid; + return size * valid / samples; +#else + (void)base; + (void)size; + (void)step; + return 0; +#endif +} + +} // namespace leonida::working_set diff --git a/runtime/test/working_set_test.cpp b/runtime/test/working_set_test.cpp new file mode 100644 index 0000000000..8f732a899e --- /dev/null +++ b/runtime/test/working_set_test.cpp @@ -0,0 +1,45 @@ +// src/working_set.h: the memory probe's estimate of how much of a range is resident in RAM. Synthetic memory only. +#include "../src/working_set.h" + +#include +#include + +namespace { +int failures = 0; +void check(bool ok, const char* what) { + if (!ok) { + std::fprintf(stderr, "FAIL: %s\n", what); + ++failures; + } +} +} // namespace + +int main() { + using leonida::working_set::resident_bytes; + constexpr SIZE_T size = 8u << 20; + auto* touched = static_cast(VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + auto* untouched = static_cast(VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + auto* reserved = static_cast(VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_NOACCESS)); + check(touched && untouched && reserved, "allocations"); + if (failures) return 1; + std::memset(touched, 1, size); + const auto base = [](const void* p) { return reinterpret_cast(p); }; + + check(resident_bytes(base(touched), size) == size, "written pages are resident"); + check(resident_bytes(base(untouched), size) == 0, "committed pages never touched are not resident"); + check(resident_bytes(base(reserved), size) == 0, "reserved pages are not resident"); + check(resident_bytes(base(touched), 0) == 0, "an empty range"); + // Half of a range written: the estimate is half of its size (every 16th page sampled, 2,048 pages). + std::memset(untouched, 1, size / 2); + check(resident_bytes(base(untouched), size) == size / 2, "half written, half resident"); + // VirtualUnlock on a range that is not locked removes it from the working set (Win32 VirtualUnlock remarks). + VirtualUnlock(touched, size); + check(resident_bytes(base(touched), size) < size / 4, "pages dropped from the working set are not resident"); + + VirtualFree(touched, 0, MEM_RELEASE); + VirtualFree(untouched, 0, MEM_RELEASE); + VirtualFree(reserved, 0, MEM_RELEASE); + if (failures) return 1; + std::printf("working_set: written, untouched, reserved, half-written and trimmed ranges\n"); + return 0; +} From 355420e812dd729663f551725b0f206e36af82cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=9A=D0=BE?= =?UTF-8?q?=D1=80=D0=BB=D0=BE=D0=B4=D0=BD=D1=8B=D0=B9?= Date: Wed, 25 Nov 2026 10:54:07 +0000 Subject: [PATCH 2/3] d3d12: count overlapping image source memory once AI-assisted by Codex. --- docs/d3d12_vulkan.md | 8 +++--- runtime/CMakeLists.txt | 19 ++++++++------ runtime/shims/d3d12_vulkan_device.cpp | 23 +++++++++------- runtime/src/working_set.h | 21 +++++++++++++++ runtime/test/working_set_test.cpp | 38 +++++++++++++++++++++++---- 5 files changed, 83 insertions(+), 26 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index ba749bcd85..135cebd670 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2803,10 +2803,10 @@ The console has two list topologies Vulkan lacks: rectangle list (`IASetPrimitiv slice can be reused. Large images, dedicated-only images, and failed pool allocations use individual memory allocations. `LEONIDA_TITLE_IMAGE_POOL=0` selects individual allocation for every image. With `LEONIDA_MEM_PROBE` enabled, the texture census also reports logical Vulkan backing bytes for the registered - slot-70 and slot-133 images and the portion on non-device-local types. It also reports the title pages behind the images - it has filled from memory, and how much of them is resident in RAM, sampled with `QueryWorkingSetEx` (one page - in 16, `runtime/src/working_set.h`): the CPU-side copy the GPU image duplicates (#1702). Images the GPU writes - (render targets, depth, unordered access) are not filled from memory and are left out. + slot-70 and slot-133 images and the portion on non-device-local types. It also reports the unique CPU memory + behind images filled from title memory and estimates how much of it is resident in RAM (#1702). The census + merges overlapping CPU address ranges before sampling one page in 16 with `QueryWorkingSetEx` + (`runtime/src/working_set.h`). Images the GPU writes (render targets, depth, unordered access) are left out. - Sampled title images can use device-local memory even when Vulkan host image copies require a host-visible memory type. The runtime queries the same image with and without host-transfer usage. When only the latter permits plain device-local backing, it detiles the complete image into a reusable diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index d72267c833..dc94c86506 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -39,6 +39,17 @@ if(MSVC) target_link_options(root_signature_test PRIVATE /OPT:NOREF) endif() +# The memory probe merges CPU ranges on every platform. Windows also tests the working-set query on real pages. +add_executable(working_set_test test/working_set_test.cpp) +if(WIN32) + target_compile_definitions(working_set_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_link_libraries(working_set_test PRIVATE psapi) +endif() +if(MSVC) + target_compile_options(working_set_test PRIVATE /W4 /permissive- /utf-8) +endif() +add_test(NAME working_set COMMAND working_set_test) + option(LEONIDA_BUILD_WINDOWS_RUNTIME "Build the Windows runtime, its shims and their tests" ${WIN32}) if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME) # Custom CMAKE_CXX_FLAGS can replace MSVC's default /EHsc. Require C++ stack unwinding so error @@ -303,14 +314,6 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME) target_link_options(xmem_test PRIVATE /OPT:NOREF) target_link_libraries(xmem_test PRIVATE psapi) - # The memory probe's resident-bytes estimate (src/working_set.h): committed and touched pages read as resident, a - # reserved range and pages dropped from the working set do not. - add_executable(working_set_test test/working_set_test.cpp) - target_compile_definitions(working_set_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) - target_compile_options(working_set_test PRIVATE /W4 /permissive- /utf-8) - target_link_libraries(working_set_test PRIVATE psapi) - add_test(NAME working_set COMMAND working_set_test) - # The title's CLZERO loops rewritten to stubs on a CPU without CLZERO (src/clzero_patch.h): both loop shapes are # found, patched and run, so the test runs on any x86-64 CPU. add_executable(clzero_patch_test test/clzero_patch_test.cpp) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index afe22e67d0..ec88a5eed8 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -11197,9 +11197,8 @@ 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; + // With the probe on, collect the CPU ranges that filled the images. Aliases may share a range. + std::vector sources; const bool probe = leonida::mem_probe::interval_seconds() != 0; for (const auto& [address, t] : state_.title_textures) { (t->owned ? owned : slot133) += 1; @@ -11208,21 +11207,27 @@ class Device final 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); + if (probe && t->fills && !t->gpu_written && t->layout_known && t->cpu && t->layout.layout.size && + t->layout.layout.size <= UINT64_MAX - t->cpu) { + sources.push_back({t->cpu, t->cpu + t->layout.layout.size}); } } - if (leonida::mem_probe::interval_seconds()) + if (probe) { + leonida::working_set::merge_cpu_ranges(sources); + std::uint64_t source_bytes = 0, resident_source_bytes = 0; + for (const auto& [begin, end] : sources) { + source_bytes += end - begin; + resident_source_bytes += leonida::working_set::resident_bytes(begin, end - begin); + } log_title_image_uses(state_.title_textures, retired_title_textures_.size(), retired_backing_, state_.zero_image_bytes.load()); - if (leonida::mem_probe::interval_seconds()) std::fprintf(stderr, "[rt] mem-probe: registered image backing: %.1f MiB from slot 70, %.1f MiB from slot " "133, %.1f MiB total on non-device-local types; staged fills %llu, %.3f s, buffer %.1f MiB; " - "title pages behind filled images %.1f MiB, %.1f MiB of them resident in RAM\n", + "unique title memory behind filled images %.1f MiB, %.1f MiB of it resident in RAM\n", double(owned_bytes) / 1048576.0, double(placed_bytes) / 1048576.0, double(host_heap_bytes) / 1048576.0, static_cast(s.staged_fills), double(s.staged_ns) / 1e9, double(state_.title_upload.capacity()) / 1048576.0, double(source_bytes) / 1048576.0, double(resident_source_bytes) / 1048576.0); + } std::fprintf(stderr, "[rt] d3d12: title textures (%s): %llu slot-70 texture views (%zu kinds), %llu of slot-133 textures, " "%llu of images made for them (%llu made, %llu over memory not readable yet, %llu retired), %llu " diff --git a/runtime/src/working_set.h b/runtime/src/working_set.h index 504a90ffa4..588c7ebac1 100644 --- a/runtime/src/working_set.h +++ b/runtime/src/working_set.h @@ -6,6 +6,7 @@ // exact count: a range smaller than `step` pages is read from its first page only. Pages that are not committed, or // not mapped at all, read as not resident. +#include #include #include @@ -21,6 +22,26 @@ namespace leonida::working_set { constexpr std::uint64_t kPage = 4096; +struct CpuRange { + std::uint64_t begin; + std::uint64_t end; +}; + +// Texture aliases and overlapping views can refer to the same CPU bytes. Count their union. +inline void merge_cpu_ranges(std::vector& ranges) { + std::sort(ranges.begin(), ranges.end(), [](const CpuRange& a, const CpuRange& b) { return a.begin < b.begin; }); + std::size_t count = 0; + for (const CpuRange range : ranges) { + if (range.begin >= range.end) continue; + if (count && range.begin <= ranges[count - 1].end) { + ranges[count - 1].end = std::max(ranges[count - 1].end, range.end); + } else { + ranges[count++] = range; + } + } + ranges.resize(count); +} + // Estimated resident bytes of [base, base + size): the share of sampled pages that are valid in the working set, // times size. 0 for an empty range or where the query is not available. inline std::uint64_t resident_bytes(std::uint64_t base, std::uint64_t size, unsigned step = 16) { diff --git a/runtime/test/working_set_test.cpp b/runtime/test/working_set_test.cpp index 8f732a899e..0c500bda6b 100644 --- a/runtime/test/working_set_test.cpp +++ b/runtime/test/working_set_test.cpp @@ -12,16 +12,36 @@ void check(bool ok, const char* what) { ++failures; } } -} // namespace +void check_cpu_ranges() { + using namespace leonida::working_set; + constexpr std::uint64_t start = 0x100000; + constexpr std::uint64_t size = 128 << 10; + std::vector ranges{{start, start + size}, {start, start + size}}; + merge_cpu_ranges(ranges); + check(ranges.size() == 1 && ranges[0].begin == start && ranges[0].end == start + size, + "two aliases count as 128 KiB of unique CPU memory"); -int main() { + ranges = {{50, 60}, {25, 40}, {10, 30}, {20, 22}, {40, 45}, {80, 80}}; + merge_cpu_ranges(ranges); + check(ranges.size() == 2 && ranges[0].begin == 10 && ranges[0].end == 45 && + ranges[1].begin == 50 && ranges[1].end == 60, + "overlapping, nested and adjacent ranges merge without filling gaps"); +} + +#ifdef _WIN32 +void check_resident_ranges() { using leonida::working_set::resident_bytes; constexpr SIZE_T size = 8u << 20; auto* touched = static_cast(VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); auto* untouched = static_cast(VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); auto* reserved = static_cast(VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_NOACCESS)); check(touched && untouched && reserved, "allocations"); - if (failures) return 1; + if (!touched || !untouched || !reserved) { + if (touched) VirtualFree(touched, 0, MEM_RELEASE); + if (untouched) VirtualFree(untouched, 0, MEM_RELEASE); + if (reserved) VirtualFree(reserved, 0, MEM_RELEASE); + return; + } std::memset(touched, 1, size); const auto base = [](const void* p) { return reinterpret_cast(p); }; @@ -39,7 +59,15 @@ int main() { VirtualFree(touched, 0, MEM_RELEASE); VirtualFree(untouched, 0, MEM_RELEASE); VirtualFree(reserved, 0, MEM_RELEASE); - if (failures) return 1; std::printf("working_set: written, untouched, reserved, half-written and trimmed ranges\n"); - return 0; +} +#endif +} // namespace + +int main() { + check_cpu_ranges(); +#ifdef _WIN32 + check_resident_ranges(); +#endif + return failures ? 1 : 0; } From 523b09fd3e918744c97bd47ff33b27a7d4a18842 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 18:06:36 +0000 Subject: [PATCH 3/3] mem-probe: the census line says "CPU virtual bytes", and that two views 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 Claude-Session: https://claude.ai/code/session_01JPqXc6svM4pbBagTsVEYA3 --- docs/d3d12_vulkan.md | 4 +++- runtime/shims/d3d12_vulkan_device.cpp | 3 ++- runtime/src/working_set.h | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 0772c719a5..96d968e697 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2909,7 +2909,9 @@ The console has two list topologies Vulkan lacks: rectangle list (`IASetPrimitiv slot-70 and slot-133 images and the portion on non-device-local types. It also reports the unique CPU memory behind images filled from title memory and estimates how much of it is resident in RAM (#1702). The census merges overlapping CPU address ranges before sampling one page in 16 with `QueryWorkingSetEx` - (`runtime/src/working_set.h`). Images the GPU writes (render targets, depth, unordered access) are left out. + (`runtime/src/working_set.h`). Images the GPU writes (render targets, depth, unordered access) are left out. The + merge is by virtual address, so the log line says "CPU virtual bytes": two distinct views of one physical backing + (the same frames mapped at two addresses) count twice, in the total and in the resident part. - A refresh pass visits a snapshot of the title-image registry, taken under its mutex, instead of walking the registry itself. Filling a texture can submit, a submission destroys a retired image, and that image's destructor erases its registry entry. Windows' mutex is recursive for its owner, so the erase lands inside the pass, and a walk diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index faae441733..7dd3c70493 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -12891,7 +12891,8 @@ class Device final log_title_image_uses(state_.title_textures, retired_title_textures_.size(), retired_backing_, state_.zero_image_bytes.load()); std::fprintf(stderr, "[rt] mem-probe: registered image backing: %.1f MiB from slot 70, %.1f MiB from slot " "133, %.1f MiB total on non-device-local types; staged fills %llu, %.3f s, buffer %.1f MiB; " - "unique title memory behind filled images %.1f MiB, %.1f MiB of it resident in RAM\n", + "CPU virtual bytes behind filled images %.1f MiB, %.1f MiB of it resident in RAM (two views " + "of one backing count twice)\n", double(owned_bytes) / 1048576.0, double(placed_bytes) / 1048576.0, double(host_heap_bytes) / 1048576.0, static_cast(s.staged_fills), double(s.staged_ns) / 1e9, double(state_.title_upload.capacity()) / 1048576.0, diff --git a/runtime/src/working_set.h b/runtime/src/working_set.h index 588c7ebac1..de98210cf2 100644 --- a/runtime/src/working_set.h +++ b/runtime/src/working_set.h @@ -27,7 +27,9 @@ struct CpuRange { std::uint64_t end; }; -// Texture aliases and overlapping views can refer to the same CPU bytes. Count their union. +// Texture aliases and overlapping views can refer to the same CPU bytes. Count their union, by virtual address: two +// distinct virtual views of one physical backing (a placeholder mapped twice, a GPU-only alias paired with a CPU view) +// do not overlap here, so their bytes count twice, and so does their residency. inline void merge_cpu_ranges(std::vector& ranges) { std::sort(ranges.begin(), ranges.end(), [](const CpuRange& a, const CpuRange& b) { return a.begin < b.begin; }); std::size_t count = 0;