Skip to content

startup: Tracy memory pools for XMemAlloc and the stand-ins' C++ heaps (X2W_TRACY) - #2573

Open
NiiRoZz wants to merge 1 commit into
R0Y00:mainfrom
NiiRoZz:startup/tracy-memory
Open

NiiRoZz wants to merge 1 commit into
R0Y00:mainfrom
NiiRoZz:startup/tracy-memory

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

What

X2W_TRACY=ON builds now report memory to the Tracy profiler, next to the existing zones:

  • XMemAlloc / XMemFree are reported as the named pool XMemAlloc.
  • Each instrumented stand-in reports its own C++ heap (operator new / delete, plain and aligned) as a pool named <target> C++ heap, through one new file, src/startup_stubs/tracy_new.cpp, added to those targets only when the option is on.
  • XMemVirtualAlloc commits are a cumulative plot (bytes).

profile_zone.h gains X2W_ALLOC, X2W_FREE and X2W_PLOT. With the option off they expand to ((void)0), tracy_new.cpp is not compiled, and no stand-in changes behavior.

Why

Long runs of the stand-ins grow in committed memory, and the log alone does not say which allocator owns the growth. Tracy's memory view gives live size and allocation counts per pool over time.

Tests

cmake --build out/build/tracy  (X2W_TRACY=ON)  --target xmem_shim d3d12_xs_shim startup_stub_acphal startup_stub_xcrdapi startup_stub_chad startup_stub_xapu startup_stub_xg_xs startup_stub_gameinput   -> builds
cmake --build out/build/off    (X2W_TRACY off) --target xmem_shim                                                                                                                   -> builds
git diff --check                                                                                                                                                                    -> clean

What stays unverified

  • No capture was taken with this build yet: that the pools appear in the profiler as intended is not measured.
  • Only the build was checked; no ctest run (the default build compiles none of the new code except the empty macros).
  • Desktop D3D12 objects and VirtualAlloc blocks made by drivers are outside these pools.
  • TRACY_NO_CALLSTACK stays set, so allocations carry no call stacks.

AI-assisted.

🤖 Generated with Claude Code

…s (X2W_TRACY)

X2W_ALLOC, X2W_FREE and X2W_PLOT join the zone macros. XMemAlloc/XMemFree feed the pool "XMemAlloc", each
instrumented DLL reports its operator new/delete as "<target> C++ heap", and XMemVirtualAlloc commits are a
cumulative plot (the title frees those ranges with Win32 calls). Without the option the macros are no-ops.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

The opt-in build works — I configured and built it — and this PR changes six files with no test at all

X2W_TRACY is OFF by default, so the default build only ever compiles the ((void)0) arms of the three new macros.
That is why I did not stop at "the default tests still pass": almost none of this PR executes there. I configured the
option on in a separate build directory instead (out/build/tracy-probe, leaving out/build/dev untouched).

cmake --preset dev -B out/build/tracy-probe -DX2W_TRACY=ON     → configure OK
    Parsing public/common/TracyVersion.hpp file
    -- TRACY_ENABLE: ON
cmake --build out/build/tracy-probe --target xmem_shim          → exit 0
    [4/14] Building CXX object CMakeFiles\xmem_shim.dir\src\startup_stubs\tracy_new.cpp.obj

Two things that were worth checking and that do hold:

  • The quoting survives. target_compile_definitions(${target} PRIVATE X2W_TRACY_HEAP="${target} C++ heap") reaches
    the compiler as
    X2W_TRACY_HEAP="\"xmem_shim C++ heap\""
    
    i.e. a string literal containing a space, which is the case this kind of CMake usually gets wrong. It is right here.
  • The global operator new/delete override links into a DLL. tracy_new.cpp defines all four forms (plain,
    array-via-runtime, aligned, and the two matching deletes) and xmem_shim.dll builds with it. Replacing the C++
    heap in a DLL is the kind of change that fails at link time with a duplicate-symbol or at run time with a mismatched
    free; neither happened.

What has no assertion

The PR touches six files and adds no fixture. Under the default build the following are therefore unconstrained:

behaviour why it matters
operator new(0) allocates 1 byte rather than calling malloc(0) a malloc(0) that returns null would turn a legal empty allocation into std::bad_alloc
operator new(std::size_t, std::align_val_t) passes bytes ? bytes : 1 same, plus the alignment argument is forwarded unchanged
operator delete(void*, std::align_val_t) pairs _aligned_free with _aligned_malloc the classic mismatch — a plain free() here is a heap corruption that only shows under ASan or a debug heap
operator delete(nullptr) is a no-op the null check is defensive; free(nullptr) is legal, so this one is genuinely inert
the X2W_PLOT counter is cumulative (committed += bytes) committed = bytes would still compile and still plot, but the curve would be wrong — and nothing reads it

The last one is the one I would not want to be wrong silently: xmem_shim.cpp plots cumulative committed bytes and the
comment says the title releases those ranges with Win32 calls this function never sees, so the plot is deliberately
not a live gauge. A regression to assignment would look identical in Tracy and be wrong.

What a check would cost

The four operator new/delete forms are testable with no GPU and no Tracy: build the file into a small fixture with
X2W_TRACY undefined, allocate 0 bytes and a 64-byte alignment, free both through the matching delete, and assert the
pointer is non-null and _CrtIsValidHeapPointer (or just a debug-heap run) stays quiet. That would catch the
_aligned_free/free mismatch, which is the one that turns into a crash much later.

If the intent is that this file is measurement-only and stays untested, docs/startup-runtime.md (which this PR does
touch) is the place to say so — the same way the dxc_compile_time_gvn capability skip is documented.

Environment

Windows 11 build 26300, MSVC x64 via the Build Tools dev shell, CMake/Ninja. Tracy v0.15.0 was fetched by the configure
above. out/build/dev was left at X2W_TRACY:BOOL=OFF for every other measurement in this session.

AI-assisted.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review of head e36ad70 (10 commits behind main; the PR's own diff is 6 files, +63/-8). Code review only; this is a Windows-only C++ change and I did not build it here.

Read through, no defects found in the logic:

  • src/xmem_shim.cpp XMemAlloc: the permissive-fallback branch now assigns allocation = local_alloc(...) and skips the graphics-tracking else if, which matches the old early return. The tracking and release-on-failure path is unchanged. X2W_FREE runs before local_free, so the pool sees every freed block once.
  • src/startup_stubs/tracy_new.cpp: bytes ? bytes : 1, _aligned_malloc paired with _aligned_free, null-safe deletes. Array, sized and nothrow forms forward to these four.
  • src/xmem_mapped_pages.cpp: the plot counter is cumulative by design (comment explains why), guarded by #if X2W_TRACY.
  • profile_zone.h: with the option off the three macros are ((void)0).

Why I am not taking this to a merge:

  1. No test guards anything new. tracy_new.cpp and the plot counter only compile with X2W_TRACY=ON, which no CTest target uses (see ChazY11's comment above; I agree with the table there).
  2. XMemAlloc is a runtime path of the stand-ins in the default build, and its control flow was rewritten. The existing xmem tests cover the allocator, but none of them exercises the permissive fallback together with the new X2W_ALLOC placement, and there is no negative control.
  3. The PR body says no capture was taken, so the pools showing up in Tracy is unmeasured.

To make it mergeable: add a small fixture that builds tracy_new.cpp with X2W_TRACY undefined-safe stubs and checks new(0), aligned new/delete and delete(nullptr) under the debug heap, plus a mutation (_aligned_free -> free) that fails it. Or, if the file is meant to stay measurement-only and untested, say so in docs/startup-runtime.md. The merge stays with @R0Y00 / MasterDomino.

Commands run:

  • git diff --check origin/main...HEAD -> clean
  • python3 tests/docs_links_check.py -> self-test passed
  • python3 tests/bidi_controls_check.py -> no bidirectional controls
  • rg over tests/ for sibling xmem tests (xmem_commit_protection_check, era_heap_check, xmem_lazy_imports_check): none covers the changed XMemAlloc ordering.

AI-assisted; commands listed were executed locally.

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.

3 participants