diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 67db43da2..5d67ec711 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -259,6 +259,8 @@ jobs: SCCACHE_IGNORE_SERVER_IO_ERROR: "1" steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Size parallel compiles to the job's memory + run: scripts/ci/build-jobs.sh - uses: dtolnay/rust-toolchain@stable - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 with: @@ -450,6 +452,8 @@ jobs: SCCACHE_IGNORE_SERVER_IO_ERROR: "1" steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Size parallel compiles to the job's memory + run: scripts/ci/build-jobs.sh - name: Warm build directory (self-hosted runner) if: runner.environment == 'self-hosted' && !(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) run: scripts/ci/warm-target.sh doc-tests @@ -501,6 +505,8 @@ jobs: SCCACHE_IGNORE_SERVER_IO_ERROR: "1" steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Size parallel compiles to the job's memory + run: scripts/ci/build-jobs.sh - name: Warm build directory (self-hosted runner) if: runner.environment == 'self-hosted' && !(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) run: scripts/ci/warm-target.sh oracle diff --git a/scripts/ci/build-jobs.sh b/scripts/ci/build-jobs.sh new file mode 100755 index 000000000..d1ae12d3f --- /dev/null +++ b/scripts/ci/build-jobs.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Size Cargo's parallel compiles to the memory the job may use, not to the CPUs it sees: inside a +# sandbox (gVisor on the shared untrusted boxes) nproc reports the host's cores while the memory +# limit is the sandbox's, and one rustc per core on the big crates runs out of memory (rustc +# killed, "exit status: 2" with no diagnostic). Sets CARGO_BUILD_JOBS for the rest of the job. +# Usage: build-jobs.sh [GB_PER_JOB] (default 3) +set -euo pipefail +per_job=${1:-3} +kb=$(awk '/MemAvailable/{print $2}' /proc/meminfo) +# A cgroup limit, where there is one, can be lower than what /proc/meminfo reports. +for f in /sys/fs/cgroup/memory.max /sys/fs/cgroup/memory/memory.limit_in_bytes; do + if [ -r "$f" ]; then + limit=$(cat "$f") + if [ "$limit" != max ] && [ "$((limit / 1024))" -lt "$kb" ]; then + kb=$((limit / 1024)) + fi + break + fi +done +cpus=$(nproc) +jobs=$((kb / 1048576 / per_job)) +[ "$jobs" -lt 1 ] && jobs=1 +[ "$jobs" -gt "$cpus" ] && jobs=$cpus +echo "build-jobs: $((kb / 1048576)) GB available, $cpus CPUs -> CARGO_BUILD_JOBS=$jobs" +echo "CARGO_BUILD_JOBS=$jobs" >> "$GITHUB_ENV"