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-window
| 1 | #!/bin/sh |
| 2 | # spark-window: one measurement window at a time on the Spark. |
| 3 | # |
| 4 | # spark-window open NAME [--ledger ID] [--lease MIN] [--gb10 exclusive|shared|none] [--quiet exclusive|none] [--wait] |
| 5 | # Refuses (exit 3) if a window is already open or anyone is in line, and |
| 6 | # lists them. With --wait it takes a ticket and waits its turn instead: |
| 7 | # first come, first served (L113, 2026-10-20: retrying every 15 s lost |
| 8 | # the turn for 4 h). |
| 9 | # Otherwise creates window id <UTC date-time>-<random>, FIRST takes the |
| 10 | # chip lock /tmp/aien-gb10.lock (default exclusive; none skips it), THEN |
| 11 | # the quiet lock exclusive. While it waits for the chip it holds nothing, |
| 12 | # so it never blocks CPU timings (L154, 2026-10-20: a window held the |
| 13 | # quiet lock 25 min while its own GPU job queued for the chip). Once it |
| 14 | # has the chip it KEEPS it while it waits for the quiet lock, and raises a |
| 15 | # gate (/tmp/aien-quiet.gate, exclusive) so new quiet-build / quiet-measure |
| 16 | # callers wait outside while the ones already holding the quiet lock finish |
| 17 | # (L322, 2026-10-21: with no gate, shared takers kept arriving, the |
| 18 | # exclusive waiter never won, and each time the window let the chip go a raw |
| 19 | # flock user took it: 40+ min for the front of the line). |
| 20 | # L405 (2026-10-22): the window drains the quiet lock for at most 5 min, then lets |
| 21 | # everything go and waits for a quiet machine again; quiet-build gives up waiting after 25 min. |
| 22 | # Deadlock guard: measurements take quiet then chip, so if a process waiting |
| 23 | # for the chip holds the quiet lock, the window lets the chip go (gate stays |
| 24 | # up), waits for quiet, then tries the chip without blocking. |
| 25 | # --quiet none: chip lock only, no quiet lock and no gate. For chip checks that |
| 26 | # compare VALUES (routes, logits, cosines), not times: the quiet lock exists for |
| 27 | # timings only. Never use it for a number you will report as a speed. |
| 28 | # A holder process keeps the locks until close or until the lease runs out |
| 29 | # (default and maximum 20 min when it takes anything exclusive; 90 min otherwise). Records box state at open in |
| 30 | # ~/yardsticks/windows/<id>/open.json and prints the id. While waiting it |
| 31 | # prints who holds each lock. |
| 32 | # spark-window run CMD... run CMD inside the open window: sets SPARK_WINDOW and |
| 33 | # SPARK_WINDOW_DIR, so spark-receipt stamps the window id and calibration, |
| 34 | # and, when the window holds the chip lock, AIEN_GB10_LOCK_HELD=1, so a |
| 35 | # job that honours it (aien-test gates, the 27B server) does not queue |
| 36 | # behind its own window. A job that takes /tmp/aien-gb10.lock itself and |
| 37 | # ignores the marker (plain flock) waits until the window closes: open |
| 38 | # such windows with --gb10 none. quiet-measure run inside the window runs CMD |
| 39 | # directly instead of waiting on our own lock. |
| 40 | # spark-window close [ID] release the quiet lock, record box state at close |
| 41 | # (close.json), print what the window holds. With an ID it closes only that |
| 42 | # window. Without one it closes the open window only if you own it: the shell |
| 43 | # that ran open is in your process tree, or $SPARK_WINDOW (set by run) is its id. |
| 44 | # Otherwise it refuses (exit 3) and prints the holder; close it by ID or wait. |
| 45 | # spark-window status show the open window (or "none") and the lock queue. |
| 46 | # spark-window id print the open, held window id (exit 1 if none). |
| 47 | # spark-window locks [gb10|quiet] who holds and who is queued on the locks (kernel lock table). |
| 48 | # |
| 49 | # L405 lock policy (Drake 2026-10-22: "stop chip locking"): |
| 50 | # 1. Correctness work (builds, host tests, QEMU, crumb checks, value gates) takes no exclusive lock and waits |
| 51 | # at most 25 min behind a timing lock (quiet-build), then runs anyway and the window is marked "shared machine". |
| 52 | # 2. Exclusive windows are for TIMING receipts only and last at most 20 min (open refuses more). A window |
| 53 | # that overruns is let go and gets NO VERDICT. Split long measurements, or run on the shared machine labelled so. |
| 54 | # 3. A timing window opens only when the machine is quiet anyway: at most 3 jobs on the quiet lock and load at |
| 55 | # most 10 (numbers from the L371 incident: 3.25 new builds per 20 min). It waits, holding nothing, until then. |
| 56 | # 4. Test model servers use their own chip lock (q36-extra sets AIEN_SERVE_GPU_LOCK), like the live server. |
| 57 | # 5. lock-long-holds (cron, 5 min) posts every exclusive hold over 20 min on the board and asks spark-window to |
| 58 | # close a window past its lease; it never kills jobs. |
| 59 | # Rule: one window at a time. Wait your turn with open --wait (first come, first served) and |
| 60 | # say so on the ledger (ledger note <your id> "measure: in line for a window"). |
| 61 | set -u |
| 62 | WR=${SPARK_WINDOWS_ROOT:-$HOME/yardsticks/windows} |
| 63 | Q=${SPARK_QUIET_LOCK:-/tmp/aien-quiet.lock}; W=${SPARK_QUIET_WANT:-/tmp/aien-quiet.want.d}; G=${SPARK_GB10_LOCK:-/tmp/aien-gb10.lock} # overrides: tests only |
| 64 | # writer-preference gate (see _hold): quiet-build and quiet-measure pass through it before taking the quiet lock |
| 65 | GATE=${SPARK_QUIET_GATE:-/tmp/aien-quiet.gate} |
| 66 | # L405 limits (numbers from the 2026-10-21 L371 incident: the build line grew from 3 to 29 in 160 min, |
| 67 | # 3.25 new jobs per 20 min, calc:691a9354): exclusive leases at most LEASE_MAX min; open only when at most |
| 68 | # IDLE_N jobs hold or wait on the quiet lock and the 1-min load is at most IDLE_LOAD; drain the quiet lock |
| 69 | # for at most DRAIN_MAX s, else let go and wait for quiet again; give up after IDLE_MAX s of waiting. |
| 70 | # Builds wait at most DRAIN_MAX + LEASE_MAX (25 min, quiet-build SPARK_BUILD_MAX_WAIT) behind a window. |
| 71 | LEASE_MAX=${SPARK_LEASE_MAX:-20}; LU=${SPARK_LEASE_UNIT_S:-60} |
| 72 | IDLE_N=${SPARK_IDLE_N:-3}; IDLE_LOAD=${SPARK_IDLE_LOAD:-10}; DRAIN_MAX=${SPARK_DRAIN_MAX:-300}; IDLE_MAX=${SPARK_IDLE_MAX:-2700} |
| 73 | BOARD=${SPARK_BOARD:-$HOME/handoffs/BOARD.md} # overrides: tests only |
| 74 | mkdir -p "$WR" |
| 75 | now() { date -u +%FT%TZ; } |
| 76 | esc() { printf '%s' "$1" | tr '\n\t' ' ' | sed 's/\\/\\\\/g; s/"/\\"/g'; } |
| 77 | age() { echo $(( $(date +%s) - $(stat -c %Y "$1" 2>/dev/null || date +%s) )); } |
| 78 | |
| 79 | # openers FILE: the processes that have FILE open right now (a lock set by a flock that has since |
| 80 | # exited lives on in whoever inherited the open file), up to 5, as "pid cmd" |
| 81 | openers() { |
| 82 | n=0; seen=" " |
| 83 | for f in /proc/[0-9]*/fd/*; do |
| 84 | [ "$(readlink "$f" 2>/dev/null)" = "$1" ] || continue |
| 85 | p=${f#/proc/}; p=${p%%/*} |
| 86 | case $seen in *" $p "*) continue ;; esac |
| 87 | seen="$seen$p "; n=$((n + 1)); [ $n -le 5 ] || continue |
| 88 | printf '%spid %s %s' "$([ $n -gt 1 ] && echo '; ')" "$p" "$(ps -o args= -p "$p" 2>/dev/null | cut -c1-70)" |
| 89 | done |
| 90 | [ $n -gt 0 ] || printf 'none found' |
| 91 | [ $n -le 5 ] || printf '+%s more' $((n - 5)) |
| 92 | } |
| 93 | # holders LOCKFILE: who holds the lock (kernel lock table) and who is queued on it, with ages |
| 94 | holders() { |
| 95 | ino=$(stat -c %i "$1" 2>/dev/null) || return 0 |
| 96 | grep -E "^[0-9]+: +(-> +)?FLOCK +[A-Z]+ +[A-Z]+ +[0-9]+ [0-9a-f]+:[0-9a-f]+:$ino " /proc/locks 2>/dev/null | |
| 97 | while read -r _ a b c d e _; do |
| 98 | if [ "$a" = "->" ]; then q="queued"; m=$d; p=$e; else q="holds"; m=$c; p=$d; fi |
| 99 | [ "$m" = READ ] && m=shared || m=exclusive |
| 100 | a=$(ps -o args= -p "$p" 2>/dev/null | cut -c1-110); [ -n "$a" ] || a="(set by a finished flock; processes with $1 open now: $(openers "$1"))" |
| 101 | printf '%s %s pid %s (%ss) %s; ' "$q" "$m" "$p" "$(ps -o etimes= -p "$p" 2>/dev/null | tr -d ' ')" "$a" |
| 102 | done |
| 103 | } |
| 104 | waiters() { |
| 105 | own=$(cat "$WR/$(cur)/qm.pid" 2>/dev/null) |
| 106 | for f in "$W"/*; do [ -f "$f" ] || continue; [ "${f##*/}" = "$own" ] && continue |
| 107 | printf 'pid %s waiting %ss: %s; ' "${f##*/}" "$(age "$f")" "$(cut -c22-140 "$f")" |
| 108 | done |
| 109 | } |
| 110 | |
| 111 | # state: one JSON object with the box state right now |
| 112 | state() { |
| 113 | sr=$(tail -n 1 "$HOME/yardsticks/sensors/$(date +%F).csv" 2>/dev/null) |
| 114 | se=${sr%%,*}; sa=$(( $(date +%s) - ${se:-0} )) |
| 115 | g=$(nvidia-smi --query-gpu=utilization.gpu,power.draw --format=csv,noheader,nounits 2>/dev/null | tr -d ' ') |
| 116 | apps=$(nvidia-smi --query-compute-apps=pid,used_memory,process_name --format=csv,noheader 2>/dev/null | tr '\n' ';') |
| 117 | printf '{"utc": "%s", "loadavg": "%s", "mem_avail_kB": %s, "gpu_util_pct": "%s", "gpu_W": "%s",\n' \ |
| 118 | "$(now)" "$(cat /proc/loadavg)" "$(awk '/MemAvailable/{print $2}' /proc/meminfo)" "${g%%,*}" "${g##*,}" |
| 119 | printf ' "gb10_lock_holders": "%s",\n "quiet_lock_holders": "%s",\n "quiet_waiters": "%s",\n "gpu_apps": "%s",\n' \ |
| 120 | "$(esc "$(holders $G)")" "$(esc "$(holders $Q)")" "$(esc "$(waiters)")" "$(esc "$apps")" |
| 121 | printf ' "sensors_row": "%s", "sensors_row_age_s": %s}\n' "$(esc "$sr")" "$sa" |
| 122 | } |
| 123 | |
| 124 | alive() { [ -n "${1:-}" ] && kill -0 "$1" 2>/dev/null; } |
| 125 | cur() { cat "$WR/OPEN" 2>/dev/null; } |
| 126 | # the waiting line for --wait: one ticket file per waiter, named <epoch ns>-<pid>; a dead waiter's ticket is dropped |
| 127 | QD=$WR/QUEUE |
| 128 | qclean() { for t in "$QD"/*; do [ -f "$t" ] || continue; alive "${t##*-}" || rm -f "$t"; done; } |
| 129 | qhead() { ls "$QD" 2>/dev/null | sort | head -n 1; } |
| 130 | qpos() { ls "$QD" 2>/dev/null | sort | grep -n -x -F "${1##*/}" | cut -d: -f1; } |
| 131 | qlist() { ls "$QD" 2>/dev/null | sort | while read -r t; do echo " in line: $(cat "$QD/$t" 2>/dev/null) (pid ${t##*-})"; done; } |
| 132 | hpid() { cat "$WR/$1/holder.pid" 2>/dev/null; } |
| 133 | |
| 134 | # Who may close a window without naming it. open records the shell that ran it ("opener", |
| 135 | # pid:start time, so a reused pid does not count); a bare close is only for that shell's tree |
| 136 | # or for a caller whose $SPARK_WINDOW is the window id (set by run). L162, 2026-10-20. |
| 137 | pstat() { sed 's/^.*) //' "/proc/$1/stat" 2>/dev/null; } # fields from the state letter on |
| 138 | ppid_of() { pstat "$1" | cut -d' ' -f2; } |
| 139 | stime() { pstat "$1" | cut -d' ' -f20; } |
| 140 | cmdl() { tr '\0' ' ' < "/proc/$1/cmdline" 2>/dev/null; } |
| 141 | # the shell that ran this command, looking past the subshell a $(...) makes (same command line) |
| 142 | anchor() { |
| 143 | a=$PPID |
| 144 | while p=$(ppid_of "$a"); [ -n "$p" ] && [ "$p" -gt 1 ] && [ "$(cmdl "$a")" = "$(cmdl "$p")" ]; do a=$p; done |
| 145 | echo "$a" |
| 146 | } |
| 147 | in_tree() { # in_tree PID START: is that same process this one or one of its ancestors? |
| 148 | a=$$ |
| 149 | while [ -n "$a" ] && [ "$a" -gt 1 ]; do |
| 150 | [ "$a" = "$1" ] && [ "$(stime "$a")" = "$2" ] && return 0 |
| 151 | a=$(ppid_of "$a") |
| 152 | done |
| 153 | return 1 |
| 154 | } |
| 155 | mine() { # mine ID: may this caller close window ID without naming it? |
| 156 | [ "${SPARK_WINDOW:-}" = "$1" ] && return 0 |
| 157 | o=$(cat "$WR/$1/opener" 2>/dev/null) |
| 158 | [ -n "$o" ] && in_tree "${o%%:*}" "${o##*:}" && return 0 |
| 159 | h=$(hpid "$1"); [ -n "$h" ] && in_tree "$h" "$(stime "$h")" && return 0 |
| 160 | return 1 |
| 161 | } |
| 162 | |
| 163 | describe() { |
| 164 | d=$WR/$1 |
| 165 | st=queued; [ -f "$d/held" ] && st=held |
| 166 | echo "window $1 name=$(cat "$d/name" 2>/dev/null) owner=$(cat "$d/owner" 2>/dev/null) gb10=$(cat "$d/gb10" 2>/dev/null || echo legacy-quiet-only) quiet=$(cat "$d/quiet" 2>/dev/null || echo exclusive) state=$st opened_request=$(cat "$d/requested" 2>/dev/null) holder_pid=$(hpid "$1") lease_min=$(cat "$d/lease" 2>/dev/null)" |
| 167 | [ -f "$d/held" ] && echo " held since $(cat "$d/held") ($(age "$d/held") s)" |
| 168 | } |
| 169 | |
| 170 | # verdict ID: what a window's timings are worth (L405): NO VERDICT after a lease overrun, "shared machine" |
| 171 | # when correctness jobs ran beside it after their 25 min cap, else "quiet machine" |
| 172 | verdict() { |
| 173 | [ -f "$WR/$1/expired" ] && { echo "NO VERDICT: the lease ran out before close"; return; } |
| 174 | n=$(cat "$WR/$1/intrusions" 2>/dev/null | wc -l) |
| 175 | [ "$n" -gt 0 ] && { echo "shared machine: $n correctness job(s) ran beside it after waiting their cap"; return; } |
| 176 | echo "quiet machine" |
| 177 | } |
| 178 | board() { echo "$(date -u +%FT%H:%MZ) $1" >> "$BOARD" 2>/dev/null; } |
| 179 | |
| 180 | # called with the state lock held: drop a window whose holder is gone |
| 181 | reap() { |
| 182 | id=$(cur); [ -n "$id" ] || return 0 |
| 183 | alive "$(hpid "$id")" && return 0 |
| 184 | why="holder process gone"; [ -f "$WR/$id/expired" ] && why="lease expired $(cat "$WR/$id/expired")" |
| 185 | [ -f "$WR/$id/gave-up" ] && why="never opened: $(cat "$WR/$id/gave-up")" |
| 186 | { printf '{"closed_by": "spark-window reap", "reason": "%s", "verdict": "%s", "state": ' "$(esc "$why")" "$(verdict "$id")"; state; echo '}'; } > "$WR/$id/close.json" |
| 187 | rm -f "$WR/OPEN" |
| 188 | echo "spark-window: closed stale window $id ($why)" >&2 |
| 189 | } |
| 190 | |
| 191 | cmd=${1:-status}; [ $# -gt 0 ] && shift |
| 192 | case $cmd in |
| 193 | open) |
| 194 | name=${1:?usage: spark-window open NAME [--ledger ID] [--lease MIN] [--gb10 exclusive|shared|none] [--quiet exclusive|none] [--wait]}; shift |
| 195 | ledger=""; lease=""; gb10=exclusive; quiet=exclusive; wait=0 |
| 196 | while [ $# -gt 0 ]; do case $1 in --ledger) ledger=$2; shift 2 ;; --lease) lease=$2; shift 2 ;; --gb10) gb10=$2; shift 2 ;; --quiet) quiet=$2; shift 2 ;; --wait) wait=1; shift ;; *) echo "spark-window: unknown option $1" >&2; exit 2 ;; esac; done |
| 197 | case $gb10 in exclusive) gflag=-x ;; shared) gflag=-s ;; none) gflag= ;; *) echo "spark-window: --gb10 must be exclusive, shared or none" >&2; exit 2 ;; esac |
| 198 | case $quiet in exclusive) ;; none) [ "$gb10" != none ] || { echo "spark-window: --gb10 none with --quiet none would lock nothing" >&2; exit 2; } ;; *) echo "spark-window: --quiet must be exclusive or none" >&2; exit 2 ;; esac |
| 199 | # L405: exclusive locks are for short timing receipts only |
| 200 | excl=0; { [ "$gb10" = exclusive ] || [ "$quiet" = exclusive ]; } && excl=1 |
| 201 | [ -n "$lease" ] || { [ $excl = 1 ] && lease=$LEASE_MAX || lease=90; } |
| 202 | case $lease in ''|*[!0-9]*|0) echo "spark-window: --lease must be a whole number of minutes" >&2; exit 2 ;; esac |
| 203 | if [ $excl = 1 ] && [ "$lease" -gt "$LEASE_MAX" ]; then |
| 204 | echo "spark-window: refused, --lease $lease: exclusive windows (chip or quiet) are at most $LEASE_MAX min (L405, Drake 2026-10-22)." >&2 |
| 205 | echo " Split the measurement into windows of $LEASE_MAX min or less, or run it on the shared machine and label the receipt \"shared machine\"." >&2 |
| 206 | echo " Value checks (tokens, routes, outputs) need no exclusive lock: --gb10 shared --quiet none, or no window at all." >&2 |
| 207 | exit 2 |
| 208 | fi |
| 209 | RES=$HOME/.local/state/aien-coordinator/CHIP-RESERVED |
| 210 | # Drake 2026-10-24 ("be coworkers"): the reservation file is a courtesy note, not a gate. Say what you run on the BOARD, let anyone mid-measurement finish, step aside when asked. No refusal here. |
| 211 | if [ -f "$RES" ] && ! head -1 "$RES" | cut -d"(" -f1 | tr " " "\n" | grep -qx "${ledger:-none}"; then echo "spark-window: note, other lanes also want the chip ($(head -1 "$RES" | cut -d'(' -f1 | xargs)). Courtesy rule (Drake 2026-10-24): post what you run and for how long on ~/handoffs/BOARD.md, let anyone mid-measurement finish, step aside if asked." >&2; fi |
| 212 | # --wait: take a numbered ticket and wait in line; the oldest live ticket opens next. Without --wait |
| 213 | # an open is refused while a window is open or anyone holds a ticket, so nobody jumps the line. |
| 214 | mkdir -p "$QD"; tk="" |
| 215 | if [ "$wait" = 1 ]; then |
| 216 | tk=$QD/$(date +%s%N)-$$; printf '%s owner=%s since=%s\n' "$name" "${ledger:-$(whoami)@pid$PPID}" "$(now)" > "$tk" |
| 217 | trap 'rm -f "$tk"' EXIT; trap 'exit 130' INT TERM |
| 218 | fi |
| 219 | tq=$(date +%s); lastq=$tq |
| 220 | while :; do |
| 221 | exec 7>"$WR/.state.lock"; flock 7 |
| 222 | reap; qclean |
| 223 | first=$(qhead) |
| 224 | if [ -z "$(cur)" ] && { [ -z "$first" ] || [ "$QD/$first" = "$tk" ]; }; then break; fi |
| 225 | if [ -z "$tk" ]; then |
| 226 | echo "spark-window: refused, a window is already open or queued:" >&2 |
| 227 | [ -n "$(cur)" ] && describe "$(cur)" >&2 |
| 228 | qlist >&2 |
| 229 | echo "Wait in line with: spark-window open $name --wait ... (first come, first served)" >&2 |
| 230 | exit 3 |
| 231 | fi |
| 232 | flock -u 7; exec 7>&- |
| 233 | if [ $(( $(date +%s) - lastq )) -ge 60 ]; then |
| 234 | lastq=$(date +%s) |
| 235 | echo "spark-window: in line $(( lastq - tq )) s, place $(qpos "$tk"); open now: $(cur || true)" >&2 |
| 236 | fi |
| 237 | sleep 2 |
| 238 | done |
| 239 | [ -n "$tk" ] && { rm -f "$tk"; trap - EXIT INT TERM; } |
| 240 | id=$(date -u +%Y%m%dT%H%M%SZ)-$(od -An -N3 -tx1 /dev/urandom | tr -d ' \n') |
| 241 | d=$WR/$id; mkdir -p "$d" |
| 242 | oa=$(anchor); echo "$oa:$(stime "$oa")" > "$d/opener" |
| 243 | echo "$name" > "$d/name"; echo "${ledger:-${SPARK_WINDOW_OWNER:-$(whoami)@pid$PPID}}" > "$d/owner" |
| 244 | echo "$lease" > "$d/lease"; now > "$d/requested" |
| 245 | echo "$id" > "$WR/OPEN.tmp" && mv -f "$WR/OPEN.tmp" "$WR/OPEN" |
| 246 | echo "$gb10" > "$d/gb10" |
| 247 | echo "$quiet" > "$d/quiet" |
| 248 | setsid "$0" _hold "$id" 7>&- > "$d/holder.log" 2>&1 < /dev/null & |
| 249 | echo $! > "$d/holder.pid" |
| 250 | flock -u 7; exec 7>&- |
| 251 | t0=$(date +%s); last=$t0 |
| 252 | while [ ! -f "$d/held" ]; do |
| 253 | if ! alive "$(hpid "$id")"; then |
| 254 | if [ -f "$d/gave-up" ]; then echo "spark-window: not opened: $(cat "$d/gave-up")" >&2; rm -f "$WR/OPEN"; exit 4; fi |
| 255 | echo "spark-window: holder died before taking the lock; see $d/holder.log" >&2; rm -f "$WR/OPEN"; exit 1 |
| 256 | fi |
| 257 | if [ $(( $(date +%s) - last )) -ge 60 ]; then |
| 258 | last=$(date +%s) |
| 259 | echo "spark-window: waiting $(( last - t0 )) s: $(cut -c22- "$d/phase" 2>/dev/null). gb10: $(holders $G) | quiet: $(holders $Q) | waiters: $(waiters)" >&2 |
| 260 | fi |
| 261 | sleep 1 |
| 262 | done |
| 263 | { printf '{"window_id": "%s", "name": "%s", "owner": "%s", "lease_min": %s, "gb10": "%s", "waited_s": %s, "state": ' \ |
| 264 | "$id" "$(esc "$name")" "$(esc "$(cat "$d/owner")")" "$lease" "$gb10" "$(( $(date +%s) - t0 ))"; state; echo '}'; } > "$d/open.json" |
| 265 | echo "spark-window: window $id open, chip lock $gb10, quiet lock $quiet (waited $(( $(date +%s) - t0 )) s); box state in $d/open.json" >&2 |
| 266 | echo "$id" |
| 267 | ;; |
| 268 | _hold) |
| 269 | # Takes the window's locks, then holds them until close or lease end. |
| 270 | # Order and rules (L154, L189, L322, L405): |
| 271 | # 0. (quiet exclusive only, L405) Wait, holding NOTHING, until the machine is quiet anyway: at most |
| 272 | # $IDLE_N jobs hold or wait on the quiet lock and the 1-min load is at most $IDLE_LOAD. A window |
| 273 | # opens when the machine is idle, it does not stop everyone. Give up after $IDLE_MAX s (exit 4). |
| 274 | # 1. Wait for the chip holding NOTHING (a window waiting for the chip must not block CPU timings). |
| 275 | # 2. With the chip in hand, raise the GATE (exclusive on $GATE). quiet-build and quiet-measure pass |
| 276 | # through it (shared, for a moment) before they take the quiet lock, so no NEW taker arrives while |
| 277 | # we wait; the ones already holding the quiet lock finish normally (L322). |
| 278 | # 3. Drain: wait for the quiet lock while KEEPING the chip, in 5 s polls, for at most $DRAIN_MAX s. |
| 279 | # If it has not drained by then, let everything go (builds pass) and go back to step 0. |
| 280 | # 4. Deadlock guard: the measurement tools take quiet THEN chip. If a process queued on the chip |
| 281 | # has the quiet lock file open, it waits for our chip while we wait for its quiet lock: let the |
| 282 | # chip go (gate stays up), wait for the quiet lock holding nothing else (same drain cap), then TRY |
| 283 | # the chip without blocking (a try cannot deadlock). If the try fails, let quiet go and start again. |
| 284 | # 5. Hold until close or lease end. Lease end (L405): let go, write NO VERDICT, post on the board. |
| 285 | # --quiet none: chip only, no quiet lock, no gate, no idle wait (value checks, not timings). |
| 286 | id=$1; d=$WR/$id; lease=$(cat "$d/lease"); mode=$(cat "$d/gb10" 2>/dev/null || echo legacy); qmode=$(cat "$d/quiet" 2>/dev/null || echo exclusive) |
| 287 | echo $$ > "$d/holder.pid.tmp" && mv -f "$d/holder.pid.tmp" "$d/holder.pid" |
| 288 | case $mode in exclusive) gf=-x ;; shared) gf=-s ;; *) gf= ;; esac |
| 289 | phase() { echo "$(now) $1" > "$d/phase"; } |
| 290 | # qjobs: rows (holders and waiters) on the quiet lock in the kernel lock table |
| 291 | qjobs() { qi=$(stat -c %i "$Q" 2>/dev/null) || { echo 0; return; }; grep -cE "^[0-9]+: +(-> +)?FLOCK +[A-Z]+ +[A-Z]+ +[0-9]+ [0-9a-f]+:[0-9a-f]+:$qi " /proc/locks 2>/dev/null; } |
| 292 | load1() { cut -d. -f1 /proc/loadavg; } |
| 293 | t_idle0=$(date +%s) |
| 294 | wait_idle() { |
| 295 | while :; do |
| 296 | n=$(qjobs); l=$(load1) |
| 297 | [ "$n" -le "$IDLE_N" ] && [ "$l" -le "$IDLE_LOAD" ] && return 0 |
| 298 | if [ $(( $(date +%s) - t_idle0 )) -ge "$IDLE_MAX" ]; then |
| 299 | echo "the machine was never quiet in ${IDLE_MAX}s ($n jobs on the quiet lock, max $IDLE_N; load $l, max $IDLE_LOAD). Split the measurement or run it on the shared machine and label it \"shared machine\"." > "$d/gave-up" |
| 300 | phase "gave up: machine never quiet"; exit 4 |
| 301 | fi |
| 302 | phase "waiting for a quiet machine, holding nothing: $n jobs on the quiet lock (max $IDLE_N), load $l (max $IDLE_LOAD)" |
| 303 | sleep 5 8>&- 9>&- 6>&- |
| 304 | done |
| 305 | } |
| 306 | # drain: wait for the quiet lock (fd 8) at most DRAIN_MAX s; with $1=guard, stop early when the deadlock guard fires |
| 307 | drain() { |
| 308 | ds=$(date +%s); yield=0 |
| 309 | while [ $(( $(date +%s) - ds )) -lt "$DRAIN_MAX" ]; do |
| 310 | flock -w 5 -x 8 && return 0 |
| 311 | if [ "${1:-}" = guard ] && [ -n "$gf" ] && chip_waiter_holds_quiet; then yield=1; return 1; fi |
| 312 | done |
| 313 | return 1 |
| 314 | } |
| 315 | # chip_waiter_holds_quiet: some process queued on the chip lock has the quiet lock file open |
| 316 | chip_waiter_holds_quiet() { |
| 317 | cino=$(stat -c %i "$G" 2>/dev/null) || return 1 |
| 318 | for p in $(awk -v i="$cino" '$2=="->" { split($7, a, ":"); if (a[3] == i) print $6 }' /proc/locks 2>/dev/null); do |
| 319 | for f in /proc/$p/fd/*; do [ "$(readlink "$f" 2>/dev/null)" = "$Q" ] && return 0; done |
| 320 | done |
| 321 | return 1 |
| 322 | } |
| 323 | # legacy: a window opened by the previous version runs _hold under quiet-measure, which already |
| 324 | # holds the quiet lock for it; taking it again here would wait on itself. |
| 325 | if [ "$mode" != legacy ]; then |
| 326 | [ -n "$gf" ] && exec 9>>"$G" |
| 327 | if [ "$qmode" = none ]; then |
| 328 | phase "waiting for the chip lock ($mode, no quiet lock)"; flock $gf 9; tries=1 |
| 329 | else |
| 330 | exec 8>>"$Q"; mkdir -p "$(dirname "$GATE")"; [ -e "$GATE" ] || { : > "$GATE"; chmod 666 "$GATE"; } 2>/dev/null; exec 6>>"$GATE" |
| 331 | tries=0 |
| 332 | while :; do |
| 333 | tries=$((tries + 1)) |
| 334 | wait_idle |
| 335 | if [ -n "$gf" ]; then phase "waiting for the chip lock ($mode)"; flock $gf 9; fi |
| 336 | flock -x 6 |
| 337 | flock -n -x 8 && break |
| 338 | phase "chip held, gate up, draining the quiet lock for at most ${DRAIN_MAX}s (try $tries)" |
| 339 | drain guard && break |
| 340 | if [ $yield = 1 ]; then |
| 341 | flock -u 9; yields=$((${yields:-0} + 1)); echo "$yields" > "$d/yields" |
| 342 | phase "chip let go: a process waiting for the chip holds the quiet lock (try $tries)" |
| 343 | drain && { flock -n $gf 9 && break; } |
| 344 | fi |
| 345 | flock -u 8; flock -u 6; [ -n "$gf" ] && flock -u 9 |
| 346 | echo "$(now) try $tries: quiet lock did not drain in ${DRAIN_MAX}s, let everything go" >> "$d/backoffs" |
| 347 | phase "quiet lock did not drain in ${DRAIN_MAX}s: let everything go so builds pass, back to waiting for a quiet machine (try $tries)" |
| 348 | sleep 2 8>&- 9>&- 6>&- |
| 349 | done |
| 350 | flock -u 6 |
| 351 | fi |
| 352 | echo "$tries" > "$d/tries" |
| 353 | fi |
| 354 | phase "holding" |
| 355 | now > "$d/held" |
| 356 | end=$(( $(date +%s) + lease * LU )) |
| 357 | while [ ! -f "$d/close-request" ]; do |
| 358 | if [ "$(date +%s)" -ge "$end" ]; then |
| 359 | now > "$d/expired" |
| 360 | echo "NO VERDICT: the lease of $lease min ran out before close; timings from this window are not valid (L405)" > "$d/verdict" |
| 361 | echo "lease of $lease min ran out" >&2 |
| 362 | board "spark-window to $(cat "$d/owner" 2>/dev/null): window $id ($(cat "$d/name" 2>/dev/null)) ran past its $lease min lease. Its locks are let go and its timings get NO VERDICT (L405). Split long measurements into windows of $LEASE_MAX min or less." |
| 363 | exit 0 |
| 364 | fi |
| 365 | sleep 2 8>&- 9>&- 6>&- |
| 366 | done |
| 367 | exit 0 |
| 368 | ;; |
| 369 | run) |
| 370 | id=$(cur) |
| 371 | if [ -z "$id" ] || [ ! -f "$WR/$id/held" ] || ! alive "$(hpid "$id")"; then echo "spark-window: no open window holds its locks; run spark-window open NAME first" >&2; exit 1; fi |
| 372 | [ $# -gt 0 ] || { echo "usage: spark-window run CMD..." >&2; exit 2; } |
| 373 | case $(cat "$WR/$id/gb10" 2>/dev/null) in exclusive|shared) AIEN_GB10_LOCK_HELD=1; export AIEN_GB10_LOCK_HELD ;; esac |
| 374 | SPARK_WINDOW=$id SPARK_WINDOW_DIR=$WR/$id exec "$@" |
| 375 | ;; |
| 376 | id) |
| 377 | id=$(cur) |
| 378 | [ -n "$id" ] && [ -f "$WR/$id/held" ] && alive "$(hpid "$id")" && { echo "$id"; exit 0; } |
| 379 | exit 1 |
| 380 | ;; |
| 381 | close) |
| 382 | exec 7>"$WR/.state.lock"; flock 7 |
| 383 | named=${1:-}; id=${named:-$(cur)} |
| 384 | case $id in */*|.*) echo "spark-window: not a window id: $id" >&2; exit 2 ;; esac |
| 385 | [ -n "$id" ] && [ -d "$WR/$id" ] || { echo "spark-window: no window to close" >&2; exit 1; } |
| 386 | [ -f "$WR/$id/close.json" ] && { echo "spark-window: window $id is already closed" >&2; exit 1; } |
| 387 | # A bare close is not allowed to end somebody else's window (L129's finishing script closed |
| 388 | # L159's window, whose own bare close then closed L127's queued one). |
| 389 | if [ -z "$named" ] && ! mine "$id"; then |
| 390 | echo "spark-window: refused, the open window is not yours (you did not open it from this shell, and SPARK_WINDOW is ${SPARK_WINDOW:-unset}):" >&2 |
| 391 | describe "$id" >&2 |
| 392 | echo "Close your own window by id (spark-window close <ID>); this one is closed by its owner or when its lease runs out." >&2 |
| 393 | exit 3 |
| 394 | fi |
| 395 | d=$WR/$id; h=$(hpid "$id") |
| 396 | now > "$d/close-request" |
| 397 | # A holder still waiting for its locks holds nothing: stop it (its own process group, from setsid). |
| 398 | [ -f "$d/held" ] || { alive "$h" && kill -- -"$h" 2>/dev/null; why_pre="closed while still waiting for its locks"; } |
| 399 | while alive "$h"; do sleep 1; done |
| 400 | why=${why_pre:-closed}; [ -f "$d/expired" ] && why="lease expired $(cat "$d/expired") before close" |
| 401 | { printf '{"window_id": "%s", "closed_by": "spark-window close", "reason": "%s", "verdict": "%s", "held_s": %s, "state": ' \ |
| 402 | "$id" "$why" "$(esc "$(verdict "$id")")" "$(( $(date +%s) - $(stat -c %Y "$d/held" 2>/dev/null || date +%s) ))"; state; echo '}'; } > "$d/close.json" |
| 403 | [ "$(cur)" = "$id" ] && rm -f "$WR/OPEN" |
| 404 | echo "spark-window: verdict for its timings: $(verdict "$id")" >&2 |
| 405 | echo "spark-window: window $id closed ($why). It holds:" >&2 |
| 406 | ls "$d" | grep -E '\.json$' | sed 's/^/ /' >&2 |
| 407 | ;; |
| 408 | locks) |
| 409 | case ${1:-all} in gb10) holders $G ;; quiet) holders $Q ;; *) echo "quiet: $(holders $Q)"; echo "gb10: $(holders $G)" ;; esac |
| 410 | echo |
| 411 | ;; |
| 412 | status) |
| 413 | id=$(cur) |
| 414 | if [ -n "$id" ]; then describe "$id"; alive "$(hpid "$id")" || echo " holder process is gone (next open reaps it)"; else echo "no window open"; fi |
| 415 | echo "quiet lock holders: $(holders $Q)" |
| 416 | echo "quiet waiters: $(waiters)" |
| 417 | echo "gb10 lock holders: $(holders $G)" |
| 418 | qclean; echo "waiting line (--wait):"; qlist |
| 419 | ;; |
| 420 | *) sed -n '2,/^set -u/p' "$0"; exit 2 ;; |
| 421 | esac |