Design: the -release build schedule
Problem
A cold tuios -release build uses 4.2 of its 8 CPUs on average (1040 s CPU in 245.6 s wall). If the idle time is in the backend's schedule, filling it cuts wall time by up to 1.5x without changing any output.
Suspects, in the order they are checked
- A serial phase: the whole-program step reads every module, builds the summaries and the import plan before any unit is optimized. The frontend writes all modules before that.
- Admission: units are admitted by a memory estimate (jobs in flight times a per-job cost against the memory limit, MARMOT_MAX_MEM_MB). With a 4 to 6 GB limit fewer than 8 units may run at once.
- A long tail: a large unit started late keeps one core busy at the end (the largest unit takes 20 s of thread time).
- Thread over-subscription: more threads than CPUs, so per-unit times overlap and a unit's wall time grows.
Step 1: measurement (no output change)
First with what exists: MARMOT_MEMLOG already writes one line per job and per split group (start, wall and thread CPU in ms since the backend began, memory, admitted text). The gap between a job's admission request and its start, the busy jobs over time and the serial gaps between phases come out of it. A cold tuios -release run at the default memory limit and at 4 GB is queued. MARMOT_TIMELINE below is only needed if MEMLOG misses something.
MARMOT_TIMELINE=FILE: the backend appends one JSON line per event, with a monotonic time in microseconds since the process started. The events are phase start and end (read, summaries, plan, imports). Per unit they are admitted, optimizer start and end, codegen start and end, and written, with the unit's input size and the thread id. The driver adds its own phases (load, SSA, MLIR emit per package) to the same file with the same clock origin. The driver passes its start time in the environment. A small script draws busy cores over time and reports the critical path.
Runs: tuios -release cold with the memory limit at the default and at 4 GB. cmd/marmot -release cold. Expected result: a breakdown of the 245 s into serial phases, admission waits and the tail.
Step 2: fixes, chosen by what step 1 shows
- Serial phases: start to optimize a unit when its imports are decided. That is as soon as its own summaries and those of its callees are read. The plan is per destination module. Overlap reading with the front end's emission through the existing job stream.
- Admission: measure the real peak per unit (MARMOT_IRSTATS has the sizes) and calibrate the per-job cost. Admit by the measured cost instead of a fixed one.
- Long tail: order units by expected cost (input instructions), largest first. Split the largest modules further when their expected cost exceeds the average per core.
- Over-subscription: one optimizer thread per CPU the build may use.
Each fix keeps the same units and the same pass pipeline, so objects stay byte-identical. The check is cmp on the perfcmp programs (36 of 36) plus a perfcmp for build time. Splitting a module further changes objects and is gated as an output change.
Risks
- Memory: more units at once raises the peak. The admission change must keep the measured peak under the limit (the host is near its memory limit).
- Determinism: the order of units must not change object contents (it does not today: units are independent and cached by content).
Measures of success
Cold -release wall time of tuios and cmd/marmot (3 runs, bench class), busy cores over time, peak memory, and byte-identical objects.
Review (tier0) and what changes
- Idle cores may be the job's CPU quota or other jobs: read the job unit's cpu.stat (nr_throttled, throttled_usec) and the host load beside MEMLOG. Measure in the bench class with MARMOT_TIER1=0.
- Report CPU time beside wall time for every fix: a fix that buys wall time with more CPU (more threads, more splits) says so.
- Largest first raises the early memory peak when admission is tight. So order by expected cost and admit by measured peak. Log admission waits apart from queue waits.
- Byte-identical output needs a fixed link order. After units are reordered, check that the object order on the link line and in the pc table does not follow completion order.
- Step 1 can show that serial phases dominate (the largest unit is under 10% of the build). Then streaming summaries comes first, and the tail work waits.
- A private cold cache per run. Judge wall time with perfcmp's noise rule, not 3 runs alone.