dshFilesSum, which says whether anything a dsh round reads changed, read every profile's patch list, the home layer, settings, .env, keys and old config into memory with os.ReadFile only to hash them. Both dsh homes do a round every 30s at rest, and a patch list names every model magpie serves (39KB each in the real account here, two profiles), so each round allocated each file's size again. Now each file is streamed into the maphash through a pooled 32KB buffer (dshSumFile); the sum is the same as for the bytes written at once, so a round after the update still matches the one before. Measured on mote with a copy of the real accounts (acidle.sh, alloc between 60s and 180s at rest, the heap dump's own allocations left out): - t18 (c02a491e8): 9.4MB, of it dshFilesSum 2.1MB - t19 (this): 7.7MB, dshFilesSum gone from the profile Test: TestDshFilesSumReadsNoFileWhole (the sum equals the one for files read whole, a change at the end of a 1.1MB list moves it, and a sum allocates under 64KB). Broken by hand: with os.ReadFile back it is red, 1149490 bytes a sum for a 1140000-byte list. Run on mote at this tree (rebased onc02a491e8): go vet ./internal/agent, gofmt -l clean, go test -timeout 45m ./internal/agent ok (246s). Not tried: Windows and Linux (os.Open/Read only, no OS-specific code).
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
package agent
|
|
|
|
import (
|
|
"bytes"
|
|
"hash/maphash"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
// TestDshFilesSumReadsNoFileWhole says a round's sum of dsh's files is
|
|
// what it was when each file was read whole, and costs no more for a large
|
|
// patch list than a small one: a list names every model magpie serves,
|
|
// and both dsh homes' rounds every 30s read each file into memory again
|
|
// only to hash it.
|
|
func TestDshFilesSumReadsNoFileWhole(t *testing.T) {
|
|
dir := t.TempDir()
|
|
list := filepath.Join(dir, "profiles", "a", "cordis.patch.yml")
|
|
if err := os.MkdirAll(filepath.Dir(list), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
big := bytes.Repeat([]byte("- id: magpie/some-provider/some-model\n"), 30000) // ~1.1MB, past one buffer
|
|
for _, f := range []struct {
|
|
path string
|
|
b []byte
|
|
}{{list, big}, {filepath.Join(dir, "settings.yaml"), []byte("a: 1\n")}} {
|
|
if err := os.WriteFile(f.path, f.b, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
files := []string{list}
|
|
|
|
// the sum as it was, each file's bytes written at once
|
|
whole := func() uint64 {
|
|
var h maphash.Hash
|
|
h.SetSeed(dshSumSeed)
|
|
for _, f := range append(files, filepath.Join(dir, "cordis.patch.yml"), filepath.Join(dir, "settings.yaml"),
|
|
filepath.Join(dir, ".env"), filepath.Join(dir, ".credentials.yaml"), filepath.Join(dir, "config.yaml")) {
|
|
h.WriteString(f)
|
|
h.WriteByte(0)
|
|
if b, err := os.ReadFile(f); err == nil {
|
|
h.WriteByte(1)
|
|
h.Write(b)
|
|
} else {
|
|
h.WriteByte(2)
|
|
}
|
|
h.WriteByte(0)
|
|
}
|
|
return h.Sum64()
|
|
}
|
|
if got, want := dshFilesSum(dir, files), whole(); got != want {
|
|
t.Fatalf("sum %x, want %x as for the files read whole", got, want)
|
|
}
|
|
before := dshFilesSum(dir, files)
|
|
big[len(big)-2] = 'X'
|
|
if err := os.WriteFile(list, big, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if dshFilesSum(dir, files) == before {
|
|
t.Fatal("a change at the end of a large patch list left the sum as it was")
|
|
}
|
|
|
|
var total uint64
|
|
allocs := testing.AllocsPerRun(10, func() {
|
|
m0 := memBytes()
|
|
dshFilesSum(dir, files)
|
|
total += memBytes() - m0
|
|
})
|
|
if per := total / 11; per > 64<<10 {
|
|
t.Errorf("a sum allocates %d bytes (%v allocations) for a %d-byte patch list; want it read through a buffer", per, allocs, len(big))
|
|
}
|
|
}
|
|
|
|
func memBytes() uint64 {
|
|
var m runtime.MemStats
|
|
runtime.ReadMemStats(&m)
|
|
return m.TotalAlloc
|
|
}
|