Skip to content

vmpool: Destroy is still ~55% of every Exec, and 89% of it is cmd.Wait (SIGKILL to reap) #307

Closed as not planned

Description

@pdettori

With the slot cap (#305) and the restore path (#303) both accounted for, the largest single term
in an Exec is Destroy, at 55%. Nothing in vmpool degrades with concurrency, so this is a
flat per-Exec cost paid on every command.

Measured on srv-r16b14s16, 2026-09-21, via the existing ExecPhased/Phases decomposition
(which vmpoolctl already drives and the worker did not).

Per-Exec decomposition, one worker, MaxConcurrent=4

phase c=8 (n=3232) c=16 (n=4088) share at c=16
Acquire 961 us 158 us 0.3%
Resume 23,472 us 23,496 us 38.3%
Run (the actual command) 4,012 us 4,027 us 6.6%
Destroy 33,752 us 33,694 us 54.9%
total 62,197 us 61,375 us

Two things stand out. The command itself is 6.6% — the other 93% is VM lifecycle. And the
phases are identical at c=8 and c=16, so none of this is contention; it is fixed cost.

At MaxConcurrent=16 (#305's configuration) Destroy grew to 46,796 us while Resume stayed
flat at 24,331 us, which makes teardown the most likely next ceiling once the slot cap is lifted.

What Destroy does

firecrackerVM.Destroy (launcher_firecracker.go) SIGKILLs the jailer's process group, reaps it
with cmd.Wait(), and os.RemoveAll(jailRoot). Candidates, in the order I would measure them:

  • Waiting for the VMM to actually die. A SIGKILL of a Firecracker holding a 256 MiB
    MAP_SHARED guest mapping has to tear that address space down; cmd.Wait() does not return
    until it is reaped. This is the term I would expect to dominate and it is not something a
    faster RemoveAll would touch.
  • os.RemoveAll(jailRoot) — unlinking 6 hardlinks (vmstate, memfile, kernel, rootfs, agent,
    workspace.img) plus the directory tree. A microbenchmark of the full 12-op jail lifecycle ran at
    13,961 ops/s in one parent directory (e11: the restore path does not serialize — 90% of a restore is a fixed 20ms poll, and the c>=16 knee is downstream #303), i.e. ~0.07 ms, so filesystem metadata is very
    unlikely to be 33 ms — but it should be timed rather than assumed.
  • Guest-side unmount. The workspace ext4 is mounted at acquire (spec section 4.3,
    mount-at-acquire). If teardown waits on a clean unmount or an implicit flush, that is real I/O.

Splitting Destroy into kill / wait / remove is the obvious next instrumentation step, and it is
the same one-line-per-phase pattern used to produce this table.

Why it matters

  • It sets the floor on per-session throughput. The per-session execGate means one Exec at a
    time per run pool (Firecracker SerializesExecsPecRun() = true), so a session's ceiling is
    1 / per-Exec latency ~= 16/s at 62 ms — and 55% of that budget is teardown of a VM that has
    already finished its work.
  • A VM serves exactly one Exec in its life (VM.Run's doc comment: "calling Run twice is a
    bug"), so this cost is paid per command, not amortised.
  • It is a plausible next ceiling for session: DefaultConcurrency=4 is the per-process throughput cap — raising it beats adding workers (2.74x) #305: at 4x the slots, Destroy was the phase that grew.

Worth asking, not assumed

Does teardown have to be synchronous on the Exec path at all? The execGate comment says the
gate must not open until the VM is destroyed and its mount released, which is a correctness
constraint about the workspace image — so some of this is load-bearing and cannot simply be moved
to a background goroutine. Whether the whole of it must be, or only the unmount, is the design
question this issue is really raising.

Refs #305, #303, #291

Activity

  1. pdettori commented on Sep 22, 2026

    @pdettori
    MemberAuthor

    Still the largest term, and now localised: 89% of Destroy is cmd.Wait

    This issue's claim holds and its share has if anything grown. Updating with post-#319 numbers, because the figures in the body are pre-#315/#317/#319 and understate what is left.

    Measured on srv-r16b14s16, one worker, 64 slots (the measured throughput peak), n = 25,985.

    Destroy sub-phases, after #317 decomposed it and #319 pooled the cgroups

    sub-phase 64 slots 128 256 note
    kill 0.55 ms 0.68 1.03 noise
    cmd.Wait 58.94 ms 104.28 240.01 89% of Destroy
    removeall 5.92 ms 11.23 22.72
    cgroupwait 0.11 ms 0.17 0.46
    cgrouprmdir 0.046 ms 0.067 0.194 was 195.52 ms before #319

    The cgroup rmdir is solved — #319 reused per-VM cgroups from a pool and took it from 195.52 ms to 0.046 ms, a 3.25x throughput win. What remains is cmd.Wait: the interval between SIGKILL and the VMM process being reaped.

    Why this is still the right issue to keep open

    At the peak, Destroy is 61.51 ms of a 110.81 ms wall-clock Exec (55.5%) — the same share this issue reported at a quarter of the absolute throughput, so the campaign's 9.2x did not touch it proportionally. And it is not contention in vmpool: this issue's observation that the phases are identical at c=8 and c=16 still holds at the sub-phase level.

    cmd.Wait scaling 58.94 → 104.28 → 240.01 across 64 → 128 → 256 slots is roughly linear in slot count, which says it is not a fixed floor either. It is load-dependent kernel work — process teardown, address-space and page-table release for a VM whose guest RAM is a MAP_SHARED mapping of the pinned snapshot.

    What has NOT been tried

    • Whether any of that teardown can be moved off the Exec's critical path without reintroducing the problem that killed the deferred-rmdir experiment (time was conserved into Acquire, and 64 slots got worse — recorded in docs/notes/2026-09-22-exec-throughput-campaign-results.md).
    • Whether SIGKILL is even the right signal, or whether a Firecracker shutdown via its API returns the process faster.
    • Whether the MAP_SHARED + mlock arrangement makes teardown cheaper or more expensive than a private mapping would.

    Relationship to the other remaining lever

    The two dominant costs left are cmd.Wait here and sockwait on the restore side (filed separately). Both are process lifecycle rather than work — starting and stopping a VMM, not running the command, which is 6.2 ms. That is the argument for #274: a VM serving several Execs pays both once instead of per command.

    Note the populations differ and must not be summed: destroy phases is logged per VM destroy, while restore phases covers every restore including background replenishment, so sockwait is not on every Exec's critical path the way cmd.Wait is.

  2. changed the title [-]vmpool: Destroy is 55% of every Exec (33.7ms of 61.4ms) and the command itself is 6.6%[/-] [+]vmpool: Destroy is still ~55% of every Exec, and 89% of it is cmd.Wait (SIGKILL to reap)[/+] on Sep 22, 2026
  3. pdettori commented on Sep 22, 2026

    @pdettori
    MemberAuthor

    Companion issue filed: #328 covers the restore-side equivalent (sockwait, 38 ms at 64 slots, 869 ms at 256). Together these two are the whole remaining cost — cmd.Wait on teardown and sockwait on bring-up, both process lifecycle, against 6.2 ms for the command itself.

  4. pdettori commented on Sep 23, 2026

    @pdettori
    MemberAuthor

    Localised: cmd.Wait is KVM VM destruction blocking on RCU grace periods — and three of this issue's premises are wrong

    Measured on srv-r16b14s16, Firecracker v1.17.0, kernel 6.8, host otherwise idle. Instruments in #334.

    Where the floor comes from

    A three-arm probe, each arm differing from the previous by exactly one thing:

    arm wait4
    a jailed static binary 0.41 ms
    firecracker running, API socket bound, KVM fd open 0.20 ms
    + snapshot loaded (never even resumed) 17.4 / 18.6 ms

    98.9% of the floor appears on snapshot load. Reaping a process — even a live Firecracker
    holding a KVM fd — is sub-millisecond. proc_gone tracks wait4 to within 0.02 ms, so the task
    really is sitting in the kernel for that whole window; this is not reap latency.

    /proc/<pid>/stack during the window names it outright:

    __synchronize_srcu <- kvm_unregister_irq_mask_notifier <- kvm_pit_set_reinject
                       <- kvm_free_pit <- kvm_arch_sync_events <- kvm_destroy_vm
    rcu_barrier        <- kvm_mmu_uninit_tdp_mmu <- kvm_mmu_uninit_vm
                       <- kvm_arch_destroy_vm <- kvm_destroy_vm
                       ... <- kvm_put_kvm <- kvm_vm_release <- __fput
                       <- task_work_run <- do_exit
    

    81 blocking samples: rcu_barrier 58%, __synchronize_srcu 28%, __flush_workqueue 14%, with
    80/81 under kvm_vm_release. It is latency, not work — perf puts do_exit at 3.16% of
    host-wide samples and exit_mmap at 1.91%, which is why profiling alone found nothing here.

    The three premises

    Q1 — SIGKILL is not the problem, and the API would be worse. The cost is in
    kvm_vm_release, reached from __fput in task_work_run during do_exit. Any exit path
    pays it, so the signal cannot move it; a Firecracker API shutdown adds guest-side work on top.
    Nothing at launcher_firecracker.go:861 needs changing, and the package comment on process
    lifetime and shared pgids stands untouched.

    Q2 — the mapping is MAP_PRIVATE, and is not the cost. /proc/<pid>/maps reads
    256MiB rw-p ... /memfile. PinMemoryFile's own doc comment ("each VMM maps the same file
    MAP_PRIVATE") was right and this issue's body wrong. THP is madvise with AnonHugePages: 0,
    so it is 4 KiB PTEs; Rss is 31 MiB of a 256 MiB region with 5.7 MiB Private_Dirty. Whether
    the mapping were shared or private changes nothing about a grace period.

    Corroborating this from another direction: teardown-standby (restored, never resumed or
    run) costs 16.04 ms against teardown-inflight's 17.48 ms. ~92% of the reap is independent of
    the guest ever having run anything
    , which also disposes of the guest-unmount hypothesis in
    the original "what Destroy does" list.

    Q3 — the flat c=8/c=16 pair was a broken probe, not a finding. vmpoolctl drove that
    table, and firecrackerLauncher.SerializesExecsPerRun() is true, pool.go gives each runPool
    an execGate of capacity 1, and ExecPhased holds it "from a successful acquire until this
    VM is destroyed" — Destroy included. runExecMode passed one key for every iteration, so
    --concurrency only changed how many goroutines queued at that single gate. Both columns ran
    one VM at a time, which is why they agreed to 0.2%.

    With a key per slot (--keys, #334) the same driver, same host, shows monotone growth:

    c 1 2 4 8 16 32 64 8 (repeat)
    wait ms 19.28 23.04 28.95 36.24 43.60 50.56 52.48 36.46

    The repeated cheapest rung agrees to 0.6%, and c=64's 52.48 ms corroborates the 58.94 ms
    this issue reports at 64 slots from the worker path — two different drivers, same quantity.

    So these two datasets should not be averaged or reconciled numerically — one of them was
    measuring c=1 twice.
    And the growth that is real is sublinear and saturating: 2.7x for a
    64x concurrency range, only +3.8% from c=32 to c=64. That is the signature of a shared grace
    period rather than per-VM work — Destroy throughput goes 49/s at c=1 to 1131/s at c=64, a 23x
    gain from concurrency alone. The 58.94 -> 240.01 growth across 64 -> 256 slots is a different
    regime: those rungs are past the replenish knee where Acquire grows 65x and the true cold rate
    reaches 0.698, so wait there inherits a degraded pipeline rather than showing its own scaling.

    Q4 — removeall frees 3.51 MiB. Inventoried per VM before the unlink, identical every time:

    entries=18 dirs=4 linked=6 linked_bytes=3,136,449,916 owned=8 owned_bytes=3,676,687
    

    The six hardlinks (five snapshot components plus workspace.img) hold 3.14 GB that this
    unlink does not free
    — the snapshot directory and the run's WorkspaceDir still reference
    those inodes. The 3,676,687 bytes it does free are the jailer's exec-file copy, matching
    #332's independently measured 3.6 MiB. A storage-layer or thin-provisioning answer therefore has
    3.5 MiB of surface per VM, against a 5.92 -> 22.72 ms cost that is dentry and
    i_mmap/refcount work rather than block work. I do not think that conversation is worth having.

    One hypothesis built, measured and rejected

    Restore sets cmd.Stdout/Stderr to io.Discard, which is not an *os.File, so os/exec
    allocates two os.Pipe()s and two copy goroutines per VM — and cmd.Wait() drains them after
    Process.Wait(). At 580 Exec/s that is ~1160 goroutines and ~2320 fds per second, and it was my
    leading candidate for both the magnitude and the load-scaling.

    It is 0.006–0.055 ms, i.e. 0.016–0.097% of Destroy. Real, but not worth a line of code.
    Recording it because it is a plausible idea with a tempting one-line fix that someone will
    propose again, and drain_us now answers it from any run's logs.

    What is left

    • P4 microVM tier: per-Exec VM lifetime is finer-grained than the design needs — worth a separate architecture pass #274 is the strongest remaining lever, and this prices it: ~19 ms of unavoidable kernel
      grace-period latency per VM destroyed, against 6.2 ms for the command itself. A VM serving
      several Execs pays it once rather than per command.
    • Getting the reap off the critical path is more promising here than the rejected cgroup-rmdir
      deferral was
      , and the reason matters because the surface analogy says the opposite: that
      experiment moved serialised work, so time was conserved into Acquire and 64 slots got
      worse. This is overlapping latency — which is precisely why it saturates at 2.7x for 64x.
      The blocker is not the cost model but a correctness constraint: execGate is held until the
      VM is destroyed because the workspace ext4 must not be mounted rw by two guests, so that has
      to be satisfied before anything here is deferred.
    • rcu_expedited=1 cuts the floor ~30% — 19.24 ms mean baseline to 13.49 ms, with the
      baseline repeated last at 18.60 ms (6.4% session drift). I am not recommending it on this
      evidence.
      It is a latency measurement with no throughput A/B, which is the exact shape that
      sank the jail pool in feat(vmpool): split the pre-socket window at the jailed execve (#328) #332, and it is a host-wide tunable that trades grace-period latency for
      IPIs. It was toggled at runtime and restored to 0.

    Method notes

    Descending ladder, cheapest rung repeated last, per-rung RESULTS directories, dying cgroups
    recorded per rung (flat at 54–55, so #319's pool is holding), SH_DIAG_PHASES=1 on the sudo
    line, and the instrument verified on a 24-iteration run before any long one. Two traps hit in
    passing, both already documented: pkill -f "jailer --id" matched this session's own bash -c
    argv and killed the shell mid-run (runbook 3a), and a -run filter that matches nothing still
    prints ok.

  5. 37 remaining items

  6. pdettori commented on Nov 16, 2026

    @pdettori
    MemberAuthor

    Closing: the investigation is complete. As root-caused in the 2026-09-23 comment, the cost is the kernel's kvm_vm_release waiting on RCU/SRCU grace periods, about 19 ms per VM on every exit path. It is not vmpool work.

  7. added this to the v0.11.0 milestone on Nov 17, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions