What:
- The thread Device::use_host_ram starts now keeps a window of chunks past the one an object
last took imported (host_ram.rs, Window): one chunk after a take outside the window (and the
RAM's first chunk before any take), AHEAD_CHUNKS = 4 once a take lands inside it. It never
imports past the window, never goes back to the RAM's start, and so faults in at most 4
chunks (256 MiB) that no object took for one take, and at most 1 for a take that starts no
run. While its window is imported it waits (Wake: a mutex and a condition variable), woken
when an object takes another chunk or the use ends, holding the device and the RAM only
weakly, as between chunks before.
- Unchanged: the pacing (SCHED_BATCH, yields, MADV_POPULATE_WRITE in 2 MiB steps), imports with
the memory lock released, the stop a quarter short of maxMemoryAllocationCount and the
give-back of untaken chunks.
- Docs: host_ram.rs, Device::use_host_ram, WorkCounts::host_ram_chunks_imported(_ahead),
docs/HOWTO.md ("One run inside one chunk"), with the numbers below and the burst caveat.
Why: reims-vgpu's time to first present on the RTX 5080 laptop went from 11-12 s to 13.9-86.1 s
with the whole-RAM ahead import, worst at low MemFree, the normal state of a Linux desktop
whose page cache fills memory. Reproduced without a VM (release probe, public API: 16 GiB of
fresh memfd RAM under MADV_HUGEPAGE through use_host_ram, an mmap+munmap sampler, CPU-bound and
bandwidth-bound workers, a toucher faulting its own THP memory, vmstat and PSI):
- The whole-RAM import took 40.4 s at 35.4 GB free with only 21 free 2 MiB blocks, and 57.4 s
at 17.1 GB free: pgmigrate 4.1-9.1 M pages, PSI memory "full" 6.6-9.6 s, another thread's
mmap up to 0.96-1.19 s, another thread's 2 MiB faults up to 0.8 s at p99 0.48 s, a CPU-bound
worker up to 51% slower at p50 and a bandwidth-bound one up to 128%.
- 99% of it is the fault-in of fresh huge pages (direct compaction and reclaim, holding the
process's memory map lock): per chunk p50 39-123 ms, worst 1.46 s. The import itself (pin and
vkAllocateMemory) of a resident chunk took 1.7-3.2 ms at p50 (0.56 s for 248 chunks).
- So the thread now faults in only what the objects are about to reach.
Measured on the same laptop (NVIDIA 615.71.09, Linux 7.2), release, one run each, MemFree
1.7-3.0 GB with 82-88 GB of page cache. Pattern: 40 2 MiB heaps at use_host_ram, then 8 bursts
of 128 consecutive heaps (4 chunks) 1 s apart, 2.08 GiB in all, with a toucher writing 1024
2 MiB pages spread over the same RAM (the guest's own faults):
ahead own imports making max (after the first 40) PSI full
adaptive (this) 36 2 26.9 ms 0.97 s
fixed 4 36 2 6.6 / 171 ms (2 runs) 0.48 / 0.97 s
fixed 1 9 26 372 / 614 ms (2 runs) 2.1 / 2.9 s
none (no thread) 0 34 544 ms 1.14 s
whole RAM (cut at 40 s) 138 2 194 ms 14.6 s
The 2 own imports are heaps 0 and 32, made as use_host_ram starts. 16 heaps over scattered
chunks, 300 ms apart: chunks faulted ahead adaptive 16, fixed 1 16, fixed 4 38, fixed 16 46;
making p50 adaptive 99 ms, fixed 1 51-60 ms, fixed 4 61-135 ms, fixed 16 160 ms, none 28 ms
(states vary run to run). A window of 8 or 16 bought nothing more for consecutive heaps and
cost more (16: PSI full 1.5 s, guest-fault p99 134 ms). A burst that runs more than 4 chunks
past the last take waits at each chunk past the window, one fault-in each (45-70 ms at p50 in
that state, at worst 0.34 s).
No DevicePolicy switch: this trades speed for speed, and no measurement favoured the whole-RAM
form.
Tests:
- host_ram::tests::a_take_inside_the_window_opens_it_and_one_outside_gives_one_chunk (the rule)
and ::the_devices_thread_imports_the_window_past_the_last_take_and_no_more (the order: the
start's one chunk, the window past a take, a held chunk passed, nothing past the window or the
RAM's end, nothing back after a jump).
- tests::host_ram_device::the_window_ahead_moves_with_the_objects_and_imports_no_more: fresh
THP RAM of 16 chunks; the first chunk alone before any take; a full window once a heap takes
it; a run of heaps over the window imports nothing on its own thread; one chunk past a jump;
a full window again once the run goes on; nothing past the RAM's end; the skipped chunk never
imported. 14 chunks ahead, 1 by an object, over 259 heap makings, on both devices.
- chunks_are_imported_ahead_so_no_making_waits_for_one and
the_ahead_thread_leaves_the_allocation_limit_to_the_objects now open the window with a heap
over the first chunk; the limit test gives back the 3 chunks no object took.
Negative controls (a temporary build with one defect switched on at a time, M3, release,
--cfg m2v_gpu); each test failed as follows:
- whole-RAM order (next_ahead wrapping as before): the order unit test ("none past the start's
one chunk", Some(1)); the window device test ("the device's thread went on past the window",
16 chunks).
- a fixed window of 4 for every take: the rule unit test (Window { from: 10, chunks: 4 } after a
jump); the window device test (13 chunks ahead after the jump).
- the window one slot off: the window device test ("no chunk past it", 6 live).
- no wake when an object takes another chunk: the window device test ("a full window past the
take", 1).
- no thread: chunks_are_imported_ahead_so_no_making_waits_for_one ("the device's thread
imported the RAM's first chunk", 0).
- no give-back: the_ahead_thread_leaves_the_allocation_limit_to_the_objects ("the untaken
chunks went back", 0).
- with no switch, the same binary passes the window device test.
Verified:
- RTX 5080 laptop, release, --cfg m2v_gpu: the host_ram and allocations tests, 30 passed,
0 failed.
- M3 Max (MoltenVK 1.4.2, macOS 27.0.1), release, --cfg m2v_gpu, under maclock: 28 passed,
0 failed (the two dma-buf tests are Linux-only).
- cargo clippy --workspace --all-targets --all-features -D warnings, plain and with
--cfg m2v_gpu; cargo doc -p metal2vulkan-runtime with -D warnings.
(cherry picked from commit 6d72e50ebb7d52e195fea5062bfed989af2877d4)