Skip to content

startup: log video memory and process commit every 5 s - #3491

Open
Leyukaka wants to merge 2 commits into
R0Y00:mainfrom
Leyukaka:runtime-video-memory-line
Open

Leyukaka wants to merge 2 commits into
R0Y00:mainfrom
Leyukaka:runtime-video-memory-line

Conversation

@Leyukaka

Copy link
Copy Markdown
Collaborator

Refs #3474

Adds the diagnostic line suggested in #3474 (comment): QueryVideoMemoryInfo local / non-local and the process commit, logged right after the existing x2w-d3d12 views noted ... placed buffers ... line, so the placed-buffer count can be read against memory on any run. It is always on; X2W_D3D12_BUDGET is not needed.

New line

x2w-d3d12 video memory (bytes, usage/budget): local=<usage>/<budget> nonlocal=<usage>/<budget> process_commit=<bytes>
  • local / nonlocal: CurrentUsage/Budget of each segment group; n/a when the adapter declines that query.
  • process_commit: PrivateUsage from K32GetProcessMemoryInfo.
  • No DXGI adapter: x2w-d3d12 video memory unavailable (no DXGI adapter); process_commit=<bytes> bytes is logged once, then x2w-d3d12 process_commit=<bytes> bytes on later intervals.

Changes (src/startup_stubs/d3d12_xs_device.cpp)

  • The adapter lookup and the two QueryVideoMemoryInfo calls move out of report_budget into sample_video_memory. It runs once per report in the existing 5 s block of queue_present, never per frame. The x2w-budget line uses the same sample instead of querying on its own; its format is unchanged.
  • The device, factory and adapter are ComPtr locals, released on return.
  • K32GetProcessMemoryInfo is exported by kernel32, so there is no new link dependency.
  • last_report becomes an atomic claimed with compare_exchange_strong, and started a function-local static set on the first present. Heartbeat timing is unchanged.

The only behaviour change is the new line.

Tests

New startup_video_memory_line_warp (tests/startup_video_memory_line_check.cpp): a wrapped WARP queue presents, waits 5.1 s and presents again. It checks that exactly one video memory line follows the placed-buffer totals line, with numeric readings for each segment WARP answers, that no x2w-budget line appears with X2W_D3D12_BUDGET=0, and that the missing-adapter text is logged once while the process commit keeps reporting.

ctest -j 1 --no-tests=error -R '^(startup_video_memory_line_warp|startup_resource_budget_warp_debug0)$'   2/2 passed
ctest -R budget                                                                                         8/8 passed, 4 capability skips

Mutations, each restored afterwards:

Mutation Result
drop the report_video_memory(video) call startup_video_memory_line_warp fails
first_calls(no_adapter, 1) -> 2 startup_video_memory_line_warp fails

Base 26df1826. Merges cleanly with #3480 and #3482.

Not covered

  • The tests run on WARP only; no hardware adapter reading is asserted.
  • The line reports totals for the process and the adapter, not a per-resource breakdown.

@Not-Saii Not-Saii 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.

Review of head 926c74922c5483171e58a7d3d1af53351105f90d (merges cleanly with current main).

Thanks: the change is small and does what it says, the 5 s report now samples once and shares the sample with x2w-budget, and the CAS on last_report also removes a latent double report. In the base code, a thread whose now was older than another thread's freshly stored last_report computed now - last_report as an unsigned underflow. No downloads, network calls or agent-config changes; the new CMake fragment follows the startup_present_log pattern.

Checked: the sample runs only inside the 5 s block, never per frame. The only added per-call cost is a magic-static guard and a relaxed atomic load. The device, factory and adapter are ComPtr locals and the only new static is the no-adapter counter. The line is always on, as asked in the linked issue. No new switch, so tools/startup/run_presets.json needs no change.

Findings

MUST: none.

SHOULD

  1. src/startup_stubs/d3d12_xs_device.cpp:7595 (called from :7925): CreateDXGIFactory1 + EnumAdapterByLuid used to run only with X2W_D3D12_BUDGET=1. Now every run pays for it inside the title's Present call every 5 s. Factory creation enumerates adapters and can take milliseconds on some systems. Please cache the IDXGIAdapter3 once (keyed by the device LUID) and reuse it, or measure and state the added time per report. The Present entry of the time inside the stand-in line already includes it.
  2. No test covers the x2w-budget line on the refactored path. The new test runs only with X2W_D3D12_BUDGET=0, and the startup_resource_budget_* tests never reach the 5 s report. I ran the new fixture by hand with X2W_D3D12_BUDGET=1: the x2w-budget line appeared with the same local=/nonlocal= values as the video memory line. As expected, the fixture then printed its FAIL because it asserts no budget line. Please add a second registration with X2W_D3D12_BUDGET=1 that asserts one x2w-budget local= line whose local=/nonlocal= fields equal the video memory line's.
  3. Docs: docs/startup-runtime.md:3911-3912 describes the five-second x2w-d3d12 views noted line, but the new always-on line and its n/a and no-adapter forms are documented nowhere. The #d3d12-resource-budget section also still reads as if the budget line were the only place video memory is reported.
  4. Evidence in the PR body (AGENTS.md "Evidence"): it names the base but not the head commit under test. The commands have no --preset or build directory. "4 capability skips" doesn't say which tests skipped. The Windows build and WARP version are missing. The two mutations are a good negative control.

NIT
5. :7628-7633: the main line puts the unit in its header (process_commit=<n>), but the no-adapter forms end in process_commit=<n> bytes. Pick one.
6. tests/startup_video_memory_line_check.cpp:37-44: the early return 1 paths after X2W_TRAP_LOG is set leave the per-PID log file in the build directory.
7. The fixture sleeps 5.1 s, so consider a COST property in the fragment (tests/cmake/README.md) so ctest --parallel starts it early.

What the tests prove

  • startup_video_memory_line_warp: on a wrapped WARP queue, the 5 s report emits exactly one video memory line, directly after the placed-buffer totals. Each segment WARP answers gets numeric usage/budget (each one it declines gets n/a), and the process commit is nonzero. With X2W_D3D12_BUDGET=0 no x2w-budget line appears. The null-queue hook shows the no-adapter text is logged once, with later calls falling back to the commit-only line. Not covered: a hardware adapter, the budget-on path, and the EnumWarpAdapter LUID fallback specifically.
  • startup_resource_budget_* and startup_present_log(_control): neighbouring regression checks. They don't reach the 5 s report.

Isolated run (Windows Sandbox, networking disabled, PR tree exported at the head SHA)

cmake --preset dev -DX2W_WITH_OPUS=OFF
cmake --build --preset dev --target startup_video_memory_line_check startup_resource_budget startup_present_log
ctest --preset dev --no-tests=error --output-on-failure -V -R '^(startup_video_memory_line_warp|startup_resource_budget_(warp|hardware)_debug[01](_inline_control)?|startup_present_log(_control)?)$'
  • Passed (7): startup_video_memory_line_warp (5.46 s), startup_resource_budget_warp_debug0, startup_resource_budget_warp_debug0_inline_control, startup_resource_budget_hardware_debug0, startup_resource_budget_hardware_debug0_inline_control, startup_present_log, startup_present_log_control. The _control and _inline_control cases pass by printing their expected FAIL line.
  • Failed: none.
  • Skipped 77 (4): startup_resource_budget_{warp,hardware}_debug1 and ..._debug1_inline_control. D3D12GetDebugInterface fails because the debug layer is unavailable (no d3d12SDKLayers.dll in the sandbox).
  • Environment: Windows 11 Enterprise 10.0.26100 (build 26100) in Windows Sandbox. Adapters: NVIDIA GeForce RTX 4090 through the sandbox vGPU (driver 10.0.26100.1150) plus Microsoft Remote Display Adapter. MSVC 14.44.35207, CMake 3.31.6, Debug (dev preset). Debug layer: unavailable. The new test uses WARP only. Video-memory numbers seen through vGPU may differ from bare metal.

Mutations (scratch copy, each restored; restored file hash matches head)

Mutation Result
M1: replace report_video_memory(video); in queue_present with a no-op builds; startup_video_memory_line_warp fails: FAIL: expected one WARP sample with the placed-buffer totals ...
M2: move that call below the heartbeat line builds; same failure (the line no longer directly follows the placed-buffer totals)
restored builds; startup_video_memory_line_warp passes

CI

The Windows PR job (run 46290965004) ran the pr preset, 82/82 passed. It compiled d3d12_xs_device.cpp but did not build or run startup_video_memory_line_warp or any startup_resource_budget_* test; none of this PR's tests are in the pr filter. The Linux Python and gfx1013 jobs passed, but they don't exercise this change.

Verdict: COMMENT. No blocking issue, and the PR's test catches the change being reverted. SHOULD 1 (an always-on factory creation on the Present thread) and SHOULD 2 (no test of the budget-on path) are worth addressing before merge.

Check the shared sample in the optional budget heartbeat and document the video memory and process commit formats.

@Not-Saii Not-Saii 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.

Re-review at head 2ecdce61182428f0e7f14865479c3d181deb909c (previous review: 926c7492). It merges cleanly with current main.

Thanks for the quick turnaround. The cache, the budget-on test, the docs and the test cleanup all address what I raised.

What changed since 926c749

One commit, 2ecdce61:

  • video_memory_adapter(LUID) (src/startup_stubs/d3d12_xs_device.cpp:7589-7611) creates the DXGI factory and looks up the adapter once per LUID, behind a mutex. Failed lookups are cached too. sample_video_memory (:7613) now costs a GetDevice, the cache lookup and two QueryVideoMemoryInfo calls per report.
  • New test startup_video_memory_line_budget_warp, plus COST 5 for both tests.
  • The test's log file is removed by an RAII guard.
  • Docs for the new line and its no-adapter forms.
  • The no-adapter forms drop the trailing bytes.

No downloads, network calls, agent-config changes or new build commands. The CMake change only adds a second add_test of the same executable.

Earlier findings

# Finding Status
S1 DXGI factory created every 5 s on the Present thread Fixed: d3d12_xs_device.cpp:7589-7611, one lookup per LUID
S2 No test of the x2w-budget line on the shared-sample path Fixed: tests/cmake/startup_video_memory_line.cmake:11-16, tests/startup_video_memory_line_check.cpp:80-93 (one budget line after the video line, same local=/nonlocal= text, an all-n/a sample refused)
S3 New line undocumented Fixed: docs/startup-runtime.md:1857-1871 and :3871-3872
S4 Evidence fields in the PR body Open: see below
N5 bytes unit differed between the forms Fixed: d3d12_xs_device.cpp:7644, :7646
N6 Log file left behind on early exits Fixed: tests/startup_video_memory_line_check.cpp:19-27 (the stream is destroyed before the guard)
N7 COST for the 5 s test Fixed: tests/cmake/startup_video_memory_line.cmake:12

New findings

MUST: none.

SHOULD

  1. Teardown of the adapter cache (d3d12_xs_device.cpp:7593-7594). The cache is a function-local static map of ComPtr<IDXGIAdapter3>, so this module's CRT teardown releases those adapters on process exit as well as on FreeLibrary. src/startup_stubs/d3d12_teardown.h sets the rule for statics that hold desktop graphics objects: detach on process exit, release on unload (#2572). The present sampler is kept alive with new for the same reason (:7558). Please follow one of those patterns, or say why DXGI adapters are safe to release at exit. I have not seen a failure from this. It is a risk read from the code against the module's own rule, not an observed crash.
  2. The PR body is out of date. It still says the device, factory and adapter are ComPtr locals released on return, and it shows process_commit=<bytes> bytes for the no-adapter forms. It doesn't mention startup_video_memory_line_budget_warp or the cache. The AGENTS.md evidence fields from the last review are still missing: the head commit under test, the commands with --preset, the names of the skipped tests, and the Windows build.

NIT: none new.

What the tests prove

  • startup_video_memory_line_warp: one video memory line, directly after the placed-buffer totals, with a numeric reading for each segment WARP answers and a nonzero process commit. No x2w-budget line with the budget off. The no-adapter text is logged once, then the commit-only line.
  • startup_video_memory_line_budget_warp: with X2W_D3D12_BUDGET=1, exactly one x2w-budget line follows the video line and carries the same local=/nonlocal= text. On WARP that was local=0/482684314 nonlocal=0/0. A sample with no DXGI reading is refused, so dropping the sample is caught.
  • No test covers the cache itself (one lookup per LUID, cached failures). That is acceptable for a diagnostic, but the PR body should say so.

Isolated run (Windows Sandbox, networking off, PR tree exported at the head SHA; merge base 26df1826 available for comparison)

cmake --preset dev -DX2W_WITH_OPUS=OFF   (with the Agility 1.620.2 and WARP 1.0.22 directories)
cmake --build --preset dev --target startup_video_memory_line_check startup_resource_budget startup_present_log
ctest --preset dev --no-tests=error --output-on-failure -V -R '^(startup_video_memory_line_(budget_)?warp|startup_resource_budget_(warp|hardware)_debug[01](_inline_control)?|startup_present_log(_control)?)$'

Head leg: 12 passed, 0 failed, 0 skipped (77).

  • Passed: startup_video_memory_line_warp, startup_video_memory_line_budget_warp, startup_resource_budget_{warp,hardware}_debug{0,1} and their _inline_control cases, startup_present_log, startup_present_log_control.
  • The control cases pass by printing their expected FAIL line.
  • No head failures, so no base comparison was needed.

Environment:

  • Windows 11 Enterprise 10.0.26100 (build 26100), Debug dev preset, MSVC 14.44.35207, CMake 3.31.6.
  • Adapters as the fixtures print them: NVIDIA GeForce RTX 4090 through the sandbox vGPU (driver 32.0.16.1088), and Microsoft Basic Render Driver (WARP 1.0.22.0).
  • Debug layer: the probe returned hr=0x00000000. The startup_resource_budget_*_debug1 fixtures report debug layer: 0x00000000 enabled. The two video memory fixtures create their WARP device directly, so they run without it.
  • Video-memory numbers through vGPU may differ from bare metal.

Mutation (scratch copy; the restored file's hash matches head)

Mutant Result
report_budget(video) -> report_budget(VideoMemorySample{}) at d3d12_xs_device.cpp:7982 (the budget line no longer gets the shared sample) Rebuild check: d3d12_xs.dll, startup_video_memory_line_check.exe and startup_present_log.exe changed. startup_video_memory_line_budget_warp fails with FAIL: expected one budget line with the heartbeat's WARP memory sample. The other 11 pass, including startup_video_memory_line_warp.
restored 12/12 pass

The previous run caught removing or moving the report_video_memory(video) call. That part of the test has not changed.

CI

The Windows PR job (run 46322245948) ran the pr preset, 82/82 passed. It compiled d3d12_xs_device.cpp but ran neither video memory test, because neither is in the pr filter. The Linux job passed all 267 checks. The gfx1013 job passed, but it doesn't exercise this change.

Verdict: COMMENT. Every earlier code finding is fixed and the new test catches its mutant. I'd approve once the adapter cache follows the teardown rule in d3d12_teardown.h (or the PR explains why it doesn't need to) and the PR body matches the code.

@Jess-Doit

Copy link
Copy Markdown
Contributor

Two small suggestions on the cached video-memory adapter, from a local version of the same change (it also covers both of tedin7's review notes on #2256):

  • Failed lookups are cached for the life of the process. If the first EnumAdapterByLuid or WARP match fails (for example a transient factory failure), later samples report n/a permanently. Retrying an empty entry on the next sample keeps the cost low, since samples are 5 s apart.
  • A removed or reset device keeps its cached adapter. When QueryVideoMemoryInfo returns DXGI_ERROR_DEVICE_REMOVED or DXGI_ERROR_DEVICE_RESET, dropping the entry lets the next sample look the adapter up again instead of querying a stale reference.

Separately, resource_budget.h still relies on a transitive windows.h. Including it directly there was the other #2256 note, and it fits in this PR if you want it.

With both changes, the eight budget tests and a repeated heartbeat check passed on a GTX 1060 and WARP. The resource-retire failures on that card also fail on clean main. Happy to share the branch if useful.

Ready for more review

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