| index ca1790c52..f68344a40 100644 |
| --- a/internal/demobuild/transfer_test.go |
| +++ b/internal/demobuild/transfer_test.go |
| @@ -12,6 +12,7 @@ import ( |
| "os" |
| "path/filepath" |
| "runtime" |
| + "runtime/debug" |
| "sync/atomic" |
| "syscall" |
| "testing" |
| @@ -145,25 +146,11 @@ func TestDemoBuildOutputTransferWaitHonoursDeadline(t *testing.T) { |
| } |
| } |
| |
| -// TestDemoBuildOutputTransferPeak is a diagnostic, not a gate: it pushes |
| -// four 32 MiB outputs, one after another, through Run's read and |
| -// validation and the control response's JSON encoding, and logs the peak |
| -// Go heap and the process's peak RSS. Run it alone so the RSS is this |
| -// test's: |
| -// |
| -// CARLOS_DEMO_TRANSFER_PEAK=1 go test ./internal/demobuild -run '^TestDemoBuildOutputTransferPeak$' -count=1 -v |
| -// |
| -// The authority for the control service's cap is its cgroup's memory.peak |
| -// on a demo box, not this figure. |
| -func TestDemoBuildOutputTransferPeak(t *testing.T) { |
| - if os.Getenv("CARLOS_DEMO_TRANSFER_PEAK") == "" { |
| - t.Skip("set CARLOS_DEMO_TRANSFER_PEAK=1 to measure") |
| - } |
| - dir := t.TempDir() |
| - if err := os.Mkdir(filepath.Join(dir, "out"), 0755); err != nil { |
| - t.Fatal(err) |
| - } |
| - // Written sparse so the fixture costs no memory of its own. |
| +// sparseOutput is a finished build whose out/app is a valid 32 MiB |
| +// executable written sparse, so the fixture costs no memory of its own. |
| +func sparseOutput(t *testing.T) (context.Context, string) { |
| + t.Helper() |
| + ctx, dir := finishedBuild(t, arm64ELF()) |
| header := arm64ELF() |
| binary.LittleEndian.PutUint64(header[96:], BinaryMaxBytes) |
| binary.LittleEndian.PutUint64(header[104:], BinaryMaxBytes) |
| @@ -180,12 +167,58 @@ func TestDemoBuildOutputTransferPeak(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| + return ctx, dir |
| +} |
| + |
| +// outputTransferHeapBudget is what output transfers may take the control |
| +// service's Go heap to at its high water. One transfer holds about 118 MiB |
| +// live at once: the raw output (32 MiB), its base64 encoding (~43 MiB) and |
| +// the encoder's copy of it (~43 MiB). At GOGC=100 the collector lets the |
| +// heap reach twice what was live at its last cycle, and one 43 MiB |
| +// allocation can overshoot that, so the bound for transfers taken one at a |
| +// time is about 2 x 118 + 43 = 279 MiB; measured on docs-t1 (amd64, |
| +// go1.26.10) 181 to 277 MiB. Four admitted together measured 426 to 469 |
| +// MiB. 320 MiB keeps 40 MiB over the bound and fails the second case. |
| +// |
| +// It does not fit the base control unit's MemoryMax=256M (#1291), so the |
| +// build worker's control service keeps its own 1 GiB cap with |
| +// GOMEMLIMIT=768MiB (claimUnitContent; pinned by TestDemoBuildInstallation): |
| +// about 700 MiB of room over this budget for the service's baseline. |
| +const outputTransferHeapBudget = 320 << 20 |
| + |
| +// F3's heap high-water gate. Four builds finish together, each with a |
| +// 32 MiB output, under the build worker's GOMEMLIMIT. Each goes through |
| +// Run's read and validation and the control response's JSON encoding, as |
| +// handleDemoBuild does, and gives its turn back after. The Go heap's |
| +// sampled high water above the starting heap stays inside one transfer's |
| +// budget. The authority for the cap remains the service cgroup's |
| +// memory.peak on a demo box (plan slice 1.1); this test is what the |
| +// branch can show without one. Process RSS is logged as a diagnostic. |
| +func TestDemoBuildOutputTransferHeapHighWater(t *testing.T) { |
| + if testing.Short() { |
| + t.Skip("reads four 32 MiB outputs") |
| + } |
| + defer debug.SetMemoryLimit(debug.SetMemoryLimit(768 << 20)) |
| + type build struct { |
| + ctx context.Context |
| + dir string |
| + } |
| + var builds []build |
| + for range 4 { |
| + ctx, dir := sparseOutput(t) |
| + builds = append(builds, build{ctx, dir}) |
| + } |
| maxRSS := func() int64 { |
| var ru syscall.Rusage |
| _ = syscall.Getrusage(syscall.RUSAGE_SELF, &ru) |
| return ru.Maxrss << 10 |
| } |
| - var peakHeap, peakSys atomic.Uint64 |
| + runtime.GC() |
| + var m runtime.MemStats |
| + runtime.ReadMemStats(&m) |
| + base := m.HeapAlloc |
| + beforeRSS := maxRSS() |
| + var peak atomic.Uint64 |
| stop := make(chan struct{}) |
| sampled := make(chan struct{}) |
| go func() { |
| @@ -193,11 +226,8 @@ func TestDemoBuildOutputTransferPeak(t *testing.T) { |
| var m runtime.MemStats |
| for { |
| runtime.ReadMemStats(&m) |
| - if m.HeapAlloc > peakHeap.Load() { |
| - peakHeap.Store(m.HeapAlloc) |
| - } |
| - if m.Sys-m.HeapReleased > peakSys.Load() { |
| - peakSys.Store(m.Sys - m.HeapReleased) |
| + if m.HeapAlloc > peak.Load() { |
| + peak.Store(m.HeapAlloc) |
| } |
| select { |
| case <-stop: |
| @@ -206,24 +236,33 @@ func TestDemoBuildOutputTransferPeak(t *testing.T) { |
| } |
| } |
| }() |
| - runtime.GC() |
| - before := maxRSS() |
| - for n := range 4 { |
| - i := &runInvocation{dir: dir, startWait: func(context.Context) error { return nil }, stop: func() error { return nil }} |
| - out, err := Run(context.WithValue(t.Context(), runContextKey{}, i), dir, testLock(t)) |
| - if err != nil || len(out.Binary) != BinaryMaxBytes { |
| - t.Fatalf("transfer %d: %v", n, err) |
| - } |
| - if err := json.NewEncoder(io.Discard).Encode(out); err != nil { |
| + errs := make(chan error, len(builds)) |
| + for _, b := range builds { |
| + go func() { |
| + out, err := Run(b.ctx, b.dir, testLock(t)) |
| + defer out.Release() |
| + if err == nil && len(out.Binary) != BinaryMaxBytes { |
| + err = ErrBuild |
| + } |
| + if err == nil { |
| + err = json.NewEncoder(io.Discard).Encode(out) |
| + } |
| + errs <- err |
| + }() |
| + } |
| + for range builds { |
| + if err := <-errs; err != nil { |
| t.Fatal(err) |
| } |
| - out.Release() |
| } |
| close(stop) |
| <-sampled |
| - after := maxRSS() |
| - t.Logf("GOMEMLIMIT=%q GOGC=%q: peak HeapAlloc=%d MiB, peak Sys-HeapReleased=%d MiB, maxrss before=%d MiB after=%d MiB (delta %d MiB)", |
| - os.Getenv("GOMEMLIMIT"), os.Getenv("GOGC"), peakHeap.Load()>>20, peakSys.Load()>>20, before>>20, after>>20, (after-before)>>20) |
| + grew := peak.Load() - min(base, peak.Load()) |
| + t.Logf("heap high water %d MiB above a %d MiB start (budget %d MiB); maxrss %d -> %d MiB", |
| + grew>>20, base>>20, outputTransferHeapBudget>>20, beforeRSS>>20, maxRSS()>>20) |
| + if grew > outputTransferHeapBudget { |
| + t.Fatalf("four finished builds took the heap %d MiB above its start, past one transfer's %d MiB", grew>>20, outputTransferHeapBudget>>20) |
| + } |
| } |
| |
| // shstrndxPanicELF is an AArch64 executable header whose section table |