Spirivia

Writing / note

Doing the most work for the least compute

Why Spirivia measures CPU-seconds from the cgroup instead of wall time, why background jobs only take CPU nobody wants, and why faster for its own sake isn't the point.

Spirivia does a lot of work in the background. Every recording in the library gets transcribed, every word gets aligned to the moment it was spoken, the voices get separated and the ones I know get named. On my laptop, one 24 minute recording used to cost an hour of CPU: 3600 CPU-seconds, MEASURED.

I want Spirivia to do as much work as possible for as little compute as possible. Faster for its own sake isn’t the point. This note is about what that sentence means in practice, because it changes which number you look at, and that changes every decision after it.

Wall time measures the neighbours

The obvious way to measure a job is to time it. Start, stop, subtract. That is wall time, and on a machine you also use for everything else it is close to useless.

I ran identical code on identical audio and got anything from 0.11 to 4.48 RTF (real-time factor: seconds of work per second of audio). MEASURED on bubble-gum, my Framework 13 with a Ryzen AI chip, under load from other work. Same code, a forty-fold spread. The job did not change. The neighbours did.

So wall time does not tell you what the job cost. It tells you how busy the machine was.

CPU-seconds from the cgroup

The unit I use instead is CPU-seconds charged to the job’s cgroup.

Each processing job runs as its own transient systemd unit, so it has its own cgroup, and the kernel keeps an exact count of CPU time spent by every process inside it. Child processes included, Python interpreters included, whatever the model library spawns included. Nothing escapes the count by forking. The service reads that counter at every stage change and records stage_cpu on the job (PR 44).

This number is honest in a way wall time can’t be. If the machine is busy, the job waits longer, but it does not get charged more. If I optimise something and the CPU-seconds go down, the job really did less work.

The first time I split it by stage, it re-cut the whole plan. One 24 minute record, MEASURED:

stageCPU-s per audio-sshare
ASR (whisperx, CT2 int8)1.11344.8%
align (wav2vec2)1.00040.3%
speaker id0.1998.0%
diarize0.1666.7%

Alignment was 40%. Earlier estimates had put it at 6 to 8%. Without the measurement I would have optimised the wrong thing.

Only take CPU that nobody wants

Measuring is half of it. The other half is not getting in the way.

The jobs used to run with CPUQuota=400% and CPUWeight=20. That looked reasonable and was wrong twice. The weight did nothing, because a weight only competes with siblings in the same parent, and the job slice had none. The quota held four cores against my interactive work when I was busy, and left the rest idle when I wasn’t.

Now the slices are CPUWeight=idle with no quota, the job units get Nice=19 and idle I/O (PR 41). A job takes the cores nobody wants and gives them back at once when something else asks. When I start typing, it yields. When I walk away, it uses the whole machine. The asymmetry lives in what the scheduler sees, not in a mode I have to switch.

Each job also picks its thread count from the cores that were idle over the last second of /proc/stat when it starts, halved on battery.

This has a cost and I’d rather say it: an idle-class job crawls while anything else keeps the cores busy, other background work included. A record may take much longer to process on a busy day. I accept that. The point of a background tool is to stay in the background.

Small measured gains count

Once the unit is fixed, the rule for choosing gets simple. It is written down as ADR 0007, “the measure decides”: when the objective is a measure, the option that measures better wins, however small the margin. Effort is not a measure of compute, so effort is never the reason to reject something.

Two of the fixes that came out of this were small and boring and worth it.

Huge pages. Setting GLIBC_TUNABLES=glibc.malloc.hugetlb=1 on the job units makes glibc’s allocator ask for transparent huge pages. Model inference touches large buffers, and with 4 KiB pages the kernel spends a lot of time on page faults. In a bench of the ASR stage, MEASURED: -23% CPU, page faults down from 20.9 million to 0.2 million, same output (PR 43). One environment variable.

That bench ran ASR on the CPU. ASR has since moved to the GPU (next note), so what huge pages save in today’s jobs is smaller and I haven’t measured it. The variable stays on the job units, because what still runs on the CPU still allocates big buffers.

I did not turn huge pages on system-wide. That would change the whole desktop, and the problem was only in the jobs. The fix stays where the cost is.

Placement. Threads pinned one per fast physical core, instead of letting the scheduler spread them over SMT siblings and the smaller cores (PR 45). On a 5 minute clip, MEASURED: pinned beat unpinned by 15% at 4 threads and by 38% at 8. Eight unpinned threads cost 1.74x the CPU of two pinned ones.

Faster for its own sake isn’t the point

Here is the result that made the principle concrete for me.

Pinning made the job about 11% slower in wall time on an idle machine, MEASURED. It also made it use 13% fewer CPU-seconds. If I were optimising wall time, I’d have thrown it away. I shipped it.

The reason: a batch job on my laptop is not racing anyone. Nobody is waiting on a progress bar for a recording from last Tuesday. What matters is how much of the machine it takes from me, from the battery, from everything else that wants to run. Fewer CPU-seconds is less heat, less fan, less battery, more room for the things I’m actually doing.

The same thing showed up later at a larger scale. When the heavy models moved to the GPU (the next note is about that), the whole pipeline went from 586.6 to 44.2 CPU-seconds on a 5 minute record and from 2471.9 to 127.0 on a 24 minute one, MEASURED. 13x and 19x less CPU. Wall time improved only 1.4x to 1.6x. By the wall clock it looks like a modest win. By what it costs the CPU, it’s most of the cost gone.

Kill rules before the run

Measuring honestly also means deciding what counts as failure before you see the result. Otherwise every number can be argued into a win.

So each experiment had its kill rule written first, and several cheap ideas died on them, MEASURED:

  • Fuller ASR windows saved 9.2% of ASR CPU but changed 13.1% of words. Kill at 5%. Killed.
  • A smaller, faster ASR model used 7x less CPU but its word error rate on Portuguese was 37.3 against 23.1. Kill at +1 point. Killed.
  • Running four single-thread workers in parallel cost 26% to 38% more CPU than one. Killed.
  • Speaker id from 24 windows instead of up to 120 dropped centroid similarity to 0.977, below the 0.99 floor. Killed.

Less compute only counts if the output is the same. A cheaper wrong answer is not a saving.

What to take from this

If you run background work on a machine people use:

  • Measure CPU-seconds from the cgroup, not wall time. Wall time measures the neighbours.
  • Split the measurement by stage before you decide what to optimise. Your guess about where the time goes is probably wrong.
  • Use CPUWeight=idle instead of a quota. A weight only competes with siblings, and a cap wastes the machine when it’s free.
  • Try GLIBC_TUNABLES=glibc.malloc.hugetlb=1 on the units that allocate big buffers, before you touch anything system-wide.
  • Write the kill rule before the run.

The numbers in this note are from docs/research/processing-performance-2026-11.md and ADR 0007 in the Spirivia repository.