Repository navigation
Description
Summary
The ADR 0022 File-backend keeps warm restores fast by mmap+mlock'ing the entire base memory.bin (the full guest RAM) of every enabled image, resident and non-reclaimable, freed only on image disable (pin_memfile). That cost is per enabled image × its guest RAM, so it grows linearly with the number of enabled images and doesn't scale.
Observed: with 3 enabled images (4 GiB + 8 GiB + default ≈ 14–16 GiB) pinned per host on a 32 GiB node, ~half the node is gone before a single session runs — independent of session activity (idle changes nothing). kubectl top shows the host-agent pod at ~5–7 GiB but the node at ~20–25 GiB; the gap is these node-level pinned memfiles. guest_rss/pss=0 (no VMs) and the chunk cache is on NVMe, so this residency is the dominant idle-memory consumer.
Key insight
A restore doesn't touch the full guest RAM to boot — it touches a working set (the pages the kernel + harness + agent fault in the first window after restore), typically a small fraction of guest RAM. So:
Pinning the full image does not make the boot faster than pinning just the working set — the boot reads only the working set either way. The extra GBs only protect against mid-session faults on cold pages, not faster boots.
So the full-image mlock is overkill for cold-boot latency specifically; it's paying full-guest-RAM-per-image for a boot that only needed the working set.
The machinery already exists
engram-uffd-handler already does the right thing:
WorkingSetRecordercaptures faulted pages in a window and publishes aWorkingSetTrace(traces/<manifest_id>/<host_id>.json).prefault_from_trace— "on restore, if a trace is supplied we prefault" — eagerly faults in just the working set, leaving the cold tail to lazy UFFD page-in.- The coordinator already wires
working_set_blob_keyinto restore metadata (ADR 0039 cache locality).
So the trace is at least plumbed; the residency path just isn't using it.
Proposal
Scope base-snapshot residency to the working set rather than the whole image:
- Prefault/pin only the recorded working-set pages of the base
memory.bin; serve the cold tail lazily from the NVMe chunk cache / GCS (UFFD). Target ~10× less pinned RAM per image with no change to boot latency. - Alternatively, converge base restores onto the UFFD working-set-prefault path (which already prefaults the working set) instead of the File-backend full-
mlock.
Either way: cold boots stay ~500 ms-class, residency drops from full-guest-RAM × #images to working-set × #images, and enabling many images no longer linearly blows up host RAM.
Measure first
- Is the working-set trace actually being captured + published for base snapshots today, and how big is it vs guest RAM? (If it's plumbed but not producing a trace, fix that first — without a trace, narrowing falls back to full.)
- Tradeoff to quantify: a mid-session fault on a page outside the recorded working set is served from NVMe (fast) or GCS (slow if uncached). Measure how often dev workloads touch beyond the captured set, so we know whether the cold tail needs the NVMe tier warm.
Code pointers
crates/engram-host-agent/src/image_prefetch.rs—pin_memfile(~L115–149)mmap+mlocks the wholememory.bin; this is the residency lever.crates/engram-uffd-handler/src/working_set.rs+runtime.rs—WorkingSetRecorder,prefault_from_trace,finish/publish_trace,load_trace_via_blob.crates/engram-coordinator/src/api/sessions.rs—base_working_set_blob_key(~L1099) wiresworking_set_blob_keyinto restore metadata.
Acceptance
- Per-image base residency drops from full guest RAM to ~working-set size.
- Cold-boot / restore latency unchanged (~500 ms class).
#enabled-imagesno longer scales host RAM linearly.
References
- ADR 0022 (File-backend density / base-memfile residency)
- ADR 0039 (cache locality / working-set narrowing)
- ADR 0014 M1.14 (working-set narrowing origin)
- ADR 0019 (cold-boot latency)
- Related: Eviction snapshots are slow and reap their own lease mid-capture: parallelize the serial per-chunk GCS memory re-chunk #147 (eviction-snapshot serial re-chunk — same substrate, different path)
Activity
nikhilunni commentedon Jun 10, 2026
This gets resolved by the ADR 0045 substrate work (#165 + #166), via this issue's own proposal option 2 — base restores converge onto the UFFD path:
ENGRAM_FC_UFFD_BASE_DIRset (the D3 per-host switch, ADR 0045 D3: cold boot on the substrate — parity-gated, base-mode knob retired #166), the residency memfile is never materialized andpin_memfilenever runs — the full-guest-RAM mlock disappears entirely (base_memfile_diris derived asuffd_base_dir_from_env().is_none(); thememfilesmap stays empty).UFFDIO_CONTINUE, hole-punchable, never mlocked. Residency = touched-working-set × #images — the acceptance criterion here — with no dependence on a trace being published (demand-driven). Cold-tail faults land on the NVMe chunk cache as with today's Uffd resumes.Caveats: tmpfs pages aren't page-cache-reclaimable (no swap on COS), so the touched subset stays resident until hole-punched — the ADR 0045 D4 retention/hole-punch policy enforces the bound, and the helm
uffdBaseTmpfsSizecap is the hard backstop. The fix lands per host with the prodfirecracker.uffdBaseDirflip (the post-#166 step), not at merge.Suggest keeping this open until the flip lands and the idle-node RAM drop is observed via the fleet memory telemetry (the
kubectl topnode-vs-pod gap described above should collapse), then closing against #166.nikhilunni commentedon Jun 10, 2026
Acceptance observed in prod (2026-06-10, the ADR 0045 substrate flip):
Mlocked: 0on both KVM nodes; node memory 23.6 GiB → 9.2 GiB (83% → 32%) on the first node, the second's residual being plain reclaimable page cache (Cached), not locked RAM. Replacement residency is lazy + working-set-sized (canary base shm: 4 GiB apparent / 49 MiB allocated). Boot/resume latency unchanged (canary full lifecycle green, resume ~1.6 s). Follow-up retention/GC = ADR 0045 D4.docs(ADR 0045): D3 done — prod-flipped, canary-validated, #148 closed