Files
w3cjandClaude Opus 5.5 dd46dd7a0f feat(tools): tool images build side by side
A first run built its tool images one after another, each paying a
build VM's boot (5.7 s on vz, 9.3 s on QEMU) before a download of a few
seconds to a minute. Share.Build now hands the images out to up to four
slots (buildAll), each slot's builds one after another in a build VM of
its own:

- slots: as many as the host's memory, less what the backend's running
  VMs have, and its CPUs give a build VM of 2 GiB and 2 CPUs each after
  keeping 6 GiB and 2 CPUs, at most backend.MaxToolSlots (4) and the
  number of images; one alone keeps base's size. A backend that can't
  list its VMs builds one at a time. A running VM's memory counts whole
  (7.5 GiB, not 7): TestBuildSlotsFit's running VMs fail when it isn't
  counted or is truncated.
- plan lowers the slots until the backend's disks have room:
  Floors.ForTools counts Tools once for each build VM at once, as on a
  disk without copy on write each is a whole copy of base.
- backend.ToolsSpec gets Slot, Slots, Size and Hold; Lima's slot VMs are
  the constants ToolsVM to ToolsVM4, sized with --cpus/--memory, and the
  first slot's build deletes what a killed run left in the slots this
  run doesn't use. Builds hold the share's lock, so one devvm uses the
  slots.
- A failed build's VM goes in the run's cleanup, which runs after the
  build let go of the share's lock: it deletes the VM only once it has
  the lock again (ToolsSpec.Hold, Env.holdShare's TryLockFile), and
  leaves it while another devvm holds it (a build, gc or the daily
  refresh) (TestBuildToolsCleanupHolds, TestBuildHoldsShareForCleanup).
  The next tools build deletes what it finds in the slots, and so does
  devvm gc, which holds the share's lock: backend.Images'
  DeleteToolsLeft, Lima's deleting the slots' build VMs that are there
  and no other VM, which gc asks of every backend first, without
  asking, a backend that can't being a run log line. It returns what
  it deleted, which gc names in one line (tools_build_vms_deleted), and
  gc then never says nothing_to_clean or nothing_deleted
  (gc_tools_build.txtar, TestDeleteToolsLeft in lima and vm, the fake's
  ToolsLeft). The cli skill's judgment.md names this exception to "gc
  --force deletes exactly what gc lists". The conformance suite's
  delete-tools-left checks that it deletes no other VM, not even the
  project VM of a folder named tools (devvm-tools-<hash>), and no image
  (TestConformanceFinds: deletesAllVMs, deletesImage, deletesByPrefix).
  The share's lock
  is the data folder's, so devvms with their own data folders and one
  Lima home don't exclude each other's builds: the docs say not to build
  tools or run gc in two at once (a lock per build VM in the Lima home
  would close it, but it would replace Hold and needs e2e).
- Side by side, one status line covers the builds
  (building_tools_together), and their commands' lines in the run log
  are tagged with their tool (msg.WithLogTag): the UI's LoggedCmd
  prefixes them and shows no status line of its own. Every write starts
  with the tag, as does every line in it, one ended by "\r" included,
  so no build can pass a line off as another's (TestTaggedWrites), and
  a tool's lines are found after another's "\r" (taggedLines splits
  there, a progress bar's updates staying one line: TestTaggedLines). The
  first failure cancels the others and is what Build returns, tagged
  (backend.LoggedIn), so ErrorLogged shows that tool's lines' end.
- A test build's DEVVM_TOOL_SLOTS caps the slots; the CLI tests set 1,
  so the stub's calls keep their order, and tools_side_by_side.txtar
  sets 3, with an interrupt that leaves no build VM. The stub keeps each
  build VM's folder by its name, and the fake backend's BuildTools runs
  its builds outside its lock, as a real backend's would, passing
  ToolsBuild its context.

Docs: images.md's Building tools and Free space, ports-size.md,
space.md, images.md (using) and gc's help (the build VMs gc deletes),
settings.md (one Lima home for two data folders), extending.md,
testing.md and tests.md; the cli skill's judgment.md; the CHANGELOG's
first highlight and the release page's A faster first run.

New messages: building_tools_together, tools_build_vms_deleted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 22:57:15 -07:00

120 lines
4.0 KiB
Go

package backend
import (
"errors"
"math"
"strconv"
"github.com/devvmhq/devvm/internal/msg"
)
// Floors is the free space, in bytes, a backend wants on its disks before
// each step that writes them, with a margin over what the step was
// measured to take. Each backend's are its own, measured with statfs
// before, during and after each step (not du, which counts a clone's
// shared blocks again).
type Floors struct {
Base uint64 // building base
Image uint64 // building any other image
VM uint64 // making a VM, with its first launch
// Tools and PerTool are what a shared tools build wants: Tools for
// each build VM running at once, and PerTool more for each tool it
// builds.
Tools, PerTool uint64
}
// ForTools is what building n shared tool images wants, in slots build
// VMs at once (at least one): each a copy of base, which a disk without
// copy on write holds whole.
func (f Floors) ForTools(slots, n int) uint64 {
return count(max(slots, 1))*f.Tools + count(n)*f.PerTool
}
// count is n, a number of tools, as a uint64; none when negative.
func count(n int) uint64 {
if n < 0 {
return 0
}
return uint64(n)
}
// ToolImages is the free space building n shared tool images wants in
// devvm's data folder, where the images come out on every backend: the
// images alone, measured with statfs (the folder on its own tmpfs: 184 MB
// for claude, 194 MB for node, pnpm and bun). Ten tools came to 1.5 GiB as images on Lima, rust alone, the largest,
// to 0.6 GiB.
func ToolImages(n int) uint64 { return 512<<20 + count(n)*(256<<20) }
// Space checks the host's free space before a step that writes disks.
// Free returns the free space on the volume holding a path
// (platform.Host's); with Free nil nothing is checked.
type Space struct {
Free func(path string) (uint64, error)
}
// Check refuses when a volume h writes to has less free than need
// returns from h's floors (disk_low, for what: the image, tools or VM the
// step makes). A backend without DiskDirs, or a system devvm can't read
// free space on, isn't checked.
func (s Space) Check(h Host, what string, need func(Floors) uint64) error {
return s.check(h.DiskDirs(), what, need(h.Floors()))
}
// CheckDir refuses when the volume holding dir, a folder the step writes
// to itself, has less than need bytes free.
func (s Space) CheckDir(dir, what string, need uint64) error {
return s.check([]string{dir}, what, need)
}
func (s Space) check(dirs []string, what string, need uint64) error {
need = wholeGiB(need)
dir, free, err := s.lowest(dirs)
if err != nil || dir == "" || free >= need {
return err
}
return msg.DiskLow(dir, GiB(free), what, strconv.FormatUint(need>>30, 10))
}
// Warning is doctor's warning when a volume h writes to has less free
// than building base wants there; false when it has enough, or can't be
// read.
func (s Space) Warning(h Host) (msg.M, bool) {
need := wholeGiB(h.Floors().Base)
dir, free, err := s.lowest(h.DiskDirs())
if err != nil || dir == "" || free >= need {
return msg.M{}, false
}
return msg.DiskLowWarning(dir, GiB(free), strconv.FormatUint(need>>30, 10)), true
}
// lowest returns the folder, of dirs, on the volume with the least free
// space, and that space; "" when none is checked.
func (s Space) lowest(dirs []string) (string, uint64, error) {
if s.Free == nil {
return "", 0, nil
}
low, lowFree := "", uint64(math.MaxUint64)
for _, d := range dirs {
free, err := s.Free(d)
if errors.Is(err, errors.ErrUnsupported) {
return "", 0, nil
}
if err != nil {
return "", 0, msg.DiskFreeUnknown(d).Because(err)
}
if free < lowFree {
low, lowFree = d, free
}
}
return low, lowFree, nil
}
// wholeGiB rounds a size up to whole GiB, which the messages show.
func wholeGiB(n uint64) uint64 { return (n + 1<<30 - 1) >> 30 << 30 }
// GiB shows a size in bytes as GiB, to one decimal, rounded down: free
// space just short of a floor never shows as the floor.
func GiB(n uint64) string {
return strconv.FormatFloat(math.Floor(float64(n)/(1<<30)*10)/10, 'f', 1, 64)
}