carlos / platform Public

cgroup: the size table drives tiers, shares and every limit validator

TierDefault and TierChildren are the standard and large rows; SharesOf
reads the row for a limit (standard's shares for a non-size);
ValidParentLimit accepts one size or the sum of any two; PrepareStrict,
PrepareStrictParent and PrepareGen accept exactly one size. MaxLimit
added for the swap's larger-size rule. (#329, plan 3 Task 1)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev d2949af4e31b6db34f87fcd0f2cee9e04fdcecec parent e5ab0e8
9 files changed, +199 −28
  • internal/cgroup/cgroup.go +31 −9
  • internal/cgroup/cgroup_test.go +83 −0
  • internal/cgroup/gen.go +7 −7
  • internal/cgroup/gen_test.go +16 −1
  • internal/cgroup/sizes.go +5 −7
  • internal/cgroup/strict_gen_linux.go +1 −1
  • internal/cgroup/strict_gen_linux_test.go +25 −0
  • internal/cgroup/strict_linux.go +4 −3
  • internal/cgroup/strict_linux_test.go +27 −0
diff --git a/internal/cgroup/cgroup.go b/internal/cgroup/cgroup.go
index a7f2bf913..fcea822d3 100644
--- a/internal/cgroup/cgroup.go
+++ b/internal/cgroup/cgroup.go
@@ -98,12 +98,25 @@ type Limit struct {
// The tiers are PLATFORM-chosen (memory-pressure spec §2.3), mirroring
// what caps/render.go already writes for unit-backed apps: the carlos-app@
// template's 512M, and the 1G a forking family gets when its manifest
-// declares `children`. No number comes from a manifest.
+// declares `children`. No number comes from a manifest. Both are rows of
+// the size table (#329, spec §5.1): the default tier is standard, the
+// children tier large. The names stay for the callers that use them.
var (
- TierDefault = Limit{MemoryMax: 512 << 20}
- TierChildren = Limit{MemoryMax: 1 << 30}
+ TierDefault = mustSize("standard").Limit
+ TierChildren = mustSize("large").Limit
)
+// mustSize is the size table's row called name. A missing row is a broken
+// build, so it panics at package initialization rather than handing a
+// caller a zero limit (which writes nothing).
+func mustSize(name string) Size {
+ s, ok := SizeByName(name)
+ if !ok {
+ panic("cgroup: no size " + name + " in the size table")
+ }
+ return s
+}
+
// Shares is the CPU and IO half of a tier (#214 item 1). Memory alone let
// one tenant take every core, or the disk's whole queue, from every other
// tenant on the box. Like the memory caps these are PLATFORM-chosen: the
@@ -123,14 +136,23 @@ type Shares struct {
IOWeight int
}
-// SharesOf is the tier's shares. A forking family gets two CPUs' worth of
-// quota, as it gets twice the memory; anything else, including a limit
-// that is not a tier, gets one: the smaller, and so the safe direction.
+// SharesOf is the shares of the size whose limit is l (the size table,
+// spec §5.1): a forking family's large gets two CPUs' worth of quota, as it
+// gets twice the memory. A limit that is no size gets standard's shares,
+// one CPU, as anything that was not a tier always did.
func SharesOf(l Limit) Shares {
- if l == TierChildren {
- return Shares{CPUWeight: 100, CPUQuotaPercent: 200, IOWeight: 100}
+ if s, ok := SizeOf(l); ok {
+ return s.Shares
+ }
+ return mustSize("standard").Shares
+}
+
+// MaxLimit is whichever of a and b has the larger MemoryMax.
+func MaxLimit(a, b Limit) Limit {
+ if b.MemoryMax > a.MemoryMax {
+ return b
}
- return Shares{CPUWeight: 100, CPUQuotaPercent: 100, IOWeight: 100}
+ return a
}
// HighOf is the memory.high for a memory.max: 90 percent, computed as the
diff --git a/internal/cgroup/cgroup_test.go b/internal/cgroup/cgroup_test.go
index 11ee4812b..61cb29901 100644
--- a/internal/cgroup/cgroup_test.go
+++ b/internal/cgroup/cgroup_test.go
@@ -460,6 +460,89 @@ func TestTiers(t *testing.T) {
}
}
+// TestSizeTableIsTheSpecsBytes pins the size table to spec §5.1's numbers,
+// byte for byte: each row's MemoryMax, the memory.high HighOf derives from
+// it, its shares, and what SharesOf and SizeOf answer for its limit.
+func TestSizeTableIsTheSpecsBytes(t *testing.T) {
+ want := map[string]struct {
+ max, high int64
+ sh Shares
+ }{
+ "small": {268435456, 241591905, Shares{CPUWeight: 50, CPUQuotaPercent: 50, IOWeight: 50}},
+ "standard": {536870912, 483183819, Shares{CPUWeight: 100, CPUQuotaPercent: 100, IOWeight: 100}},
+ "large": {1073741824, 966367638, Shares{CPUWeight: 100, CPUQuotaPercent: 200, IOWeight: 100}},
+ "xlarge": {2147483648, 1932735276, Shares{CPUWeight: 200, CPUQuotaPercent: 400, IOWeight: 200}},
+ }
+ order := []string{"small", "standard", "large", "xlarge"}
+ if len(Sizes) != len(order) {
+ t.Fatalf("Sizes has %d rows, want %d", len(Sizes), len(order))
+ }
+ for i, row := range Sizes {
+ if row.Name != order[i] {
+ t.Errorf("Sizes[%d].Name = %q, want %q", i, row.Name, order[i])
+ continue
+ }
+ w := want[row.Name]
+ if row.Limit.MemoryMax != w.max {
+ t.Errorf("%s: MemoryMax = %d, want %d", row.Name, row.Limit.MemoryMax, w.max)
+ }
+ if got := HighOf(row.Limit.MemoryMax); got != w.high {
+ t.Errorf("%s: HighOf = %d, want %d", row.Name, got, w.high)
+ }
+ if row.Shares != w.sh {
+ t.Errorf("%s: Shares = %+v, want %+v", row.Name, row.Shares, w.sh)
+ }
+ if got := SharesOf(row.Limit); got != w.sh {
+ t.Errorf("%s: SharesOf = %+v, want %+v", row.Name, got, w.sh)
+ }
+ if got, ok := SizeOf(row.Limit); !ok || got != row {
+ t.Errorf("%s: SizeOf = %+v, %v; want the row", row.Name, got, ok)
+ }
+ }
+}
+
+// TestTiersAreTheirSizesByteForByte: the two tier names existing callers
+// use are the standard and large rows, shares included.
+func TestTiersAreTheirSizesByteForByte(t *testing.T) {
+ standard, _ := SizeByName("standard")
+ large, _ := SizeByName("large")
+ if TierDefault != standard.Limit {
+ t.Errorf("TierDefault = %+v, want standard %+v", TierDefault, standard.Limit)
+ }
+ if TierChildren != large.Limit {
+ t.Errorf("TierChildren = %+v, want large %+v", TierChildren, large.Limit)
+ }
+ if got, want := SharesOf(TierChildren), (Shares{CPUWeight: 100, CPUQuotaPercent: 200, IOWeight: 100}); got != want {
+ t.Errorf("SharesOf(TierChildren) = %+v, want %+v", got, want)
+ }
+ if got, want := SharesOf(TierDefault), (Shares{CPUWeight: 100, CPUQuotaPercent: 100, IOWeight: 100}); got != want {
+ t.Errorf("SharesOf(TierDefault) = %+v, want %+v", got, want)
+ }
+}
+
+// TestSharesOfANonSizeIsStandards: a limit that is no size gets standard's
+// shares, the safe direction it always got.
+func TestSharesOfANonSizeIsStandards(t *testing.T) {
+ standard, _ := SizeByName("standard")
+ for _, l := range []Limit{{}, {MemoryMax: 7}, {MemoryMax: 768 << 20}} {
+ if got := SharesOf(l); got != standard.Shares {
+ t.Errorf("SharesOf(%+v) = %+v, want standard's %+v", l, got, standard.Shares)
+ }
+ }
+}
+
+func TestMaxLimit(t *testing.T) {
+ if got := MaxLimit(TierDefault, TierChildren); got != TierChildren {
+ t.Errorf("MaxLimit(TierDefault, TierChildren) = %+v, want %+v", got, TierChildren)
+ }
+ if got := MaxLimit(TierChildren, TierDefault); got != TierChildren {
+ t.Errorf("MaxLimit(TierChildren, TierDefault) = %+v, want %+v", got, TierChildren)
+ }
+ if got := MaxLimit(TierDefault, TierDefault); got != TierDefault {
+ t.Errorf("MaxLimit(TierDefault, TierDefault) = %+v, want %+v", got, TierDefault)
+ }
+}
+
func TestSetupAtRetainingPreservesClaimEvidence(t *testing.T) {
root := newDelegatedRoot(t)
retained := filepath.Join(root, "carlos-exec@claim-old.local")
diff --git a/internal/cgroup/gen.go b/internal/cgroup/gen.go
index 6aba3fce2..1f2a764ba 100644
--- a/internal/cgroup/gen.go
+++ b/internal/cgroup/gen.go
@@ -59,16 +59,16 @@ func OverlapLimit(incumbent, successor Limit) Limit {
}
// ValidParentLimit reports whether l is a limit a generation parent may
-// carry: one tier, or the sum of two. Strict containment and the root
-// control service accept nothing else.
+// carry: one size, or the sum of any two (an overlap across a size
+// change). Strict containment and the root control service accept nothing
+// else.
func ValidParentLimit(l Limit) bool {
- tiers := []int64{TierDefault.MemoryMax, TierChildren.MemoryMax}
- for _, a := range tiers {
- if l.MemoryMax == a {
+ for _, a := range Sizes {
+ if l == a.Limit {
return true
}
- for _, b := range tiers {
- if l.MemoryMax == a+b {
+ for _, b := range Sizes {
+ if l.MemoryMax == a.Limit.MemoryMax+b.Limit.MemoryMax {
return true
}
}
diff --git a/internal/cgroup/gen_test.go b/internal/cgroup/gen_test.go
index 9228131af..c9ac918bc 100644
--- a/internal/cgroup/gen_test.go
+++ b/internal/cgroup/gen_test.go
@@ -92,7 +92,7 @@ func TestOverlapAndValidParentLimits(t *testing.T) {
}
for max, want := range map[int64]bool{
512 << 20: true, 1 << 30: true, 1536 << 20: true, 2 << 30: true,
- 0: false, 64 << 20: false, 3 << 30: false,
+ 0: false, 64 << 20: false, 6 << 30: false,
} {
if got := ValidParentLimit(Limit{MemoryMax: max}); got != want {
t.Errorf("ValidParentLimit(%d) = %v, want %v", max, got, want)
@@ -100,6 +100,21 @@ func TestOverlapAndValidParentLimits(t *testing.T) {
}
}
+// TestValidParentLimitIsOneSizeOrTwo: a generation parent carries one size
+// or the sum of any two (an overlap across a size change), nothing else.
+func TestValidParentLimitIsOneSizeOrTwo(t *testing.T) {
+ for _, mib := range []int64{256, 512, 768, 1024, 1280, 1536, 2048, 2304, 2560, 3072, 4096} {
+ if !ValidParentLimit(Limit{MemoryMax: mib << 20}) {
+ t.Errorf("ValidParentLimit(%d MiB) = false, want true", mib)
+ }
+ }
+ for _, max := range []int64{0, 7, 384 << 20, 3584 << 20, 6144 << 20} {
+ if ValidParentLimit(Limit{MemoryMax: max}) {
+ t.Errorf("ValidParentLimit(%d) = true, want false", max)
+ }
+ }
+}
+
func TestStartChildPutsTheTierOnTheGenerationAndTheParent(t *testing.T) {
const host = "h.example.com"
m, parent := genManager(t, host)
diff --git a/internal/cgroup/sizes.go b/internal/cgroup/sizes.go
index 5c2d6aed3..42473a826 100644
--- a/internal/cgroup/sizes.go
+++ b/internal/cgroup/sizes.go
@@ -9,13 +9,11 @@ type Size struct {
Shares Shares
}
-// Sizes is the table of named sizes, smallest first. Today only the console
-// validates against it; TierDefault, TierChildren and SharesOf still decide
-// what a box writes. Plan 3 makes this table the source of every number
-// (spec §5.1) and derives TierDefault, TierChildren and SharesOf from it,
-// which is why every row is a literal: a row built from them would then be
-// an initialization cycle. standard and large equal today's two tiers; the
-// test, not this initializer, holds them equal.
+// Sizes is the table of named sizes, smallest first: the source of every
+// cgroup number (spec §5.1). TierDefault, TierChildren and SharesOf are
+// derived from it, and ValidParentLimit and the strict validators accept
+// its rows, which is why every row is a literal: a row built from any of
+// them would be an initialization cycle.
var Sizes = []Size{
{Name: "small", Limit: Limit{MemoryMax: 256 << 20}, Shares: Shares{CPUWeight: 50, CPUQuotaPercent: 50, IOWeight: 50}},
{Name: "standard", Limit: Limit{MemoryMax: 512 << 20}, Shares: Shares{CPUWeight: 100, CPUQuotaPercent: 100, IOWeight: 100}},
diff --git a/internal/cgroup/strict_gen_linux.go b/internal/cgroup/strict_gen_linux.go
index ce71653f6..69923813c 100644
--- a/internal/cgroup/strict_gen_linux.go
+++ b/internal/cgroup/strict_gen_linux.go
@@ -159,7 +159,7 @@ func (m *Manager) PrepareStrictParent(host string, tier Limit) (*Prepared, error
// and memory.oom.group=1 (spec §7.3). tier is this generation's own and may
// differ from the incumbent's when a release changes tier.
func (p *Prepared) PrepareGen(n uint64, tier Limit) (out *Prepared, err error) {
- if p == nil || p.strictPreparedState == nil || n == 0 || (tier != TierDefault && tier != TierChildren) {
+ if _, ok := SizeOf(tier); p == nil || p.strictPreparedState == nil || n == 0 || !ok {
return nil, ErrStrictRefused
}
p.mu.Lock()
diff --git a/internal/cgroup/strict_gen_linux_test.go b/internal/cgroup/strict_gen_linux_test.go
index 556a94d1f..4aec80d5f 100644
--- a/internal/cgroup/strict_gen_linux_test.go
+++ b/internal/cgroup/strict_gen_linux_test.go
@@ -62,6 +62,31 @@ func TestPrepareGenWritesTheFullTier(t *testing.T) {
}
}
+// TestPrepareGenWritesEverySize: each of the four sizes is a generation's
+// own limit, written in full; a sum of two (768 MiB) is a parent's limit
+// only, never a generation's.
+func TestPrepareGenWritesEverySize(t *testing.T) {
+ for _, size := range Sizes {
+ p := genParentFixture(t)
+ d := makeGenFixture(t, p, 1, "populated 0\n")
+ child, err := p.PrepareGen(1, size.Limit)
+ if err != nil {
+ t.Fatalf("%s: %v", size.Name, err)
+ }
+ for name, value := range tierExpected(size.Limit, "1") {
+ if got := readFixture(t, filepath.Join(d, name)); got != value {
+ t.Errorf("%s: g1/%s = %q, want %q", size.Name, name, got, value)
+ }
+ }
+ child.Close()
+ }
+ p := genParentFixture(t)
+ makeGenFixture(t, p, 1, "populated 0\n")
+ if child, err := p.PrepareGen(1, Limit{MemoryMax: 805306368}); child != nil || !errors.Is(err, ErrStrictRefused) {
+ t.Fatalf("PrepareGen(768 MiB) = %v, %v; want ErrStrictRefused", child, err)
+ }
+}
+
func TestPrepareGenReservesPerChild(t *testing.T) {
p := genParentFixture(t)
makeGenFixture(t, p, 1, "populated 0\n")
diff --git a/internal/cgroup/strict_linux.go b/internal/cgroup/strict_linux.go
index 245b8e12e..f1921d8b6 100644
--- a/internal/cgroup/strict_linux.go
+++ b/internal/cgroup/strict_linux.go
@@ -200,8 +200,9 @@ func (m *Manager) StrictPreflight() error {
return root.Close()
}
-// PrepareStrict never uses accounting's fallback. Only the two existing tiers
-// are accepted; a zero or missing limit cannot create an uncapped command.
+// PrepareStrict never uses accounting's fallback. Only a size's own limit is
+// accepted (one row of the size table, never a sum); a zero or missing limit
+// cannot create an uncapped command.
func (m *Manager) PrepareStrict(host string, limit Limit) (*Prepared, error) {
return m.prepareStrict(host, limit, false)
}
@@ -213,7 +214,7 @@ func (m *Manager) prepareStrict(host string, limit Limit, genParent bool) (out *
if m == nil || m.root == "" {
return nil, ErrStrictUnsupported
}
- if !controlname.ValidHostShape(host) || (limit != TierDefault && limit != TierChildren) {
+ if _, ok := SizeOf(limit); !controlname.ValidHostShape(host) || !ok {
return nil, ErrStrictRefused
}
root, e := m.strictRoot()
diff --git a/internal/cgroup/strict_linux_test.go b/internal/cgroup/strict_linux_test.go
index 6c33bfd8a..a0fa429bc 100644
--- a/internal/cgroup/strict_linux_test.go
+++ b/internal/cgroup/strict_linux_test.go
@@ -108,6 +108,33 @@ func TestStrictRejectsUnavailableManagerHostAndTier(t *testing.T) {
}
}
}
+
+// TestPrepareStrictTakesEverySize: the strict validators take exactly one
+// size, any of the four, and never a sum (spec §5.3 rev 8). A size passes
+// the limit check and reaches the runtime checks; a placement with no v2
+// line stops it there as ErrStrictUnsupported before anything on disk is
+// touched, which tells "passed the size check" apart from ErrStrictRefused.
+func TestPrepareStrictTakesEverySize(t *testing.T) {
+ fakeSelfCgroup(t, "1:name=systemd:/")
+ m := &Manager{root: "/sys/fs/cgroup"}
+ prepares := map[string]func(Limit) (*Prepared, error){
+ "PrepareStrict": func(l Limit) (*Prepared, error) { return m.PrepareStrict("x.example", l) },
+ "PrepareStrictParent": func(l Limit) (*Prepared, error) { return m.PrepareStrictParent("x.example", l) },
+ }
+ for name, prepare := range prepares {
+ for _, size := range Sizes {
+ p, e := prepare(size.Limit)
+ if p != nil || errors.Is(e, ErrStrictRefused) || !errors.Is(e, ErrStrictUnsupported) {
+ t.Errorf("%s(%s) = %v, %v; want the runtime check's ErrStrictUnsupported", name, size.Name, p, e)
+ }
+ }
+ for _, l := range []Limit{{MemoryMax: 7}, {MemoryMax: 805306368}} {
+ if p, e := prepare(l); p != nil || !errors.Is(e, ErrStrictRefused) {
+ t.Errorf("%s(%d) = %v, %v; want ErrStrictRefused", name, l.MemoryMax, p, e)
+ }
+ }
+ }
+}
func TestStrictStartConsumesOneAttemptWithoutFallback(t *testing.T) {
p := strictFixture(t)
var calls atomic.Int32