| index 819a496ad..dfb9d215e 100644 |
| --- a/internal/host/size_rootrunner_linux_test.go |
| +++ b/internal/host/size_rootrunner_linux_test.go |
| @@ -51,9 +51,10 @@ var ( |
| sizeXLarge = sizeRow{name: "xlarge", max: "2147483648", high: "1932735276", quota: "4s", cpuW: "200", ioW: "200", memoryMax: 2147483648} |
| ) |
| |
| -// sizeHoldMiB is what the allocating tenant holds: more than standard's |
| -// 512 MiB cap, less than large's MemoryHigh (about 921 MiB). |
| -const sizeHoldMiB = 700 |
| +// sizeHoldMiB is what the allocating tenant holds: well inside standard's |
| +// MemoryHigh (about 460 MiB), so a live size that lowered memory under it |
| +// would show, and one that left memory alone kills nothing. |
| +const sizeHoldMiB = 300 |
| |
| // The size-tenant payload: a serving tenant on the unix socket named by |
| // its one argument (never PLAN14_TENANT_SOCKET, which TestMain takes for |
| @@ -297,22 +298,37 @@ func sizeCgroupFileOrErr(unit, name string) string { |
| } |
| |
| // sizeReadBackIs checks POST /size's read-back against the size applied |
| -// and the memory held. |
| -func sizeReadBackIs(t *testing.T, step string, got instancectl.SizeReadBack, shares sizeRow, memory int64, gen bool) { |
| +// (its CPU and IO shares) and the slice memory still in force. |
| +func sizeReadBackIs(t *testing.T, step string, got instancectl.SizeReadBack, shares sizeRow, memory int64) { |
| t.Helper() |
| s, ok := cgroup.SizeByName(shares.name) |
| if !ok { |
| t.Fatal("no size", shares.name) |
| } |
| want := instancectl.SizeReadBack{SliceMemoryMax: memory, CPUQuotaPercent: s.Shares.CPUQuotaPercent, CPUWeight: s.Shares.CPUWeight, IOWeight: s.Shares.IOWeight} |
| - if gen { |
| - want.UnitMemoryMax = memory |
| - } |
| if got != want { |
| t.Fatalf("%s: read-back %+v, want %+v", step, got, want) |
| } |
| } |
| |
| +// sizeMixed is a slice row with mem's memory lines and cpu's CPU and IO: |
| +// what a live size leaves (ruling 12). |
| +func sizeMixed(mem, cpu sizeRow) sizeRow { |
| + return sizeRow{name: cpu.name + " with " + mem.name + "'s memory", max: mem.max, high: mem.high, |
| + quota: cpu.quota, cpuW: cpu.cpuW, ioW: cpu.ioW, memoryMax: mem.memoryMax} |
| +} |
| + |
| +// sizeSliceCgroupFile reads a cgroup v2 file of the host slice's own |
| +// cgroup, as the kernel holds it. |
| +func sizeSliceCgroupFile(t *testing.T, slice, name string) string { |
| + t.Helper() |
| + raw, err := os.ReadFile(filepath.Join("/sys/fs/cgroup", egressShow(t, slice, "ControlGroup"), name)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return strings.TrimSpace(string(raw)) |
| +} |
| + |
| // TestSizesThroughEveryStartPathRootRunner: for small and xlarge, each |
| // legacy start path (run, generation, seed, sidecar, cmd) writes the size |
| // table's row onto the host slice, as systemd shows it. The sizes |
| @@ -405,12 +421,15 @@ func TestSizesThroughEveryStartPathRootRunner(t *testing.T) { |
| } |
| } |
| |
| -// TestLiveGrowthReachesTheServingGenerationsOwnCapRootRunner: a |
| -// generation started at standard is grown to large live (POST /size with |
| -// its gen). The unit's own MemoryMax and its cgroup's memory.max read |
| -// 1073741824, and a process in it then allocating 700 MiB, more than |
| -// standard's cap, survives holding it. |
| -func TestLiveGrowthReachesTheServingGenerationsOwnCapRootRunner(t *testing.T) { |
| +// TestLiveSizeMovesOnlyCPUAndIORootRunner (ruling 12, spec §5.4 rev 10): |
| +// a generation started at standard holds 300 MiB. POST /size xlarge moves |
| +// the slice's CPU and IO to xlarge's shares and leaves every memory limit |
| +// where the start put it: the slice's and the unit's MemoryMax, and both |
| +// cgroups' memory.max. A daemon-reload leaves the CPU and IO values in |
| +// place (the slice has no drop-in of its own to restore over them) and the |
| +// process alive. A stop and a cold start at xlarge then give the slice, |
| +// the unit and the kernel xlarge's 2147483648. |
| +func TestLiveSizeMovesOnlyCPUAndIORootRunner(t *testing.T) { |
| b := newSizeBox(t) |
| ctx := t.Context() |
| sizeAllocTenant(t, b) |
| @@ -421,124 +440,93 @@ func TestLiveGrowthReachesTheServingGenerationsOwnCapRootRunner(t *testing.T) { |
| b.tenant(t, unit, "gen.sock") |
| wantSliceRow(t, "start", b.slice, sizeStandard) |
| wantGenMemory(t, "start", unit, sizeStandard) |
| - |
| - rb, err := b.client.Size(ctx, b.host, egressGen, "large", sizeLarge.memoryMax) |
| - if err != nil { |
| - t.Fatal("POST /size large:", err) |
| - } |
| - sizeReadBackIs(t, "growth", rb, sizeLarge, sizeLarge.memoryMax, true) |
| - wantSliceRow(t, "growth", b.slice, sizeLarge) |
| - wantGenMemory(t, "growth", unit, sizeLarge) |
| - if got, err := os.ReadFile(filepath.Join("/sys/fs/cgroup", egressShow(t, b.slice, "ControlGroup"), "memory.max")); err != nil || strings.TrimSpace(string(got)) != sizeLarge.max { |
| - t.Fatalf("growth: slice cgroup memory.max = %q (%v), want %s", got, err, sizeLarge.max) |
| - } |
| sizeHolding(t, b, unit) |
| -} |
| + inv, pid := egressShow(t, unit, "InvocationID"), egressShow(t, unit, "MainPID") |
| |
| -// TestMemoryShrinkLeavesARunningProcessAloneRootRunner: a generation at |
| -// large holds 700 MiB. POST /size small with memory_max=1073741824 (memory |
| -// held, CPU and IO shrunk) leaves the process alive; a POST with |
| -// memory_max=268435456 is refused with 409 and writes nothing; after a |
| -// stop, a cold start at small puts the slice at 268435456. |
| -func TestMemoryShrinkLeavesARunningProcessAloneRootRunner(t *testing.T) { |
| - b := newSizeBox(t) |
| - ctx := t.Context() |
| - sizeAllocTenant(t, b) |
| - unit := instancectl.GenUnitName(b.host, egressGen) |
| - if _, err := b.client.StartGen(ctx, b.host, egressGen, sizeLarge.memoryMax); err != nil { |
| - t.Fatalf("POST /start gen: %v\n%s", err, egressJournal(unit)) |
| + live := sizeMixed(sizeStandard, sizeXLarge) |
| + check := func(step string) { |
| + t.Helper() |
| + wantSliceRow(t, step, b.slice, live) |
| + wantGenMemory(t, step, unit, sizeStandard) |
| + if got := sizeSliceCgroupFile(t, b.slice, "memory.max"); got != sizeStandard.max { |
| + t.Fatalf("%s: slice cgroup memory.max = %q, want %s", step, got, sizeStandard.max) |
| + } |
| + if got := sizeSliceCgroupFile(t, b.slice, "cpu.max"); got != "400000 100000" { |
| + t.Fatalf("%s: slice cgroup cpu.max = %q, want xlarge's 400000 100000", step, got) |
| + } |
| + if got := sizeSliceCgroupFile(t, b.slice, "cpu.weight"); got != sizeXLarge.cpuW { |
| + t.Fatalf("%s: slice cgroup cpu.weight = %q, want %s", step, got, sizeXLarge.cpuW) |
| + } |
| + sizeAlive(t, b, unit, step, inv, pid) |
| } |
| - b.tenant(t, unit, "gen.sock") |
| - wantSliceRow(t, "start", b.slice, sizeLarge) |
| - wantGenMemory(t, "start", unit, sizeLarge) |
| - sizeHolding(t, b, unit) |
| - inv, pid := egressShow(t, unit, "InvocationID"), egressShow(t, unit, "MainPID") |
| |
| - rb, err := b.client.Size(ctx, b.host, egressGen, "small", sizeLarge.memoryMax) |
| + rb, err := b.client.Size(ctx, b.host, "xlarge") |
| if err != nil { |
| - t.Fatal("POST /size small, memory held:", err) |
| + t.Fatal("POST /size xlarge:", err) |
| } |
| - sizeReadBackIs(t, "shrink, memory held", rb, sizeSmall, sizeLarge.memoryMax, true) |
| - held := sizeRow{name: "small with large's memory", max: sizeLarge.max, high: sizeLarge.high, quota: sizeSmall.quota, cpuW: sizeSmall.cpuW, ioW: sizeSmall.ioW} |
| - wantSliceRow(t, "shrink, memory held", b.slice, held) |
| - wantGenMemory(t, "shrink, memory held", unit, sizeLarge) |
| - sizeAlive(t, b, unit, "shrink, memory held", inv, pid) |
| + sizeReadBackIs(t, "live xlarge", rb, sizeXLarge, sizeStandard.memoryMax) |
| + check("live xlarge") |
| |
| - _, err = b.client.Size(ctx, b.host, egressGen, "small", sizeSmall.memoryMax) |
| - if !errors.Is(err, instancectl.ErrSizeRefused) || !strings.Contains(err.Error(), ": 409: ") { |
| - t.Fatalf("POST /size small with memory_max=%d = %v, want a 409 refusal", sizeSmall.memoryMax, err) |
| + egressMustCtl(t, "daemon-reload") |
| + check("after daemon-reload") |
| + if rb, err := b.client.ReadSize(ctx, b.host); err != nil { |
| + t.Fatal("GET /size:", err) |
| + } else { |
| + sizeReadBackIs(t, "GET after daemon-reload", rb, sizeXLarge, sizeStandard.memoryMax) |
| } |
| - wantSliceRow(t, "refused shrink", b.slice, held) |
| - wantGenMemory(t, "refused shrink", unit, sizeLarge) |
| - sizeAlive(t, b, unit, "refused shrink", inv, pid) |
| |
| if err := b.client.StopGen(ctx, b.host, egressGen, false); err != nil { |
| t.Fatal("stop gen:", err) |
| } |
| - // The cold start goes through the run path, so a fault in a reused |
| - // generation number's start (the next witness) cannot mask this one. |
| - run := instancectl.UnitName(instancectl.RoleRun, b.host) |
| - if _, err := b.client.Start(ctx, b.host, instancectl.RoleRun, sizeSmall.memoryMax); err != nil { |
| - t.Fatalf("cold start at small: %v\n%s", err, egressJournal(run)) |
| + if _, err := b.client.StartGen(ctx, b.host, egressGen, sizeXLarge.memoryMax); err != nil { |
| + t.Fatalf("cold start at xlarge: %v\n%s", err, egressJournal(unit)) |
| + } |
| + b.tenant(t, unit, "gen.sock") |
| + wantSliceRow(t, "cold start at xlarge", b.slice, sizeXLarge) |
| + wantGenMemory(t, "cold start at xlarge", unit, sizeXLarge) |
| + if got := sizeSliceCgroupFile(t, b.slice, "memory.max"); got != sizeXLarge.max { |
| + t.Fatalf("cold start at xlarge: slice cgroup memory.max = %q, want %s", got, sizeXLarge.max) |
| } |
| - b.tenant(t, run, "run.sock") |
| - wantSliceRow(t, "cold start", b.slice, sizeSmall) |
| - if err := b.client.Stop(ctx, b.host, instancectl.RoleRun); err != nil { |
| - t.Fatal("stop run:", err) |
| + if err := b.client.StopGen(ctx, b.host, egressGen, false); err != nil { |
| + t.Fatal("stop gen:", err) |
| } |
| } |
| |
| -// TestReusedGenerationNumberStartsAtItsOwnSizeRootRunner: generation 1 is |
| -// grown to xlarge live, which leaves a runtime set-property drop-in |
| -// (MemoryMax=2147483648) for its unit, and stopped; generation 1 started |
| -// again at standard runs at 536870912. Twice: once as the stop leaves it |
| -// (StopGen removes the drop-in), and once with that drop-in put back and |
| -// loaded by systemd, as a stop that could not remove it would leave it: |
| -// the start itself must clear it (Task 5, ruling C5 (d)). |
| +// staleGenMemoryDropIn is the runtime drop-in `systemctl set-property |
| +// --runtime <unit> MemoryMax=` would leave in the control directory: what |
| +// a generation unit of the same name could have left behind before a stop |
| +// that did not remove it. |
| +const staleGenMemoryDropIn = "# This is a drop-in unit file extension, created via \"systemctl set-property\"\n" + |
| + "# or an equivalent operation. Do not edit.\n[Service]\nMemoryMax=2147483648\n" |
| + |
| +// TestReusedGenerationNumberStartsAtItsOwnSizeRootRunner: generation 1 |
| +// runs at xlarge and is stopped; generation 1 started again at standard |
| +// runs at 536870912 (unit, kernel and slice). Twice: once as the stop |
| +// leaves it, and once with a stale runtime MemoryMax=2147483648 drop-in |
| +// for the unit put in the control directory and loaded by systemd, as a |
| +// stop that could not remove one would leave it. The reused start runs at |
| +// its own size and the running unit carries no stale drop-in: the start |
| +// clears it (tidying; the generation's own drop-in sorts after it and |
| +// wins either way, spec rev 10). |
| func TestReusedGenerationNumberStartsAtItsOwnSizeRootRunner(t *testing.T) { |
| b := newSizeBox(t) |
| ctx := t.Context() |
| unit := instancectl.GenUnitName(b.host, egressGen) |
| control := filepath.Join(instancectl.RuntimeControlDir, unit+".d") |
| |
| - grow := func(step string) map[string][]byte { |
| + runXLarge := func(step string) { |
| t.Helper() |
| - if _, err := b.client.StartGen(ctx, b.host, egressGen, sizeStandard.memoryMax); err != nil { |
| + if _, err := b.client.StartGen(ctx, b.host, egressGen, sizeXLarge.memoryMax); err != nil { |
| t.Fatalf("%s: POST /start gen: %v\n%s", step, err, egressJournal(unit)) |
| } |
| b.tenant(t, unit, "gen.sock") |
| - wantGenMemory(t, step+": start", unit, sizeStandard) |
| - rb, err := b.client.Size(ctx, b.host, egressGen, "xlarge", sizeXLarge.memoryMax) |
| - if err != nil { |
| - t.Fatal(step, "POST /size xlarge:", err) |
| - } |
| - sizeReadBackIs(t, step+": growth", rb, sizeXLarge, sizeXLarge.memoryMax, true) |
| - wantGenMemory(t, step+": growth", unit, sizeXLarge) |
| - // What systemd wrote for the unit's live growth. |
| - files := map[string][]byte{} |
| - entries, err := os.ReadDir(control) |
| - if err != nil { |
| - t.Fatalf("%s: the live growth left no control drop-in for %s: %v", step, unit, err) |
| - } |
| - var stale bool |
| - for _, e := range entries { |
| - raw, err := os.ReadFile(filepath.Join(control, e.Name())) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - files[e.Name()] = raw |
| - stale = stale || strings.Contains(string(raw), "MemoryMax="+sizeXLarge.max) |
| - } |
| - if !stale { |
| - t.Fatalf("%s: %s holds no MemoryMax=%s: %v", step, control, sizeXLarge.max, files) |
| - } |
| + wantGenMemory(t, step, unit, sizeXLarge) |
| if err := b.client.StopGen(ctx, b.host, egressGen, false); err != nil { |
| t.Fatal(step, "stop gen:", err) |
| } |
| if _, err := os.Lstat(control); !errors.Is(err, fs.ErrNotExist) { |
| t.Fatalf("%s: %s left behind by the stop: %v", step, control, err) |
| } |
| - return files |
| } |
| reuse := func(step string) { |
| t.Helper() |
| @@ -549,7 +537,7 @@ func TestReusedGenerationNumberStartsAtItsOwnSizeRootRunner(t *testing.T) { |
| wantGenMemory(t, step, unit, sizeStandard) |
| wantSliceRow(t, step, b.slice, sizeStandard) |
| // The running reused generation carries no control drop-in with |
| - // the old growth's memory: the start cleared it. |
| + // the old memory: the start cleared it. |
| if entries, err := os.ReadDir(control); err == nil { |
| for _, e := range entries { |
| raw, err := os.ReadFile(filepath.Join(control, e.Name())) |
| @@ -557,7 +545,7 @@ func TestReusedGenerationNumberStartsAtItsOwnSizeRootRunner(t *testing.T) { |
| t.Fatal(err) |
| } |
| if strings.Contains(string(raw), "MemoryMax="+sizeXLarge.max) { |
| - t.Fatalf("%s: %s still holds the old growth's MemoryMax=%s", step, filepath.Join(control, e.Name()), sizeXLarge.max) |
| + t.Fatalf("%s: %s still holds the old MemoryMax=%s", step, filepath.Join(control, e.Name()), sizeXLarge.max) |
| } |
| } |
| } else if !errors.Is(err, fs.ErrNotExist) { |
| @@ -568,17 +556,15 @@ func TestReusedGenerationNumberStartsAtItsOwnSizeRootRunner(t *testing.T) { |
| } |
| } |
| |
| - grow("first") |
| + runXLarge("first") |
| reuse("reused after a clean stop") |
| |
| - files := grow("second") |
| + runXLarge("second") |
| if err := os.Mkdir(control, 0o755); err != nil { |
| t.Fatal(err) |
| } |
| - for name, raw := range files { |
| - if err := os.WriteFile(filepath.Join(control, name), raw, 0o644); err != nil { |
| - t.Fatal(err) |
| - } |
| + if err := os.WriteFile(filepath.Join(control, "50-MemoryMax.conf"), []byte(staleGenMemoryDropIn), 0o644); err != nil { |
| + t.Fatal(err) |
| } |
| egressMustCtl(t, "daemon-reload") |
| if got := egressShow(t, unit, "MemoryMax"); got != sizeXLarge.max { |