Skip to content

SBX0 KVM host: set the read-ahead of the snapshot device, so cold starts read at the volume's speed #780

Closed

Description

@webchang

Problem

A template worker's cold start reads its snapshot's memory file (4 GiB for a 4 GiB guest) from
$SBX_SRV (/srv/sbx). install-host.sh leaves that device at the kernel's default read-ahead of
128 KiB. On a network block volume whose largest request is 128 KiB (max_hw_sectors_kb), that keeps
too few reads in flight to reach the volume's throughput.

Measured on one KVM host with a network block volume rated 750 MiB/s and 3,000 IOPS, page cache
dropped before each run:

What read_ahead_kb 128 512 1024
One reader of the memory files 227 MiB/s 730 MiB/s ~850 MiB/s
Four readers 435 MiB/s ~779 MiB/s
8 cold worker starts posted at once, 4 at a time all active at 100.7 s all active at 62.2 s
Their read rate ~420 MiB/s 736-742 MiB/s

The bytes read per start were the same, 4.0 GiB, at both settings. Every read stayed 128 KiB, at
about 5,900-6,900 reads/s at 512 and above (about 5,950 in the cold starts): what binds is reads in
flight, not IOPS. At 512 the starts run at the volume's
limit.

Proposal

install-host.sh sets the read-ahead of the block device holding $SBX_SRV on every worker install,
and keeps it set across reboots:

  • A new setting SBX_READ_AHEAD_KB (default 512). 0 leaves the device alone (and removes what an
    earlier install set up, writing the earlier value back). Anything other than a whole number from 0
    to 16384 is refused before the host is touched.
  • The device is found by path, never by a recorded name (a disk's kernel name can change across
    reboots): findmnt's MAJ:MIN for $SBX_SRV, then /sys/dev/block/MAJ:MIN. A disk, dm/LVM, md or
    loop device is set through its own queue/read_ahead_kb; a partition through its parent disk's.
  • A sysfs write does not survive a reboot, so the install adds a small script
    (/usr/local/libexec/sbx-read-ahead.sh, which does the lookup above at every run) and a oneshot
    sbx-read-ahead.service (RequiresMountsFor=$SBX_SRV, Before=sbx.slice,
    WantedBy=multi-user.target; the worker template gains After=sbx-read-ahead.service), enables it
    and runs the script once.
  • The device's value before the first install is kept in /etc/sbx/read-ahead.orig (a re-run keeps
    the first one). The last worker's --uninstall writes it back, then disables and removes the unit,
    the script and that file. All three are recorded in /etc/sbx/manifest under their own kind.
  • Never a failed install: a $SBX_SRV on no block device (tmpfs, overlay, btrfs's anonymous device), a
    device without a queue, a host without findmnt, or a value the device does not take (re-read after
    the write) is a warning; the old value stays. The boot run exits 0 in those cases too, with the
    warning in its journal.

What the PR will contain

One PR, about 315 added lines with tests.

  • deploy/sbx/sbx-read-ahead.sh (new): resolve PATH and set PATH KB, with SBX_SYSFS as a test
    seam.
  • deploy/sbx/install-host.sh: the setting and its check, the install after the shared units and
    before the worker starts, the restore and removal with the last worker (and with
    SBX_READ_AHEAD_KB=0).
  • deploy/sbx/[email protected]: After=sbx-read-ahead.service.
  • deploy/sbx/tests/install-host.test.sh (bash 5 and 3.2), with a mocked findmnt and a fake sysfs:
    a bad value is refused before systemd; the default 512 goes into the unit and onto a partition's
    parent disk; a dm device is set through its own queue; a re-run keeps the first recorded value; the
    last uninstall restores it and removes the unit; 0 installs no unit; an unresolvable path and a
    refused value warn and the install succeeds.
  • deploy/sbx/README.md (the setting) and a RUNBOOK section: why, the figures above with their
    conditions, how to check it, how to turn it off.

Depends on

Nothing.

How to verify

  • bash deploy/sbx/tests/install-host.test.sh
  • On a KVM host, after a worker install: systemctl is-enabled sbx-read-ahead.service, and
    cat /sys/block/<dev>/queue/read_ahead_kb for the device findmnt -no SOURCE --target /srv/sbx
    names (its disk, for a partition) reads 512, also after a reboot.
  • After the last --uninstall: the value is the one in effect before the first install, and
    sbx-read-ahead.service is gone.

Limits / out of scope

  • It is device-wide: everything else on that volume (another Moca tier, other sandboxes, the host's
    own files if $SBX_SRV is on the root disk) reads ahead as much.
  • 512 suits a throughput-bound network volume; local NVMe may want less. The setting is the knob; no
    value is chosen per device.
  • It changes the size of the reads, not the bytes a cold start reads.
  • A value set by hand on the device after the install is overwritten at the next boot, and the last
    uninstall restores the value recorded at the first install, not that hand-set one.

Assisted-By: Claude Code

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions