fix(agc): check free memory before a large pipeline cache save and add an opt-in mapped save #28121
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/28121"
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 zeezey on 2026-11-28T16:41:12Z.
What
A pipeline cache save asked the driver for the whole cache in one host buffer (
vkGetPipelineCacheDataintosizeof(FileHeader) + sizebytes) and then wrote that buffer out. The process's private memory therefore rose by the size of the cache for the length of every save, and a save runs at each 10 s interval that has new pipelines.Seen in PPSA38500 on Windows (RTX 4090): two runs ended at a save of a 5.5 GiB cache. Private memory went from 45.4 GB to 50.6 GB within 5 s; that save took 5.8 s. With the mapped save a third run saved the same 5.5 GiB cache in 6.9 to 8.3 s per save with no rise in private memory.
The mapped save is opt-in (
APS5_PIPELINE_CACHE_MAPPED_SAVE_MB), because a storage error on the mapped file cannot be turned into a failed save (see the first point below). Without the switch a save is still written from a buffer; what is on by default is the free-memory check, which puts a large save off instead of letting it take the last free memory, and the save without a read-back, which removes the second copy of the cache a save held. The title needs the switch for a cache of that size.Change (driver only,
Graphics/src/PipelineCache.cpp,.hppand the newGraphics/src/MappedCacheFile.hpp):APS5_PIPELINE_CACHE_MAPPED_SAVE_MBset to a size in MiB (0maps every save; unset oroff, the default, maps none), a save of that size or more has the driver write into a mapped new file beside the cache file (<cache file>.tmp.map.<pid>.<tid>.<n>). When the header is written the file is cut to its final size, flushed and renamed over the cache file, so a reader still sees either the old file or the new one. If the save stops for any reason the temporary file is deleted; one left by a killed process has.tmp.in its name, so the shader cache housekeeping that already exists removes it. It is off by default because a storage error on the mapped file raises an in-page error inside the driver'svkGetPipelineCacheDataor the save's checksum read: that cannot be unwound into a failed save, so the process ends, where a save from a buffer only fails to write. The debt entry says what a safe path would need.APS5_PIPELINE_CACHE_SAVE_WINDOW_KIB;0opens it whole). The pages after the open part are no-access; the driver's first write there faults, and a vectored exception handler writes the part before out, drops it from the working set and opens the next window. A helper thread drops the pages that are read again for the checksum. The handler returns at once for every fault while no save is running, and looks only at addresses inside a file being saved.APS5_PIPELINE_CACHE_SAVE_FREE_MB;0saves regardless). The file is kept, the next interval tries again, and one line a minute says so. A buffer save counts the whole cache, a mapped one four windows. The save at teardown is never put off, since no later attempt follows it.agc-mapped-pipeline-cache-save.md).What the mapped save costs: the driver fills the mapped file while the save holds the cache's snapshot lock, at the disk's speed, so pipeline creations wait for the length of a large save (6.9 to 8.3 s for 5.5 GiB here) where before they waited only for the copy into the buffer. Without the switch every save is written from a buffer as before.
This is not a speed claim: the mapped save is slightly slower. With the switch it removes a memory rise that ends runs; without it a large save is put off while free memory is short.
Overlap: #28002 (repeat a skipped periodic save) edits
save()in the same file, a few lines away from this change; the two do different things and whichever merges second needs a small rebase.tools/pr_overlap.py --branchreported no line overlap, and 29 open PRs it could not confirm indocs/dev/ENVIRONMENT.md(sorted rows).Tested
Windows 11, WinLibs GCC 15.2.0, Release,
-DBUILD_TESTING=ON, built from this branch on currentmain.agc_pipeline_cache_*tests pass on head5cb4dea2. Eleven are new.external_mergewas repeated 25 times on this head; on the head before it the mapped ones andsettingwere repeated 10 times andmapped_stress25 times. Every mapped save test sets the switch itself:agc_pipeline_cache_mapped(Windows): a 70 MiB cache is saved through a mapped file in 16 MiB windows. The file holds the cache's bytes under a header a second cache loads, the driver's writes opened between one and four further windows, no temporary file stays, and an idle save afterwards leaves the file alone.agc_pipeline_cache_mapped_low_memory(Windows): with more memory to stay free than the host has, the save is put off, counted, the file kept and no temporary file left; the teardown save then writes the cache.agc_pipeline_cache_mapped_default(Windows): without the switch a 257 MiB cache is saved from a buffer, not through a mapped file. It fails with the earlier default of 256 MiB.agc_pipeline_cache_mapped_working_set(Windows):QueryWorkingSetExover the view. After a writer went through a 16 MiB file in 1 MiB windows, 0 of the 3,840 pages behind it and 256 of 256 in its last window were in the working set, and no flush or release call failed. For a 64 MiB file opened whole, the helper thread brings the written pages, and again the pages read back, under a tenth resident. Without theVirtualUnlockcall the test fails.agc_pipeline_cache_mapped_stress(Windows): four threads write interleaved pages of one 8 MiB file through 64 KiB windows while a fifth creates, writes and commits other mapped files (about 500 windows and 60 files per run); every page holds what its writer wrote.agc_pipeline_cache_setting(every system): whole numbers in range are read; text, signs, spaces and sizes that overflow when scaled are refused.agc_pipeline_cache_first_save(every system): the save after a cache's first one in a new folder creates no second Vulkan cache. It fails without the read-back change.agc_pipeline_cache_mapped_no_slot(Windows): four live mapped files hold every handler slot, so a fifth file larger than one window gets no mapping; the save is put off under the reserve, then written through the buffer path with the check off, then mapped again once a slot is free.agc_pipeline_cache_mapped_no_protection(Windows): the same, with the window protection refused through a test-only access struct.agc_pipeline_cache_mapped_no_helper(Windows): a helper thread that cannot start rejects the mapping, leaves no file and frees the handler slot.agc_pipeline_cache_external_merge(every system): each of two caches marks an entry of its own, and the test's merge keeps both caches' bytes. After the second cache changes the file, three later saves of the first (one settling without a write, two grown) each merge the file, and both entries are in the file after each grown save. Without the fix it fails with "a save after an external merge lost the other writer's entry".agc_pipeline_cache_allocation_*tests relied on that read-back to reach the merged-cache path. They now let a second cache write the file first and check that the failed snapshot did merge, so they cover the same path as before.5cb4dea2(ctest -R "^agc_|frame_replay"withANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1): 2,336 pass. Three fail, and fail on the branch's base onmain(16329049) too on this GPU:agc_driver_gcn_indirect_draw_location("covered 0 of 1024 bytes, expected an edge through the target"),agc_driver_label_ordered_flush("the label was visible before the image store it follows"),agc_driver_linear_target_import_creation("a color target's import of direct memory under the settle floor was made where it would be compared").VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer,ctest -V -R "agc_pipeline_cache|agc_tessellation_pipeline_cache|frame_replay"plus the three failing tests, on the branch's base onmain(16329049, 80 tests) and on head5cb4dea2(91): both logs showInsert instance layer "VK_LAYER_KHRONOS_validation"68 times, neither haswas not found but was requested by env var VK_INSTANCE_LAYERS, and neither has aVUID-line. No test saves a cache of 256 MiB through a real driver, so only the title run below exercised the mapped save with NVIDIA'svkGetPipelineCacheData.tools/check_conventions.py,tools/env_reference.py --checkandtools/debt_fold.py --checkpass, as do the 374 tooling tests (ctest -R "^(conventions|public_headers|env_reference|debt|agc_test_split|check_)").Title: PPSA38500 on Windows 11, RTX 4090, driver 621.24, the numbers under What. Those three runs were made with an earlier revision of this change on a local branch that also carries other open PRs; that revision mapped saves of 256 MiB or more by default, had a fixed 6144 MiB reserve and took the handler's lock exclusively. The code in this PR was run through the tests above, not through the title again.
Not tested: Linux and macOS (the change compiles to the old path there; the new
first-savetest and the changed allocation tests are registered on every system and were only run on Windows). A slow disk.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)🤖 Generated with Claude Code
https://claude.ai/code/session_01NAAUzVZz5igZcosJFfZ3kV
Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T16:50:21Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: make the mapped-save memory limit hold when window protection cannot be enabled.
What I ran
Reviewed head
d6557298363696ef783cd7810c46013d83d93079. I did not run commands or tests;repo_read.pywas the only repository reader used.Results
Negative control
Not run; no test was changed during this review.
Findings
Blocking
core/libs/prx/libSceAgcDriver/Graphics/src/MappedCacheFile.hpp:51— If vectored-handler enrollment fails orVirtualProtectcannot protect the tail, creation still succeeds withopenedset to the full mapping size. The save path has already checked affordability as though at most four windows would be open (PipelineCache.cpp:346), so the driver can write across the entire cache despite that limit. This can consume substantially more physical memory than the reserve check allows. On either failure, fall back to the buffer path or reject the mapping so the save can re-check affordability for the full size.Sources
Not verified
PipelineCache.hpp,MappedCacheFile.hpp,PipelineCache.cpp,PipelineCacheFailures.cpp,agc_pipeline_cache_allocation_tests.cmake,docs/dev/ENVIRONMENT.md(including its changed lines), anddocs/dev/debt/changes/agc-mapped-pipeline-cache-save.md. The review-folderfiles.jsonwas unavailable; the review folder listing marked all seven changed files as included. Linux and macOS behavior and real-driver behavior were not independently verified.To get this to a good state
Make window-protection failure prevent the mapped path from proceeding under the four-window memory estimate. Re-run the mapped and low-memory tests on the new head.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-28T16:53:16Z.
Review: COMMENTED
Actionable comments posted: 3
75914e3a-8597-40cc-a503-b1b13e27b4afReviewing files that changed from the base of the PR and between
16329049afandd655729836.core/libs/prx/libSceAgcDriver/Graphics/include/PipelineCache.hppcore/libs/prx/libSceAgcDriver/Graphics/src/MappedCacheFile.hppcore/libs/prx/libSceAgcDriver/Graphics/src/PipelineCache.cppcore/libs/prx/libSceAgcDriver/tests/PipelineCacheFailures.cppcore/libs/prx/libSceAgcDriver/tests/cmake/agc_pipeline_cache_allocation_tests.cmakedocs/dev/ENVIRONMENT.mddocs/dev/debt/changes/agc-mapped-pipeline-cache-save.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-28T17:10:16Z.
Review: COMMENTED
Actionable comments posted: 1
1ecf1829-0f1b-4006-9638-c98c40dcf6efReviewing files that changed from the base of the PR and between
d655729836and0fa316e113.core/libs/prx/libSceAgcDriver/Graphics/src/MappedCacheFile.hppcore/libs/prx/libSceAgcDriver/Graphics/src/PipelineCache.cppcore/libs/prx/libSceAgcDriver/tests/PipelineCacheFailures.cppcore/libs/prx/libSceAgcDriver/tests/cmake/agc_pipeline_cache_allocation_tests.cmakedocs/dev/debt/changes/agc-mapped-pipeline-cache-save.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T17:18:32Z.
Review: COMMENTED
Reviewed — comments
The mapped cache save has concrete correctness and robustness concerns that should be addressed before relying on it for large saves.
What I ran
Reviewed head
0fa316e1134c5ff2ff03c23eb35f4a15b3a48646using the prepared diff andrepo_read.py. No build or tests were run; the reported test results are author supplied. No merged preview was run.Results
Findings
Blocking
None verified.
Non-blocking
core/libs/prx/libSceAgcDriver/Graphics/src/MappedCacheFile.hpp:52: IfVirtualProtectfails afterenrollsucceeds,Createreturns null and destruction callsrelease/withdraw, so the registration is cleaned up. However, the fault handler can hold the registry shared lock while invokingopenThrough, which takes a per-file exclusive SRW lock; a concurrent commit first stops its trimmer and withdraws under the registry exclusive lock. This lock ordering means the commit waits until the handler exits, but it is not evident that a driver fault that re-enters the same handler can safely take these locks. This concurrency path merits stress testing.core/libs/prx/libSceAgcDriver/Graphics/src/MappedCacheFile.hpp:219:trimcallsVirtualUnlockon every chunk without the code shown pinning these mapped pages withVirtualLock. The return values from bothFlushViewOfFileandVirtualUnlockare ignored, so the claim that the helper reliably drops written pages from the working set is not established by the inspected implementation.core/libs/prx/libSceAgcDriver/Graphics/src/PipelineCache.cpp:344: Environment values are parsed withstrtoulland shifted without validation or overflow checks. Malformed, negative, or excessively large values can wrap thresholds/window sizes and produce surprising mapping or reserve behavior. Validate the full string and range before using these settings.Not verified
The seven changed files listed in the manifest were read.
description.md,commits.md,manifest.json, anddiff.patchwere read;files.jsonwas not available through the review reader. No files were excluded according to the prepared file listing. Windows build/test output and title measurements are author reported, not independently run. Linux/macOS behavior, actual storage failures, and mapped saves with a real driver were not run in this review.To get this to a good state
Nothing is required. Optional points: stress the fault/commit concurrency path, verify the intended working-set trimming behavior, and reject invalid environment setting values.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-28T17:39:00Z.
Review: COMMENTED
9d8f59ee-93a2-4b9b-8ddd-8554938da07eReviewing files that changed from the base of the PR and between
9e46aaf1f6and9ffe1b9a06.core/libs/prx/libSceAgcDriver/Graphics/include/PipelineCache.hppcore/libs/prx/libSceAgcDriver/Graphics/src/MappedCacheFile.hppcore/libs/prx/libSceAgcDriver/Graphics/src/PipelineCache.cppcore/libs/prx/libSceAgcDriver/tests/PipelineCacheFailures.cppcore/libs/prx/libSceAgcDriver/tests/cmake/agc_pipeline_cache_allocation_tests.cmakedocs/dev/ENVIRONMENT.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T17:40:19Z.
Review: COMMENTED
Reviewed — no blocking findings
The reviewed change adds opt-in mapped pipeline-cache saves and Windows free-memory checks; I found no verified blocking defect, but could not run the reported checks here.
What I ran
Reviewed head
9ffe1b9a0681ac8c44d2db2bbec7ca6f7f0032b9from the prepared diff and pinned reader. No tests or build commands were run.Results
Findings
Blocking
None verified.
Non-blocking
None.
Not verified
Builds and tests were not run. I read the full changed
PipelineCache.hppandMappedCacheFile.hpp; relevant portions ofPipelineCache.cpp,PipelineCacheFailures.cpp, the CMake test registration,ENVIRONMENT.md, and the debt note. The remaining portions of those files were not read. The review manifest lists seven reviewed files; I fully read two and partially read five. The prepared diff excludes lockfiles, minified files,dist/,build/, and.svgfiles; nofiles.jsonwas available through the reader.To get this to a good state
Nothing is required. Optional: run the build and tests on the exact reviewed head to independently verify the reported results.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T18:04:46Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change based on the source inspected; the mapped save is opt-in and its known storage-fault limitation is documented.
What I ran
Reviewed head
5cb4dea2794b4a10444582374d1b2f579c154445. No commands or tests were run;repo_read.pywas used to inspect the prepared diff and pinned source files. The author reports Windows tests and tooling checks in the PR description; those results were not independently run here.Results
Findings
Blocking
None identified in the inspected code.
Non-blocking
None.
Not verified
No tests or builds were run. Windows runtime behavior, including mapped-file storage faults, and Linux/macOS behavior were not independently verified. The author reports Linux and macOS were not tested. Read:
PipelineCache.hpp,MappedCacheFile.hpp,PipelineCache.cpp,PipelineCacheFailures.cpp,agc_pipeline_cache_allocation_tests.cmake,ENVIRONMENT.md, andagc-mapped-pipeline-cache-save.md. Excluded: none of the changed files; review-bundle metadata and description were also read.To get this to a good state
Nothing is required.