diff --git a/CHANGELOG.md b/CHANGELOG.md index 896844a..fd372db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,13 @@ has a matching GitHub release whose body comes from its section below. `window`, with its hours, whether they are open, and how long until they close or open. An engine shows before its window opens, and a `berth run --window` lease shows its own hours. The status JSON carries the same rows as `schedules`, and each lease carries its `window`. +- `berth contract ENGINE` prints one engine's terms in one place: its priority, the VRAM a lease asks for, the last + run's measured peak, the RAM it needs and holds parked, its minimum turn and window, and how long a switch to it + takes from each tier. Each number says whether berth.toml declares it, Berth's default applies, Berth measured + it, or nothing is known. ENGINE can be an `[[engine]]` name, a discovered engine, or a llama-swap model's + `berth run --as` name. Without berthd it prints the declared terms. The status JSON's `footprints` now carry each + engine's `hot_peak_vram_bytes`, `ram_peak_bytes` and `parked_ram_bytes`. A llama-swap model whose last-run + figure sits on its group's entry is now listed under its own name too. - `berth wire llama-swap` names each llama-swap group where llama-swap swaps two or more wired models itself. There llama-swap unloads the old model before Berth sees the request, so `min_turn`, the engine ranking and the juggle guard never apply between them. The JSON carries the groups as `swapping`. The llama-swap section of diff --git a/docs/interfaces.md b/docs/interfaces.md index f5f5dbe..625c4ea 100644 --- a/docs/interfaces.md +++ b/docs/interfaces.md @@ -2,7 +2,7 @@ - **CLI:** one command tree with one output contract. `berth status`, `berth events -f`, `berth run`, `berth hold`, `berth stop`, `berth cordon|uncordon`, `berth notify`, `berth claims`, `berth pull`, `berth verify`, `berth models`, `berth gateway`, - `berth wire llama-swap`, `berth engine check`, `berth config check`, `berth config show`, + `berth wire llama-swap`, `berth engine check`, `berth contract ENGINE`, `berth config check`, `berth config show`, `berth export`, `berth key`, `berth skill install|show`, `berth update`, `berth version`, and for the node itself `berth daemon`, `berth harbor`, `berth service install|uninstall|status`, `berth reclaim`, `berth resume`, `berth prefetch`, `berth storage`, `berth civitai search|versions` diff --git a/internal/broker/footprint.go b/internal/broker/footprint.go index 23e4153..355419d 100644 --- a/internal/broker/footprint.go +++ b/internal/broker/footprint.go @@ -132,15 +132,18 @@ func sizeHuman(n uint64) string { return fmt.Sprintf("%d MiB", n/gpu.MiB) } -// footprintStatus is the table as the status reply carries it: the last -// run's peak per engine, sorted by name. b.mu must be held. +// footprintStatus is the table as the status reply carries it: per engine +// key, the last run's peak and the peaks learn_size and RAM figures read, +// sorted by name. A llama-swap model's entry may carry only its own peaks; +// the last-run figure sits on its group's. b.mu must be held. func (b *Broker) footprintStatus() []proto.EngineFootprint { out := []proto.EngineFootprint{} for k, f := range b.footprints { - if f == nil || f.Last == 0 { + if f == nil || f.Last == 0 && f.HotPeak == 0 && f.RAMPeak == 0 && f.ParkedRAM == 0 { continue } - out = append(out, proto.EngineFootprint{Engine: k, Bytes: f.Last, Runs: f.Runs, At: f.At}) + out = append(out, proto.EngineFootprint{Engine: k, Bytes: f.Last, Runs: f.Runs, At: f.At, + HotPeakBytes: f.HotPeak, RAMPeakBytes: f.RAMPeak, ParkedRAMBytes: f.ParkedRAM}) } sort.Slice(out, func(i, j int) bool { return out[i].Engine < out[j].Engine }) return out diff --git a/internal/broker/footprint_test.go b/internal/broker/footprint_test.go index c9dcc3c..961760d 100644 --- a/internal/broker/footprint_test.go +++ b/internal/broker/footprint_test.go @@ -208,3 +208,29 @@ func TestFootprintFoldsAtTheFreeNotTheRelease(t *testing.T) { t.Fatalf("Footprint(llama) = %d, %v after the free; want %d, true", last, ok, 9*GiB) } } + +// A llama-swap model's own peak sits on its name's entry, its last run on the +// group's. The status reply carries both, so a reader of the model's name +// (berth contract, learn_size) finds the peak the broker sizes it from. +func TestFootprintStatusCarriesAModelsOwnPeak(t *testing.T) { + b, c, _ := setup(t, procs.Map{101: 100}) + l := must(t, acquire(b, Request{Engine: "dirk", Group: "llama-swap", Priority: Foreground})) + l.Attach(100) + c.set(gpu.Proc{PID: 101, Name: "llama-server", Used: 12 * GiB}) + b.Tick(context.Background()) + l.Release() + c.set() + b.Tick(context.Background()) + + got := map[string]proto.EngineFootprint{} + for _, f := range b.Status().Footprints { + f.At = time.Time{} + got[f.Engine] = f + } + if f := got["dirk"]; f != (proto.EngineFootprint{Engine: "dirk", HotPeakBytes: 12 * GiB}) { + t.Fatalf("dirk's entry = %+v, want its own 12 GiB peak", f) + } + if f := got["llama-swap"]; f.Bytes != 12*GiB || f.Runs != 1 { + t.Fatalf("llama-swap's entry = %+v, want the last run's 12 GiB", f) + } +} diff --git a/internal/cli/commands.go b/internal/cli/commands.go index a4f5def..338880c 100644 --- a/internal/cli/commands.go +++ b/internal/cli/commands.go @@ -52,6 +52,22 @@ materialised, and the environment that decides which file that is. }, }, }, + { + Name: "contract", + Summary: "one engine's terms: what berth.toml declares and what Berth measured", + Args: "ENGINE [--config PATH] [--costs PATH] [--socket P] [--format F]", + Help: `Print one engine's terms in one place: its priority, the VRAM a lease asks for, +the last run's measured peak, the RAM it needs and holds parked, its minimum +turn and window, and how long a switch to it takes from each tier. Every +number says where it came from: declared in berth.toml, Berth's default, +measured on the card or by the gateway, or unknown. + +ENGINE is an [[engine]] name, an engine berthd discovered, or a name Berth +measured leases under (a llama-swap model's berth run --as). Read only: it +changes nothing, and without berthd it prints the declared terms.`, + Formats: []Format{FormatHuman, FormatJSON, FormatYAML}, + Run: runContract, + }, { Name: "civitai", Summary: "search and browse CivitAI, for finding things to pull", diff --git a/internal/cli/contract.go b/internal/cli/contract.go new file mode 100644 index 0000000..1660b93 --- /dev/null +++ b/internal/cli/contract.go @@ -0,0 +1,181 @@ +// Copyright 2026 Trevin Tindall (T-Crypt) +// SPDX-License-Identifier: Apache-2.0 + +package cli + +import ( + "fmt" + "sort" + "strings" + "text/tabwriter" + "time" + + "github.com/T-Crypt/berth/internal/config" + "github.com/T-Crypt/berth/internal/contract" + "github.com/T-Crypt/berth/internal/proto" + "github.com/T-Crypt/berth/internal/server" + "github.com/T-Crypt/berth/internal/swapcost" +) + +// contractView is one engine's contract. +type contractView struct { + c contract.Contract + offline string // why berthd gave no status, "" when it did +} + +func (v contractView) Data() any { return v.c } + +func (v contractView) Human(c *Context, ok bool) error { + k := v.c + where := "not in berth.toml" + if k.InConfig { + where = "in berth.toml" + } + if k.Type != "" { + where = k.Type + ", " + where + } + c.Printf("%s (%s)\n", k.Engine, where) + if v.offline != "" { + c.Bullet("berthd unreachable (%s): measured numbers are unknown", v.offline) + } + tw := tabwriter.NewWriter(c.Out, 0, 4, 2, ' ', 0) + row := func(name, value string, src contract.Source, why string) { + fmt.Fprintf(tw, " %s\t%s\t%s\t%s\n", name, value, src, why) + } + row("priority", k.Priority.Value, k.Priority.Source, "") + row("vram", contractSize(k.VRAM, "whole card"), k.VRAM.Source, k.VRAM.Why) + row("last peak", contractSize(k.LastPeak, "-"), k.LastPeak.Source, k.LastPeak.Why) + row("ram", contractSize(k.RAM, "-"), k.RAM.Source, k.RAM.Why) + row("parked ram", contractSize(k.ParkedRAM, "-"), k.ParkedRAM.Source, k.ParkedRAM.Why) + minTurn := "off" + if k.MinTurn.Seconds > 0 { + minTurn = (time.Duration(k.MinTurn.Seconds) * time.Second).String() + } + row("min turn", minTurn, k.MinTurn.Source, "") + row("window", k.Window.Value, k.Window.Source, "") + for _, s := range k.Switch { + took, why := "-", s.Took.Why + switch s.Took.Source { + case contract.Measured: + took, why = fmt.Sprintf("%.1f s", s.Took.Seconds), fmt.Sprintf("median of %d", s.Samples) + case contract.Default: + took = "0 s" + } + row("switch "+string(s.Tier), took, s.Took.Source, why) + } + return tw.Flush() +} + +// contractSize is a contract size for people: MiB, or none when it carries no bytes. +func contractSize(b contract.Bytes, none string) string { + if b.Bytes == 0 { + return none + } + return server.MiB(b.Bytes) +} + +func runContract(c *Context) int { + fs := c.FlagSet() + sock := fs.String("socket", defaultSocket(), "berthd socket ($BERTH_SOCKET)") + cfgPath := fs.String("config", config.DefaultPath(), "berth.toml ($BERTH_CONFIG): the engine's declared terms") + costs := fs.String("costs", defaultStateFile("swapcost.json"), "the gateway's measured switch times") + if code, done := c.ParseInterspersed(fs); done { + return code + } + if len(c.Args) != 1 { + return c.fail(Systemf("berth contract takes one engine name"). + WithHint("for example: berth contract comfyui")) + } + name := c.Args[0] + + cfg, err := config.Load(*cfgPath) + if err != nil { + return c.fail(Systemf("%s: %v", *cfgPath, err).WithHint("run berth config check")) + } + var entry *config.Engine + for i := range cfg.Engine { + if cfg.Engine[i].Name == name { + entry = &cfg.Engine[i] + } + } + + st, offline := contractStatus(*sock) + if entry == nil && !knownTo(st, name) { + known := knownNames(cfg, st) + e := Failf("no engine named %q in berth.toml or berthd's status", name) + if len(known) > 0 { + e.WithHint("known: %s", strings.Join(known, ", ")) + } + return c.fail(e) + } + + book, err := swapcost.NewBook(*costs) + if err != nil { + book = nil + } + return c.Write(contractView{c: contract.Build(name, entry, st, book), offline: offline}, nil) +} + +// contractStatus is berthd's status, or nil and why when it did not answer. +func contractStatus(sock string) (*proto.Status, string) { + conn, raw, err := server.Dial(sock) + if err != nil { + return nil, err.Error() + } + defer raw.Close() + if err := conn.Send(proto.Msg{Op: proto.OpStatus}); err != nil { + return nil, err.Error() + } + m, err := conn.Recv() + if err != nil { + return nil, err.Error() + } + if m.Status == nil { + return nil, "empty reply" + } + return m.Status, "" +} + +// knownTo says whether berthd's status names the engine: discovered, or +// measured under its own name (a llama-swap model). +func knownTo(st *proto.Status, name string) bool { + for _, n := range statusNames(st) { + if n == name { + return true + } + } + return false +} + +func statusNames(st *proto.Status) []string { + if st == nil { + return nil + } + var out []string + for _, e := range st.Engines { + out = append(out, e.Name) + } + for _, f := range st.Footprints { + out = append(out, f.Engine) + } + return out +} + +func knownNames(cfg config.Config, st *proto.Status) []string { + seen := map[string]bool{} + var out []string + for _, e := range cfg.Engine { + if !seen[e.Name] { + seen[e.Name] = true + out = append(out, e.Name) + } + } + for _, n := range statusNames(st) { + if !seen[n] { + seen[n] = true + out = append(out, n) + } + } + sort.Strings(out) + return out +} diff --git a/internal/cli/contract_test.go b/internal/cli/contract_test.go new file mode 100644 index 0000000..29aef29 --- /dev/null +++ b/internal/cli/contract_test.go @@ -0,0 +1,61 @@ +// Copyright 2026 Trevin Tindall (T-Crypt) +// SPDX-License-Identifier: Apache-2.0 + +package cli + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" +) + +// Without berthd, berth contract still prints what berth.toml declares and +// marks every measured number unknown; a name nobody knows is refused with +// the names that exist. +func TestContractWithoutBerthd(t *testing.T) { + home := isolate(t) + cfg := filepath.Join(home, "berth.toml") + if err := os.WriteFile(cfg, []byte("[[engine]]\nname = \"qwen\"\ntype = \"llamacpp\"\nurl = \"http://127.0.0.1:8080\"\npriority = \"foreground\"\nneeds_mib = 14000\nmin_turn = \"5m\"\n"), 0o600); err != nil { + t.Fatal(err) + } + + code, out, errOut := runCLI(t, "contract", "qwen", "--config", cfg, "--format", "json") + if code != 0 { + t.Fatalf("exit %d: %s", code, errOut) + } + var got struct { + InConfig bool `json:"in_config"` + VRAM struct { + Bytes uint64 `json:"bytes"` + Source string `json:"source"` + } `json:"vram"` + LastPeak struct { + Source string `json:"source"` + Why string `json:"why"` + } `json:"last_peak"` + MinTurn struct { + Seconds float64 `json:"seconds"` + Source string `json:"source"` + } `json:"min_turn"` + } + if err := json.Unmarshal([]byte(out), &got); err != nil { + t.Fatalf("%v: %s", err, out) + } + if !got.InConfig || got.VRAM.Bytes != 14000<<20 || got.VRAM.Source != "declared" || + got.LastPeak.Source != "unknown" || got.LastPeak.Why != "berthd did not answer" || + got.MinTurn.Seconds != 300 || got.MinTurn.Source != "declared" { + t.Fatalf("contract = %+v", got) + } + + code, out, _ = runCLI(t, "contract", "qwen", "--config", cfg) + if code != 0 || !strings.Contains(out, "berthd unreachable") || !strings.Contains(out, "14000 MiB") { + t.Fatalf("human (exit %d):\n%s", code, out) + } + + code, _, errOut = runCLI(t, "contract", "nope", "--config", cfg) + if code == 0 || !strings.Contains(errOut, `no engine named "nope"`) || !strings.Contains(errOut, "known: qwen") { + t.Fatalf("unknown name (exit %d): %s", code, errOut) + } +} diff --git a/internal/cli/status.go b/internal/cli/status.go index 1b62235..30c5b95 100644 --- a/internal/cli/status.go +++ b/internal/cli/status.go @@ -183,7 +183,9 @@ func (v statusView) Human(c *Context, ok bool) error { // of what a run needs; it is never a reservation. est := map[string]uint64{} for _, f := range st.Footprints { - est[f.Engine] = f.Bytes + if f.Bytes > 0 { + est[f.Engine] = f.Bytes + } } fmt.Fprint(tw, "\nENGINE\tTYPE\tUP\tMODELS\tDECODE\tPREFILL\tOUT TOKENS\tQUEUE\tEST. VRAM\n") for _, e := range st.Engines { diff --git a/internal/contract/contract.go b/internal/contract/contract.go new file mode 100644 index 0000000..e74ce8f --- /dev/null +++ b/internal/contract/contract.go @@ -0,0 +1,209 @@ +// Copyright 2026 Trevin Tindall (T-Crypt) +// SPDX-License-Identifier: Apache-2.0 + +// Package contract is one engine's terms as a single view: what berth.toml +// declares for it, joined with what Berth measured, every number tagged with +// where it came from. It is read only and keeps no file of its own, so it +// cannot drift from the config it reads. +package contract + +import ( + "fmt" + "time" + + "github.com/T-Crypt/berth/internal/config" + "github.com/T-Crypt/berth/internal/proto" + "github.com/T-Crypt/berth/internal/swapcost" +) + +// Source is where a number came from. +type Source string + +const ( + Declared Source = "declared" // berth.toml says so + Default Source = "default" // berth.toml is silent; Berth's default applies + Measured Source = "measured" // Berth saw it on the card or in a switch + Unknown Source = "unknown" // nothing declares it and nothing measured it +) + +// besideHeadroom is the broker's margin on a learned size (broker.besideHeadroom). +const besideHeadroom = 512 << 20 + +// Bytes is a size and its source. Bytes is 0 when the source is unknown, or +// when the size is the whole card. +type Bytes struct { + Bytes uint64 `json:"bytes,omitempty" yaml:"bytes,omitempty"` + Source Source `json:"source" yaml:"source"` + Why string `json:"why,omitempty" yaml:"why,omitempty"` +} + +// Duration is a time and its source. +type Duration struct { + Seconds float64 `json:"seconds" yaml:"seconds"` + Source Source `json:"source" yaml:"source"` + Why string `json:"why,omitempty" yaml:"why,omitempty"` +} + +// Text is a setting and its source. +type Text struct { + Value string `json:"value" yaml:"value"` + Source Source `json:"source" yaml:"source"` +} + +// Switch is how long a switch to the engine takes from one tier: the median of +// the gateway's measured switches, or unknown with no samples. +type Switch struct { + Tier swapcost.Tier `json:"tier" yaml:"tier"` + Took Duration `json:"took" yaml:"took"` + Samples int `json:"samples,omitempty" yaml:"samples,omitempty"` +} + +// Contract is one engine's terms. +type Contract struct { + Engine string `json:"engine" yaml:"engine"` + // Type is the engine type from berth.toml or discovery, "" for a name + // Berth knows only from its leases (a llama-swap model). + Type string `json:"type,omitempty" yaml:"type,omitempty"` + InConfig bool `json:"in_config" yaml:"in_config"` + Priority Text `json:"priority" yaml:"priority"` + // VRAM is what a lease asks for: needs_mib, the learned size, or the + // whole card. + VRAM Bytes `json:"vram" yaml:"vram"` + // LastPeak is the most VRAM the last run held, what a next run most + // likely needs. A reading, never a reservation. + LastPeak Bytes `json:"last_peak" yaml:"last_peak"` + // RAM is the host RAM a run needs: needs_ram_mib, else the most a run + // held while active. + RAM Bytes `json:"ram" yaml:"ram"` + // ParkedRAM is the host RAM the engine last held parked. + ParkedRAM Bytes `json:"parked_ram" yaml:"parked_ram"` + MinTurn Duration `json:"min_turn" yaml:"min_turn"` + Window Text `json:"window" yaml:"window"` + Switch []Switch `json:"switch" yaml:"switch"` +} + +// Tiers is the switch tiers in the order a contract lists them, fastest first. +var Tiers = []swapcost.Tier{swapcost.Hot, swapcost.Warm, swapcost.Cool, swapcost.Cold} + +// Build joins an engine's berth.toml entry (nil when it has none) with what +// st measured for it and the switch book (nil when there is none). st is nil +// when berthd did not answer; every measured number is then unknown. +func Build(name string, e *config.Engine, st *proto.Status, book *swapcost.Book) Contract { + c := Contract{Engine: name, InConfig: e != nil} + var fp *proto.EngineFootprint + var card uint64 + if st != nil { + for i := range st.Footprints { + if st.Footprints[i].Engine == name { + fp = &st.Footprints[i] + } + } + for _, en := range st.Engines { + if en.Name == name { + c.Type = en.Type + } + } + for _, g := range st.GPUs { + if g.Scheduled { + card = g.Total - min(g.Total, g.Baseline) + break + } + } + } + if e != nil && e.Type != "" { + c.Type = e.Type + } + + c.Priority = Text{Value: "interactive", Source: Default} + if e != nil && e.Priority != "" { + c.Priority = Text{Value: e.Priority, Source: Declared} + } + + c.VRAM = vram(e, fp, card, st != nil) + + c.LastPeak = measured(fp, func(f *proto.EngineFootprint) uint64 { return f.Bytes }, st != nil) + if fp != nil && fp.Bytes > 0 { + c.LastPeak.Why = fmt.Sprintf("%d runs measured", fp.Runs) + } + + c.RAM = measured(fp, func(f *proto.EngineFootprint) uint64 { return f.RAMPeakBytes }, st != nil) + if c.RAM.Source == Measured { + c.RAM.Why = "the most a run held while active" + } + if e != nil && e.NeedsRAMMiB > 0 { + c.RAM = Bytes{Bytes: e.NeedsRAMMiB << 20, Source: Declared, Why: "needs_ram_mib"} + } + c.ParkedRAM = measured(fp, func(f *proto.EngineFootprint) uint64 { return f.ParkedRAMBytes }, st != nil) + + c.MinTurn = Duration{Source: Default, Why: "off"} + if e != nil && e.MinTurn.Duration > 0 { + c.MinTurn = Duration{Seconds: e.MinTurn.Seconds(), Source: Declared} + } + c.Window = Text{Value: "always", Source: Default} + if e != nil && e.Window.Set() { + c.Window = Text{Value: e.Window.String(), Source: Declared} + } + + for _, t := range Tiers { + s := Switch{Tier: t, Took: Duration{Source: Unknown, Why: "no switch measured"}} + if book != nil { + if est := book.Estimate(name, t); est.Known && est.Samples == 0 { + s.Took = Duration{Source: Default, Why: "already on the card, no switch"} + } else if est.Known { + s.Took = Duration{Seconds: est.Took.Round(100 * time.Millisecond).Seconds(), Source: Measured} + s.Samples = est.Samples + } + } + c.Switch = append(c.Switch, s) + } + return c +} + +// vram is the size a lease of the engine asks for, the way the broker sizes +// it: needs_mib as declared, else the learned size under learn_size, else +// the whole card. +func vram(e *config.Engine, fp *proto.EngineFootprint, card uint64, read bool) Bytes { + if e != nil { + if b := e.NeedsMiB.Bytes(); len(b) > 0 { + why := "needs_mib" + if len(b) > 1 { + why = fmt.Sprintf("needs_mib, the largest of %d cards", len(b)) + } + return Bytes{Bytes: largest(b), Source: Declared, Why: why} + } + if e.LearnSize { + switch { + case !read: + return Bytes{Source: Unknown, Why: "learn_size, but berthd did not answer"} + case fp == nil || fp.HotPeakBytes == 0: + return Bytes{Source: Default, Why: "learn_size, not measured yet, so the whole card"} + case card > 0 && fp.HotPeakBytes+besideHeadroom >= card: + return Bytes{Source: Measured, Why: "learn_size, its measured peak fills the card, so the whole card"} + default: + return Bytes{Bytes: fp.HotPeakBytes + besideHeadroom, Source: Measured, + Why: fmt.Sprintf("learn_size: measured peak %d MiB + 512 MiB", fp.HotPeakBytes>>20)} + } + } + } + return Bytes{Source: Default, Why: "the whole card"} +} + +// measured is one footprint figure, unknown when berthd did not answer or +// never measured it. +func measured(fp *proto.EngineFootprint, get func(*proto.EngineFootprint) uint64, read bool) Bytes { + if !read { + return Bytes{Source: Unknown, Why: "berthd did not answer"} + } + if fp == nil || get(fp) == 0 { + return Bytes{Source: Unknown, Why: "not measured yet"} + } + return Bytes{Bytes: get(fp), Source: Measured} +} + +func largest(b []uint64) uint64 { + var m uint64 + for _, v := range b { + m = max(m, v) + } + return m +} diff --git a/internal/contract/contract_test.go b/internal/contract/contract_test.go new file mode 100644 index 0000000..3b4d3a9 --- /dev/null +++ b/internal/contract/contract_test.go @@ -0,0 +1,119 @@ +// Copyright 2026 Trevin Tindall (T-Crypt) +// SPDX-License-Identifier: Apache-2.0 + +package contract + +import ( + "path/filepath" + "testing" + "time" + + "github.com/T-Crypt/berth/internal/config" + "github.com/T-Crypt/berth/internal/proto" + "github.com/T-Crypt/berth/internal/swapcost" +) + +const mib = 1 << 20 + +func card(fps ...proto.EngineFootprint) *proto.Status { + return &proto.Status{ + GPUs: []proto.GPU{{Scheduled: true, Total: 24564 * mib, Baseline: 10 * mib}}, + Footprints: fps, + } +} + +// The VRAM a lease asks for comes out the way the broker sizes it, and says +// where it came from. +func TestBuildVRAM(t *testing.T) { + cases := []struct { + name string + e *config.Engine + st *proto.Status + want Bytes + }{ + {"needs_mib", &config.Engine{NeedsMiB: config.MiBs{14000}}, card(), + Bytes{Bytes: 14000 * mib, Source: Declared, Why: "needs_mib"}}, + {"needs_mib per card", &config.Engine{NeedsMiB: config.MiBs{9000, 12000}}, card(), + Bytes{Bytes: 12000 * mib, Source: Declared, Why: "needs_mib, the largest of 2 cards"}}, + {"learned", &config.Engine{LearnSize: true}, card(proto.EngineFootprint{Engine: "e", HotPeakBytes: 10000 * mib}), + Bytes{Bytes: 10512 * mib, Source: Measured, Why: "learn_size: measured peak 10000 MiB + 512 MiB"}}, + {"learned peak fills the card", &config.Engine{LearnSize: true}, card(proto.EngineFootprint{Engine: "e", HotPeakBytes: 24100 * mib}), + Bytes{Source: Measured, Why: "learn_size, its measured peak fills the card, so the whole card"}}, + {"learn_size never measured", &config.Engine{LearnSize: true}, card(), + Bytes{Source: Default, Why: "learn_size, not measured yet, so the whole card"}}, + {"learn_size without berthd", &config.Engine{LearnSize: true}, nil, + Bytes{Source: Unknown, Why: "learn_size, but berthd did not answer"}}, + {"nothing declared", &config.Engine{}, card(proto.EngineFootprint{Engine: "e", HotPeakBytes: 10000 * mib}), + Bytes{Source: Default, Why: "the whole card"}}, + {"not in berth.toml", nil, card(), + Bytes{Source: Default, Why: "the whole card"}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := Build("e", tc.e, tc.st, nil).VRAM; got != tc.want { + t.Fatalf("VRAM = %+v, want %+v", got, tc.want) + } + }) + } +} + +// Measured figures come from the footprint, declared ones win where +// berth.toml names them, and without berthd every measured figure is unknown. +func TestBuildMeasuredAndDeclared(t *testing.T) { + fp := proto.EngineFootprint{Engine: "e", Bytes: 13000 * mib, Runs: 4, RAMPeakBytes: 9000 * mib, ParkedRAMBytes: 20000 * mib} + e := &config.Engine{Type: "llamacpp", Priority: "foreground", MinTurn: config.Duration{Duration: 5 * time.Minute}} + + c := Build("e", e, card(fp), nil) + if c.Type != "llamacpp" || !c.InConfig || c.Priority != (Text{"foreground", Declared}) { + t.Fatalf("type %q in_config %v priority %+v", c.Type, c.InConfig, c.Priority) + } + if c.LastPeak != (Bytes{Bytes: 13000 * mib, Source: Measured, Why: "4 runs measured"}) { + t.Fatalf("LastPeak = %+v", c.LastPeak) + } + if c.RAM != (Bytes{Bytes: 9000 * mib, Source: Measured, Why: "the most a run held while active"}) { + t.Fatalf("RAM = %+v", c.RAM) + } + if c.ParkedRAM != (Bytes{Bytes: 20000 * mib, Source: Measured}) { + t.Fatalf("ParkedRAM = %+v", c.ParkedRAM) + } + if c.MinTurn != (Duration{Seconds: 300, Source: Declared}) || c.Window != (Text{"always", Default}) { + t.Fatalf("MinTurn %+v Window %+v", c.MinTurn, c.Window) + } + + e.NeedsRAMMiB = 16000 + if got := Build("e", e, card(fp), nil).RAM; got != (Bytes{Bytes: 16000 * mib, Source: Declared, Why: "needs_ram_mib"}) { + t.Fatalf("declared RAM = %+v", got) + } + + off := Build("e", nil, nil, nil) + if off.LastPeak != (Bytes{Source: Unknown, Why: "berthd did not answer"}) || off.Priority != (Text{"interactive", Default}) { + t.Fatalf("without berthd: LastPeak %+v Priority %+v", off.LastPeak, off.Priority) + } +} + +// A switch time is the book's median for the tier, hot is free by rule, and +// a tier with no samples is unknown. +func TestBuildSwitch(t *testing.T) { + book, err := swapcost.NewBook(filepath.Join(t.TempDir(), "swapcost.json")) + if err != nil { + t.Fatal(err) + } + for _, s := range []time.Duration{6 * time.Second, 8 * time.Second, 7 * time.Second} { + book.Record(swapcost.Sample{Engine: "e", Tier: swapcost.Cold, Took: s, At: time.Now()}) + } + want := []Switch{ + {Tier: swapcost.Hot, Took: Duration{Source: Default, Why: "already on the card, no switch"}}, + {Tier: swapcost.Warm, Took: Duration{Source: Unknown, Why: "no switch measured"}}, + {Tier: swapcost.Cool, Took: Duration{Source: Unknown, Why: "no switch measured"}}, + {Tier: swapcost.Cold, Took: Duration{Seconds: 7, Source: Measured}, Samples: 3}, + } + got := Build("e", nil, nil, book).Switch + if len(got) != len(want) { + t.Fatalf("switch = %+v", got) + } + for i := range want { + if got[i] != want[i] { + t.Errorf("switch[%d] = %+v, want %+v", i, got[i], want[i]) + } + } +} diff --git a/internal/proto/proto.go b/internal/proto/proto.go index 178c882..eb21cb9 100644 --- a/internal/proto/proto.go +++ b/internal/proto/proto.go @@ -414,6 +414,13 @@ type EngineFootprint struct { Bytes uint64 `json:"estimated_vram_bytes"` Runs int `json:"runs"` At time.Time `json:"at"` + // HotPeakBytes is the most VRAM a run under this exact name held, the + // figure learn_size sizes a lease from. + HotPeakBytes uint64 `json:"hot_peak_vram_bytes,omitempty"` + // RAMPeakBytes is the most host RAM a run held while active. + RAMPeakBytes uint64 `json:"ram_peak_bytes,omitempty"` + // ParkedRAMBytes is the host RAM the engine last held parked. + ParkedRAMBytes uint64 `json:"parked_ram_bytes,omitempty"` } // Claim is one claim rule and what it matches right now: work Berth does not diff --git a/internal/skill/SKILL.md b/internal/skill/SKILL.md index 9c7e93a..36de244 100644 --- a/internal/skill/SKILL.md +++ b/internal/skill/SKILL.md @@ -18,6 +18,7 @@ berth status # the card, leases, waiters, stowaways, claims, the boar berth status --format json | jq '.leases, .waiting, .squatters' berth claims # games and apps Berth holds the card for, or names berth engine check -v # what every engine reports right now +berth contract ENGINE # one engine's VRAM, RAM, turn and switch times, each declared, measured or unknown berth events -f # live: grants, drains, refusals, stowaways ```