aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / tools / ops / spark-calibrate
| 1 | #!/bin/sh |
| 2 | # spark-calibrate: the one calibration file of a measurement window. |
| 3 | # |
| 4 | # Run inside an open window (spark-window run spark-calibrate, or with |
| 5 | # SPARK_WINDOW set). It holds the GPU lock (default /tmp/aien-gb10.lock; |
| 6 | # SPARK_GB10_LOCK overrides it, for tests only (see spark-window:63)), waits |
| 7 | # for it, and prints the holders every 60 s. It measures: |
| 8 | # 1. CPU memory bandwidth with the omega#583 memcpy probe (pinned source |
| 9 | # omega research/cost-sim-583@3bd56731 tools/dagsim/memcpy_probe.c), the |
| 10 | # same call research/583 used: nice -n 10 memcpy_probe 1 K 10, K = 1 and 8. |
| 11 | # Skipped (and said so) when MemAvailable is under 14.5 GiB (12 GiB |
| 12 | # headroom rule plus the probe's 2 GB). |
| 13 | # 2. Idle power at the wall: 30 s of nothing, through spark-receipt. |
| 14 | # Writes ~/yardsticks/windows/<id>/calibration.json (raw CSVs and the idle |
| 15 | # receipt beside it). Every receipt taken later in the window links that file. |
| 16 | set -u |
| 17 | WR=${SPARK_WINDOWS_ROOT:-$HOME/yardsticks/windows} |
| 18 | id=${SPARK_WINDOW:-$(spark-window id)} || { echo "spark-calibrate: no open window; run spark-window open NAME first" >&2; exit 1; } |
| 19 | [ "$(cat "$WR/OPEN" 2>/dev/null)" = "$id" ] && [ -f "$WR/$id/held" ] || { echo "spark-calibrate: window $id is not the open, held window" >&2; exit 1; } |
| 20 | held() { [ "$(cat "$WR/OPEN" 2>/dev/null)" = "$id" ] && [ -f "$WR/$id/held" ] && [ ! -f "$WR/$id/expired" ] && kill -0 "$(cat "$WR/$id/holder.pid" 2>/dev/null)" 2>/dev/null; } |
| 21 | d=$WR/$id; mkdir -p "$d/calibration" |
| 22 | export SPARK_WINDOW=$id SPARK_WINDOW_DIR=$d |
| 23 | G=${SPARK_GB10_LOCK:-/tmp/aien-gb10.lock} # SPARK_GB10_LOCK is for tests only (see spark-window:63) |
| 24 | |
| 25 | # the probe, built once per source blob + flags |
| 26 | REPO=${OMEGA_REPO:-$HOME/workspace/omega}; PIN=3bd56731c5517e2a9c8f195955c71e73dc6f9269; SRC=tools/dagsim/memcpy_probe.c |
| 27 | CFLAGS="-O2 -pthread" |
| 28 | blob=$(git -C "$REPO" rev-parse "$PIN:$SRC" 2>/dev/null) || { echo "spark-calibrate: cannot read $SRC at $PIN in $REPO (git fetch origin research/cost-sim-583)" >&2; exit 1; } |
| 29 | P=$HOME/.cache/spark-shared/memcpy_probe-$(echo "$blob $CFLAGS" | sha256sum | cut -c1-12) |
| 30 | if [ ! -x "$P" ]; then |
| 31 | mkdir -p "${P%/*}" |
| 32 | git -C "$REPO" cat-file -p "$blob" > "$P.c" && cc $CFLAGS -o "$P.tmp.$$" "$P.c" && mv -f "$P.tmp.$$" "$P" || { echo "spark-calibrate: probe build failed" >&2; exit 1; } |
| 33 | fi |
| 34 | |
| 35 | t0=$(date +%s) |
| 36 | # A window opened with --gb10 exclusive|shared already holds the GPU lock for us (spark-window, L154); |
| 37 | # taking it again here would wait on our own window forever. |
| 38 | gmode=$(cat "$WR/$id/gb10" 2>/dev/null || echo legacy) |
| 39 | case $gmode in |
| 40 | exclusive|shared) ;; |
| 41 | *) exec 9>"$G" |
| 42 | # Wait in the queue without stepping out of it (a timed retry loses the place to newer waiters); |
| 43 | # a separate ticker prints who holds the lock every 60 s. |
| 44 | ( exec 9>&-; while sleep 60; do echo "spark-calibrate: waiting $(( $(date +%s) - t0 )) s for the GB10 lock: $(spark-window locks gb10)" >&2; done ) & |
| 45 | tick=$! |
| 46 | flock 9 |
| 47 | kill $tick 2>/dev/null ;; |
| 48 | esac |
| 49 | gwait=$(( $(date +%s) - t0 )) |
| 50 | held || { echo "spark-calibrate: window $id lapsed (lease ran out or closed) while waiting for the GB10 lock; nothing measured" >&2; exit 1; } |
| 51 | start=$(date -u +%FT%TZ) |
| 52 | |
| 53 | stat_csv() { # median/min/max of column 5 (GB/s) in a probe CSV, as JSON |
| 54 | grep -v '^#' "$1" | grep -E '^[0-9]' | cut -d, -f5 | sort -n | awk ' |
| 55 | { v[NR] = $1 } END { if (!NR) { print "null"; exit } |
| 56 | m = (NR % 2) ? v[(NR+1)/2] : (v[NR/2] + v[NR/2+1]) / 2 |
| 57 | printf "{\"reps\": %d, \"GBps_median\": %s, \"GBps_min\": %s, \"GBps_max\": %s}", NR, m, v[1], v[NR] }' |
| 58 | } |
| 59 | avail=$(awk '/MemAvailable/{print $2}' /proc/meminfo) |
| 60 | probe_json="" |
| 61 | for k in 1 8; do |
| 62 | f=$d/calibration/memcpy_k$k.csv |
| 63 | if [ "$avail" -lt 15204352 ]; then |
| 64 | echo "# SKIPPED k=$k: MemAvailable $avail kB under 14.5 GiB" > "$f"; s=null |
| 65 | else |
| 66 | nice -n 10 "$P" 1 $k 10 > "$f" 2>&1; s=$(stat_csv "$f") |
| 67 | fi |
| 68 | probe_json="$probe_json\"k$k\": $s, " |
| 69 | avail=$(awk '/MemAvailable/{print $2}' /proc/meminfo) |
| 70 | done |
| 71 | |
| 72 | # 30 s idle at the wall; receipt name carries the window id so windows never overwrite each other |
| 73 | spark-receipt "calib-idle-$id" --idle 0 -- sleep 30 > "$d/calibration/idle.out" 2>&1 |
| 74 | r=$(sed -n 's/^receipt: //p' "$d/calibration/idle.out") |
| 75 | cp "$r" "$d/calibration/idle-receipt.json" |
| 76 | jv() { sed -n "s/^ *\"$1\": \([^,]*\),\{0,1\}$/\1/p" "$d/calibration/idle-receipt.json"; } |
| 77 | held && wheld=true || wheld=false |
| 78 | flock -u 9 |
| 79 | |
| 80 | cat > "$d/calibration.json.tmp" <<EOF |
| 81 | { |
| 82 | "window_id": "$id", |
| 83 | "start": "$start", |
| 84 | "gb10_lock_waited_s": $gwait, |
| 85 | "gb10_lock_held_by": "$( case $gmode in exclusive|shared) echo "window ($gmode)" ;; *) echo calibrate ;; esac )", |
| 86 | "window_held_throughout": $wheld, |
| 87 | "memcpy_probe": {"source": "omega research/cost-sim-583@$PIN $SRC", "blob": "$blob", "cflags": "$CFLAGS", |
| 88 | "binary_sha256": "$(sha256sum "$P" | cut -c1-64)", "cmd": "nice -n 10 memcpy_probe 1 K 10", |
| 89 | "unit": "GB/s, read+write counted twice, as in M1 block4", ${probe_json%, }}, |
| 90 | "idle": {"seconds": 30, "wall_W_mean": $(jv wall_W_mean), "gpu_W_mean": $(jv gpu_W_mean), "gpu_util_mean": $(jv gpu_util_mean), |
| 91 | "samples": $(jv samples), "load1_start": $(jv load1_start), "receipt": "$r"}, |
| 92 | "files": "calibration/memcpy_k1.csv calibration/memcpy_k8.csv calibration/idle-receipt.json", |
| 93 | "note": "wall W is the whole UPS output (UPS ups.load x 10, 10 W steps, ~2 s), not the Spark alone; the GPU is held idle by the GB10 lock during the 30 s but servers stay resident" |
| 94 | } |
| 95 | EOF |
| 96 | mv -f "$d/calibration.json.tmp" "$d/calibration.json" |
| 97 | cat "$d/calibration.json" |
| 98 | echo "calibration: $d/calibration.json" |