Skip to content

perf(kyty): allocate device memory in 64 MiB VMA blocks - #290

Open
SleglJan wants to merge 1 commit into
simnJS:AnyKytyfrom
SleglJan:kyty-vma-block-64
Open

SleglJan wants to merge 1 commit into
simnJS:AnyKytyfrom
SleglJan:kyty-vma-block-64

Conversation

@SleglJan

Copy link
Copy Markdown

What

GraphicContext::CreateAllocator creates the VMA allocator with VMA's default block size. On heaps above 1 GiB VMA suballocates images and buffers from 256 MiB blocks, and its heuristic gives an allocation memory of its own when it is larger than half a block. VMA frees a block only when it is empty, so freed allocations leave partly used blocks behind.

  • CreateAllocator sets preferredLargeHeapBlockSize to 64 MiB. Heaps of 1 GiB or less keep VMA's own size (one eighth of the heap). VMA's heuristic now gives allocations above 32 MiB memory of their own.

In 007 on a 16 GB card, in a series with diagnostic logging, this takes device-local usage from 1.9-2.3 GiB over the memory budget to 0.1-0.7 GiB under it. Without the logging the title's GTT use drops by about 2.1 GiB and the frame rate rises by 39 % (see Tested).

This treats the symptom. The unused block memory builds up as 007 frees and creates memory, not only in large images: in a diagnostic run with a switch that gave every image of 8 MiB or more memory of its own, 007 created about 7 such images per frame on average and 2.6 GiB of block memory was still unused. Reducing that churn is separate work.

Known limits:

  • Measured on one title and one GPU (RX 9070 XT, 16 GB, RADV, Linux); not tested on Windows. A card with more VRAM may stay under its budget with 256 MiB blocks; for it the change only means more, smaller memory allocations.
  • More memory allocations: about 256 blocks per 16 GiB in use, across the memory types of a heap, plus one per allocation above 32 MiB. RADV reports maxMemoryAllocationCount = 4294967295; the Vulkan minimum is 4096.
  • 128 MiB blocks gave about half the gain (2 runs, see Tested), so the change uses 64 MiB. Sizes below 64 MiB were not tried.
  • Usage still sits close to the budget (median 15.2-15.7 GiB of 15.8-15.9 GiB in the series with the diagnostic logging), and 0.4-0.7 GiB still goes to GTT at the end of loading (none earlier in the load) and stays about constant during gameplay.

Tested

007 First Light (PPSA11386), Linux (CachyOS, kernel 7.3.4), Ryzen 9 7950X3D, AMD Radeon RX 9070 XT (RADV GFX1201, Mesa 26.2.6). Release builds, SDL_VIDEODRIVER=x11, scripted pad input, one save and route, shader caches warmed from one earlier run, the local 5-byte title patch and the setup described in #98.

This PR is based on 7de61e3451. All title runs used 7de61e3451 + #278 + the code of #289, the stack of the #98 runs. This PR changes no file that #278 or #289 change. It was not measured on 7de61e3451 alone.

Diagnosis: a diagnostic build (the stack above plus logging, not part of this PR; logging on, its diagnostic switches unset) wrote a VRAM breakdown every 60 frames. 6 runs with the default block size, 20 dumps each, median of the per-run medians:

  • VMA allocations 13.1 GiB in 17.3 GiB of blocks; unused block memory 4.2 GiB. Device-local usage 17.8 GiB against a budget of 15.9 GiB.
  • The unused part grows as allocations are freed: in one run 1.9, 3.3 and 4.3 GiB in the first three dumps while allocations fell from 15.8 to 12.8 GiB.

The block size as a switch (an environment variable, not part of this PR), both arms in the same binary, 300 s per run. FPS is the median of the window-title FPS over 200-300 s.

Build Run order (D = default) 256 MiB (default) 64 MiB 128 MiB
the stack above + the switch D 64 128 64 D D 64 128 D 64 29.47 / 29.59 / 28.93 / 30.38 41.11 / 42.06 / 40.20 / 41.40 36.88 / 34.86
the diagnostic build + the switch D 64 64 D 64 D D 64 29.40 / 30.86 / 29.13 / 29.74 44.29 / 40.51 / 41.91 / 43.12 not run
  • Without the logging: mean 29.59 FPS with the default, 41.19 with 64 MiB (+11.60, +39.2 %). With it: 29.78 and 42.46 (+42.6 %). In both series every 64 MiB run is faster than every default run, exact two-sided permutation p = 2/70 = 0.029 (the smallest possible with 4 + 4 runs). For the series without the logging that rule was fixed before the runs.
  • 128 MiB (2 runs, exploratory): mean 35.87 FPS (+21 %), above every default run and below every 64 MiB run.
  • With the logging: device-local usage (median of each run's dumps) 1.9-2.3 GiB over the budget with the default and 0.1-0.7 GiB under it with 64 MiB; at or under the budget on 0-20 % of the dumps with the default and on 75-90 % with 64 MiB; median unused block memory 4.2-4.7 GiB against 1.8-1.9 GiB.
  • Without the logging, system side (the title's DRM fdinfo on the card, from the end of loading to 5 s before the end of the run): requested VRAM that is not resident in VRAM, so sits in GTT, 2.2-2.8 GiB with the default, 0.4-0.7 GiB with 64 MiB and 0.8-1.0 GiB with 128 MiB; title GTT 2.9-3.5, 1.0-1.3 and 1.4-1.6 GiB. Title CPU time per frame (user + sys of all threads per second over the same span, divided by the FPS above): 149 ms with the default, 125 ms with 64 MiB (-16 %, every 64 MiB run lower). The kernel evicted no VRAM in any run (amd-evicted-vram 0).

Flip telemetry, one more pair on the build with the switch: the default, then 64 MiB, APS5_VIDEOOUT_FLIP_TIMING=9000:2000 in both runs, the same revision twice as ANYKYTY.md describes for two settings of one build (7af6efb7b9 is that local test build). Mean FPS here is over the completed flips of the window, not the window-title median above. tools/videoout_flip_compare.py default/videoout.jsonl 64mib/videoout.jsonl --baseline-revision 7af6efb7b9... --candidate-revision 7af6efb7b9...:

Arm Revision First flip Flips Mean FPS p50 ms p99 ms Telemetry drops observed
Baseline 7af6efb7b99fb679615db08b6b1fa4dfbd01eadd 9000 2000 29.52 33.580 56.448 0
Candidate 7af6efb7b99fb679615db08b6b1fa4dfbd01eadd 9000 2000 43.63 22.219 43.964 0
Candidate minus baseline +14.11 -11.361 -12.484
  • The full test set (110 tests: the Kyty shim tests, agc_kyty_shaders, guest_gpu_fault, guest_host_write_filter) gives the same result per test on 7de61e3451 and on this PR: 107 passed, kyty_shim_rt_corpus skipped, kyty_shim_wave64_cross_lane and kyty_shim_wave_split_written_reads fail here on both. No test is added: the change is one allocator parameter.
  • kyty_shim_image_write_back_alias also passes with VK_LAYER_KHRONOS_validation 1.4.363, ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1 (the layer loaded, per the loader log; no validation messages).
  • tools/check_conventions.py --base 7de61e3451 and tools/check_kyty_settings.py pass.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 9070 XT, RADV GFX1201, Mesa 26.2.6, Linux (CachyOS, kernel 7.3.4)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

VMA allocates memory for large heaps in 256 MiB blocks by default. Freed
allocations leave partly used blocks behind: in 007 on a 16 GB card the
allocator held 4.2-4.7 GiB of block memory that no allocation used, and
device-local usage stayed 1.9-2.3 GiB over the memory budget, with the
excess placed in GTT.

Use 64 MiB blocks for large heaps. VMA's heuristic now gives allocations
above 32 MiB memory of their own.
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.

1 participant