Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
292 changes: 156 additions & 136 deletions .github/workflows/publish.yml

Large diffs are not rendered by default.

31 changes: 25 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ FROM ${BASE_IMAGE} AS toolchain

ARG LLVM_VERSION=22

# Exact package versions of the compilers. pace-cc keys every cache entry on the
# compiler it fronts, so a seed warmed against one patch release does not hit
# under another, and an unpinned nightly rebuild would pick up a new release and
# silently re-key every seed (including the ones warmed on a developer machine).
# apt keeps only the newest version of a package in its index: when upstream
# publishes a newer one, the build fails below with "Version ... was not found"
# instead of shipping a different compiler. To bump, read the new versions from
# `docker run --rm <image> sh -c 'apt-get update -q && apt-cache policy clang-22
# gcc-14 gcc-15'`, set them here, publish, and warm the locally warmed projects
# against the new image: the publish job warns about every seed whose toolchain
# label no longer matches the image's.
ARG CLANG_PKG_VERSION="1:22.1.8~++20260714014902+ca7933e47d3a-1~exp1~20260714135019.80"
ARG GCC14_PKG_VERSION="14.2.0-4ubuntu2~24.04.1"
ARG GCC15_PKG_VERSION="15.2.0-14ubuntu1~24~ppa1"

RUN printf 'Acquire::Retries "5";\n' > /etc/apt/apt.conf.d/80-retries \
&& apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
Expand All @@ -35,8 +50,8 @@ RUN printf 'Acquire::Retries "5";\n' > /etc/apt/apt.conf.d/80-retries \
curl \
file \
gawk \
g++-14 \
gcc-14 \
g++-14="${GCC14_PKG_VERSION}" \
gcc-14="${GCC14_PKG_VERSION}" \
git \
gpg \
iproute2 \
Expand Down Expand Up @@ -86,13 +101,17 @@ RUN printf 'Acquire::Retries "5";\n' > /etc/apt/apt.conf.d/80-retries \
&& chmod +x llvm.sh \
&& ./llvm.sh ${LLVM_VERSION} \
&& apt-get install -y --no-install-recommends \
clang-${LLVM_VERSION} \
clang-tidy-${LLVM_VERSION} \
llvm-${LLVM_VERSION} \
clang-${LLVM_VERSION}="${CLANG_PKG_VERSION}" \
clang-tidy-${LLVM_VERSION}="${CLANG_PKG_VERSION}" \
llvm-${LLVM_VERSION}="${CLANG_PKG_VERSION}" \
&& rm llvm.sh \
&& add-apt-repository -y ppa:ubuntu-toolchain-r/test \
&& apt-get update \
&& apt-get install -y --no-install-recommends gcc-15 g++-15 \
&& apt-get install -y --no-install-recommends gcc-15="${GCC15_PKG_VERSION}" g++-15="${GCC15_PKG_VERSION}" \
&& for pkg in clang-${LLVM_VERSION}:"${CLANG_PKG_VERSION}" gcc-14:"${GCC14_PKG_VERSION}" gcc-15:"${GCC15_PKG_VERSION}"; do \
test "$(dpkg-query -W -f='${Version}' "${pkg%%:*}")" = "${pkg#*:}" \
|| { echo "${pkg%%:*} is not the pinned ${pkg#*:}"; exit 1; }; \
done \
&& test -d /usr/lib/gcc/x86_64-linux-gnu/14 \
&& for driver in clang clang++; do \
printf -- '--gcc-install-dir=/usr/lib/gcc/x86_64-linux-gnu/14\n' \
Expand Down
158 changes: 137 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,63 @@ authenticated `pace-kv` alone does not distinguish cache readers from writers.

`publish.yml` runs in four stages:

1. **resolve** pins an immutable Ubuntu base-image digest and the exact `pace`
`master` commit, and reads the warm-up groups from `warm-targets.json`.
1. **resolve** pins an immutable Ubuntu base-image digest and the head commit of
every project warmed in CI (`pace` is also the commit `pace-cc` and `pace-kv`
are built from), and reads the warm-up groups from `warm-targets.json`.
2. **build** builds the toolchain image with no seed, smoke-tests it, and pushes
it as `<sha>-base`. Pull requests stop here.
3. **warm** runs one job per group in parallel, inside the base image. Each job
starts from the previously published seed for its group, so only translation
units whose inputs changed are recompiled. Each job pushes its result as a
candidate seed image, `cpp-ci-seed-pace-<group>:<sha>`. A store that came
out byte-identical to the previous seed re-tags the previous seed image, so
3. **warm** runs one job per group of each project with `"warm": "ci"`, in
parallel, inside the base image. Each job starts from the previously
published seed for its group, so only translation units whose inputs changed
are recompiled. Each job pushes its result as a candidate seed image,
`cpp-ci-seed-<project>-<group>:<sha>`. A store that came out byte-identical
to the previous seed gives an image that is the previous one plus labels, so
its layer is not uploaded again.
4. **publish** smoke-tests the candidate seeds with the base image's `pace-kv`,
adds them to `<sha>-base` as one layer per group, and pushes `<sha>`
directly from buildkit, so layers the registry already holds are skipped. On
`master` it also moves `latest` and each group's seed to the new candidates.
4. **publish** collects the seeds of every group of every project: the
candidates of the warm jobs, and the latest `cpp-ci-seed-<project>-<group>`
of each `"warm": "local"` project. It smoke-tests them with the base image's
`pace-kv`, adds them to `<sha>-base` as one layer per group, and pushes
`<sha>` directly from buildkit, so layers the registry already holds are
skipped. On `master` it also moves `latest` and each CI-warmed group's seed
to the new candidates.

If any warm-up group fails, `publish` does not run, and `latest` stays on the
previous image with its previous seeds. Consumers pick up each new image
automatically, so no changes are needed in their repositories.

A locally warmed seed is optional to the publish. A group with no seed pushed
yet is left out with a warning, a seed this image's `pace-kv` cannot open is
dropped with a warning, and a seed warmed under another toolchain than the
image's (see [Toolchain pinning](#toolchain-pinning)) is assembled with a
warning, because it will not hit.

## Seed warmer

The warmer builds each configured project's presets with the compile cache
enabled, into single-file `pace-kv` stores. Each store is one group of presets,
and the stores are baked into the image as `/opt/pace/seed-cache/<project>/<group>.cow`.
A consumer copies the store for the group it builds.

**Status:** `pace` is the first project onboarded. The other three are not yet
onboarded. The blockers are tracked upstream:
**Status:** `pace` (warmed in CI) and `morph` (warmed locally) are onboarded. The
other two are not yet. The blockers are tracked upstream:

- `Lightweight`: a test compile definition embeds an absolute build-tree path,
so cache entries are not shared across checkouts. See
[Lightweight#693](https://github.com/LASTRADA-Software/Lightweight/issues/693).
- `formula-cpp`: no cache integration. See
[formula-cpp#36](https://github.com/LASTRADA-Software/formula-cpp/issues/36).
- `morph`: not yet measured.

### Manifest

[`warm-targets.json`](warm-targets.json) lists the groups. `pace` has three:
[`warm-targets.json`](warm-targets.json) lists the projects. A project declares
its `repo`, its `ci_workspace` (the path its own CI checks it out at, which the
warm-up builds at), its `cpm_source_cache` (relative to the checkout, as its CI
keeps it), and how it is warmed: `"warm": "ci"` runs in `publish.yml`'s warm
jobs, `"warm": "local"` is warmed by [`scripts/warm-local.sh`](scripts/warm-local.sh)
on a developer machine. Seed images are named
`ghcr.io/lastrada-software/cpp-ci-seed-<project>-<group>`.

`pace` has four groups:

| Group | Presets |
|---|---|
Expand All @@ -75,26 +93,48 @@ key of every translation unit it reaches (`PACE_ENABLE_TLS` adds a PUBLIC define
to the `Pace` library, so it reaches all of them), and a seed warmed without it
never hits.

`morph` has three. Its presets are the ones morph's `ci.yml` builds on Linux,
with the flags of that job's `cmake --preset` line (minus the self-hosted
runner's `PACE_AUTO_INSTALL` flag):

| Group | Presets | `ci.yml` job |
|---|---|---|
| `clang` | clang-debug, clang-release, clang-shared-debug | `linux-compilers` |
| `gcc` | gcc-release | `linux-compilers` |
| `sanitizers` | clang-asan, clang-ubsan, clang-tsan | `linux-sanitizers` |

`gcc-release` is the one preset that differs from its job: `ci.yml` makes GCC 15
the default `gcc` with `update-alternatives`, which this image does not (its
`gcc` is GCC 14), so the preset selects `gcc-15`/`g++-15` by flag. A job that
runs in this image must pass the same flag.

Coverage presets are excluded, because `pace-cc` binds coverage objects to the
checkout root and they never hit from another checkout. Windows presets are out
of scope.

### What a group job does

[`scripts/warm-project.sh`](scripts/warm-project.sh) runs inside the base image:
[`scripts/run-warm-container.sh`](scripts/run-warm-container.sh) starts the
container, for the CI warm jobs and for `warm-local.sh` alike: the checkout is
mounted at the project's `ci_workspace`, the preset list is written from the
manifest, and the environment is the same. It runs
[`scripts/warm-project.sh`](scripts/warm-project.sh) inside the base image:

- starts a local `pace-kv` on a single-file store, with an on-disk cap of
`WARM_MAX_DISK` (default `4g`), which evicts least-recently-used entries;
- if the previous seed exists, starts from a copy of it. A store the new
`pace-kv` refuses (for example after a storage format change) is discarded,
and the group starts empty;
- for each preset, configures with `-DUSE_COMPILER_CACHE=ON -DENABLE_TIDY=OFF`
- for each preset, removes the preset's build directory (a reused checkout must
not carry the cache variables of an earlier configure), then configures with `-DUSE_COMPILER_CACHE=ON -DENABLE_TIDY=OFF`
plus the preset's `configure_args`, and builds it. clang-tidy is off because
it re-runs on every file, even on a cache hit, and adds nothing to the store.
It doesn't change the compile command, so the seeded entries still hit for
consumers that build with tidy on;
- fails fast: a failing configure or build stops the group, and a configure that
disables the cache fails it too;
- fails fast: a failing configure or build stops the group, a configure that
disables the cache fails it too, and so does one whose output matches the
project's `forbid_configure_pattern` (for `pace`, the fallback version
`0.0.0-unknown`, which means `git describe` found nothing);
- fails if `pace-cc` records no statistics, so an uncached warm-up is never
published;
- stops `pace-kv` with SIGTERM, so batched writes are flushed, and reopens the
Expand All @@ -109,6 +149,80 @@ checkout path, and CMake names the object files of zstd, lz4 and catch2's main b
an absolute path when CPM's cache is outside the source tree. Warmed at any other
path, those 43 gcc-release units miss on every CI run.

### Local warm-up

`morph` does not fit a hosted runner (16 GB, 4 vCPU, 330 minutes), so a developer
machine warms it and pushes the seeds. The publish workflow assembles whatever
was pushed last.

```sh
scripts/warm-local.sh [--jobs N] [--ref REF] [--image IMAGE] [--push] morph [group...]
```

It checks the project out at `--ref` (default: the remote's `HEAD`) under
`~/.cache/cpp-ci-warm/<project>/src` (`--checkout-dir` moves it), pulls the image
(`cpp-ci:latest`, or `--image`) and each group's previous seed, and runs
`run-warm-container.sh` with the checkout bind-mounted at the project's
`ci_workspace` and the container running as the calling user. A group's seed
image is built with
[`scripts/build-seed-image.sh`](scripts/build-seed-image.sh), which labels it
with the toolchain fingerprint, the digest of the image it was warmed against and
the project commit.

All groups are warmed and built before anything is pushed, so a failing group
leaves the registry as it was. Only with `--push` does it push each seed as
`:latest` and `:<project-commit>`; without it, it builds the images locally and
prints what it would push. The previous seed is fetched strictly: an
authentication or network failure stops the run, and only "manifest unknown"
means there is none yet.

Pushing needs a prior `docker login ghcr.io` with a token that may write
packages in the organization.

**Nightly.** [`local/systemd/`](local/systemd/) holds a user service and timer
that run `warm-local.sh morph --push` at 00:30 UTC, ahead of the 02:23 UTC
publish, with `Persistent=true` so a night the machine was off is made up. To
install:

```sh
mkdir -p ~/.config/systemd/user
cp local/systemd/cpp-ci-warm-morph.{service,timer} ~/.config/systemd/user/
# If this clone is not at ~/repo/cpp-ci-image, point ExecStart at it:
systemctl --user edit cpp-ci-warm-morph.service
systemctl --user daemon-reload
systemctl --user enable --now cpp-ci-warm-morph.timer
loginctl enable-linger "$USER" # run while logged out
```

`systemctl --user list-timers` shows the next run and `journalctl --user -u
cpp-ci-warm-morph` its output.

**Using the seed from morph's CI.** A morph job uses a seed when it runs in
`ghcr.io/lastrada-software/cpp-ci:latest`, needs `packages: read`, selects the
group's store (`seed: clang|gcc|sanitizers`, as pace's `build.yml` does), leaves
the compiler launcher unset (a `-DCMAKE_*_COMPILER_LAUNCHER=sccache` flag
bypasses `pace-cc`), checks out at `/home/runner/work/morph/morph`, and passes
the same configure flags as the manifest lists for the preset.

### Toolchain pinning

`pace-cc` keys each cache entry on the compiler it fronts, so a seed only hits
under the exact compilers it was warmed with. The `Dockerfile` therefore pins
`clang-22` (with `clang-tidy-22` and `llvm-22`), `gcc-14` and `gcc-15` to exact
package versions (`CLANG_PKG_VERSION`, `GCC14_PKG_VERSION`, `GCC15_PKG_VERSION`)
and fails the build if the installed version differs. clang comes from
`apt.llvm.org`, `gcc-14` from Ubuntu noble, and `gcc-15` from the
`ubuntu-toolchain-r/test` PPA; each keeps only its newest version in its index,
so a pinned build fails once upstream publishes a newer one, and the last good
image stays `latest` until someone bumps the pin.

To bump: set the new versions in the `Dockerfile`, let `publish` build and
publish the image, then warm the locally warmed projects again. Until then the
publish step warns, for every seed whose `org.lastrada.seed.toolchain` label
(the hash printed by [`scripts/toolchain-fingerprint.sh`](scripts/toolchain-fingerprint.sh),
over `pace-cc --print-toolchain-fingerprint` of each compiler) differs from the
image's, that the seed will not hit.

### Decisions

- **Parallel groups, one store each.** `pace-kv` cannot merge stores, so each
Expand All @@ -127,8 +241,10 @@ path, those 43 gcc-release units miss on every CI run.
- **Groups stay under the job limit.** The clang group ran for 5 h 30 min in
run 42095583072 and was cancelled by the 330-minute `timeout-minutes`, which
skipped `publish`. Its gate and tracy presets moved to `clang-extra`.
- **Warm-up runs inside `publish.yml`,** not on a separate schedule. The seed is
produced by the same `pace-cc`, `pace-kv`, and compilers it is baked beside.
- **CI-warmed warm-ups run inside `publish.yml`,** not on a separate schedule.
The seed is produced by the same `pace-cc`, `pace-kv`, and compilers it is
baked beside. A locally warmed seed cannot be, which is what the toolchain
label and the pins are for.
Cache keys include the compiler banner, and `pace-kv` refuses stores written
with a different storage format.
- **Credentials.** The warm-up uses a local `pace-kv`, so it needs no
Expand Down
14 changes: 14 additions & 0 deletions local/systemd/cpp-ci-warm-morph.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=Warm morph's compile-cache seed and push it to the cpp-ci seed registry
Wants=network-online.target docker.service
After=network-online.target docker.service

[Service]
Type=oneshot
# Assumes this repository is cloned at ~/repo/cpp-ci-image; point ExecStart at
# the real clone with `systemctl --user edit cpp-ci-warm-morph.service`.
ExecStart=%h/repo/cpp-ci-image/scripts/warm-local.sh morph --push
# A warm-up that starts from scratch takes hours.
TimeoutStartSec=infinity
Nice=10
IOSchedulingClass=idle
12 changes: 12 additions & 0 deletions local/systemd/cpp-ci-warm-morph.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=Nightly warm-up of morph's compile-cache seed

[Timer]
# The publish workflow runs at 02:23 UTC and assembles the latest pushed morph
# seeds into the image; this finishes well before when it takes under two hours.
OnCalendar=*-*-* 00:30:00 UTC
# A night the machine was off is made up at the next boot or login.
Persistent=true

[Install]
WantedBy=timers.target
56 changes: 56 additions & 0 deletions scripts/build-seed-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
# Build the seed image for one group's store: a single-layer image holding
# /cache.cow, labelled with the toolchain it was warmed under.
#
# Usage:
# build-seed-image.sh --tag NAME:TAG --store FILE --base-image IMAGE \
# [--previous FILE --previous-image NAME:TAG] [--project-ref SHA]
#
# When --previous is byte-identical to --store, the image is the previous seed
# image plus new labels, so its layer digest stays the same and nothing is
# uploaded again for it. LABEL adds no filesystem layer.
#
# Labels:
# org.lastrada.seed.toolchain toolchain-fingerprint.sh's hash for --base-image
# org.lastrada.seed.base-image the digest (else the id) of --base-image
# org.lastrada.seed.project-ref --project-ref, when given
set -euo pipefail

HERE="$(cd "$(dirname "$0")" && pwd)"
tag="" store="" base="" previous="" previous_image="" ref=""
while [ $# -gt 0 ]; do
case "$1" in
--tag) tag="$2" ;;
--store) store="$2" ;;
--base-image) base="$2" ;;
--previous) previous="$2" ;;
--previous-image) previous_image="$2" ;;
--project-ref) ref="$2" ;;
*) echo "unknown option: $1" >&2; exit 2 ;;
esac
shift 2
done
for v in tag store base; do
[ -n "${!v}" ] || { echo "--${v//_/-} is required" >&2; exit 2; }
done

listing="$("$HERE/toolchain-fingerprint.sh" "$base")"
printf '%s\n' "$listing" >&2
fingerprint="$(sed -n 's/^toolchain-sha256: //p' <<<"$listing")"
test -n "$fingerprint"
base_id="$(docker image inspect --format '{{if .RepoDigests}}{{index .RepoDigests 0}}{{else}}{{.Id}}{{end}}' "$base")"

ctx="$(mktemp -d)"
trap 'rm -rf "$ctx"' EXIT
labels="LABEL org.lastrada.seed.toolchain=\"$fingerprint\" org.lastrada.seed.base-image=\"$base_id\""
if [ -n "$ref" ]; then
labels+=" org.lastrada.seed.project-ref=\"$ref\""
fi
if [ -n "$previous" ] && [ -n "$previous_image" ] && cmp -s "$previous" "$store"; then
echo "store unchanged; relabelling the previous seed"
printf 'FROM %s\n%s\n' "$previous_image" "$labels" > "$ctx/Dockerfile"
else
cp "$store" "$ctx/cache.cow"
printf 'FROM scratch\nCOPY cache.cow /cache.cow\n%s\n' "$labels" > "$ctx/Dockerfile"
fi
docker build -t "$tag" "$ctx"
27 changes: 27 additions & 0 deletions scripts/fetch-previous-seed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
# Extract the store from a published seed image.
#
# Usage: fetch-previous-seed.sh [--strict] SEED_IMAGE DEST
#
# Exit status 0 with DEST written: found. Exit status 1 with nothing written: no
# such image, so the warm-up starts empty. By default any pull failure counts as
# "no such image". With --strict only a registry answer that says the image does
# not exist does; an authentication or network failure fails the script (exit
# 2), so a warm-up that would silently start from nothing stops instead.
set -euo pipefail

strict=""
if [ "${1:-}" = "--strict" ]; then strict=1; shift; fi
image="$1" dest="$2"

if ! err="$(docker pull "$image" 2>&1 >/dev/null)"; then
if [ -n "$strict" ] && ! grep -qiE 'manifest unknown|not found|name unknown' <<<"$err"; then
echo "cannot pull $image: $err" >&2
exit 2
fi
exit 1
fi
cid="$(docker create "$image" /cache-copy)"
docker cp "$cid:/cache.cow" "$dest"
docker rm "$cid" >/dev/null
echo "found previous seed $image: $(stat -c %s "$dest") bytes"
Loading
Loading