diff --git a/docs/ops/MEASUREMENT_WINDOWS.md b/docs/ops/MEASUREMENT_WINDOWS.md index 3b1a8ca..b844089 100644 --- a/docs/ops/MEASUREMENT_WINDOWS.md +++ b/docs/ops/MEASUREMENT_WINDOWS.md @@ -44,17 +44,21 @@ the window is marked "shared machine" (L405). ## Exact commands -Open a window (waits its turn; prints the window id when it holds the box): +Open a window that will run the commands below, including `spark-baseline` (waits its turn; prints the +window id when it holds the box). `spark-baseline` refuses a window that holds the GPU lock, so this +open passes `--gb10 none`: ```sh -id=$(spark-window open my-measurement --ledger L109 --lease 20 --wait) +id=$(spark-window open my-measurement --ledger L109 --lease 20 --gb10 none --wait) ``` -By default the window also holds the GPU lock (`--gb10 exclusive`, added by L154), so GPU-only jobs -cannot run inside it. The 27B server takes that lock itself for every reply, so a window that measures -the 27B server, including `spark-baseline`, must be opened with `--gb10 none`; `spark-baseline` refuses -otherwise. `spark-calibrate` works in both: it uses the window's GPU lock when the window holds it and -takes the lock itself when it does not. +By default `open` holds the GPU lock (`--gb10 exclusive`, added by L154), so GPU-only jobs cannot run +inside it. The 27B server takes that lock itself for every reply, so a window that measures the 27B +server, including `spark-baseline`, must be opened with `--gb10 none`; `spark-baseline` refuses +otherwise. A window that does not measure the 27B can leave that default and skip `spark-baseline`. +To hold the GPU lock for other work and still take a 27B baseline, use two windows: the default for +the GPU-holding work, and `--gb10 none` for `spark-baseline`. `spark-calibrate` works in both: it +uses the window's GPU lock when the window holds it and takes the lock itself when it does not. Calibrate and take the baseline inside it: diff --git a/tools/ops/README.md b/tools/ops/README.md index 71bb46a..c322a42 100644 --- a/tools/ops/README.md +++ b/tools/ops/README.md @@ -9,11 +9,14 @@ How and when to use them: [docs/ops/MEASUREMENT_WINDOWS.md](../../docs/ops/MEASU | `spark-build-once` | Build cache keyed by the source tree, command and env knobs; concurrent callers build once | | `spark-window` | One measurement window at a time; holds the quiet lock exclusive; box state at open and close. A bare `close` only closes a window you opened (same shell tree, or `SPARK_WINDOW` set); otherwise refused with exit 3, use `close ` | | `test_spark_window_close.sh` | Offline test for the close rule above (scratch locks and folder, never the live windows) | +| `test-spark-window-starve.sh` | Offline test, scratch locks and window root only. The gated case proves a window opens while two shared holders take `$SPARK_QUIET_GATE` and then the quiet lock, the quiet-build order, so the gate in spark-window (L322) keeps new holders out and the drain finishes. Plain `flock -s` skips that gate and can starve an exclusive waiter, because Linux flock gives the waiter no priority. That plain-flock case is labeled known-starvable and is skipped unless `SPARK_STARVE_PLAIN=1` | | `spark-calibrate` | Per-window calibration: memcpy bandwidth (omega#583 probe) and 30 s idle power at the wall | | `spark-baseline` | Per-window 27B baseline: fixed 64-token streamed reply on 127.0.0.1:18091, decode tok/s and J/token | | `spark-receipt` | Energy receipt for any command; now stamps `window_id` and `calibration` | | `quiet-measure` | Exclusive quiet lock for a measurement; runs directly inside its own open window | -| `quiet-build` | Builds: since 2026-10-20 (Drake, L118) they never wait; they run on the efficient cores at low priority and are logged for overlap checks. Copy of the installed version | +| `quiet-build` | CPU-heavy jobs. Waits for a live measurement flag (at most 1 min), then the quiet gate, then a shared quiet lock. The whole wait is capped at 25 min (`SPARK_BUILD_MAX_WAIT`, default 1500 s). If the lock is acquired in time, the command runs as given. Past the cap it runs at nice 19, with no lock and no core pinning, and the open window is marked shared machine (L405). Waits are logged | + +`SHA256SUMS` in this directory is the sha256 of each script. From `tools/ops`: `sha256sum -c SHA256SUMS`. Shown on the Spark (2026-10-19/20): two callers on one source built once in a scratch repo (the second reused the stored result), two windows opened at the same moment (one refused), lease expiry, the diff --git a/tools/ops/SHA256SUMS b/tools/ops/SHA256SUMS new file mode 100644 index 0000000..07dc0f0 --- /dev/null +++ b/tools/ops/SHA256SUMS @@ -0,0 +1,9 @@ +b8b40d2c1221d6067a49cebd288fcf0b8fb34c616562f58569385d81de62d8dc quiet-build +c838569e1a940e904bc226bcbb192fdf013d0b70913325d3e5af3cdb93ab6b67 quiet-measure +925e55b6348ba8de23c863cc7476735eda6d1bb43c953b16e1fac04cc565c42a spark-baseline +f271b0ae027c35e855d855a528fa61d0f4e6f11dc44c390da2754623d129db46 spark-build-once +09c45c5a664a3ca1dba3a083cbbcd16d643f114345952868b13c5bafe7e9c40b spark-calibrate +aca797f0d7fa228452712c56a32530865c9c8b7c1d9448fc8ded15b44a39468c spark-receipt +01ac196e3e0fdcf7c2a69706ab0b6ca813af1bf67a4b56599fb65c351169fe14 spark-window +b33a7df6e31ae75816a1d1c733750d0d18edb69bf6f4aa4f6bc9f1cd6870085b test-spark-window-starve.sh +5e744cad81dc823c488f3b330b3c04ead6a62ad806c3ca8b379a334ff82c79d8 test_spark_window_close.sh diff --git a/tools/ops/spark-baseline b/tools/ops/spark-baseline index 36d9c20..40ba191 100755 --- a/tools/ops/spark-baseline +++ b/tools/ops/spark-baseline @@ -40,12 +40,13 @@ sexe=$(readlink "/proc/$spid/exe" 2>/dev/null); sargs=$(tr '\0' ' ' < "/proc/$sp models=$(curl -s --max-time 10 "http://127.0.0.1:$port/v1/models") case $models in *"\"$mid\""*) ;; *) echo "spark-baseline: server on :$port does not serve $mid: $models" >&2; exit 1 ;; esac -# The server takes /tmp/aien-gb10.lock itself for each generation (holding it here would deadlock), +# The server takes the GPU lock itself for each generation (holding it here would deadlock), # and GPU-only jobs keep that lock busy. So the reply is streamed and every chunk is timestamped: # from the first to the last token the server holds the GPU lock for us, which makes the decode # rate and its energy clean whatever the queue did before. The lock table is sampled throughout; # any other process seen holding the GPU lock during the decode marks the baseline not ok. -G=/tmp/aien-gb10.lock; gino=$(stat -c %i "$G") +# Sample the lock spark-window uses. SPARK_GB10_LOCK is for tests only (see spark-window:63). +G=${SPARK_GB10_LOCK:-/tmp/aien-gb10.lock}; gino=$(stat -c %i "$G") # SPARK_GB10_LOCK is for tests only (see spark-window:63) held || { echo "spark-baseline: window $id is not held; nothing measured" >&2; exit 1; } ( while :; do grep -E ":$gino " /proc/locks | sed "s/^/$(date +%s.%N) /"; sleep 0.5; done ) > "$b/gb10-during.txt" 2>/dev/null & smp=$! diff --git a/tools/ops/spark-calibrate b/tools/ops/spark-calibrate index c8f3b52..fe5c257 100755 --- a/tools/ops/spark-calibrate +++ b/tools/ops/spark-calibrate @@ -2,8 +2,9 @@ # spark-calibrate: the one calibration file of a measurement window. # # Run inside an open window (spark-window run spark-calibrate, or with -# SPARK_WINDOW set). Holding /tmp/aien-gb10.lock (waits for it, printing the -# holders every 60 s), it measures: +# SPARK_WINDOW set). It holds the GPU lock (default /tmp/aien-gb10.lock; +# SPARK_GB10_LOCK overrides it, for tests only (see spark-window:63)), waits +# for it, and prints the holders every 60 s. It measures: # 1. CPU memory bandwidth with the omega#583 memcpy probe (pinned source # omega research/cost-sim-583@3bd56731 tools/dagsim/memcpy_probe.c), the # same call research/583 used: nice -n 10 memcpy_probe 1 K 10, K = 1 and 8. @@ -19,7 +20,7 @@ id=${SPARK_WINDOW:-$(spark-window id)} || { echo "spark-calibrate: no open windo 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; } d=$WR/$id; mkdir -p "$d/calibration" export SPARK_WINDOW=$id SPARK_WINDOW_DIR=$d -G=/tmp/aien-gb10.lock +G=${SPARK_GB10_LOCK:-/tmp/aien-gb10.lock} # SPARK_GB10_LOCK is for tests only (see spark-window:63) # the probe, built once per source blob + flags REPO=${OMEGA_REPO:-$HOME/workspace/omega}; PIN=3bd56731c5517e2a9c8f195955c71e73dc6f9269; SRC=tools/dagsim/memcpy_probe.c diff --git a/tools/ops/test-spark-window-starve.sh b/tools/ops/test-spark-window-starve.sh index 8d4cd86..1a913b2 100755 --- a/tools/ops/test-spark-window-starve.sh +++ b/tools/ops/test-spark-window-starve.sh @@ -1,11 +1,18 @@ #!/bin/sh -# test-spark-window-starve.sh [SCRIPT]: L189 regression test for spark-window _hold. -# A window must open even while the quiet lock is held shared by one holder after another -# (builds and tests in turn) and while the chip lock is free. Uses only temp lock files and a -# temp window root; never touches /tmp/aien-quiet.lock or /tmp/aien-gb10.lock. +# test-spark-window-starve.sh [SCRIPT]: regression test for spark-window _hold. +# Uses only temp lock files and a temp window root; never touches /tmp/aien-quiet.lock +# or /tmp/aien-gb10.lock. # case 1 (no contention): opens fast. -# case 2 (shared quiet holders back to back, short gaps): opens within LIMIT seconds. -# Exit 0 = all pass. Pass an older script path to see it fail (red). +# case 2 (one shared holder, plain flock -s, no pause): opens within LIMIT seconds. +# One holder still leaves a gap, which is the L189 path. +# case 3 (two shared holders through $SPARK_QUIET_GATE, no pause): opens within LIMIT. +# Same order as quiet-build, so the gate at spark-window L322 keeps new holders out +# and the drain finishes. This is the case the default run treats as deterministic. +# case 4 (chip busy): waits for the chip, then opens. +# case 5 (two plain flock -s holders, no gate): known-starvable. Linux flock gives an +# exclusive waiter no priority, so two holders with no gap can starve the window. +# Skipped unless SPARK_STARVE_PLAIN=1. +# Exit 0 = every case that ran passed (a skip is not a failure). Pass an older script path to see it fail (red). set -u S=${1:-$(dirname "$0")/spark-window}; LIMIT=${LIMIT:-60} T=$(mktemp -d /tmp/spark-window-starve.XXXXXX) @@ -14,6 +21,7 @@ export SPARK_WINDOWS_ROOT=$T/win SPARK_QUIET_LOCK=$T/quiet.lock SPARK_GB10_LOCK= # these temp locks have no other users, so the real machine load says nothing about them. export SPARK_QUIET_GATE=$T/quiet.gate SPARK_QUIET_WANT=$T/want.d SPARK_BOARD=$T/BOARD.md SPARK_IDLE_LOAD=100000 : > "$SPARK_QUIET_LOCK"; : > "$SPARK_GB10_LOCK" +: > "$SPARK_QUIET_GATE" pids="" cleanup() { for p in $pids; do kill "$p" 2>/dev/null; done; for h in "$T"/win/*/holder.pid; do [ -f "$h" ] && kill -- -"$(cat "$h")" 2>/dev/null; done; sleep 0.5; rm -rf "$T"; } trap cleanup EXIT @@ -29,9 +37,15 @@ close_it() { "$S" close "$(cat "$SPARK_WINDOWS_ROOT/OPEN" 2>/dev/null)" > /dev/n if opens_within plain 10; then echo "PASS case 1: no contention opens"; close_it; else echo "FAIL case 1: no contention did not open"; fail=1; close_it; fi -# Shared quiet holders back to back, like builds and tests in turn. A waiting exclusive lock can only -# be won in a gap; the old _hold let it go again at once and almost never won it a second time. -stream() { # SECONDS GAP: one shared holder after another, forever +# Shared holders, one after another. SECONDS is how long each hold lasts; GAP is the pause after it. +# gated: quiet-build order. Pass $SPARK_QUIET_GATE shared, then hold the quiet lock shared. +# spark-window holds that gate exclusive while it drains (L322), so the next iteration waits +# outside and does not take the quiet lock. The holders already inside finish, then the window wins. +# plain: flock -s on the quiet lock only, no gate. Two of these can starve an exclusive waiter. +stream_gated() { # SECONDS GAP + ( while :; do flock -s "$SPARK_QUIET_GATE" true; flock -s "$SPARK_QUIET_LOCK" sleep "$1"; sleep "$2"; done ) & pids="$pids $!" +} +stream_plain() { # SECONDS GAP ( while :; do flock -s "$SPARK_QUIET_LOCK" sleep "$1"; sleep "$2"; done ) & pids="$pids $!" } stop_streams() { for p in $pids; do kill "$p" 2>/dev/null; done; pids=""; sleep 1.2; } @@ -40,12 +54,12 @@ case_run() { # LABEL: open under the running streams else echo "FAIL $1: no window in $LIMIT s under shared quiet holders back to back"; fail=1; fi close_it; stop_streams } -stream 1.0 0 +stream_plain 1.0 0 case_run "case 2 one holder, no pause between holders" -stream 0.9 0; stream 0.6 0 -case_run "case 3 two holders, no pause between holders" +stream_gated 0.9 0; stream_gated 0.6 0 +case_run "case 3 two holders through the quiet gate, no pause" # chip busy too: the window must wait for the chip, then open once it is free (no deadlock, no early open) -stream 1.0 0 +stream_plain 1.0 0 ( flock -x "$SPARK_GB10_LOCK" sleep 6 ) & pids="$pids $!"; sleep 0.5; t0=$(date +%s) # the open time comes from the window's own held file, not from when this loop noticed it (the poll is slow on a loaded box) if opens_within "case 4 chip busy 6 s" "$LIMIT"; then @@ -54,4 +68,11 @@ if opens_within "case 4 chip busy 6 s" "$LIMIT"; then else echo "FAIL case 4 chip busy 6 s: opened while the chip was held ($dt s)"; fail=1; fi else echo "FAIL case 4 chip busy 6 s: no window in $LIMIT s"; fail=1; fi close_it; stop_streams +# Two plain flock -s holders, no gate. Known-starvable: opt in with SPARK_STARVE_PLAIN=1. +if [ "${SPARK_STARVE_PLAIN:-}" = 1 ]; then + stream_plain 0.9 0; stream_plain 0.6 0 + case_run "case 5 known-starvable: two plain flock -s holders, no gate" +else + echo "SKIP case 5 known-starvable: two plain flock -s holders, no gate (set SPARK_STARVE_PLAIN=1 to run)" +fi exit $fail