Skip to content

Scope base-snapshot residency to the working set instead of mlock'ing the full guest RAM per image #148

Closed

Description

@nikhilunni

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:

  • WorkingSetRecorder captures faulted pages in a window and publishes a WorkingSetTrace (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_key into 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:

  1. 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.
  2. 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 whole memory.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) wires working_set_blob_key into 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-images no longer scales host RAM linearly.

References

Activity

  1. added
    enhancement
    on Jun 9, 2026
  2. nikhilunni commented on Jun 10, 2026

    @nikhilunni
    ContributorAuthor

    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:

    • With ENGRAM_FC_UFFD_BASE_DIR set (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 and pin_memfile never runs — the full-guest-RAM mlock disappears entirely (base_memfile_dir is derived as uffd_base_dir_from_env().is_none(); the memfiles map stays empty).
    • Its replacement is the per-template base shm: populated lazily (only faulted/working-set-prefaulted chunks materialize), shared across same-template VMs via 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.
    • Parity-gated before adoption (ADR 0045 D3: cold boot on the substrate — parity-gated, base-mode knob retired #166): density Σpss/Σrss 35% == 35% vs File mode, restore median 61ms vs 56ms.

    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 uffdBaseTmpfsSize cap is the hard backstop. The fix lands per host with the prod firecracker.uffdBaseDir flip (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 top node-vs-pod gap described above should collapse), then closing against #166.

  3. nikhilunni commented on Jun 10, 2026

    @nikhilunni
    ContributorAuthor

    Acceptance observed in prod (2026-06-10, the ADR 0045 substrate flip): Mlocked: 0 on 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions