Skip to content

vmpool: waitForUnixSocket's fixed 20ms retry is 90% of every restore (TDD) #304

Closed

Description

@pdettori

waitForUnixSocket retries on a fixed 20 ms sleep, which makes every Firecracker restore
about 10x slower than the work it does. Measured in #303: of a 27,706 us restore,
24,956 us (90.1%) is this sleep and 2,748 us is real work.

The code

remote-worker/internal/vmpool/launcher_firecracker.go:

func waitForUnixSocket(ctx context.Context, path string, timeout time.Duration) error {
	deadline := time.Now().Add(timeout)
	for {
		if conn, err := net.DialTimeout("unix", path, 100*time.Millisecond); err == nil {
			_ = conn.Close()
			return nil
		}
		if time.Now().After(deadline) {
			return fmt.Errorf("timed out after %s waiting for %s", timeout, path)
		}
		select {
		case <-ctx.Done():
			return ctx.Err()
		case <-time.After(20 * time.Millisecond):   // <-- every miss costs a full 20ms
		}
	}
}

The first dial fires immediately and fails, because the jailer has not yet chrooted, dropped
privileges and exec'd Firecracker, and Firecracker has not yet bound its API socket — which
takes on the order of 1-3 ms. The loop then sleeps a full 20 ms before looking again.

Evidence it is the 20 ms and not the socket

The measured distribution over 3249 restores is quantized with nothing in between and, decisively,
nothing below 20 ms — a socket that genuinely took 20 ms would produce a spread, not steps:

sockwait restores
~20-22 ms 2,535 (78%)
~40-42 ms 714 (22%)
< 20 ms 0

At c=16 a third step appears at ~60 ms (28 restores) and the ~40 ms step grows to 26%, i.e. under
more load Firecracker occasionally needs a second or third quantum — but the step structure is
unchanged, so the sleep sets the floor in every case.

Suggested direction

Poll far more finely, or not at all:

  • Fine/exponential backoff — start at ~250-500 us and grow (e.g. x1.5, capped at a few ms).
    Cheap, local, no new dependency. Should land restores at ~3-5 ms.
  • Watch instead of poll — inotify on the jail's run/ directory for the socket's creation,
    then dial. Removes the guess entirely, at the cost of a Linux-specific path (the launcher is
    already Linux-only in practice, see fcPlatformSupported).

Either way keep the existing 5 s overall timeout and the ctx cancellation, and keep dialing
rather than stat-ing: the current comment is right that the socket file appears before
Firecracker is accept()ing, and that is load-bearing for the id-collision guard
(fcJailOccupied) too.

Expected impact — real but not dramatic, stated honestly

Restore ~28 ms -> ~3-5 ms. Because a cold acquire is only part of an Exec, the throughput effect
at the measured operating points is roughly:

  • c=8 (coldAcquireRate 0.76): per-Exec 118 ms -> ~100 ms, about +18%
  • c>=16 (coldAcquireRate ~1.0): per-Exec 254 ms -> ~231 ms, about +10%

It also improves cold-acquire latency for every user, which matters independently of
aggregate throughput, and it lowers p95 (measured 139-155 ms at these rungs).

It is not the scale-out answer — #259 measured 1.82x from a second worker process. This is a
cheap, safe, independent win.

Doing it under TDD

A test can pin this without hardware: drive waitForUnixSocket against a socket that is created
after a known short delay (say 2 ms) on a temp path, and assert it returns in well under one
current quantum. That test fails today (it would take >=20 ms) and passes with a finer poll, which
is exactly the failing-test-first shape. Add a second case asserting the 5 s timeout and ctx
cancellation still hold, so the fix cannot regress the refusal paths.

Note SH_DIAG_PPROF-style phase timing is what made this visible; #303 has the method.

Refs #303, #259, #291

Activity

  1. pdettori commented on Sep 21, 2026

    @pdettori
    MemberAuthor

    Still worth doing, but the "Expected impact" section above is wrong and should not be used to
    justify or size it.
    The correction matters because it changes why you would do this.

    What was wrong

    The estimate ("+18% at c=8, +10% at c>=16") was derived from the driver's coldAcquireRate of
    0.76 and ~1.0 — i.e. from the assumption that most Execs pay a restore. That figure is a latency
    proxy and it is wrong by 13x: the true cold rate, read from vmpool.Phases.Cold on the same runs,
    is 0.056 at c=8 and 0.0039 at c=16 (#306).

    So at the measured operating points a restore is almost never on the Exec critical path:
    Acquire is 961 us at c=8 and 158 us at c=16, against a ~62 ms Exec. Removing 25 ms from a
    restore that >99% of Execs never wait for buys ~0% throughput there. The claimed +18% / +10% is
    not real.

    Why it is still worth fixing

    Three reasons, none of which depend on the bad estimate:

    1. It matters once the slot cap is lifted, and that is the direction of travel. At
      WORKER_MAX_CONCURRENT=16 (session: DefaultConcurrency=4 is the per-process throughput cap — raising it beats adding workers (2.74x) #305, which took one worker to 172.88 Exec/s) Acquire rose from
      158 us to 13,338 us — because 16 concurrent Execs drain standbys faster than D=2 per pool
      replenishes. At that operating point the restore is on the hot path, and 90% of it is this
      sleep. The fix becomes a throughput fix precisely where throughput is going.
    2. First-Exec latency is user-visible. first-exec was a recorded cold cause on every run. A
      session's very first command pays a full restore, and today that is ~28 ms of which ~25 ms is a
      sleep. That is a cold-start cost a user feels directly.
    3. Replenishment rate. Standby refill is ReplenishDelay + restore per slot; cutting restore
      from ~28 ms to ~3-5 ms materially raises how fast a pool recovers from a burst, which is what
      keeps Acquire near zero in the first place.

    Restated expectation

    • At the current default (MaxConcurrent=4): negligible throughput change; a real improvement
      to first-Exec latency and to burst recovery.
    • At raised concurrency: a genuine throughput contribution, since Acquire is ~13 ms of an
      ~89 ms Exec there and ~90% of that is the sleep.

    The TDD shape suggested above is unaffected: drive waitForUnixSocket against a socket created
    after a known short delay and assert it returns in well under one 20 ms quantum, plus cases
    holding the 5 s timeout and ctx cancellation.

    Refs #305, rossoctl/moca-experiments#8, #303

  2. pdettori commented on Sep 21, 2026

    @pdettori
    MemberAuthor

    Measured: this is the binding constraint on throughput, not a ~0 latency nicety

    This issue's own correcting comment says the throughput effect "at the current default is ~0"
    because >99% of acquires are warm, and the campaign plan sequences it last for that reason. A slot
    sweep on srv-r16b14s16 (full data on #305) says that holds at 4 slots and nowhere above it.

    Restore sub-phases, microVM arm, D=2, 256 MiB guests, SH_DIAG_PHASES=1, one run per point with
    slots == c, 400 iters/slot, main at e170ae0:

    slots restores prep wsimg jailer sockwait loadsnap total sockwait share
    4 1,625 0.22 0.05 0.39 24.92 2.14 27.73 89.9%
    8 3,249 0.23 0.06 0.41 24.11 2.25 27.06 89.1%
    16 6,497 0.25 0.10 0.48 54.23 2.45 57.52 94.3%
    32 12,993 0.26 0.28 0.52 331.98 2.67 335.71 98.9%
    64 25,962 0.24 0.30 0.50 1722.55 3.04 1726.62 99.8%

    (ms, means.) Every other sub-phase is flat across a 16x range of concurrency. Real work in a
    restore is 2.80 ms.

    The 20 ms poll is a hard floor, not a tail effect

    slots=4   n=1625   min=20.06ms   p50=20.57ms   max=42.19ms
              strictly below one 20ms quantum: 0 (0.00%)
              [20-40ms)=1270 (78.2%)    [40-60ms)=355 (21.8%)
    

    Zero of 1,625 restores finished inside one quantum, over ~2.8 ms of real work. The 78/22 split
    across the first two quanta reproduces the distribution recorded on this issue earlier.

    Why it is now a throughput issue and not a latency one

    The sweep found throughput peaks at 16 slots / 170.92 Exec/s and then regresses — 120.49 at
    32 slots, 59.69 at 64, with the host never above 18.1% CPU. What binds is the rate at which
    the pool turns out a fresh warm standby. The cold-acquire cause is overwhelmingly exhausted
    (replenishment behind demand): 19,577 of 25,856 Execs at 64 slots. Acquire therefore grows
    4.18 -> 940.18 ms while Resume and Run stay flat.

    Replenishment is the restore path, and 90% of a restore is this sleep. So the sleep sets the
    ceiling.

    It also fails outright above the knee

    At 64 slots, 396 replenishments hit this function's 5 s overall timeout and failed:

    vmpool: replenish "...-slot40": firecracker: restore vm-25607: API socket never appeared:
    timed out after 5s waiting for /srv/jail/firecracker/vm-25607/root/run/firecracker.socket
    

    Those surfaced as 23 spawn-failure Execs. So Step 4's "the 5 s overall timeout and ctx
    cancellation still hold" is not only a regression guard — that timeout is being hit in practice,
    and a fix needs to say whether 5 s is still the right ceiling once the poll is fine-grained.

    One thing the data cannot settle

    At 4 slots the socket clearly appears well inside 20 ms and the caller sleeps a full quantum, so
    the win there is unambiguous: ~27.7 ms -> ~5 ms per restore, ~5x on replenishment rate. At 64 slots
    sockwait is not quantized (p50 1540 ms), so the socket really is slow under load. Whether that
    is caused by the slow cycle — slow restores mean more restores in flight, which means more
    contention, a feedback loop — or by an independent bottleneck cannot be told apart from this data.

    So this issue is both the fix and the experiment. Re-running the same five-point sweep after it
    distinguishes the two: if the knee moves past 16, the feedback loop was real; if each restore gets
    cheap but the knee stays, something else binds and that becomes the next question.

    Suggested sequencing change: promote ahead of #258. #258's Destroy is still 52.3% of an Exec at
    the knee and worth ~1.3x on per-Exec latency, but it is not what stops throughput scaling.

  3. pdettori commented on Sep 21, 2026

    @pdettori
    MemberAuthor

    Note on my comment above: the clean-box numbers stand, the "above the knee" framing does not

    My previous comment argued two things. The first survives; the second was confounded and I am
    withdrawing it. Detail in #305.

    Stands — the clean-box restore decomposition. That came from the first run of the sweep, on
    a box with zero accumulated state:

    • restore total 27.73 ms, sockwait 24.92 ms = 89.9%, over 2.80 ms of real work;
    • 0 of 1,625 restores finished inside one 20 ms quantum (min 20.06 ms; 78.2% in [20,40),
      21.8% in [40,60)).

    That is a quantization proof of the fixed sleep, on an idle box, and no confound touches it. The
    expected win — a restore from ~27.7 ms to ~5 ms, so ~5x on replenishment rate — is unchanged.

    Withdrawn — the escalating sockwait column and the "binding constraint above the knee"
    argument.
    Those rows (54 ms at 16 slots, 332 at 32, 1722 at 64) are contaminated: every Exec
    leaks a per-VM cgroup directory (#255), the sweep ran in ascending order without removing them,
    so stale-cgroup count grew in lockstep with slot count. A same-configuration control with 41,000
    stale directories ran at 25.84 Exec/s with sockwait 1008 ms; with the directories removed and
    nothing else changed, 136.51 Exec/s and 98 ms. So most of that column is cgroup-creation cost,
    not concurrency.

    The 396 restore timeouts at 64 slots are likewise attributable to that, not to load as such —
    though it does make the point that this function's 5 s ceiling is reachable in practice, so a
    fix should still say whether 5 s remains right once the poll is fine-grained.

    Sequencing. I previously argued this should jump ahead of #258. I still think it should, but
    the reason is now the clean-box decomposition (90% of a restore is a sleep over 2.8 ms of work)
    rather than any claim about where throughput stops scaling. And #255 should go first — while
    that leak is present, no throughput measurement on a long-lived worker means anything.

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