Skip to content

mem-probe: the breakdown line says how much of each part is in host memory (RT-MEM-PROBE-HOST-SHARE) - #2767

Closed
kawaii-shakal wants to merge 3 commits into
ProjectLeonida:mainfrom
kawaii-shakal:mem-probe-host-share
Closed

kawaii-shakal wants to merge 3 commits into
ProjectLeonida:mainfrom
kawaii-shakal:mem-probe-host-share

Conversation

@kawaii-shakal

Copy link
Copy Markdown
Contributor

Item: RT-MEM-PROBE-HOST-SHARE (claimed in #2570). Part of step 1 (accounting) of #1702.

The [rt] mem-probe breakdown line splits the runtime's vkAllocateMemory bytes by consumer, but where they landed is only on the mem-probe t= line, summed over all consumers per memory type. A sampled texture that did not fit in the card's memory, the page mirror's homes once the budget is near, or the arena on a type without DEVICE_LOCAL (#1732) all look like memory on the card in the breakdown. Now each part adds its share in memory types outside the device-local heaps, which the GPU reads over the bus, and the total follows the parts:

allocated 7077 MiB = arena 4095 (1; 4095 in host memory) + ... + sampled textures 1800 (3; 900 in host memory) + render targets and depth 1180 (1) + ... + other 2 (1; 2 in host memory); in host memory 4997 MiB (4); imported 6 MiB (1); ...
  • Tally::add takes host (default false, so other callers are unchanged); mem_probe::sample passes it from the memory type's flags. Imported memory stays apart, as before.
  • A part with nothing in host memory prints as before, and the line's start (allocated N MiB = arena ...) is unchanged.
  • Diagnostic only: nothing changes what is allocated or where.
  • runtime/README.md: the LEONIDA_MEM_PROBE row says so.

Tests (Linux, CPU only, no GPU or game files):

  • runtime/test/memory_breakdown_test.cpp: new host_share case (per-consumer host bytes and counts, imported memory left out, an out-of-range consumer counted as other, the text) and the total in text(). Passes natively (g++ 13, -Wall -Wextra, no warnings) and as a clang-cl 21 + xwin build with /W4 /permissive- under Wine (3/3); fails with the host part no longer counted in Tally::add.
  • src/mem_probe.h type-checks with clang-cl /Zs (0 errors, 0 warnings, as on main). Not built with MSVC here, and not run with LEONIDA_MEM_PROBE on a GPU.
  • check_no_game_data.py on the four files and git diff --check are clean. Merges cleanly into main f44904d.

Heads-up: #2547 and #2549 (Tyrannys) also change memory_breakdown.h and its test, in other hunks (DeviceShare/format additions); both need a rebase on main already, and whichever lands second has a small textual merge.

AI-assisted (Claude Code): the change and the tests were written with it; I reviewed them and ran the tests.

🤖 Generated with Claude Code

…emory (RT-MEM-PROBE-HOST-SHARE)

The breakdown splits the runtime's allocations by consumer but not by where they landed; the memory types are only on
the mem-probe t= line, summed over every consumer. A texture that did not fit in the card's memory, or the arena on a
type without DEVICE_LOCAL, was not told apart from one in the card's memory. Each part now adds its share in memory
types outside the device-local heaps ("sampled textures 2300 (412; 870 in host memory)"), and "in host memory" sums
them after the parts. Imported memory stays apart, as before. Diagnostic only: nothing changes what is allocated or
where.

memory_breakdown_test: a new host_share case and the total in text(); it passes natively (g++) and as a clang-cl
build under Wine, and fails with the host part no longer counted. mem_probe.h type-checks with clang-cl /Zs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JPqXc6svM4pbBagTsVEYA3
@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR on macOS 27.2.1, Apple M5, Apple clang (-std=c++20 -Wall -Wextra -fsanitize=address,undefined), main ad25611b5, head 092834570.

  • MEASURED, green (main + this PR, merged; runtime/test/memory_breakdown_test.cpp built alone against runtime/src): memory_breakdown: ok, exit 0, no sanitizer report, no warning. The breakdown line now prints each part's host-memory share, e.g. arena 4095 (1; 4095 in host memory) and in host memory 4997 MiB (4).
  • MEASURED, red (main's headers + this PR's test): does not build (no member named 'host_total' in 'leonida::memory_breakdown::Tally'; the calls with a 4th argument). So no red failure can be shown: the test needs the new API.
  • Not covered: the CMake/ctest registration and the mem_probe.h call sites on Windows (CI), and the line in a real run.

AI-assisted: Claude Code

@fiestorra

Copy link
Copy Markdown
Contributor

Independent verification on this machine (Windows 11, RTX 4090, MSVC 19.44.35226), with a control - and with the first version of that control, which lied to me.

1. It applies and builds. merge-tree clean onto main be7690805; the change is runtime/src/memory_breakdown.h (+30/-5), runtime/src/mem_probe.h (+3/-1), its test (+33/-2), and one line of runtime/README.md.

2. With the branch: ctest -R '^memory_breakdown' -> 100% tests passed, 0 failed out of 1.

3. The control is at the compiler, not at the assertion. Revert only the two headers, keep the test, build:

runtime/test/memory_breakdown_test.cpp(43): error C2039: 'host_total': is not a member of ...
runtime/test/memory_breakdown_test.cpp(51): error C2660: 'add': function does not take 4 arguments
runtime/test/memory_breakdown_test.cpp(52): error C2660: same
runtime/test/memory_breakdown_test.cpp(53): error C2660: same

So the new test cannot exist without the change: it calls Tally::host_total(), add(..., host) and asserts the rendered line contains 900 in host memory, none of which main has. That is a compile-level control, and it is the honest classification - the behavioural half ("the line says how much of each part is in host memory") is asserted by the test, but I did not run the new binary against the old headers, because it cannot be built there.

The mistake worth recording, because it produced a false result in my own hands: the first time I ran that control, I piped the build to Out-Null without checking $LASTEXITCODE, then ran ctest — and it reported 100% tests passed. The build had failed and ctest ran the previous binary, the one built with the branch's headers. For a minute I had a "control that does not bite" in front of me, and it was my script, not the pull request.

What that costs and what it teaches: ctest after a failed build is a measurement of the last successful build, and it will pass happily. Every control I have published today says "revert, rebuild, run"; this one is the first where the rebuild failing is the answer, and checking the exit status is what distinguishes "the test is insensitive" from "the test could not be built".

What I did not test: the runtime path that feeds Tally with the host-memory flag (a texture that did not fit in VRAM, the arena on a non-DEVICE_LOCAL type). The test drives the type directly; whether mem_probe passes host correctly on a real card is not covered by this control.

AI-assisted: yes - DeepSeek v4.1 (flash) as the coding agent. Measured on Windows 11, MSVC 19.44.35226, RelWithDebInfo, no GPU needed for this test.

Written with an AI assistant.

@KelboLive

Copy link
Copy Markdown

Independent check on Windows (not the author), at 092834570.

  • Built memory_breakdown_test and ran it: memory_breakdown: ok (exit 0).
  • The line now reports host-resident amounts per part, e.g. arena 4095 (1; 4095 in host memory) and in host memory 4997 MiB (4), alongside the existing device-side breakdown.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711.

AI-assisted: yes (opencode).

@KelboLive KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The arithmetic is right: host bytes are a subset of bytes, not an addition (memory_breakdown.h:96-109), so total() and the allocated … = start are unchanged and the parts still sum. The test's line is 7077 = 4095 + 1800 + 1180 + 2 MiB, with 4997 host = 4095 + 900 + 2; imported memory is excluded from both; a part with no host memory prints exactly as before; and a zero-byte part prints 0 (0). The sample is recomputed from the live records, so nothing can go stale. Mutations behave: counting host bytes twice fails the total, and shrinking char item[256] to 48 fails the exact-text check.

The gap that matters: the rule lives only in mem_probe.h:550-552 and nothing tests it — memory_breakdown_test exercises Tally and format, but no test calls mem_probe::sample, so inverting the DEVICE_LOCAL test or dropping the flag would pass everything. A case with a synthetic DeviceFacts (one device-local memory type, one not) and two recorded allocations would pin it without a GPU; otherwise the feature is only verified by a run with LEONIDA_MEM_PROBE set.

Minor, all read-the-line issues:

  • The trailing (4) is the host allocation count while a part's (N) is that part's own count — the README example reads like "4 memory types"; say what it counts.
  • Parts and the host total are rounded independently to whole MiB, so the printed shares can be a MiB off the printed total (the parts already had this; now the host clause does too).
  • char item[256] truncates silently if a consumer name grows; asserting the snprintf return would make that visible (the exact-text checks catch it only because they exist).

Diagnostics only, nothing else touched, and the README row matches the text.

…tested; the line says what its counts are (RT-MEM-PROBE-HOST-SHARE)

Review follow-up (KelboLive):
- Which memory type counts as host memory was decided inline in mem_probe::sample and nothing tested it. It is now
  memory_breakdown::in_host_memory(type, type_count, flags): a reported type without DEVICE_LOCAL. sample() calls
  it. The new host_types case pins it for a discrete GPU (VRAM, system memory, the BAR window), an integrated one
  and a type index the device does not report; it fails with the type bound dropped.
- The format comment and runtime/README.md say that a number in parentheses is an allocation count, that each MiB
  figure is rounded on its own, and how long the longest items get against the 256-character buffer.

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

Thanks, all addressed in 5211e5c:

  • The rule is no longer inline in mem_probe::sample: it is memory_breakdown::in_host_memory(type, type_count, flags) (a reported type without DEVICE_LOCAL), which sample() calls. The new host_types case pins it without a GPU: a discrete GPU's VRAM, system memory and BAR window (device-local, so not host), an integrated GPU (all device-local), and a type index the device does not report (not counted). It fails with the type bound dropped. The one line in sample() that passes the allocation's type to it is still only type-checked (clang-cl /Zs), not run.
  • What the counts are: the format comment and the LEONIDA_MEM_PROBE row in runtime/README.md now say that a number in parentheses is an allocation count (in host memory 4997 MiB (4) is 4 allocations), as it already was for every other part of the line.
  • Rounding: both now say each MiB figure is rounded on its own, so the parts can be a MiB off the total.
  • item[256]: the longest items (the arena's bracket, the closing note) stay near 200 characters even with ten-digit figures, so nothing is cut today; the comment says so.

Linux (g++) and clang-cl + Wine (3/3) pass; merges cleanly into main aefb250.

AI-assisted (Claude Code); I ran the tests and checked the results.


Generated by Claude Code

@fiestorra

Copy link
Copy Markdown
Contributor

done: verified 5211e5c5 on today's main - it applies cleanly, builds, and the test passes.

main cd73ddab7, the branch's commits cherry-picked (merge-base 8f41d7d01, no conflict):

build:  cmake --build ... --target memory_breakdown_test     exit 0
test:   memory_breakdown_test.exe                            exit 0
  allocated 7077 MiB = arena 4095 (1; 4095 in host memory) + geometry copies 0 (0) + ...
  allocated 9961 MiB = arena 4095 (1) [reserved 4095, bound 4095, in use 757: buffers 500, ...]
  memory_breakdown: ok

So the rule moved to memory_breakdown::in_host_memory does the same job in the same place: 7077 MiB splits into 4095 (1; 4095 in host memory) and the rest, which is what the line was for. And the item[256] case does not wrap even with ten-digit figures, judged by the two rows above.

What I checked and what I did not: the build and the test binary on Windows/MSVC, so the g++ and clang-cl + Wine arms kawaii-shakal ran are not repeated here - the point of that matrix is that somebody ran them on other toolchains, and they did. What I add is that it still merges into the main of this hour, which is not the aefb2508 they merged into.

Nothing else to change from me: the two commits are the feature and the fix, and the review's four points are all visible in the diff.

AI-assisted: yes - DeepSeek v4.1 (flash), a build and a run on the merged tree.

Written with an AI assistant.

@aslan2142

Copy link
Copy Markdown
Collaborator

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

@aslan2142 aslan2142 closed this Dec 1, 2026
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