#!/usr/bin/env bash # How wide the `rust` job may build and test ON THIS MACHINE. # # bash scripts/ci/rust-job-sizing.sh # # Writes `CARGO_BUILD_JOBS` and `NEXTEST_TEST_THREADS` to `$GITHUB_ENV`, and # prints what it decided and why. Run by `verify-main.yml`'s `rust` job right # after the toolchain step, so every cargo step after it is sized. # # WHY IT IS A STEP AND NOT A JOB-LEVEL `env:` ANY MORE. `rust` used to name one # machine (the Linux CI box) and two constants were the whole answer. # Since it became a `cotton-rust` job it can land on the Linux host OR on a Mac, # and the two are not alike in the way that matters: the Linux host is 8 cores / # 16 threads with 125 GB, and a Mac is 8-12 cores with SIXTEEN, running a # desktop and Xcode besides. One pair of numbers cannot be right for both, and # the wrong half is not a slow build — it is a 16 GB laptop swapping. # # THE MEMORY MODEL, and it is the binding constraint on a Mac, not the cores. # A concurrent `rustc` on this workspace peaks around 2-3 GB (cotton-server and # cotton-tools are the large ones, and the link is the peak); a concurrent test # PROCESS is far smaller, a few hundred MB, because nextest runs each test in # its own short-lived process. So: # # reserve 8 GB — macOS, Xcode's daemons, the app, the page cache # a cargo/sccache workload actually lives on # per build job 3 GB # per test thread 1.5 GB # # and both are capped by the core count as well, because memory arithmetic that # outruns the CPU only buys context switches. # # 16 GB Mac → 8 GB usable → 2 build jobs, 5 test threads # 24 GB Mac → 16 GB usable → 4 build jobs, 8 test threads (both at their cap) # # The caps are 4 and 8. Not larger on a Mac because the thing that actually # keeps two heavy builds apart there is `/tmp/tinmen-build.lock`, which the # `rust` job takes exactly as `swift` and `ios` do — so the number here sizes # ONE job, and a second waits rather than competing. # # AND ON LINUX THE PAIR IS NO LONGER A CONSTANT — IT IS WHAT THE MACHINE IS # DOING AT THE MOMENT THE STEP RUNS. `3` and `8` were measured against the # lane's WORST case: four `rust` jobs at once, each entitled to a quarter of the # host — and the lane is not always at four. Run 181 is the measurement that # retired them — 16 cores, 125 GB, one `rust` job on the host, and: # # the test profile's build 6m45s at CARGO_BUILD_JOBS=3 # nextest 11,586 tests, 550 s wall, 4,475 test-SECONDS # clippy 1m43s # # 4,475 / 8 = 559, and the wall was 550: the test phase is not waiting on any # one slow test, it is waiting on the eight threads it was given. Both numbers # were sized for the busiest the host gets and then paid for whatever it is # actually doing. # # So read the host instead of assuming it: `nproc`, the ONE-MINUTE load average # (`/proc/loadavg`, which is what the other three lane slots show up in) and # `MemAvailable`. The headroom is `cores - load` — the cores nobody else is # using right now — and: # # CARGO_BUILD_JOBS round(headroom * 0.75) clamped to [3, 12] # NEXTEST_TEST_THREADS round(headroom) clamped to [8, 16] # # Build jobs get three quarters rather than all of it because a rustc is ~2-3 GB # and a link is single-threaded and long: the last quarter buys queueing, not # throughput. Test threads get the whole of it because a nextest process is a # few hundred MB and the phase is embarrassingly parallel — 16 is the host's # hardware threads exactly once. # # THE FLOORS ARE THE OLD CONSTANTS, deliberately. A busy host reads as `3` and # `8` and this step can only ever widen the job, never narrow it: whatever this # arithmetic decides, no run is sized smaller than the pair that was measured to # work with four jobs on the host. The memory belt below is a ceiling on top of # that and cannot push under the floors either — on a 125 GB host it never # binds, and it exists so that this file does not become a swap generator the # day the `cotton-rust` label reaches a machine with less. # # THE ONE CASE THIS READS WRONG, AND WHY IT IS ACCEPTED. A load average is # exponentially weighted over a minute, so it LAGS: two `rust` jobs that start # in the same second both see an idle host and both take sixteen threads. That # is 32 test processes on 16 hardware threads — mild oversubscription, which # costs context switches and not much else; a nextest process is a few hundred # MB and the memory belt below still holds. It is not the failure the Darwin arm # exists to prevent (a 16 GB laptop paging), and the alternative — sizing every # run for a coincidence — is what the constants did and what run 181 measured # the cost of. Anything sharper than the load average has to count the lane's # own live jobs, which is the runner's knowledge and not this script's. # # Env knobs, both for the tests and for a hand run: # COTTON_RUST_SIZING_OS override `uname -s` # COTTON_RUST_SIZING_CORES override the core count # COTTON_RUST_SIZING_MEM_GB override the physical memory, in GB # COTTON_RUST_SIZING_LOAD override the one-minute load average # COTTON_RUST_SIZING_FREE_GB override the available memory, in GB set -euo pipefail GITHUB_ENV="${GITHUB_ENV:-/dev/null}" os="${COTTON_RUST_SIZING_OS:-$(uname -s)}" cores="${COTTON_RUST_SIZING_CORES:-}" if [ -z "$cores" ]; then if [ "$os" = "Darwin" ]; then # PHYSICAL cores, not `logical`: the two differ on Intel and the memory # arithmetic below is about work that actually runs at once. cores="$(sysctl -n hw.physicalcpu 2>/dev/null || echo 4)" else cores="$(nproc 2>/dev/null || echo 4)" fi fi mem_gb="${COTTON_RUST_SIZING_MEM_GB:-}" if [ -z "$mem_gb" ]; then if [ "$os" = "Darwin" ]; then bytes="$(sysctl -n hw.memsize 2>/dev/null || echo 0)" mem_gb=$(( bytes / 1024 / 1024 / 1024 )) else kb="$(awk '/^MemTotal:/ {print $2}' /proc/meminfo 2>/dev/null || echo 0)" mem_gb=$(( kb / 1024 / 1024 )) fi fi # A machine whose memory cannot be read is sized as the smallest one we support # rather than as an unbounded one: guessing high here is the swap. if [ "$mem_gb" -le 0 ]; then echo "rust-sizing: could not read this machine's memory — sizing as 16 GB" >&2 mem_gb=16 fi clamp() { # clamp local v=$1 lo=$2 hi=$3 [ "$v" -lt "$lo" ] && v=$lo [ "$v" -gt "$hi" ] && v=$hi printf '%s' "$v" } # `round(a * b)` in a shell that has no floats. The load average is the only # fractional input here and it is read as a string, so every sum it takes part # in goes through awk and comes back as a whole number. scale() { # scale awk -v v="$1" -v m="$2" 'BEGIN { x = v * m; if (x < 0) x = 0; printf "%d", int(x + 0.5) }' } if [ "$os" = "Darwin" ]; then usable=$(( mem_gb - 8 )) [ "$usable" -lt 2 ] && usable=2 # Integer arithmetic throughout: 1.5 GB a thread is `usable * 2 / 3`. jobs_by_mem=$(( usable / 3 )) threads_by_mem=$(( usable * 2 / 3 )) jobs=$(clamp "$(( jobs_by_mem < cores ? jobs_by_mem : cores ))" 1 4) threads=$(clamp "$(( threads_by_mem < cores ? threads_by_mem : cores ))" 2 8) why="Darwin, ${cores} physical cores, ${mem_gb} GB (${usable} GB after an 8 GB reserve)" else # WHAT THE HOST IS DOING RIGHT NOW — the procedure “Reach and maintain the CI host”, "The Cotton lane # runs the merge gate". The floors (3 and 8) are the pair this used to hand # out unconditionally, so a busy host gets exactly what it got before. load="${COTTON_RUST_SIZING_LOAD:-}" if [ -z "$load" ]; then # A host whose load cannot be read is treated as FULLY loaded, which lands # on the floors: an unreadable machine gets the conservative old answer. load="$(awk '{print $1}' /proc/loadavg 2>/dev/null || true)" [ -z "$load" ] && load="$cores" fi free_gb="${COTTON_RUST_SIZING_FREE_GB:-}" if [ -z "$free_gb" ]; then # MemAvailable, not MemFree: the page cache a cargo build lives on is # "used" and is also available, and MemFree on this host reads near zero. kb="$(awk '/^MemAvailable:/ {print $2}' /proc/meminfo 2>/dev/null || true)" [ -z "$kb" ] && kb=0 free_gb=$(( kb / 1024 / 1024 )) fi headroom="$(awk -v c="$cores" -v l="$load" 'BEGIN { h = c - l; if (h < 0) h = 0; print h }')" jobs_by_cpu="$(scale "$headroom" 0.75)" threads_by_cpu="$(scale "$headroom" 1)" # The belt. 3 GB a concurrent rustc, 1.5 GB a concurrent test process — the # same per-unit numbers the Darwin arm uses, against what is available rather # than against what is installed. It cannot reach below the floors. if [ "$free_gb" -gt 0 ]; then jobs_by_mem=$(( free_gb / 3 )) threads_by_mem=$(( free_gb * 2 / 3 )) [ "$jobs_by_mem" -lt "$jobs_by_cpu" ] && jobs_by_cpu=$jobs_by_mem [ "$threads_by_mem" -lt "$threads_by_cpu" ] && threads_by_cpu=$threads_by_mem fi jobs=$(clamp "$jobs_by_cpu" 3 12) threads=$(clamp "$threads_by_cpu" 8 16) why="${os}, ${cores} cores, load ${load}, ${free_gb} GB available of ${mem_gb} GB — ${headroom} cores of headroom" fi echo "rust-sizing: CARGO_BUILD_JOBS=$jobs NEXTEST_TEST_THREADS=$threads ($why)" { echo "CARGO_BUILD_JOBS=$jobs" echo "NEXTEST_TEST_THREADS=$threads" } >> "$GITHUB_ENV"