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:
- At 04:52 UTC on 2026-11-05, holders had taken 12 of 15 slots. Eight of the 12 were full lint
runs, each parked inside a section that ran
script/verifythrough the real Kani pool and asked for 20 of its 20 units. They had waited from 200 to 4,069 seconds, at 0% CPU and 20 MB each. #3247, now on main, stops that section from taking the pool, but those lanes still ran the older script. - The 10 Kani units in use (20 GiB reserved) belonged to two proofs whose process trees held 1.13 and 0.81 GiB. Their rows' measured peaks are 5,140 MiB and 410 MiB.
- The host at that moment: load 12.9, 47 GiB available, CPU 29% busy in a three-second sample a minute later. The cap was full; the machine was not.
- A request for all 20 units waits until the pool is empty, so any 5-unit run that fits sooner overtakes it. Smaller runs starved those eight lints, not only blocked them.
- The kernel's own out-of-memory killer ended nine
cbmcprocesses between 2026-11-04 20:32 and 2026-11-05 01:02 UTC, at 18.5 to 51.5 GiB resident, every one a global kill with no cgroup constraint. The user manager's cgroup reached a peak of 52.4 GiB. The pool admitted those runs and nothing stopped them.
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.
- A holder whose worktree is gone, like the orphan lint that kept a slot after its worktree went.
The ledger records the worktree.
--reapends such a holder by writing to the scope'scgroup.killon Linux, which kills the whole tree at once, or by signaling the process group on macOS, when a person runs it.--statusand every waiter report such a holder, and the ledger does not end one on its own. - A holder that runs but does nothing, as the eight lints did. #3244's idle hint names it. With (c) the main cause has no code path, so the ledger reports it and leaves the kill to a person.
(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.
- The ledger. Holders and waiters as
flockfiles, tickets, aging, memory in bytes, every class at 8 GiB, and the old slots honored both ways. Behavior otherwise matches today's. - Requests declared up front. Each heavy script states its class; a waiter with a granted ancestor
is an error;
script/verifysizes its request from #3214's rows. - The Kani pool folded into the ledger, and
--poolretired once no open lane predates step 2. - Observability: the per-class
--status,log.tsvand--report. - Linux scopes: a scope per job, the
memory.statsampler, the machine-local peaks file, and self-correcting weights. - The safety net:
MemoryMax,memory.oom.group, the parent slice at capacity, and the overrun report. On macOS the sampling wrapper and its kill. - Cores in the request, exported as
CARGO_BUILD_JOBSandVERIFY_JOBS. - Capacity file and reaping:
~/.nife/heavy/capacityreplaces #3244's count file, and--reapends an orphan when a person runs it. - Retire
NIFE_HEAVY_SLOTSand the slot files.
Steps 1 and 2 alone remove the hold-and-wait and the starvation. Step 5 brings the throughput gain.
Rulings
- Option A, the ledger, ruled by the architect on 2026-11-05 (UTC).
- 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).
- The ledger reports a holder whose worktree no longer exists and does not end it;
--reapends it when a person runs it. Provisional, ruled by the maintainer on 2026-11-05 (UTC). - The default capacity on macOS is half of physical memory. Provisional, ruled by the maintainer on 2026-11-05 (UTC).
Follow-on
- Outstanding. Steps 2 to 9. Step 1 is
helpers/heavy_ledger.py; the classes, cgroups and cores are absent fromhelpers/heavy_slot.py.
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.