interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / tools / ops / spark-calibrate

98 lines · 5542 bytes Raw Blame History
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.
16set -u
17WR=${SPARK_WINDOWS_ROOT:-$HOME/yardsticks/windows}
18id=${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; }
20held() { [ "$(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; }
21d=$WR/$id; mkdir -p "$d/calibration"
22export SPARK_WINDOW=$id SPARK_WINDOW_DIR=$d
23G=${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
26REPO=${OMEGA_REPO:-$HOME/workspace/omega}; PIN=3bd56731c5517e2a9c8f195955c71e73dc6f9269; SRC=tools/dagsim/memcpy_probe.c
27CFLAGS="-O2 -pthread"
28blob=$(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; }
29P=$HOME/.cache/spark-shared/memcpy_probe-$(echo "$blob $CFLAGS" | sha256sum | cut -c1-12)
30if [ ! -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; }
33fi
34
35t0=$(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.
38gmode=$(cat "$WR/$id/gb10" 2>/dev/null || echo legacy)
39case $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 ;;
48esac
49gwait=$(( $(date +%s) - t0 ))
50held || { echo "spark-calibrate: window $id lapsed (lease ran out or closed) while waiting for the GB10 lock; nothing measured" >&2; exit 1; }
51start=$(date -u +%FT%TZ)
52
53stat_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}
59avail=$(awk '/MemAvailable/{print $2}' /proc/meminfo)
60probe_json=""
61for 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)
70done
71
72# 30 s idle at the wall; receipt name carries the window id so windows never overwrite each other
73spark-receipt "calib-idle-$id" --idle 0 -- sleep 30 > "$d/calibration/idle.out" 2>&1
74r=$(sed -n 's/^receipt: //p' "$d/calibration/idle.out")
75cp "$r" "$d/calibration/idle-receipt.json"
76jv() { sed -n "s/^ *\"$1\": \([^,]*\),\{0,1\}$/\1/p" "$d/calibration/idle-receipt.json"; }
77held && wheld=true || wheld=false
78flock -u 9
79
80cat > "$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}
95EOF
96mv -f "$d/calibration.json.tmp" "$d/calibration.json"
97cat "$d/calibration.json"
98echo "calibration: $d/calibration.json"