Skip to content
pi-c Artifact [5d0bf8ffd7]
Login

Artifact [5d0bf8ffd7]

Artifact 5d0bf8ffd704ba79e838ff8f468716b5425fc2e618f87607f7bd691c6b63cd17:


#!/bin/sh
# S0 informational benchmark driver (build orchestration only).
#
# Builds bench/native.c at O2 and O3 (required) plus an optional LTO candidate,
# runs bench/measure.c against each, and hands the recorded metadata to the
# report-only summarizer in tools/benchmark.py. Measurement, timing, child
# ownership, and bounds live in bench/measure.c; no timing happens in Python.
#
# Exit codes: 0 PASS, 1 FAIL, 2 GATED (no usable compiler).
usage() {
    cat >&2 <<'EOF'
usage: benchmark.sh [--out DIR] [--cc PATH] [--samples N] [--iterations N]
                    [--deadline SECONDS]
EOF
    exit 2
}

ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
OUT="$ROOT/build/qualification/bench"
CC_ARG=""
SAMPLES=20
ITERATIONS=2000
DEADLINE=300
MAX_SAMPLES=200
MAX_ITERATIONS=100000000
# Must not exceed what build/qualification/run-bounded accepts (610s).
MAX_DEADLINE=600

while [ $# -gt 0 ]; do
    case "$1" in
        --out) [ $# -ge 2 ] || usage; OUT=$2; shift 2 ;;
        --cc) [ $# -ge 2 ] || usage; CC_ARG=$2; shift 2 ;;
        --samples) [ $# -ge 2 ] || usage; SAMPLES=$2; shift 2 ;;
        --iterations) [ $# -ge 2 ] || usage; ITERATIONS=$2; shift 2 ;;
        --deadline) [ $# -ge 2 ] || usage; DEADLINE=$2; shift 2 ;;
        -h|--help) usage ;;
        *) usage ;;
    esac
done

# Strip leading zeros: shell arithmetic reads 008 as invalid octal, so a
# normalised decimal is required before any $(( )) use.
normalize_decimal() {
    value=$1
    while :; do
        case "$value" in 0?*) value=${value#0} ;; *) break ;; esac
    done
    printf '%s\n' "${value:-0}"
}

# The benchmark contract requires at least 20 samples per measurement.
MIN_SAMPLES=20
for pair in "SAMPLES:$SAMPLES:$MIN_SAMPLES:$MAX_SAMPLES" \
            "ITERATIONS:$ITERATIONS:1:$MAX_ITERATIONS" \
            "DEADLINE:$DEADLINE:1:$MAX_DEADLINE"; do
    label=${pair%%:*}; rest=${pair#*:}
    value=${rest%%:*}; rest=${rest#*:}
    low=${rest%%:*}; high=${rest#*:}
    case "$value" in ''|*[!0-9]*) usage ;; esac
    value=$(normalize_decimal "$value")
    case "$value" in ''|*[!0-9]*) usage ;; esac
    [ "$value" -ge "$low" ] && [ "$value" -le "$high" ] || {
        echo "benchmark: $label must be $low..$high" >&2
        exit 2
    }
    case "$label" in
        SAMPLES) SAMPLES=$value ;;
        ITERATIONS) ITERATIONS=$value ;;
        DEADLINE) DEADLINE=$value ;;
    esac
done

# Each child is owned by the shared bounded-command helper: it sets the group,
# enforces the deadline, and reaps descendants on every exit path. The binary is
# retained beside the other qualification artifacts so evidence can hash it.
BOUNDED="$ROOT/build/qualification/run-bounded"
bounded() {
    limit=$1
    shift
    "$BOUNDED" "$limit" -- "$@"
}

ensure_bounded() {
    mkdir -p "$(dirname "$BOUNDED")"
    if [ -x "$BOUNDED" ] && [ "$ROOT/tools/run-bounded.c" -ot "$BOUNDED" ]; then
        return 0
    fi
    "$CC" -std=c11 -O2 "$ROOT/tools/run-bounded.c" -o "$BOUNDED" >&2
}

WARN="-std=c11 -Wall -Wextra -Wpedantic -Werror -Wshadow -Wconversion \
-Wstrict-prototypes -Wmissing-prototypes -Wvla -Wcast-qual"

sha256() {
    if command -v sha256sum >/dev/null 2>&1; then
        sha256sum "$1" | cut -d' ' -f1
    else
        shasum -a 256 "$1" | cut -d' ' -f1
    fi
}

mkdir -p "$OUT"
rm -f "$OUT/results.json" "$OUT/report.csv" "$OUT/metadata.txt"

CC=""
if [ -n "$CC_ARG" ]; then
    [ -x "$CC_ARG" ] && CC=$CC_ARG
elif command -v clang >/dev/null 2>&1; then
    CC=$(command -v clang)
elif command -v cc >/dev/null 2>&1; then
    CC=$(command -v cc)
fi

META="$OUT/metadata.txt"
if [ -z "$CC" ]; then
    {
        printf 'samples=%s\niterations=%s\ndeadline=%s\n' "$SAMPLES" "$ITERATIONS" "$DEADLINE"
        printf 'compiler=%s\ncompiler_available=0\n' "${CC_ARG:-none}"
    } > "$META"
    python3 -I "$ROOT/tools/benchmark.py" --metadata "$META" \
        --report-json "$OUT/results.json" --report-csv "$OUT/report.csv"
    exit 2
fi

ensure_bounded || { echo "benchmark: cannot build the bounded-command helper" >&2; exit 1; }

SOURCE="$ROOT/bench/native.c"
MEASURE_SRC="$ROOT/bench/measure.c"
MEASURE="$OUT/measure"
SOURCE_SHA=$(sha256 "$SOURCE")
# Inner measure deadline plus a bounded grace for the outer watchdog.
OUTER_LIMIT=$((DEADLINE + 10))
if [ "$OUTER_LIMIT" -gt 610 ]; then
    OUTER_LIMIT=610
fi

# shellcheck disable=SC2086
if ! bounded 120 "$CC" $WARN -O2 "$MEASURE_SRC" -o "$MEASURE" \
        >"$OUT/measure_build.log" 2>&1; then
    echo "benchmark: failed to build measure.c" >&2
    exit 1
fi

{
    printf 'samples=%s\niterations=%s\ndeadline=%s\n' "$SAMPLES" "$ITERATIONS" "$DEADLINE"
    printf 'source=%s\nsource_sha256=%s\n' "$SOURCE" "$SOURCE_SHA"
    printf 'compiler=%s\n' "$CC"
    printf 'compiler_version=%s\n' "$("$CC" --version 2>/dev/null | head -n 1)"
    printf 'host_platform=%s\n' "$(uname -s)"
    printf 'host_machine=%s\n' "$(uname -m)"
    printf 'compiler_available=1\n'
} > "$META"

run_candidate() { # run_candidate NAME REQUIRED FLAGS...
    name=$1
    required=$2
    shift 2
    binary="$OUT/$name"
    raw="$OUT/$name.measure.json"
    log="$OUT/$name.build.log"
    rm -f "$binary" "$raw"
    # shellcheck disable=SC2086
    if ! bounded 120 "$CC" $WARN "$@" "$SOURCE" -o "$binary" >"$log" 2>&1; then
        {
            printf 'candidate_begin=%s\n' "$name"
            printf 'required=%s\nflags=%s\n' "$required" "$*"
            printf 'candidate_status=failed\nreason=compile failed\n'
            printf 'candidate_end\n'
        } >> "$META"
        return
    fi
    # The outer watchdog allows a brief grace past the inner measure deadline,
    # so measure.c can write its own failure JSON before we classify by status.
    measure_exit=0
    bounded "$OUTER_LIMIT" "$MEASURE" --binary "$binary" --out "$raw" \
        --samples "$SAMPLES" --iterations "$ITERATIONS" --deadline "$DEADLINE" \
        >"$OUT/$name.measure.log" 2>&1 || measure_exit=$?
    case "$measure_exit" in
        0) measurement_status=measured ;;
        124) measurement_status=timed_out ;;
        125|126|127) measurement_status=failed ;;
        *) measurement_status=failed ;;
    esac
    {
        printf 'candidate_begin=%s\n' "$name"
        printf 'required=%s\nflags=%s\n' "$required" "$*"
        printf 'binary=%s\nbinary_sha256=%s\n' "$binary" "$(sha256 "$binary")"
        printf 'source_sha256=%s\n' "$SOURCE_SHA"
        printf 'raw=%s\n' "$raw"
        printf 'measurement_status=%s\nmeasurement_exit=%s\n' \
            "$measurement_status" "$measure_exit"
        printf 'candidate_end\n'
    } >> "$META"
}

run_candidate o2 1 -O2
run_candidate o3 1 -O3
run_candidate lto 0 -O3 -flto

python3 -I "$ROOT/tools/benchmark.py" --metadata "$META" \
    --report-json "$OUT/results.json" --report-csv "$OUT/report.csv"