Skip to content

perf(agc): stop pooling released device buffers over the memory budget - #31

Open
Thimo188 wants to merge 4 commits into
mainfrom
upstream-pr/4472
Open

Thimo188 wants to merge 4 commits into
mainfrom
upstream-pr/4472

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#4472, originally opened by solbjorg1245.

Source commit: c58f968028ccef496eb2270918aac49579780d6d. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

The buffer pool's device tier keeps released device-local buffers up to an eighth of the largest device-local heap (at least 512 MiB), however full that heap is. Near the budget that VK_EXT_memory_budget reports, this retained memory is what pushes the process over it. Past the budget an allocation can still succeed while the OS starts paging device memory or DeviceMemoryType places it in host memory, so the out-of-memory trim in BufferPool::AllocateMemory comes late or not at all.

  • VramBudget (new, Graphics/src/VramBudget.cpp) holds the policy for the largest device-local heap: the target is the reported budget itself (never below a quarter of the heap or 1 GiB, whichever is smaller). It does not query the driver itself. The pool's existing heap budget read (heapOverBudget, at most every 100 ms, the one the image memory path already uses) feeds it, so buffer and image releases share one vkGetPhysicalDeviceMemoryProperties2 call.
  • BufferPool::Put: while the reported usage, minus what the pool freed from the device tier since that read, is over the target, the pool first gives back retained image memory (the image path already frees it over the reported budget, and image takes are not demand). Then the device tier keeps released buffers only up to a demand floor: what it handed out (hits and misses) in the last second, in eight 125 ms buckets, capped at a sixteenth of the heap. Anything beyond the floor is destroyed, least recently used first. A title that doesn't churn has a small floor and gives back everything else; one whose working set sits just over the budget keeps what it is about to ask for again instead of re-allocating it every frame. Host tiers, the small tier and slabs are unchanged, and a Put that lands in another tier is not counted as device memory released.
  • Every device-tier free the pool makes counts as released until the next read: Put evictions, image memory dropped or evicted in PutImageMemory, ReclaimIdleDevice under storage cache pressure, and trim() before an allocation retry. The bytes are counted under the pool mutex before the memory is destroyed, so Excess() never counts a free twice.
  • The pool behaves exactly as before in three cases: without the extension, on an integrated GPU (Context::integratedGpu, set from the device type: there the device-local heap is a carve-out the driver spills into system memory) and with APS5_NO_VRAM_BUDGET=1. APS5_VRAM_BUDGET_MIB caps the budget, which lets a large card behave like a smaller one. Both are in docs/dev/ENVIRONMENT.md.
  • A [vram] line is printed once, at the first read: the heap, its budget and usage. With APS5_PROFILE_DRAW, another [vram] line follows the [bufferpool] line every 10 s with the budget, usage, target, floor, the released device allocations not kept (count and MiB), the device allocations made while the pool was dropping (the churn) and the allocations placed in host memory, both by DeviceMemoryType and by the refused-allocation fallback in AllocateMemory (counted in HostPlacements() instead of two function-local statics).

When it matters. The pool only drops buffers once the device-local heap is over the budget VK_EXT_memory_budget reports. At default settings Minecraft Dungeons II on an 8 GB card settles below it, and nothing changes. It matters for a heap that is already full: a bigger title, a forced or oversized APS5_TEXTURE_CACHE_MIB, or other processes using VRAM. There, without this PR, the pool's retained buffers push new device allocations into host memory.

The crash it prevents (AlxcBTW, RTX 5070 Laptop 8 GB, Minecraft Dungeons II world load + walk): with the device-local heap over its budget, the automatic budget evicted a 256 MiB 3D storage texture (1024x1024x256 R8). It was re-created in host memory, and a compute pass over it ran past the 2 s TDR limit (nvlddmkm 153 / LiveKernelEvent 141). Over the earlier rows that stack lost the device in 2 of 11; with this PR, 0 of 5. Logs: to be attached by AlxcBTW.

Found with Demon's Souls on our fork: on a 16 GiB card the device-local heap reached 15.3 GiB at a level load while about 2 GiB of it was released buffers held for reuse. The demand floor replaces the first version's fixed headroom after CnRJay's churn review; it is parl0v's commit, with his lab rows in the thread (RTX 4060 8 GB, forced 3 GiB texture cache: walk 3.94 FPS with the headroom, 5.63 with the 1 s floor). Reclaiming cold cache entries would be a separate follow-up. The change itself has no title check.

Tested

On main 85a4d7f8c: agc_buffer_pool, agc_buffer_pool_small_device_shared, agc_buffer_pool_small_device_no_staging, agc_device_memory_fallback, the sampled and storage cache budget tests and agc_driver_pooled_buffer_range pass; python tools/check_conventions.py --base origin/main passes.

  • agc_buffer_pool budget tests (mock device, no GPU needed):
    • the target is the budget, with the quarter-heap / 1 GiB lower bound;
    • under the target, released device buffers are still kept;
    • over the target, the floor keeps what was taken in the last second and gives everything back once the window has passed;
    • over the target with no recent demand, released buffers are destroyed and the next request allocates;
    • a small device-tiered Put that evicts from the small tier leaves Excess() unchanged;
    • when usage rises after a fresh read, Put trims the oldest retained buffers;
    • an integrated GPU keeps its pool and the budget stays disabled;
    • a buffer release and an image release within 100 ms make one budget query;
    • image memory freed over the budget and buffers freed by ReclaimIdleDevice lower Excess() by their size;
    • over the target, retained image memory goes before the buffers the floor keeps.
  • Main's image pool, idle reclaim, attachment reclaim and host placement tests run unchanged.
  • Not yet run in a title on current main.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01VFVE5WbaUJ2vyj8NAW44CS

solbjorg1245 and others added 4 commits October 18, 2026 04:18
The buffer pool's device tier keeps released device-local buffers up to
an eighth of the largest device-local heap (at least 512 MiB), however
full the heap is. Near the budget VK_EXT_memory_budget reports, that
retained memory pushes the process over it, and past the budget an
allocation can still succeed while the OS pages device memory, so the
out-of-memory trim in AllocateMemory comes late or never.

VramBudget reads the budget and usage of the largest device-local heap
at most every 100 ms; its target is the budget less a headroom (a
sixteenth of the heap, 256 MiB to 1 GiB), never below a quarter of the
heap or 1 GiB, whichever is smaller. While the usage, less what the pool
released since the read, is over the target, Put destroys a released
device-tier buffer instead of retaining it, together with the least
recently used retained device buffers until the excess is covered.

Without the extension, on an integrated GPU (Context::integratedGpu) or
with APS5_NO_VRAM_BUDGET=1 the pool behaves as before.
APS5_VRAM_BUDGET_MIB caps the budget and APS5_VRAM_HEADROOM_MIB sets the
headroom. A [vram] line is printed at the first read and, with
APS5_PROFILE_DRAW, every 10 s after the [bufferpool] line.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VFVE5WbaUJ2vyj8NAW44CS
…eadroom

Over the VRAM target the device tier destroyed every released buffer,
so a title whose working set sits just above the target re-allocated
the buffers it asks for again within milliseconds (40k-210k drops per
120 s walk in Minecraft Dungeons II on an 8 GB card), and the headroom
that decided where that started had a different best value per title.

The target is now the reported budget itself, and the device tier keeps
released buffers up to what it handed out (hits and misses) in the last
second, in eight buckets of 125 ms, at most a sixteenth of the heap (a
load hands out gigabytes per second); only what is beyond that floor is
destroyed. A title that does not churn has a small floor and gives back
everything else. APS5_VRAM_HEADROOM_MIB and VramBudget::HeadroomFor are
gone; APS5_VRAM_BUDGET_MIB still caps the budget.

The [vram] line also reports the floor, the device-tier allocations made
while the pool was dropping (the churn the drops cause), and the
device-local allocations DeviceMemoryType placed in host memory, now
counted in HostPlacements() instead of function-local statics.

The budget tests now take their buffers under the budget and raise the
usage before releasing them: on current main DeviceMemoryType places a
device buffer in host memory once the heap is over its budget, so a
buffer allocated there never reaches the device pool.

A Put whose buffer lands in another tier no longer counts as device
memory released: since small device-local buffers go to the small tier,
Put read the excess for them too and reported whatever the small tier
evicted (possibly host-visible buffers) through VramBudget::Released,
so Excess() read low until the next budget query.
…et read

Main's pool now keeps released image memory in the device tier and reads
the heap budget itself (at most every 100 ms) to drop that image memory
over the budget, frees idle device buffers under storage cache pressure,
and trims the pool before an allocation retry. The VRAM budget had its
own reader and only knew about the buffers its own Put destroyed.

VramBudget no longer queries the driver: the pool's heap budget read
feeds it, so a device buffer Put and an image Put within 100 ms share one
vkGetPhysicalDeviceMemoryProperties2 call, and the read happens under the
pool mutex like the image path's.

Every device-tier free the pool makes between two reads is counted as
released, so Excess() stays right until the next read: Put's evictions,
the image path's drops and evictions, ReclaimIdleDevice and trim(). The
bytes are counted under the pool mutex before the memory is destroyed, so
a read that lands in between can only see the memory still in use, never
count it twice.

Over the target, Put gives back retained image memory before the buffers
the demand floor keeps (main already drops it over the reported budget,
and image takes are not demand), and the floor compares against the
buffers alone.

The refused-allocation host fallback in AllocateMemory now counts in
HostPlacements() too, so the [vram] line reports both placement paths.
APS5_NO_VRAM_BUDGET and APS5_VRAM_BUDGET_MIB are documented in
ENVIRONMENT.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NWqixRjxAxzP2dBDkLsjZ6
@Thimo188 Thimo188 closed this Dec 2, 2026
@Thimo188 Thimo188 reopened this Dec 2, 2026
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed perf Performance change perf numbers missing perf pull request without a before and after summary line labels Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with main, rebase needed perf numbers missing perf pull request without a before and after summary line perf Performance change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants