Files
metal2vulkan/crates
Anees Iqbal b8ed1b8185 Free released blocks and events' semaphores on a free thread of the device's
An object's release that frees a block of the runtime's memory ran its
vkFreeMemory on the releasing thread. A driver that unmaps the block's pages
charges that to the free: on the M3 Max a release that freed a block took up
to 24 ms (a 2048x2048 Shared texture's, a 16 MiB buffer's), and the consumer
measured 2.4% of drops at 0.5 ms or more on an RTX 5080, up to 9.5 ms. An
event's release destroyed its timeline semaphore inline, about 400 us on the
5080 (the consumer's measurement), in the kernel driver.

Now the device has one free thread (free_thread.rs), started by the first
post, with a bounded queue (64 frees):

- Memory::let_go posts the block's buffer of addresses and memory, and the
  fresh pages a buffer's block was imported from (unmapped there after the
  free). Never an import of the caller's memory (a no-copy object's, a
  read-only view of the caller's pages, a guest RAM chunk): the spec's
  "the application must ensure that the imported memory range remains valid
  and accessible for the lifetime of the imported memory object"
  (VkImportMemoryHostPointerInfoEXT), and the caller may free its pages once
  the release returns, so those stay freed before it returns. On a device
  that parks freed memory (MoltenVK), a block whose batches had completed as
  it went is posted; a parked one is freed by the handler thread as before.
- EventShared::drop posts its semaphore's destroy: at the last holder's drop
  no batch (each holds the event until it completed), no reserved caller
  signal (held until it landed) and no caller submission
  (SharedEvent::vk_semaphore's contract) uses it. It leaves the device's set
  of own timelines at once; the driver hands the handle out again only after
  the destroy. Never recycled.
- A full queue, a teardown begun or a thread that cannot start frees inline
  (WorkCounts::free_thread_queue_full).
- Posted frees still count: against maxMemoryAllocationCount until their
  vkFreeMemory (allocations.rs), so an allocation at the limit waits for them
  before it is refused; and against their heap's size (Memory tracks each
  heap's live block bytes and the thread its posted bytes), so a block that
  would take its heap past the heap's size waits for them first, as one whose
  making failed for room does (memory.rs place, make_room). The runtime has
  no memory-budget query, so the heap's size is the bound.
  WorkCounts::memory_free_waits counts the waits.
- The device's teardown stops the thread after Memory::destroy and before
  vkDestroyDevice: every posted free runs and the thread is joined.
- The thread holds a share of the device's functions and its queue only, and
  runs each free in a section of the exit guard.

Address and ID reuse need no free: the Metal address space is the runtime's
own (address.rs; pinned addresses are its ranges, Vulkan device addresses are
never shown, no capture-replay address is asked for), and resource IDs and
ranges go back with the object as before.

Counts: WorkCounts::memory_frees_posted, semaphore_destroys_posted,
free_thread_queue_full, memory_free_waits (api_signatures blessed).

Verified on the M3 (MoltenVK, which parks freed memory, so the posts there
come from blocks whose batches had completed): runtime tests posted_frees (5:
a pinned address and a forced resource ID take new resources while the old
blocks' frees are held pending; an allocation at the limit waits for 3 held
frees and is made, none refused; a block past its heap's held size waits;
the teardown returns only after the held frees ran, the thread joined; an
event's semaphore destroy posted once the command buffer that signalled it
let go), with freed_memory, host_ram_device, teardown_threads, memory,
allocations, recycle, pool_room, fresh_pages and event tests: 105 passed.
Validation posted_frees (each release of a 96 MiB buffer, Private, Shared
over fresh pages or Managed, an 8192x8192 texture, a 96 MiB heap posts its
block; a no-copy buffer's import is freed inline, none posted; 8 events post
8 semaphore destroys) with pool_churn, placement, host_memory,
staging_batches and event lanes: 59 passed. Negative controls from archived
sabotaged builds: without the waits and the teardown's drain, the limit, heap
and teardown tests fail (3 of 5); with nothing posted, the lane fails
("private buffer: 0 posted").

Release bench on the M3 (C mode, before 4bbafa667d / after, same hold): the
releases that free a block had maxima of 4.5-5.2 ms (2048x2048 Managed and
Shared textures, 16 MiB buffers of each mode, a 64 MiB heap) and now 16-106 us;
16 MiB buffers' 300 releases total 10-11 ms before, 0.4-0.6 ms after.
No-copy releases are unchanged (inline by design, up to 1.3 ms for 16 MiB).
Events on MoltenVK: make about 2 us, release 0.2-0.4 us either way.

fmt, clippy (workspace, all targets, all features, plain and --cfg m2v_gpu),
rustdoc of the runtime, the release check with --cfg m2v_gpu and the
signature table are clean. The 5080 legs run on msi.

(cherry picked from commit 6d722a6769e1e5e7f4e7ff789f050a966b68c128)
2026-11-04 18:50:29 -08:00
..