Skip to content

vmpool: the pre-socket wait is 82-85% JAILER chroot construction, not Firecracker (which binds in ~1.5ms) #328

Closed

Description

@pdettori

Summary

sockwait — the wait for Firecracker's API socket to appear after the jailer starts — is 38.07 ms of a ~47 ms restore at 64 slots, and 869 ms at 256 slots. It is the dominant cost of a restore and therefore of the pool's replenishment rate, which is what now bounds throughput.

This is not the quantization artifact #304 addressed. #304's fix was correct and is merged (#315), but it only removed the polling granularity; the underlying wait is real, and it is much larger than that issue's framing implied.

Measured on srv-r16b14s16 (bare metal, 72 cpu / 754 GiB, governor performance, swap off), one worker, merged main with pooled cgroups (#319), via SH_DIAG_PHASES.

The measurement

restore phases, mean per restore:

slots prep wsimg jailer sockwait loadsnap restore total
64 0.62 ms 0.25 ms 2.18 ms 38.07 ms 5.56 ms ~46.7 ms
128 — — 2.91 ms 238.72 ms 8.02 ms —
256 — — 4.14 ms 869.43 ms 11.47 ms —

p50 tracks the mean closely (34.47 / 235.96 / 858.40 ms), so this is not a tail effect — it is where the distribution sits. n = 25,985 / 51,969 / 103,937.

It degrades 23x from 64 to 256 slots, far worse than linearly, while jailer (the fork/exec itself) grows only 1.9x and loadsnap 2.1x.

Why this is not a polling artifact

waitForUnixSocket polls at socketPollMin = 250us growing to socketPollMax = 1ms (launcher_firecracker.go), so the resolution is 1 ms or better throughout. A 38 ms reading means the socket genuinely was not there for ~38 ms.

This was A/B tested during the campaign and the poll cap was explicitly falsified as a throughput cause — raising the cap changed nothing. That result stands; it just means the remaining time is real work rather than sleep.

Worth stating plainly because it contradicts how #303 and #304 have been cited: "90% of a restore is a fixed 20 ms poll" was true of the instrument, and the reading was quantized to the 20 ms quantum. With fine-grained polling the true value is larger than 20 ms at the operating point, not smaller.

Why it matters

  • This is plausibly the mechanism behind "the replenishment rate", which is the campaign's remaining bound. Every restore pays sockwait, replenishment is restores, and throughput peaks at 64 slots because demand outruns the rate the pool can restore VMs (Acquire 4.30 → 281.20 ms and true cold rate 0.142 → 0.698 across the same range).
  • It would explain why a second worker process does not help. Two workers at the same total slot count deliver 1.00x (580.58 vs 580.60) while host CPU stays at 66–73%. If what serialises is kernel-side process bring-up, both processes contend for the same thing, which is consistent with a host-wide bound that scale-out cannot relieve.
  • Together with vmpool: Destroy is still ~55% of every Exec, and 89% of it is cmd.Wait (SIGKILL to reap) #307's cmd.Wait (58.94 ms, 89% of Destroy), the two dominant remaining costs are both process lifecycle rather than work. The command itself is 6.2 ms.

What to investigate

  1. What is Firecracker doing for those 38 ms before it binds its API socket? Under the jailer, with --cgroup-version 2 --parent-cgroup and a MAP_SHARED pinned memfile. Candidates: the jailer's own chroot/pivot_root and mount setup, seccomp filter installation, the binary's dynamic-link and init path, or simply CPU starvation at 47 of 72 cores busy.
  2. Is it CPU starvation or serialisation? The 23x degradation across a 4x slot range is a queueing signature, but host CPU is flat at 66–70%, so it is not simple saturation. A perf/strace pass on one restore under load, compared against an idle restore, should separate these.
  3. Does it scale with VM count or with restore rate? Distinguishable by holding slots fixed and varying ITERS_PER_SLOT.
  4. Is the socket wait avoidable at all? If Firecracker can be given a pre-bound socket (inherited fd) rather than creating its own, the wait disappears by construction. Worth checking what the API supports.

Relationship to #274

If per-Exec VM lifetime lands, a VM serving several Execs pays sockwait and cmd.Wait once per session instead of once per command. That does not make this issue moot — a lower restore cost raises the replenishment rate for any lifetime policy — but it does mean the two should be sequenced deliberately rather than pursued independently.

A caution on the arithmetic

restore phases is logged for every restore, including background replenishment, while exec phases's resume_us is per Exec. These are different populations and must not be summed: at 64 slots sockwait (38.07 ms) exceeds per-Exec resume (32.50 ms), which is not a contradiction — most Execs pop a warm standby and never wait for a socket. sockwait bounds the rate at which standbys can be produced, which is the constraint, rather than being a term on every Exec's critical path.

References

Activity

  1. pdettori commented on Sep 22, 2026

    @pdettori
    MemberAuthor

    Root-caused: it is the JAILER, not Firecracker — and two numbers in the issue body are wrong

    Three measurements on srv-r16b14s16, idle host except where stated. Correcting this issue's own figures first, then the mechanism.

    Correction 1 — the 38.07 ms figure was inflated by host state

    A clean ladder (drop_caches before every rung, dying cgroups verified 0 at every rung start, slots pinned at 64 so only load varies) gives:

    c Exec/s restores jailer phase sockwait mean p50 p95 host CPU sw % of restore
    1 17.97 87 0.34 16.73 17.67 23.33 1.8% 85%
    2 33.74 173 0.38 13.56 13.75 25.38 3.2% 82%
    4 59.40 345 0.39 11.73 7.05 27.56 5.5% 80%
    8 106.16 689 0.42 9.78 6.76 25.80 10.0% 75%
    16 186.58 1377 0.49 10.11 7.49 27.73 18.9% 74%
    32 332.14 2753 0.75 12.10 9.61 28.00 34.4% 72%
    64 541.94 5505 1.62 22.31 17.67 49.69 65.5% 73%
    64 rep 549.21 5505 1.61 25.43 20.07 58.94 65.1% 75%

    So at 64 slots it is 22–25 ms, not 38.07. The original figure came from a sweep where caches were dropped once at the start and dying cgroups had accumulated to ~1000 — host state, not sockwait. Drift check on the repeated rung: +1.3%.

    The 238.72 / 869.43 ms figures are also mislabelled: those were at 128 and 256 slots, i.e. over-subscription past the throughput peak, a different configuration rather than the same configuration under more load.

    Correction 2 — sockwait is U-shaped, and only about half of it is contention

    It minimises at c=8 (9.78 ms mean / 6.76 ms p50) and rises in both directions. The decisive row is c=1: with the host at 1.8% CPU and nothing to contend with, sockwait is still 16.73 ms. I checked for cold page cache from the drop_caches — the 87 values run 29.9, 18.7, 13.0, 16.8, 18.8, 19.2 … 21.5, 14.7, 14.8, 18.5, 16.2, 20.5, so only the first is elevated and there is no decay. The floor is real.

    Reading it as components: an irreducible floor around 7 ms p50, a load-dependent increment (c=8 → c=64 nearly doubles it), and a rise at near-idle whose cause is not established (plausibly CPU idle-state exit latency, untested).

    The mechanism: 82-85% of the pre-socket window is the jailer

    strace -f -tt on the jailer, splitting execve(jailer) → execve(firecracker) → bind():

    phase run 1 run 2
    jailer startup + chroot construction (before the first mount) 5.85 ms 4.59 ms
    mount x2, pivot_root, mknod x4 (/dev/net/tun, /dev/kvm, /dev/urandom, /dev/userfaultfd) 2.45 ms 1.72 ms
    jailer total 8.30 ms 6.32 ms
    Firecracker execve -> bind 1.52 ms 1.42 ms
    total pre-socket 9.82 ms 7.74 ms

    Corroborated without strace, same idle host: bare Firecracker binds in 2.87-3.14 ms against a harness floor of 2.44 ms (each date +%s%N is itself a fork+exec), while the jailed path takes 7.4-7.6 ms. Consistent with the traced split.

    Firecracker is not slow. It binds its socket in ~1.5 ms. The wait is the jailer building a fresh chroot per VM.

    Absolute values are inflated by ptrace overhead, but the split is measured within single runs and is corroborated by the untraced comparison, so the 82-85% share is the robust part.

    This is also an instrumentation bug in our own phase accounting

    The jailer phase reads 0.34-1.62 ms across the whole ladder because it is measured as time.Since after cmd.Start() returns — i.e. it times fork/exec returning, not the jailer's work. All 6-8 ms of chroot construction happens after that and lands in the sockwait bucket.

    That mis-attribution is why this looked like "Firecracker is slow to bind" for two rounds of analysis. Worth splitting sockwait into jailer_setup (until the jailed execve) and fc_bind, so the field names say where the time is.

    What this changes about the fix

    --no-api is dead. Tested directly: --config-file requires drives and boot-source and then asks for the kernel image — it is a fresh-boot config, and a snapshot key is silently ignored (unknown fields are not rejected). Snapshot restore is API-only in v1.17.0. Separately, we need the API twice: PUT /snapshot/load and later PATCH /vm {state: Resumed}, since standbys are deliberately left paused.

    The promising direction is pooling the jail, exactly as #319 pooled the cgroups. The per-VM work the jailer repeats — create the chroot dir, bind-mount, pivot_root, four mknods — is the same class of per-VM kernel/filesystem churn that cost 195.52 ms per Destroy until it was pooled. Options worth costing:

    1. Reuse jail directories from a pool, as cgroupPool does, so the chroot and its device nodes are created once and rebound rather than rebuilt.
    2. Skip the jailer for the restore path and do the chroot/pivot_root/mknod setup ourselves against a pre-built template, keeping the isolation properties but not the per-VM construction.
    3. Check whether all four mknods are needed — /dev/net/tun in particular, given this launcher never issues PUT /network-interfaces and standbys are headless by construction.

    Option 3 is the cheapest to test and option 1 has a proven precedent in this codebase.

    Relationship to #274: unchanged in direction but weaker in isolation. #274 amortises the whole restore across a session, which still helps. But since most of this cost is our own per-VM setup rather than an inherent VMM property, it may be removable outright — which would be worth more than amortising it, and would compose with #274 rather than competing.

  2. changed the title [-]vmpool: sockwait is 38ms of a restore at 64 slots and 869ms at 256 — the 20ms poll fix removed the quantization, not the wait[/-] [+]vmpool: the pre-socket wait is 82-85% JAILER chroot construction, not Firecracker (which binds in ~1.5ms)[/+] on Sep 22, 2026
  3. pdettori commented on Sep 22, 2026

    @pdettori
    MemberAuthor

    The three options are now one: option 3 is unavailable, option 1 is validated (~2x)

    Two cheap tests on srv-r16b14s16 narrow this considerably.

    Option 3 (drop /dev/net/tun) — NOT AVAILABLE

    jailer --help on v1.17.0 exposes no device-node control. The four mknods are hardcoded, so
    dropping tun would mean patching or replacing the jailer rather than configuring it. Off the table
    as a cheap win.

    Option 1 (pool the jail) — VALIDATED, and it halves the pre-socket time

    The jailer reuses an existing jail directory rather than refusing it. Same --id, three
    sequential runs, idle host, time from execve(jailer) to the API socket being bound:

    attempt jail dir state time to socket
    1 created fresh 8275 us
    2 reused 4025 us
    3 reused 4008 us

    ~4.3 ms saved per restore, about half the pre-socket window, and 4.0 ms is close to the
    ~2.9 ms bare-Firecracker floor (measured against a 2.44 ms harness floor). The saving is presumably
    the chroot construction and the exec-file copy that reuse skips; the mknods appear to be the
    cheaper part.

    What a reused jail leaves behind, i.e. what a pool would have to reset:

    /srv/jail/firecracker/<id>/root/api.sock
    /srv/jail/firecracker/<id>/root/firecracker.pid
    /srv/jail/firecracker/<id>/root/firecracker      <- the copied exec file
    /srv/jail/firecracker/<id>/root/dev/{kvm,urandom,userfaultfd,net}
    /srv/jail/firecracker/<id>/root/run
    

    The device nodes can stay. api.sock and firecracker.pid are per-VM residue.

    Two things to get right, and one is a security question

    1. Residue between tenants. A pooled jail is a directory a previous VM had write access to.
      cgroupPool handles the analogous risk by refusing to reuse a cgroup with a process still in it
      and counting that as a leak rather than reusing optimistically; a jail pool needs the equivalent,
      plus an explicit decision about what must be removed before reuse. This is the part that deserves
      care — it is the only place in this issue where a performance change touches isolation.
    2. Concurrent reuse. The test above reuses one id sequentially. A pool needs a free list with
      verify-on-pop, exactly as cgroup_pool.go does, and the same "never reclaim a name after a
      failed mint" discipline (perf(vmpool): reuse per-VM cgroups from a pool instead of creating one per VM (#258) #319 review).

    Prerequisite: fix the phase split first

    The jailer phase currently measures time.Since after cmd.Start() returns — fork/exec
    returning, not the jailer's work — so all 6-8 ms of chroot construction lands in sockwait. Any
    fix here will be measured with that blind spot unless sockwait is first split into jailer_setup
    (until the jailed execve) and fc_bind. That mis-attribution already cost two rounds of analysis
    chasing "Firecracker is slow to bind"; it should not also obscure the fix.

    Revised recommendation

    1. Split the phase accounting (small, and it is the measuring instrument for everything below).
    2. Implement jail pooling mirroring cgroup_pool.go, with the residue policy decided explicitly.
    3. Re-measure; expect the pre-socket window to drop from ~8 ms toward ~4 ms at low load, and check
      what it does to the load-dependent half, which this test does not address.

    Option 2 (skip the jailer entirely) drops in priority — option 1 gets most of the win without
    reimplementing isolation.

  4. pdettori commented on Sep 22, 2026

    @pdettori
    MemberAuthor

    Handoff note for whoever picks this up: PR #331 (docs/notes/2026-09-22-328-jail-pooling-handoff.md).

    It pins the order of work (phase-split first, because it is the instrument for measuring the fix), the residue/isolation decision to settle before writing the pool, what to measure afterwards (floor and slope), the three dead ends with their evidence, the two corrected figures from this issue body, and the rig gotchas — including the one that cost a full ladder in this session.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions