Skip to content

perf: a runner container skips work its job does not need - #355

Merged
ayagmar merged 12 commits into
mainfrom
perf/container-runtime
Nov 24, 2026
Merged

ayagmar merged 12 commits into
mainfrom
perf/container-runtime

Conversation

@ayagmar

@ayagmar ayagmar commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Three runtime changes to what a runner container does per job, recorded in ADR 0170.

What changed

  • Log capture without forks (observe/capture.rs).
    • Before, the capture script ended each pass with sleep 0.2 and read page numbers through command substitutions. That is at least one process per running container five times a second, over 100 a second with 20 runners.
    • It now waits with read -rt 0.2 on a pipe it holds both ends of, opened once with exec {tick}<> <(:), so the read never hits end of file and never spins.
    • If the pipe cannot be opened, for example without /dev/fd, it picks sleep 0.2 once and stays quiet. It does not fail a read -u on every pass.
    • Output framing and the page/offset resume are unchanged. stat, tail and head still fork for each kept page.
  • The cleanup prunes only what the build cache holds (image/job-cleanup.sh).
    • One call, GET /system/df?type=build-cache (API 1.42, 10 s deadline), lists the records.
    • Build contexts and git checkouts are pruned only when a record of that type exists, counting records shared with images.
    • Layers are pruned to half the budget only when a layer record exists and the unshared records hold at least half the budget. BuildKit's prune uses the same measure (cache/manager.go).
    • When the layer prune is skipped, the cleanup does not walk the store a second time.
    • If the answer is missing, late or malformed, every prune runs as before.
    • A failed du now reports the store as unmeasured, not 0.
    • The start-up cleanup still runs every time. A clean-exit marker was tried in an earlier revision and dropped, because the marker could outlive the cleanup it certified. ADR 0170 explains why.
  • Opt-in tmpfs /tmp (tmp_tmpfs on a profile, off by default).
    • /var/tmp gets a quarter and /tmp the rest, each in whole 64 KiB, so the kernel rounding up to a page never takes the two mounts past the bound.
    • Both are mounted rw,exec,nosuid,nodev,mode=1777 through HostConfig.Tmpfs, which any Docker API version accepts. Docker's tmpfs default is noexec (moby daemon/oci_linux.go; a later exec wins in MergeTmpfsOptions).
    • The value must be at least 1M and needs a min_memory above it. That is where sizing stops lowering memory, so applied advice always passes the config check.
    • A runner carries its bound in its occupant and in the container label runnerd.tmp-tmpfs, which a restart reads. A reload that turns it on, resizes it or turns it off replaces idle runners.
    • _diag and _work are never mounted on tmpfs.
  • No change to seed.sh or BuildKit. cp already defaults to reflink=auto on coreutils 9.x, and its source is an overlay view, so a clone fails with EXDEV. A persistent docker-container builder would conflict with the per-job cleanup, the GC policy and setup-buildx-action. ADR 0170 gives the details.

How it was verified

  • Full gate from CLAUDE.md on the merge with main (c37f0db): fmt, three clippy runs, cargo test --workspace (2317 passed), doc, boundaries, state, wire-compat, docs-commands, machete, typos, taplo, deny, shellcheck -x image/*.sh, the site build, the web demo browser test, and git diff --check.

  • Capture: three tests run the real script under bash over a fake _diag. They check:

    • it follows a page until the page is deleted, resumes mid-page, and never calls sleep;
    • it makes about 5 passes a second when idle;
    • with the pipe open failing, it still makes about 5 passes a second and prints nothing on stderr.

    All three fail on the old scripts.

  • Cleanup: three tests run job-cleanup.sh over a temporary root with stand-in docker, curl and du. They cover which prunes run for each cache listing, including shared-only layer records with unshared cache mounts over half the budget; {}, null, records without sizes and non-JSON answers, which all fall back to every prune; the --max-time deadline; one store walk; and a failed du leaving the report unmeasured.

  • tmpfs:

    • config tests: off by default, 1M minimum, min_memory required, 0 refused;
    • create-body tests: only /tmp and /var/tmp, exec, mode, whole-64-KiB sizes that never add up past the bound;
    • engine tests: the bound reaches the container and its label, and the warm-reload test covers enable, resize and disable, which fails without the fix;
    • a sizing test: auto sizing stops at min_memory and the result validates.
  • scrub.sh on a real tmpfs /tmp and /var/tmp, checked by hand in an unshare -rm namespace: it deleted the job's files and left the file from before the marker, both mounts, and mode 1777.

  • Not run: anything against real Docker or Sysbox, including a tmpfs runner and /system/df?type=build-cache against the image's dockerd. These need a sandbox e2e and rebuilt images.

An opt-in tmpfs /tmp, one Docker cleanup per job, a log capture that does not fork to wait, and why seed.sh's copy and a persistent BuildKit builder stay as they are.
The capture script ended each pass with sleep 0.2 and took page numbers through command substitutions: at least one process per running container five times a second. It now waits with read -t on a pipe it holds both ends of, and sets the page number in a variable. Output and resume protocol are unchanged.
job-cleanup.sh leaves /state/.runnerd-cleaned with its budget once every step succeeded; the entrypoint removes it before the runner starts, and skips the start-up cleanup when the marker is there, its budget is no lower and the Engine holds no container or volume. The cleanup prunes build contexts, git checkouts and layers only when GET /system/df?type=build-cache says the cache holds them, and walks the store once when the layer prune does not run.
tmp_tmpfs = "2G" mounts /tmp and /var/tmp as tmpfs, three quarters and a quarter, rw,exec,nosuid,nodev,mode=1777, through HostConfig.Tmpfs, which any Docker API version takes. Off by default: the pages count against the container's memory. The config refuses 0 and a size not below memory and min_memory. Never _diag or _work.
…ore report fail safe

A marker written by the job-completed hook could outlive the cleanup it certified: a job process can change the store after the hook, an older image never removes the marker, and a crash can leave it behind. The start-up cleanup now always runs. The build-cache probe keeps shared records for the type checks, counts only unshared bytes against --max-used-space as BuildKit does, gives the Engine 10 s, and prunes everything on an answer without a valid list. A store du cannot measure is reported unmeasured instead of 0.
A runner keeps its tmpfs bound in its occupant and the container label runnerd.tmp-tmpfs, which a restart reads; Occupant::outdated compares it with the profile's.
…memory

Two mounts of a page each held more than a tiny bound said, and without a min_memory above it sizing could advise a memory the config then refused.
@ayagmar
ayagmar merged commit 9a40234 into main Nov 24, 2026
14 of 27 checks passed
@ayagmar
ayagmar deleted the perf/container-runtime branch November 24, 2026 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant