Files
metal2vulkan/crates
Anees Iqbal 9b60da4bee host_ram: import a used RAM's chunks ahead, on a thread of the device's
What: Device::use_host_ram now starts a thread ("m2v-host-ram", through exit_guard::spawn)
that imports the RAM's 64 MiB chunks one after another, ahead of the objects over them. It
goes on from the chunk past the one an object last took, then from the RAM's start. It
skips a chunk whose lock is held (an object is importing it) and one it failed to import
(left to the first object over it, as before). It stops once every chunk is imported, the
use ends, the device goes or is lost, or the process exits (each import inside an
exit_guard section). It holds the device and the RAM weakly between chunks. An object over
a chunk the thread has not reached imports it on its own thread, as before. One over the
chunk being imported waits for that one import. New counter:
WorkCounts::host_ram_chunks_imported_ahead.

Why: a consumer makes 2 MiB heaps over consecutive fresh guest RAM (memfd). Each 32nd heap
was the first object over a chunk. Its making waited for the chunk's import, which faults
in and pins 64 MiB of fresh pages. That took ~7.5 ms exclusive on the RTX 5080 laptop, and
0.4-4.0 s in a shared boot with compaction (compact_stall +10,889). Keeping chunks imported
for the RAM's life already held at the start of this branch (a chunk stays in its slot
while the use lasts). Measured here: a heap made again after every heap was dropped
imported 0 chunks.

The trade-off: the whole RAM is resident and pinned from soon after use_host_ram, not only
the chunks objects touch. 16 GiB of fresh THP memfd RAM took 3.47 s on the thread, with
compact_stall 1152, compact_fail 56, pgmigrate_success 935291 and allocstall_normal 54,
all off the making threads. No kernel, boot or module parameter is needed. Recorded in
docs/HOWTO.md ("One run inside one chunk").

Vulkan rules relied on (checked against the 1.4.366 spec): the import itself is
Device::import_chunk, unchanged, under VUID-VkImportMemoryHostPointerInfoEXT-pHostPointer-01749
"pHostPointer must be a pointer aligned to an integer multiple of
VkPhysicalDeviceExternalMemoryHostPropertiesEXT::minImportedHostPointerAlignment",
VUID-VkImportMemoryHostPointerInfoEXT-handleType-01750 "pHostPointer must be a pointer to
allocationSize number of bytes of host memory", VUID-VkMemoryAllocateInfo-allocationSize-01745
"allocationSize must be an integer multiple of ...minImportedHostPointerAlignment", and
VUID-VkMemoryAllocateInfo-memoryTypeIndex-01744 "the value of memoryTypeIndex must be one of
those returned by vkGetMemoryHostPointerPropertiesEXT". The spec's "Externally Synchronized
Parameters" list names no parameter of vkAllocateMemory, so the thread's imports may run
while other threads use the device. Each chunk's import is made once, under the chunk's own
lock. A chunk is freed only on the existing path (vkFreeMemory's memory parameter is
externally synchronized: one owner frees it).

Verified on the RTX 5080 laptop (NVIDIA 615.71.09), release, --cfg m2v_gpu:
- All 21 host_ram tests pass, among them the new device test
  chunks_are_imported_ahead_so_no_making_waits_for_one. It covers 4 chunks of fresh memfd
  RAM under MADV_HUGEPAGE, imported ahead in 32.5 ms. Then 40 heaps of 2 MiB over
  consecutive runs: 0 chunk imports, 0 host-pointer imports and 40 chunk-bound buffers
  during the makings, the slowest making 78 us. Heap 33 passes a CPU write -> GPU read ->
  GPU write -> CPU read round trip. A heap made again after all were dropped imports
  nothing. The other device tests hold the thread back (Device::hold_host_ram_imports,
  test-only), so they still see each chunk imported by its first object.
- Negative control: with the spawn disabled, the new test fails ("the device's thread
  imported 0 of 4 chunks").
- On the M3 Max (MoltenVK, macOS 27.0.1), release, --cfg m2v_gpu: all 19 host_ram tests
  pass (the two dma-buf tests are Linux-only). In the new test, 4 chunks were imported ahead
  in 7.2 ms; across the 40 makings the slowest took 35 us.
- Unit test the_devices_thread_goes_on_past_the_chunk_an_object_took_then_from_the_start
  covers the order, the skip of a held or failed chunk, and the end.

Heap makings, 128 heaps of 2 MiB over fresh THP memfd RAM (wall time per
new_heap_with_bytes_no_copy):

  case                                          p50       p90       max      makings > 5 ms
  before, 1 GiB RAM                             0.001 ms  0.002 ms  7.39 ms  heaps 0, 32, 64, 96
  after, made at once                           0.001 ms  0.003 ms  7.54 ms  heaps 0, 32, 64, 96
  after, made once the thread is done (122 ms)  0.001 ms  0.002 ms  0.05 ms  none
  before, 16 GiB RAM                            0.001 ms  0.002 ms  7.54 ms  heaps 0, 32, 64, 96
  after, 16 GiB, made once done (3468 ms)       0.001 ms  0.002 ms  0.03 ms  none

Made at once, the makings outran the thread: 5 chunks were imported during them, 4 of
them by the thread. Once the thread is done, no making waits for an import.

(cherry picked from commit 7baa553a75515909f2ce9701c1e1bea4c408845a)
(cherry picked from commit 97a69f70ab96d37dcdf269192a6935f8c4126749)
2026-11-03 10:11:04 -08:00
..