roadmap PARTIAL

1044. Heavy jobs wait in order for the memory they measured

The lane lane/heavy-job-admission wrote this on 2026-11-05 (UTC). Every file, command and variable name here is provisional.

Reuse: flock(2) liveness and the ancestor walk from helpers/heavy_slot.py, #3214's measured Kani rows, and systemd's user scopes for measuring and capping. No existing tool admits by measured memory across independent processes without a server; the prior art appendix lists what this lane searched.

The architect chose option A on 2026-11-05 (UTC); the last section lists that ruling and three provisional ones. The ledger is a format that checkouts of different ages read and write at once, which makes it the one part that behaves like a wire format. The rest is local code.

The design in brief

Replace the slot count and the Kani pool with one ledger of reservations. A heavy job declares its whole need before it starts (memory from a measured per-class weight, and cores), takes all of it in one step or waits, and waits in ticket order. On Linux each admitted job runs in its own memory cgroup, which measures its peak to correct the weight and caps it so a wrong weight kills that job and never the host.

No process then waits while it holds a grant, no two processes need to agree on a count, and a dead job still frees its reservation through flock(2), with no daemon.

Background

Today helpers/heavy_slot.py gives the machine N slots, one per 8 GiB of memory, as flock files under ~/.nife/heavy-slots. A heavy script (lint, test, mutation, ci-build and the others that source helpers/heavy-slot.sh) holds one slot for its whole run. A nested heavy script runs in its parent's slot. NIFE_HEAVY_SLOTS overrides N, read per process, so two processes with different values each count only the slots they know. Lane lane/heavy-slots-per-machine (#3244) adds a machine-local count file and an orphan mark.

Beside the slots sits the Kani pool: physical memory less a 12 GiB reserve, in 2 GiB units. A harness weighs 5 units (10 GiB) by default, and a run takes every harness it runs at once, all or none. Lane lane/kani-memory-inventory (#3214) weighs a harness by its row's measured peak instead. Its pushed table has 34 rows, 28 of them at one unit, and a fingerprint that falls back to the default when the crate changes.

So the tool has two currencies (a count and a memory pool), two acquisitions in sequence (the slot, then the units), and a weight that is a guess for everything except Kani. The two acquisitions in sequence are the hold-and-wait. The guess is why the cap measures jobs and not resources.

What went wrong, measured

The measurements appendix has each command and its output. In short, from the 80-core, 53.9 GiB Linux host with no swap:

The maintainer also reported a run charged 40 GiB that used 12 to 13 GiB; this lane did not see it.

The design

(a) Memory reserved by measured weight

Every heavy job belongs to a class: lint, lint-quick, test, mutation, build, verify and so on. A class weight is the peak memory of one run of that class, rounded up to whole GiB. A Kani run keeps #3214's per-row weights and asks for its harness count times its largest row's weight, plus its compile.

Linux measures the peak from the job's own cgroup. Each admitted job runs as systemd-run --user --scope. This host allows that without root: the user manager delegates the cpu, memory and pids controllers. The kernel's memory.peak counts page cache, which the kernel can drop, so it overstates what a job reading many files needs. This lane's first full lint, a cold build, peaked at 6.63 GiB by memory.peak. A warm one peaked at 1.70 GiB, of which 1.33 GiB was memory the kernel cannot drop. So the wrapper samples memory.stat each second and weighs a job by its peak of anonymous, shared and kernel memory, and the log keeps memory.peak beside it.

The weight corrects itself. A machine-local file under ~/.nife/heavy/ keeps the last ten peaks of each class. The weight is the largest of them plus 10%, so a bigger peak raises it at once and a run of smaller ones lowers it slowly. A committed table holds defaults so a new machine starts somewhere sensible. Kani rows stay in #3214's committed table, because there the code changes the peak, and a fingerprint ties a peak to its code.

macOS has no cgroups. There the wrapper samples the process tree's resident memory once a second with ps, which misses short spikes, and also reads getrusage(RUSAGE_CHILDREN)'s maximum, which covers one process and not the tree. A peak measured on macOS is a lower bound. A macOS host therefore uses the larger of its own peaks and the committed default.

(b) CPU

Each request also names a core count, and the grant exports it as CARGO_BUILD_JOBS and VERIFY_JOBS. Capacity is 1.5 times the core count, because CPU overcommit only slows jobs down while memory overcommit kills them.

This lane tested a shared jobserver and recommends against one for now. Cargo honors a GNU make jobserver in both the pipe and the fifo style, even with -j8 given, and it honors a bare fifo named in MAKEFLAGS with no make process at all. But every client keeps one implicit token, so two cargo runs on a two-token fifo ran four build scripts at once: the bound is tokens plus clients. A cargo killed with SIGKILL mid-build returned none of its two tokens, so a leaked token stays leaked until something refills the fifo. Kani's harness parallelism (-j) is a thread pool in kani-driver, whose binary has no jobserver strings, so it would not take part (inferred, not run).

(c) No hold-and-wait

A job's request is its whole need, decided before it starts. A script states its class at the top; script/verify computes its Kani request from the rows it will prove and VERIFY_JOBS. When script/lint reaches a section that would start a heavier class, that section runs inside lint's grant: it lowers its parallelism to fit, or it fails and says what it needed. The waiting function asserts that no ancestor holds a grant, so a regression fails loudly instead of hanging.

A long job with phases of different size (lint's 88 Python sections, then its cargo half) may release its grant and ask again between phases. It never holds one grant while asking for another.

(d) Order and starvation

A waiter takes a ticket from a counter under a short lock, and holds a flock on its queue file while it waits. Every second, each waiter takes the ledger lock and computes the same answer: the oldest waiter that fits may start; a younger waiter may start if it fits and the oldest has waited under ten minutes. After ten minutes nothing younger starts until the oldest has. The ledger clamps a request above capacity to capacity, as today, so every request can start eventually.

This is backfill by age. Slurm backfills by estimated end time, which nife does not have.

(e) A safety net

On Linux the scope carries MemoryMax at twice the job's weight, MemorySwapMax=0, and memory.oom.group=1, so an overrun kills that whole job and nothing else. A probe scope at 64 MiB killed a 200 MiB allocation and only that process. The wrapper reads memory.events, reports the kill with the class and the cap, and records a peak of twice the old weight. Every heavy scope lives under one slice whose MemoryMax is the machine's capacity, so heavy jobs together cannot take the memory the desktop, the agents and the shells need. If that slice fills, the kernel kills its largest job, usually the one that overran.

systemd-oomd is inactive on this host, and it needs root to enable. The slice cap gives the same protection for heavy jobs without it.

macOS has no equivalent. The wrapper there kills a job's process group when the sampled tree passes twice its weight, best effort at one-second resolution. The capacity default on macOS is half of physical memory, against Linux's physical memory less 12 GiB. Admission there also refuses to start anything while kern.memorystatus_vm_pressure_level reads warning or worse (recalled from Apple's documentation, not tested here). A 16 GiB Mac remains the host with no hard backstop.

(f) Per-machine configuration

Capacity is one machine-local file, ~/.nife/heavy/capacity, with a memory figure and a core figure. A waiter reads it under the ledger lock at every decision, so a change reaches every waiter within a second. The ledger records reservations in bytes, so two processes never disagree about what a number means. An environment variable may shrink one job's own request; it never changes the machine's capacity. #3244's count file is the interim form and becomes this file.

(g) Stale and leaked holders

A dead holder frees its reservation through flock, as now. Two leaks remain.

(h) Observability

--status prints one table per class: weight and its source (measured, default or stale), running count, waiting count, memory reserved, and memory in use from each scope's memory.current. Below it, the queue in ticket order with each waiter's age and request. Each job appends one line to ~/.nife/heavy/log.tsv: ticket, class, lane, request, seconds waited, seconds run, peak and how it ended. --report totals waits by class and day, so a slow queue becomes a number.

(i) Migration, and what stays simple

Lanes cut from an older main keep running the old script for days, so the two must coexist. While the old slots exist, each new holder also takes one old slot file, so old-code lanes still count it. Old holders appear in the new ledger at today's 8 GiB per slot. The Kani pool goes last, once no open lane predates the release that folds it in.

These stay as they are: the flock liveness rule, a nested job running in its parent's grant, CI taking nothing (GITHUB_ACTIONS), --no-cargo taking nothing, and --status as the one front door.

Options

A, chosen: the ledger above, with memory and cores in one request, tickets, cgroup scopes on Linux and the weaker wrapper on macOS. It removes the hold-and-wait, the starvation, the job-not-resource cap and the per-process disagreement on N.

B: keep the count and fix its symptoms. Merge #3247 and #3244, order the slot waiters, and take the Kani units before the slot in script/verify. This costs perhaps a tenth of A. It still caps jobs and not memory, so the 80-core host idles while its count is full and a 51 GiB proof still reaches the global killer. It is the right answer for a host that never runs more than two lanes.

C: a load or pressure gate only. Admit while the one-minute load is under the core count and memory pressure is low. It needs no weights, but load lags, a burst of waiters all see the same quiet moment, and pressure rises only after the memory is gone.

D: a machine-wide jobserver for CPU on top of A or B. It bounds cargo well and Kani's harness pool not at all, it leaks tokens on SIGKILL, and it bounds clients plus tokens. It answers a CPU problem this host does not have.

Would this lane still choose A if A and B cost the same? Yes. The case for A rests on the measurements above, not on effort; B is the cheaper option.

Worked example: the 04:52 UTC state under the ledger

Capacity: 53.9 GiB less 12 GiB, so 41.9 GiB, and 120 cores. Lint weighs 2 GiB, from the warm run's 1.33 GiB. The classes with no measurement weigh 8 GiB, today's slot.

holder today under the ledger
eight lint runs 8 slots, all idle, waiting for 20 Kani units 16 GiB, 2 GiB each, all running
file_allocation_table proof 1 slot and 10 GiB of pool 6 GiB (5,140 MiB rounded up)
one generational_table harness 1 slot and 10 GiB of pool 2 GiB (410 MiB)
script/test, a crate move 2 slots 16 GiB, at the default

The twelve jobs reserve 40 GiB of 41.9, so all of them run, and the eight lints make progress instead of idling. A thirteenth job at the default waits. A full glob proof at four harnesses asks for 40 GiB; it waits for the lints to finish, and after ten minutes nothing younger starts ahead of it. Today the same moment admits three more jobs of any size, and nothing caps what each grows to.

Until a class has a measurement, its default reserves what a slot does today, so the gain arrives with the peaks. A cold lint may well need more than 2 GiB; its first run raises the weight.

The cost, and when a slot count is better

The machinery is perhaps 800 lines of Python and a selftest of the same order, against today's 400. It adds a scope per job on Linux, a machine-local state directory, and a ledger format that must stay readable across checkouts of different ages. Weights can be wrong in both directions, and the safety net turns a weight that is too low into a job killed at its cap. That failure is loud and local, but a lane still loses a run.

A slot count stays the better answer on a host with one or two lanes. It also wins where one job uses most of memory, since a 16 GiB Mac fits two jobs either way. And without cgroups, the macOS wrapper adds little over a count. Option B is cheap insurance for those hosts.

Steps

Each step fits one pull request, in order.

  1. The ledger. Holders and waiters as flock files, tickets, aging, memory in bytes, every class at 8 GiB, and the old slots honored both ways. Behavior otherwise matches today's.
  2. Requests declared up front. Each heavy script states its class; a waiter with a granted ancestor is an error; script/verify sizes its request from #3214's rows.
  3. The Kani pool folded into the ledger, and --pool retired once no open lane predates step 2.
  4. Observability: the per-class --status, log.tsv and --report.
  5. Linux scopes: a scope per job, the memory.stat sampler, the machine-local peaks file, and self-correcting weights.
  6. The safety net: MemoryMax, memory.oom.group, the parent slice at capacity, and the overrun report. On macOS the sampling wrapper and its kill.
  7. Cores in the request, exported as CARGO_BUILD_JOBS and VERIFY_JOBS.
  8. Capacity file and reaping: ~/.nife/heavy/capacity replaces #3244's count file, and --reap ends an orphan when a person runs it.
  9. Retire NIFE_HEAVY_SLOTS and the slot files.

Steps 1 and 2 alone remove the hold-and-wait and the starvation. Step 5 brings the throughput gain.

Rulings

  1. Option A, the ledger, ruled by the architect on 2026-11-05 (UTC).
  2. On Linux a job that reaches twice its weight is ended, and its overrun raises the class weight. Provisional, ruled by the maintainer on 2026-11-05 (UTC).
  3. The ledger reports a holder whose worktree no longer exists and does not end it; --reap ends it when a person runs it. Provisional, ruled by the maintainer on 2026-11-05 (UTC).
  4. The default capacity on macOS is half of physical memory. Provisional, ruled by the maintainer on 2026-11-05 (UTC).

Follow-on

Index row

A fixed count of heavy-job slots lets a job hold one resource while it waits for another. Proposed: one ledger of reservations in ticket order, sized by measured memory and capped by a memory cgroup.