feat(agc): trace device memory allocations and cache budgets #10194
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10194"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by harry-z-kang on 2026-10-23T12:39:16Z.
What
Add optional Vulkan allocation accounting for the missing device-local memory breakdown in #10041.
APS5_TRACE_MEMORY_BUDGET=1records actual allocation sizes by device, heap and allocation category, including host fallback, image-memory reuse and the active/retained parts of buffer slabs. Dynamic storage-cache budget queries report those counts beside heap budget/usage and the sampled, cached, reserved, available, cap and selected-budget terms.The allocation, fallback, cache-limit and eviction policies keep their existing behavior. The new
unattributed_usage/tracked_above_usagefields expose the difference from Vulkan's estimated heap usage; that difference is not claimed to consist only of driver allocations. Failed diagnostic metadata recording marks reports incomplete. The reported storage cap uses main's sharedStorageTextureCeiling, including its 512 MiB floor. This provides measurement instrumentation for #10041; no title-unblock or performance improvement is claimed.Integration with current main preserves #8672's idle-buffer reclaim and fresh budget read, and uses #10551's shared ceiling helper for both reclaim and reporting. Reporting happens once after any reclaim/re-read, using the final snapshot and the budget actually returned. Both the accounting checks and main's
IdleReclaimPreservesImagescase are retained. The direct-memory audit found the newly merged pixel function LDS pool: its allocation, host fallback attempt and cleanup now use accounting. The existingFirstSampleCopycleanup hook is retained. This rebase also preserves main's shared ownership of depth-plane images and staging through cache eviction and recorded GPU work. Direct cleanup before the shared image owner exists uses the accounting helper.Pooled dedicated buffers are reclassified when
BufferandDeviceBufferreuse each other's allocations. Shared slab blocks keep their block category and partial-retention accounting. This addresses the reported category mismatch.Merge order: #9712 must land first, as declared below. #10807, #10551 and #10062 have merged and are integrated here. The remaining earlier open overlap is #9188 (render-target image-pool release); its integration point is described in the coordination comment. #8672 has merged and is integrated here; #7833 has closed. The original audit also reported shared files it could not compare because those proposals already conflicted with main, so it did not establish zero overlap with them.
The resolution for #10807's merge order keeps its
DeviceMemoryType/AllocateImageMemory/ReleaseImageMemorysource-image path. Allocation, host fallback, reuse and retention are counted inside the image pool. The direct source-imageMemoryAccounting::AllocateandFreecalls are removed; the accounting include stays for the black/letterbox image paths. The new Vulkan regression checks one source allocation through active use, pooled retention, reuse and pool destruction.The refreshed overlap comment also flags #4472 (pool release policy), #8748 (host-placed buffer reuse) and #10472 (image suballocation). These do not provide the current accounting API. Later integration must preserve ledger removal at actual frees, the host-placement key together with the category argument, and per-block accounting for shared image ranges respectively. No merge order has been agreed for those overlaps; the original audit's incomplete comparisons remain a limitation.
#10062's MoltenVK resolver defers Vulkan memory frees until the graphics queue is idle. The accounting helper recognizes that resolver and leaves queued allocations in the ledger; the deferred drain removes each entry immediately before the underlying Vulkan free, including device cleanup and immediate fallback paths. Queued allocations keep their category and active/retained charge until the actual free.
agc_deferred_memory_accountingchecks queuing through both APIs, failed idle waits, device loss, final cleanup, direct frees and allocator forwarding. Its mock free callback checks that the ledger entry is already gone when a handle becomes reusable. Reverting the deferred-resolver guard makes this test fail. This path was tested with mocked Vulkan callbacks on both Windows toolchains; no native MoltenVK/macOS run is claimed.Main's per-slot presentation-upload images are also accounted. Their allocations and frees now go through the ledger as
presentation upload; the Vulkan accounting regression uploads two different extents, submits and waits for each, checks exactly one live upload-image entry after resize, then verifies image and buffer-pool cleanup return to the initial allocation count. Omitting the new allocation hook fails this regression with "presentation upload allocation or resize was not counted exactly once" (CTest exit 8, no VUIDs); restoring it passes. Cleanup assertions drain deferred frees first rather than treating queued memory as already released. The rebase also preserves main's bounded search for an exact-sized buffer inside a pool class and its new job-manager test registration.The October 28 rebase preserves main's per-device deferred-destruction resolvers and its allowance for a later Vulkan loader after the last live device is gone. The immediate fallback resolves the free callback for that device, then removes its accounting entry immediately before the actual free. Both main's loader-reload test and the accounting cases are retained. The added accounting regression checks an earlier device's immediate free while a later resolver is installed and queues and drains the later device through its own callback, including heap/byte charges and allocator forwarding. Deliberately selecting the latest resolver instead of the device's resolver makes this case fail; restoring the resolution passes. The fresh raw-allocation/free audit found no additional allocation sites to instrument. Vorbis is checked out at main's required
c2aa86b05e981c96bf381fc6aa11cdd03eccc2fb.The October 29 rebase applied without textual conflicts. The allocation/free audit found main's new mesh-window storage buffer. Its allocation and cleanup now use
MemoryAccounting::Allocate/Freeundermesh window, preserving main's placement, initialization and failure cleanup. The real Vulkan regression prepares the pool twice, checks exactly one live allocation with actual bytes, heap and active state, verifies the second preparation reuses it, then releases and drains it back to the initial ledger count. Omitting the allocation hook makes the new check fail with "mesh window allocation was not counted exactly once" (CTest exit 8, zero VUIDs); restoring it passes. Main's existingagc_driver_mesh_vector_window_wavesdraw test is added to both comparison arms, and the new accounting case executes on the RTX 5080 with both Windows toolchains. FreeType is at main's required37515cf56ba3349656049edbed89ffc6f6466f2drevision.The latest October 31 rebase onto main
684dfc1d56keeps the merged DLL initialization fix #13668. Main moved AGC test registrations intotests/cmake/<executable>.cmake. During each stopped rebase,python3 tools/agc_test_split.py resolvemigrated this PR's registrations into the buffer-pool, deferred-destruction and Vulkan accounting fragments. The five accounting cases retain their commands, environment settings, 30-second timeouts and budget-output checks. Main's sorted fragment inclusion and automatic execution-test registration are preserved. The runtime accounting changes remain intact, and the raw-allocation/free audit found no new sites to instrument.Tested
Windows 11 10.0.26300, Ryzen 7 9800X3D, NVIDIA GeForce RTX 5080, NVIDIA 617.42, Release, BUILD_TESTING=ON. Rebased onto main with #10807, #10551 and #10062 integrated and repeated focused validation:
684dfc1d561d09a8c35c7c153c95bcfed7e49896.4eb530d609478d091dd97a52be738ffcfb2cc453, with the accounting, category and integration fixes on that baseline.libsbuild on main and PRThe earlier unmodified-main run at
b7105f7285719ead25fde7cf44f5c891e790e756failed configuration because nghttp2's install export includedwindows_seh_personalities, which is not in an export set. Both refreshed MSYS2 arms retain the local-DCMAKE_SKIP_INSTALL_RULES=ONsetting to validate compilation and runtime without generating install/export rules. No source workaround is included in this PR; installation was not validated. WinLibs uses its existing default install-rule setting.The previous main builds at
319621ba2e578bd053b0baba2877fe70520e0737stalled in ccache while compiling SDL. This refresh usesCCACHE_DISABLE=1from the start for main and PR builds with both compilers. Compiler flags and dependency revisions are unchanged by that bypass.The same thirteen-test pattern in all four comparison runs was
^(agc_buffer_pool.*|agc_device_memory_fallback|agc_driver_graphics|agc_driver_pooled_buffer_range|agc_driver_multisample_retirement|agc_driver_host_import_failure_handles|agc_storage_write_back|agc_driver_depth_plane_sampling_evicting|agc_driver_presentation_pass|agc_driver_mesh_vector_window_waves|agc_deferred_destruction)$. Every run used CTest-V,ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1,VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,VK_LOADER_DEBUG=error,warn,layer,ANYPS5_GPU=RTX 5080andAPS5_TRACE_MEMORY_BUDGET=1. The trace flag is ignored by main and exercises accounting on the PR.All four comparison logs confirm eight validation-layer insertions. Neither baseline nor PR logs contain the missing-layer warning or any
VUID-error. Both accounting-test runs also loaded validation, reportedcomplete=1and contained zero VUIDs. All 62 test executions pass; none skipped.The MSYS2
0xc0000142startup failure recorded on the prior baselinede83e8f270is resolved by the merged #13668 in this baseline. Main includes its merge commitbdabbb8415aecb83d30300c6e7b485981d3c70ce. The selected MSYS2 tests now run through DLL initialization and Vulkan execution successfully. This is focused validation of this PR; the full MSYS2 test suite was not rerun.agc_memory_accountingcovers image retention/reclassification, host fallback heap attribution, failed allocation output, slab sharing/retention/reuse, separate devices and cleanup. The added reclaim case runs throughReclaimIdleDeviceandBufferPool::destroy: the cold 2 MiB allocation leaves the ledger, while the active 8 MiB buffer and retained 4 MiB image keep their correct charges; fixture cleanup leaves no Vulkan or ledger allocation.agc_memory_accountingalso checksDeviceBuffer->Buffer->DeviceBufferreuse of one allocation, both with equal buffer sizes and with exact-size rebinding. Each step verifies category, heap, allocated and retained bytes, allocation count and final cleanup; the slab reuse check also verifies its block category. Before the fix, the new test fails ona2fd88fb9ebecauseBufferretains the device-buffer category.agc_memory_accounting_budget_capchecks the emitted diagnostic cap and selected budget for an unpressured 2 GiB heap. The old report printed a 512 MiB cap beside the actual 1 GiB selection and fails this test; the corrected report prints 1 GiB for both.agc_memory_accounting_budget_flooralso checks a 256 MiB heap and budget: the diagnostic cap and actual selection both use the shared 512 MiB floor. Restoring the old raw half-budget formula makes this new test fail with a 128 MiB cap beside a 512 MiB selection. Cache policy is unchanged.agc_driver_memory_accountingcreates and binds real Vulkan images, verifies allocation bytes and heap, checks image-pool reuse, prints a budget report and verifies cleanup returns to the initial allocation count. It now checks the merged presentation-source path: exactly one allocation is tracked, retained bytes match the image pool, a retained allocation is reused, and pool destruction restores the initial ledger count. It also retains the first-sample image cleanup case and now creates, submits work using, and releases a real pixel function LDS buffer. The focused comparison now also includesagc_driver_depth_plane_sampling_evictingwith its 1 MiB copy-cache budget, exercising the depth-plane ownership change. Both toolchain logs confirm that the added idle-reclaim and pixel-LDS cases executed and passed. The pixel-LDS case uses a 16 MiB test limit and runs when the device supports the fragment subgroup operations that path needs.Conventions (
python3 tools/check_conventions.py --base origin/main) andgit diff --checkpass. The original overlap audit completed before opening; merge-order requests and the concrete integration resolutions are recorded in the coordination comment.Example from the real Vulkan test: the 4,194,304-byte second image appears once on heap 0, with its new category, zero retained bytes and
complete=1. The shared host buffer slab is charged to heap 1 and reports active and retained byte counts separately. The forced device-memory refusal case inagc_driver_presentation_passran on main and PR with both toolchains and placed the source in host memory. No game binary or private capture was used. Linux runtime and macOS were not tested.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T12:45:13Z.
Review: DISMISSED
Reviewed exact head
3bd56fff7e331c100c4bb70c3eb9f8de99e6d8ed. The accounting API and tests are well scoped for the #10041 measurement step, and the Windows validation runs are detailed. The tested main basee84c529d6is now 18 commits behind current mainfa3dc7f3; that interval modifiesExecution/src/VulkanDevice.cpp(including AGC allocation behavior) andtests/ShaderMemory.cpp, which overlap the accounting integration/test surface. Please rebase onto current main and repeat the same focused main-vs-PR allocation/cache tests with validation enabled, then update the base/head and results in the PR description. Current hosted product/runtime jobs are skipped by path policy, so they do not replace that focused rerun.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-24T00:45:16Z.
Review: CHANGES_REQUESTED
The accounting itself reads well: every allocate/free in Graphics and Presentation goes through the ledger, slab and image-pool retention are separated from active bytes, and nothing changes without
APS5_TRACE_MEMORY_BUDGET. What blocks it is the state of the branch:mainnow that #8672 is merged.Graphics/src/ShaderResources.cpp:storageBudgetbecame areadlambda withcached.ceilingand aReclaimIdleDevicere-read, soMemoryAccounting::Reporthas to be called from there (after the re-read, so the report shows the budget actually used).tests/BufferPool.cpp: the newMemoryAccountingChecksblock collides withIdleReclaimPreservesImages.mainagain for rawvkAllocateMemory/vkFreeMemorycalls that landed since your base and route them throughMemoryAccounting; a missed allocation shows up asunattributed_usage, a missed free as a leaked ledger entry. Add a case in the tests that goes through the reclaim path (ReclaimIdleDevicedestroys throughBufferPool::destroy).Depends on: #9712is still open, so this can only land after it.Welcome back once it is rebased.