mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
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]>
156 lines
4.5 KiB
Go
156 lines
4.5 KiB
Go
package tools
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"github.com/devvmhq/devvm/internal/backend"
|
|
"github.com/devvmhq/devvm/internal/image"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
)
|
|
|
|
// Side by side, each build VM gets toolCPUs and toolMemoryGiB. The host
|
|
// keeps reserveGiB of its memory and reserveCPUs for itself, and the
|
|
// memory devvm's running VMs have for them. A tool's build is mostly its
|
|
// download and packing its image, which need neither much memory nor
|
|
// many CPUs.
|
|
const (
|
|
toolCPUs = 2
|
|
toolMemoryGiB = 2
|
|
reserveGiB = 6
|
|
reserveCPUs = 2
|
|
)
|
|
|
|
// slots is how many of n tool images build side by side, and each build
|
|
// VM's size: as many as the host's memory, less running (what devvm's
|
|
// running VMs have, in bytes), and its CPUs give a build VM of
|
|
// toolMemoryGiB and toolCPUs each beyond what it keeps, at most
|
|
// backend.MaxToolSlots. One at a time gets base's size, as builds had
|
|
// before they ran side by side.
|
|
func (t Share) slots(n int, running uint64) (int, backend.Size) {
|
|
if t.Memory == nil || t.CPUs == nil || n < 2 {
|
|
return 1, backend.Size{}
|
|
}
|
|
mem, err := t.Memory()
|
|
if err != nil || mem < running {
|
|
return 1, backend.Size{}
|
|
}
|
|
most := backend.MaxToolSlots
|
|
if t.MaxSlots > 0 {
|
|
most = min(most, t.MaxSlots)
|
|
}
|
|
byMemory := (int((mem-running)>>30) - reserveGiB) / toolMemoryGiB
|
|
byCPUs := (t.CPUs() - reserveCPUs) / toolCPUs
|
|
if s := min(n, most, byMemory, byCPUs); s > 1 {
|
|
return s, backend.Size{CPUs: toolCPUs, MemoryGiB: toolMemoryGiB}
|
|
}
|
|
return 1, backend.Size{}
|
|
}
|
|
|
|
// plan is how many of the n tool images label names build side by side
|
|
// in b, and each build VM's size (slots), as many as b's disks have room
|
|
// for, each build VM counted, and at least the one the build needs:
|
|
// disk_low when b's disks lack even that. A backend that can't say what
|
|
// its running VMs have builds one at a time.
|
|
func (t Share) plan(ctx context.Context, b backend.Backend, label string, n int) (int, backend.Size, error) {
|
|
slots, size := 1, backend.Size{}
|
|
if n > 1 && t.Memory != nil {
|
|
stop := t.Status(msg.ReadingVMs())
|
|
running, ok := runningMemory(ctx, b)
|
|
stop()
|
|
if ok {
|
|
slots, size = t.slots(n, running)
|
|
}
|
|
}
|
|
space := backend.Space{Free: t.FreeSpace}
|
|
need := func(s int) func(backend.Floors) uint64 {
|
|
return func(f backend.Floors) uint64 { return f.ForTools(s, n) }
|
|
}
|
|
for ; slots > 1; slots-- {
|
|
if space.Check(b, label, need(slots)) == nil {
|
|
return slots, size, nil
|
|
}
|
|
}
|
|
return 1, backend.Size{}, space.Check(b, label, need(1))
|
|
}
|
|
|
|
// runningMemory is the memory b's running VMs have, in bytes; false when
|
|
// b can't say.
|
|
func runningMemory(ctx context.Context, b backend.Backend) (uint64, bool) {
|
|
ins, err := b.List(ctx)
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
var n uint64
|
|
for _, in := range ins {
|
|
if in.Status == backend.StatusRunning && in.Size.MemoryGiB > 0 {
|
|
n += uint64(in.Size.MemoryGiB * (1 << 30)) // a VM of 7.5 GiB has all of it
|
|
}
|
|
}
|
|
return n, true
|
|
}
|
|
|
|
// buildAll builds these tool images in n slots side by side, of size,
|
|
// each slot's builds one after another in its own build VM. Side by side,
|
|
// one status line covers them, and each build's lines in the run log are
|
|
// named for its tool (msg.WithLogTag). The first failure stops the
|
|
// others, which are cancelled, and is what it returns, named for its
|
|
// tool's lines, so the end of the log shown is its own.
|
|
func (t Share) buildAll(ctx context.Context, b backend.Backend, base string, tg Target, lock Lock, files []string, n int, size backend.Size) error {
|
|
if n == 1 {
|
|
for _, f := range files {
|
|
if err := t.buildOne(ctx, b, base, tg, lock, f, slot{n: 1}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
defer t.Status(msg.BuildingToolsTogether(Label(files)))()
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
defer cancel()
|
|
var (
|
|
mu sync.Mutex
|
|
first error
|
|
wg sync.WaitGroup
|
|
)
|
|
todo := make(chan string)
|
|
for s := range n {
|
|
wg.Go(func() {
|
|
for f := range todo {
|
|
tool, _, _ := image.ParseToolFile(f)
|
|
err := t.buildOne(msg.WithLogTag(ctx, tool), b, base, tg, lock, f, slot{s, n, size})
|
|
if err == nil {
|
|
continue
|
|
}
|
|
mu.Lock()
|
|
if first == nil {
|
|
first = backend.LoggedIn(err, tool)
|
|
}
|
|
mu.Unlock()
|
|
cancel()
|
|
}
|
|
})
|
|
}
|
|
feed:
|
|
for _, f := range files {
|
|
select {
|
|
case todo <- f:
|
|
case <-ctx.Done():
|
|
break feed
|
|
}
|
|
}
|
|
close(todo)
|
|
wg.Wait()
|
|
if first == nil {
|
|
return ctx.Err()
|
|
}
|
|
return first
|
|
}
|
|
|
|
// slot is where a tool image builds: the slot of n side by side (buildAll)
|
|
// whose build VM it has, of size.
|
|
type slot struct {
|
|
at, n int
|
|
size backend.Size
|
|
}
|