Files
metal2vulkan/docs
Anees Iqbal 899270a674 rt: the pool is free ranges with a zero mark; hooks per allocation; one VkBuffer per allocation
What:
- One mechanism. A block given back is a free range of its allocation at once. The kept list, the
  returned fast path, unkeep and the take's wait for the block-maker thread are deleted.
- A take is the first free range that fits whose bytes are all zeroed, else the first that fits,
  in the order the allocations were made. It runs under the pool's lock, with no Vulkan call and
  no hook (Metrics::takes_from_free_ranges).
- A long allocation, which a take longer than 16 MiB made for itself, serves only takes of exactly
  its length. Metrics::shared_long_reuses and private_long_reuses count such takes after the
  first. Gauges::shared_long_reused_bytes and private_long_reused_bytes count the bytes of long
  allocations that hold one.
- The block-maker thread zeroes free Shared bytes that are not clean in 256 KiB steps, the
  allocations in the order they were made (Metrics::free_bytes_zeroed, free_zeroing_nanos). It
  starts no step while a consumer memset runs (Pool::fill, Metrics::zero_steps_held), the rule of
  76c6f5d071.
- Metrics::zeroed_bytes_used counts the bytes of zeroed Shared buffers that a zero mark covered,
  so the buffer's memset skipped them: the block-maker thread's zeroing that a buffer used,
  against Metrics::bytes_zeroed, the consumer's thread's. Per stream, the two decide whether the
  thread's zero steps are worth keeping.
- The hooks are per allocation:
  - BlockMappings::map(&AllocationInfo) runs before any block of an allocation goes out, outside
    the pool's lock: in Device::new for each class's first, on the block-maker thread ahead of
    need (Metrics::allocations_mapped), and inside a take that made its own
    (Metrics::allocations_mapped_on_takes).
  - A refusal frees the allocation, hands out none of its blocks and fails the take with
    Limit::AllocationNotMapped (Metrics::allocation_map_refusals).
  - BlockMappings::unmap(AllocationId) runs before vkFreeMemory, outside the lock. Quiesce and
    teardown go through it.
  - The per-block hook, BlockMappings::unmap(BlockId), is deleted. Its consumer, reims-vgpu,
    promises (its merge batch 232):
      "reims-vgpu's pool calls release_block only after the block's piece is out of the guest's
      address space (the host answered pool_removed; at the device's release the host has
      removed every piece first, as the API requires) and no thread of the device holds it, and
      it never touches the block afterwards, so the runtime may free any block it gets back at
      once, with no call into the consumer."
- BlockInfo carries its allocation's number and its offset there.
- One VkBuffer per allocation:
  - a block's commands reach it through Memory::range(offset), which gives (allocation buffer,
    base + offset);
  - the block's address is set at carve, with no Vulkan call;
  - the per-block VkBuffer, texture blocks' included, is deleted.
- The reserve: the newest idle 16 MiB allocations with free bytes that keep 8 MiB free. With no
  kept blocks, an idle allocation's bytes are all free, so the reserve is one 16 MiB allocation of
  each class at most.
- The README states the contract: the pool's allocations are in the guest's range whole, free
  ranges included; the consumer hands the guest blocks only; a guest kernel that writes a free
  range damages only its own later buffers.

Why: the planner's pool ruling. Each allocation costs a VMM ask once:
- WHPX: about 0.55 ms to map and 0.8 ms to unmap per 16 MiB;
- HVF: 3 us to map and 71 us to unmap;
- KVM: 116-124 us of BQL per ask.
A take from an allocation already made costs no Vulkan call and no VMM ask. The kept list held
blocks by exact length for the device's life and gave their bytes back only through an unkeep.

Tests (memory::on_device):
- each allocation mapped before its blocks go out, on the thread that made it, and a take not
  mapped refused, its allocation freed;
- zeroed bytes taken first, given-back bytes taken as written;
- a long allocation taken again at once, counted;
- idle frees, oldest first, the first fit taken again;
- the long allocation never the reserve;
- the zero mark on a long allocation's free bytes, in steps, and a zeroed buffer over it counting
  the mark's bytes as used;
- the zero hold beside a consumer memset;
- seeded takes with every block's allocation mapped.
The command tests' quiesce and teardown check each allocation mapped is unmapped once, on the
right thread.

Verified: nothing yet; the check job, the device legs and the measures follow.
2026-11-18 19:07:15 -08:00
..