carlos / platform Public

Tenant instances run with no GOMEMLIMIT or GOMAXPROCS; set both from the size Feature Request closed

Claimed by claude~paul@keymail.dev 2 weeks ago

Problem

Tenant instances start with no GOMEMLIMIT (and no GOMAXPROCS). The cgroup caps them (memory.max from the size, memory.high at 90%, internal/cgroup/cgroup.go), but the Go runtime does not know about the cap. With the default GOGC=100 a heap is allowed to grow to twice its live size before a collection, so each process keeps headroom it never needs, and it meets memory.high throttling or an OOM kill before the collector works hard.

On a box with ~100 awake Go instances (a storefront app whose instances crawlers keep awake), that headroom is most of the per-process cost. Today GOMEMLIMIT is set only on platform units (Litestream, claim units, the gVisor run sandbox in internal/instancectl/ocibundle.go).

GOMAXPROCS: since Go 1.25 the runtime reads the cgroup CPU limit, but instances run with CPU weight, not cpu.max, so every instance sees every core of the box. Each P carries its own caches and the GC uses 25% of GOMAXPROCS as workers, so 100 processes each sized for 8+ cores cost more memory and cause GC CPU spikes across the box.

Proposal

When the activator starts a binary instance, set in its environment, unless the release or app config already sets them:

  • GOMEMLIMIT = about 80% of the size's memory.max (below memory.high at 90%, leaving room for stacks, cgo and SQLite page cache).
  • GOMAXPROCS = a per-size value (e.g. small 1, medium 2, large+ 4), or the box's cores, whichever is lower.
  • Also CARLOS_MEMORY_MIB, matching #892 S1 for rootfs processes, so apps that tune themselves have one number.

App overrides win (an app that sets its own values keeps them). Non-Go apps ignore the variables.

Why now

A developer evaluating CARLOS for a multi-tenant storefront raised per-process Go overhead as a reason to prefer one shared process. This change is small, invisible to apps that already tune themselves, and lowers resident memory for every awake Go instance on shared boxes.

Related: #892 (S1 CARLOS_MEMORY_MIB), #323, #946, #524 (memory-aware wake admission).

Correction to the GOMAXPROCS half (my mistake in the original post). On the nested layout, the default on ready boxes (flagship showed 128 nested, 4 flat), instancectl sets CPUQuota= from the size on the instance's slice (internal/instancectl/server.go:854). Small 50%, standard 100%, large 200%, xlarge 400%. Go 1.25 and later read the cgroup cpu.max and default GOMAXPROCS to the rounded-up limit, never below 2. So Go apps on nested instances already get GOMAXPROCS of 2 (small to large) or 4 (xlarge). The flat layout writes only memory, so it has no CPU quota, but flat is the exception.

So the scope here is GOMEMLIMIT only. Go never derives it from the cgroup. Building now: when the platform knows the instance's memory limit, it sets GOMEMLIMIT from it in the child's environment unless the app's own env already sets it.

Built on instance-gomemlimit-991: GOMEMLIMIT = 80% of the memory.max applied to that start, on the flat child and the nested unit, unless the app sets it. Astra round 1 READY AFTER FIXES; fixes check READY; census approved (findings in docs/superpowers/reviews/CODEX-FINDINGS-INSTANCE-GOMEMLIMIT-991-*.md).

Follow-ups, not in this branch:

  • Claim-backed binary launches (claim_snapshot.go) don't get the default. Claims are shelved; pick this up if claims switch on.
  • Sidecar members (StartWorkerStaged, startHAWorker) run alone in their own cgroup with a known tier and could take the same default.
  • P3: in the flat PID-move fallback, a child whose move into the group fails keeps the default.

Merged as ded2dab (Paul's go). Its subject on main is the bare branch name because the -message file didn't reach the merge command (my error); main isn't rewritten for it. The intended message:

> activator: set GOMEMLIMIT on binary instances from their size (#991) > > A native binary instance whose memory cap was applied gets GOMEMLIMIT at 80% of its memory.max (flat child and nested unit), unless the app's config or <host>.env sets it. Uncapped fallbacks, rootfs, seeds, carlos run and gVisor are unchanged. Astra R1 READY AFTER FIXES, fixes check READY, census approved. Claim-backed launches are a follow-up.

Not released: it reaches boxes with the next release. CI flakes met on the way: #995 (new) and #880.