Files
metal2vulkan/crates
Anees Iqbal 0280c19285 rt: the block-maker thread frees idle allocations one at a time, only in quiet gaps
The planner's ruling (free when quiet), replacing "free whenever the window lets go": a driver may
hold every submission of the process for the whole of a vkFreeMemory, and on the NVIDIA cards we
measured in Thunderbolt enclosures freeing host-visible memory takes about 1.1 ms per MiB (15-17 ms
for one 16 MiB allocation), where an internal NVIDIA card frees 16 MiB in about 0.5 ms (store lane
and planner rows). Under the 10 s window a give-back freed every surplus allocation at once, back
to back, beside the guest's submissions.

What changed:
- The idle rule (which allocations are surplus: the window's peak, the cap, the reserve) is as it
  was. It now answers one allocation at a time (Rule::Free), and the block-maker thread frees it
  only once no commit or submission has come on any of the device's queues for 20 ms
  (QUIET_NANOS), then looks again. CommandBuffer::commit and the submit thread's vkQueueSubmit2
  stamp the pool (one atomic fetch_max of host_time, nothing else on those paths).
- The rule runs on the block-maker thread's own time too: it sleeps until the next change, the
  end of the quiet time where it has an allocation to free, or the second the window's peak can
  fall where it keeps idle allocations. So a program that goes quiet gives its idle allocations
  back without a call of its own (before, the rule ran only on a take or give-back).
- A spilled allocation that empties retires: no take lands in it, its bytes are not free bytes,
  and it is the rule's next to free, through the same quiet path (it was freed at once).
- Metrics::allocations_freed counts the frees; Metrics::submissions_during_frees counts the
  submissions made while the thread was inside a free's vkFreeMemory. The replay's per-frame
  counters carry both.
- README: the idle paragraph, and a known gap of memory blocks in the planner's wording.
  release_block's doc. api_signatures gains the two fields.

Tests:
- every_device_frees_idle_allocations_as_the_window_s_peak_falls: the three oldest freed one at
  a time when the clock passes the window, and the fourth freed five seconds later with no take
  or give-back (the timer path); four frees counted.
- every_device_frees_idle_allocations_in_quiet_gaps: command buffers committed every 2 ms while
  four idle allocations are surplus; every free begins at least 20 ms after the last commit before
  it (checked against the test thread's own commit times, whatever its gaps); the four are freed
  once the commits stop. Then a free held open: a submission during it is counted, one after it
  is not.
- The spill test exercises the retiring path (it failed while a newly made spilled allocation was
  taken for an emptied one: 3090, 2080, RX 550 in m3-max-44872/44873/44874; fixed).

Verified at d7918e27fa (this commit's code on a6520161e8; the tree here is the same diff applied on
development 61f2799088): check m3-max-44879 (clippy all-features, --cfg m2v_gpu, rt under
--cfg m2v_cpu_icd, translator no-feature; rt and harness suites); legs 3090 m3-max-44882,
2080 44883, RX 550 44884, lavapipe 44885: 174/174 each. The old runtime and the translator are
untouched.
2026-11-18 00:20:41 -08:00
..