Files
metal2vulkan/docs
Anees Iqbal 074d8e762e Blocks made ahead: a block's worth of bytes per pool, a block for large placements, and calls wait instead of making
The problem, from reims's pin 31 scroll round on um870's 780M. After slow makings, its plain
blocks were made at 16 MiB (RT 1984). Over two boots:
- 250 and 264 pooled blocks were made;
- 114 and 82 image blocks were made in place on the guest's threads;
- 110 and 159 blocks made ahead were waited for (91 and 122 hurried);
- direct compaction on guest threads took 833 and 306 ms.

Two causes:
- The ready thread kept one block ahead per pool. With 16 MiB blocks that is a quarter of the
  reserve that one 64 MiB block used to give. A call that found no block made and no making under
  way made its own.
- A placement larger than a 16 MiB block takes a 32 MiB block of its own size
  (pooled_block_bytes). No block made ahead could hold it, so it was always made in place.

What changed (memory/ready.rs, memory.rs):
- A pool keeps a block's worth of bytes ahead: as many of its blocks as one 64 MiB block holds
  (4 of 16 MiB).
- While the pool's placements larger than its blocks are recent (the last within
  KEPT_EMPTY_FOR), the reserve also holds one block of the largest such size. The pool's own
  blocks fill the rest of the 64 MiB (32 MiB + 2 x 16 MiB).
- One thread per pool makes them one after another, one vkAllocateMemory each, after a single
  wait for a burst's end. A block a call or a top-up queues while the thread runs joins its queue;
  a waiting call's block goes first.
- A call that finds no made block of the size it needs no longer makes one itself. It waits for
  the block being made or queued, or queues one first and waits. If the pool has no thread, it
  starts one. The waited block's pages are not read.
- The host copies' pool is now waited for too, except while its thread reads a block's pages
  (whose first touch may wait behind the program's GPU work). The waiter counts itself before it
  checks a reading flag; the thread sets the flag before it checks for waiters.
- A block is made in place only in these cases: a host-copies placement while that pool's thread
  reads pages; a making that failed or a thread that could not start; a placement before the
  device's first creation call.
- A placement over half of BLOCK_BYTES stays dedicated, as before.
- keep_room's per-pool step moves into keep_pool_room.

Resting cost (bytes kept ahead per pool, counted against the kept-blocks budget):
- Against pre-1984 (one 64 MiB block per growing or near-next pool): unchanged at 64 MiB.
- Against RT 1984 and RT 1987 (one block of the pool's size): unchanged for 64 MiB pools. A pool
  of 16 MiB blocks keeps up to 48 MiB more while it grows or is near its next block.
- At the device's first creation call, a pool of 16 MiB blocks also makes 3 blocks ahead after
  its first. If the pool never nears its next block they go after KEPT_EMPTY_FOR
  (blocks_ahead_unused). Pools of 64 MiB blocks, the default before any slow making, make none
  extra.

Work counts expected to move, for the cost records:
- buffer_blocks_made_in_place and image_blocks_made_in_place fall to 0, except in the cases above.
  That includes each kept pool's first block at a device's first creation call, which the call
  now waits for on the ready thread.
- buffer_blocks_made_ahead and image_blocks_made_ahead rise by as many. So does
  blocks_ahead_waited_for, since a call waited for each of them. These are not hurried: the
  making started at once for the call.
- host_copy_blocks_readied rises, and host_copy_blocks_made_in_place falls, where a host-copies
  placement found no making at all.
- pooled_memory_blocks_made is unchanged with 64 MiB blocks. With 16 MiB blocks the extra reserve
  blocks count there when added. When swept unused they count as blocks_ahead_unused and
  pooled_memory_blocks_freed.
- Kept-ahead bytes against the heap budget: a block's worth per pool, as above.

Docs: memory/ready.rs, memory.rs, the WorkCounts fields, and FAST-PATHS' kept-blocks row, which
now describes the 64 MiB reserve and the large block.

Tests (tests::blocks_ahead), on lavapipe under --cfg m2v_cpu_icd:
- a_pool_of_small_blocks_keeps_a_block_s_bytes_ahead_and_no_creation_call_makes_one: 16 MiB
  blocks, 4 MiB textures. 0 in place, 64 MiB kept ahead. The base fails it: 1 in place, 16 MiB
  ahead.
- placements_past_a_pool_s_small_blocks_find_a_block_of_their_size_made_ahead: a 20 MiB texture
  every 4. 0 in place, four 32 MiB blocks, 0 waits after the first 20 MiB texture. The base fails
  it: 5 in place, four of them 32 MiB.
- The memory, blocks_ahead, plain_blocks, zeroed_start, clip_bursts, exported/freed memory,
  device_recovery and host_copy tests passed, 78 of 78 (jobs 12486 and 12491).
- The 780M bench before and after (images, mixed and burst arms, fragmented) is running on gem10
  (job 12492). Its numbers go in the pin notes.

(cherry picked from commit 096241d0964536db875ab2957c64b40922e7fa8e)
2026-11-09 15:19:28 -08:00
..