Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-architecture-mv/blob/main/tools/ops/spark-receipt # aien-architecture-mv Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-architecture-mv / tools / ops / spark-receipt` 102 lines ยท 4908 bytes Raw Blame History 1 #!/bin/sh 2 # spark-receipt: run a command between sensor samples and print a receipt. 3 # 4 # usage: spark-receipt NAME [--idle SECS] -- CMD ARGS... 5 # Records start/end, reads ~/yardsticks/sensors/.csv for the window, 6 # and writes ~/yardsticks/receipts/-.json with: 7 # wall energy (J) over the run, mean wall W, mean GPU W, idle baseline W 8 # (mean of the --idle seconds before start, default 30), incremental J, 9 # load and MemAvailable at start/end, lock holders, NVRM error delta, 10 # command, exit code, git head of cwd if any. Energy resolution: the UPS 11 # reports 1% of 1000 W every ~2 s, so numbers under ~20 J are noise. 12 # Measurement windows (spark-window): when SPARK_WINDOW is set (spark-window 13 # run sets it), the receipt also carries window_id and the path of that 14 # window's calibration.json (null when either is absent). 15 # Shell + awk port of the earlier Python version: same keys, same file name, 16 # same stdout (command output, then the JSON, then "receipt: PATH"). 17 set -u 18 usage() { sed -n '2,17p' "$0" | sed 's/^# \{0,1\}//'; exit 2; } 19 [ $# -ge 1 ] || usage 20 name=$1; shift; idle=30 21 while [ $# -gt 0 ] && [ "$1" != "--" ]; do 22 case $1 in --idle) idle=${2:?}; shift 2 ;; *) shift ;; esac 23 done 24 [ $# -gt 0 ] || usage 25 shift 26 [ $# -gt 0 ] || usage 27 S=$HOME/yardsticks/sensors; R=$HOME/yardsticks/receipts; WR=${SPARK_WINDOWS_ROOT:-$HOME/yardsticks/windows} 28 t_idle0=$(( $(date +%s) - idle )) 29 head=$(git rev-parse --short HEAD 2>/dev/null) 30 start=$(date -u +%Y-%m-%dT%H:%M:%S.%6NZ) 31 t0=$(date +%s.%N) 32 "$@"; rc=$? 33 t1=$(date +%s.%N) 34 sleep 3 35 win=${SPARK_WINDOW:-}; cal="" 36 if [ -n "$win" ]; then 37 wd=${SPARK_WINDOW_DIR:-$WR/$win} 38 [ -f "$wd/calibration.json" ] && cal=$wd/calibration.json 39 fi 40 i0=${t0%.*}; i1=${t1%.*} 41 files="" 42 for d in $(date -d "@$t_idle0" +%F) $(date -d "@$i0" +%F) $(date -d "@$i1" +%F); do 43 case " $files " in *" $S/$d.csv "*) ;; *) [ -f "$S/$d.csv" ] && files="$files $S/$d.csv" ;; esac 44 done 45 mkdir -p "$R" 46 p=$R/$(date +%F)-$name.json 47 cmd=$(printf '%s ' "$@"); cmd=${cmd% } 48 # shellcheck disable=SC2086 49 awk -F, -v name="$name" -v cmd="$cmd" -v rc="$rc" -v head="$head" -v start="$start" \ 50 -v t0="$t0" -v t1="$t1" -v ti="$t_idle0" -v i0="$i0" -v i1="$i1" -v win="$win" -v cal="$cal" ' 51 function isnum(s) { return s ~ /^[ \t]*[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)?[ \t]*$/ } 52 function esc(s) { gsub(/\\/, "\\\\", s); gsub(/"/, "\\\"", s); gsub(/\t/, "\\t", s); gsub(/\n/, "\\n", s); gsub(/\r/, "\\r", s); return s } 53 function str(s) { return "\"" esc(s) "\"" } 54 function strn(s) { return s == "" ? "null" : str(s) } 55 function n(x, s) { s = sprintf("%.10g", x); if (s !~ /[.eEn]/) s = s ".0"; return s } # floats keep a decimal point, as the Python version printed them 56 !isnum($1) { next } 57 { t = int($1) } 58 t >= ti && t <= i0 && isnum($3) { pre_w += $3; pre_n++ } 59 t >= i0 && t <= i1 { 60 nr++; ts[nr] = t; ix[nr] = nr; row[nr] = $0 61 } 62 END { 63 # rows in time order, stable (insertion sort on the epoch) 64 for (i = 2; i <= nr; i++) { v = ts[i]; x = ix[i]; j = i - 1; while (j > 0 && ts[j] > v) { ts[j+1] = ts[j]; ix[j+1] = ix[j]; j-- } ts[j+1] = v; ix[j+1] = x } 65 for (i = 1; i <= nr; i++) { 66 split(row[ix[i]], c, ",") 67 if (isnum(c[3])) { w += c[3]; wn++ } 68 if (isnum(c[4])) { g += c[4]; gn++ } 69 if (isnum(c[5])) { u += c[5]; un++ } 70 if (isnum(c[8]) && (lk == "" || c[8] + 0 > lk + 0)) lk = c[8] + 0 71 if (i == 1) { l0 = c[6]; m0 = c[7]; e0 = c[9] } 72 if (i == nr) { l1 = c[6]; m1 = c[7]; e1 = c[9] } 73 } 74 dur = t1 - t0 75 wm = wn ? w / wn : ""; im = pre_n ? pre_w / pre_n : "" 76 print "{" 77 print " \"name\": " str(name) "," 78 print " \"cmd\": " str(cmd) "," 79 print " \"exit\": " rc "," 80 print " \"git_head\": " strn(head) "," 81 print " \"start\": " str(start) "," 82 print " \"seconds\": " sprintf("%.3f", dur) "," 83 print " \"samples\": " nr + 0 "," 84 print " \"wall_W_mean\": " (wn ? n(wm) : "null") "," 85 print " \"wall_J\": " (wn ? n(wm * dur) : "null") "," 86 print " \"idle_W_before\": " (pre_n ? n(im) : "null") "," 87 print " \"incremental_J\": " ((wn && pre_n) ? n((wm - im) * dur) : "null") "," 88 print " \"gpu_W_mean\": " (gn ? n(g / gn) : "null") "," 89 print " \"gpu_util_mean\": " (un ? n(u / un) : "null") "," 90 print " \"load1_start\": " (nr ? str(l0) : "null") "," 91 print " \"load1_end\": " (nr ? str(l1) : "null") "," 92 print " \"mem_avail_kB_start\": " (nr ? str(m0) : "null") "," 93 print " \"mem_avail_kB_end\": " (nr ? str(m1) : "null") "," 94 print " \"lock_holders_max\": " (lk == "" ? "null" : lk) "," 95 print " \"nvrm_errors_delta\": " ((nr && isnum(e0) && isnum(e1)) ? e1 - e0 : "null") "," 96 print " \"note\": \"wall W = UPS ups.load x 10 (CP1500PFCLCD, 1 percent steps, ~2 s); whole UPS output, not the Spark alone\"," 97 print " \"window_id\": " strn(win) "," 98 print " \"calibration\": " strn(cal) 99 print "}" 100 }' $files /dev/null > "$p.tmp.$$" && mv -f "$p.tmp.$$" "$p" 101 cat "$p"; echo "receipt: $p" 102 exit $rc