Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tools/bench/cpuprof_sessions.sh

3.5 KiB, 1 run, executable

created by r1870400018:66232, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1#!/usr/bin/env bash
2# CPU-profiled wasm runs (cpuprof_runs.mjs), one fresh node process a run, each under both Austenite
3# build-slot locks (lib/quiet_host.sh) and released, with a pause, before the next. cpu and io PSI avg10
4# and the 1-minute load are read at each end; a run over PSI_LIMIT in cpu at either end is run once more
5# and a second flagged run is kept flagged. The profile of each run is OUT_DIR/<doc>-<mode>.cpuprofile
6# (the last attempt's) and one JSONL line carries the run's own report and the host readings.
7#
8# Usage: AUST_VENDOR=DIR OUT_DIR=DIR [EDITS=10] [REPS=1] cpuprof_sessions.sh <doc.typ> <mode>...
9# mode is cold, warm, edits or unchanged (see cpuprof_runs.mjs)
10set -uo pipefail
11
12HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
13# shellcheck source=lib/host.sh
14source "$HERE/lib/host.sh"
15# shellcheck source=lib/quiet_host.sh
16source "$HERE/lib/quiet_host.sh"
17
18AUST_VENDOR="${AUST_VENDOR:?set AUST_VENDOR to the Austenite package dir}"
19OUT_DIR="${OUT_DIR:?set OUT_DIR to the output directory}"
20EDITS="${EDITS:-10}"
21REPS="${REPS:-1}"
22PSI_LIMIT="${PSI_LIMIT:-20}"
23GAP_S="${GAP_S:-12}"
24DOC="${1:?usage: cpuprof_sessions.sh <doc.typ> <mode>...}"
25shift
26DOCNAME="$(basename "$DOC" .typ)"
27mkdir -p "$OUT_DIR"
28
29# One attempt inside the lock-holding subshell. Globals: S_MODE, S_ATTEMPT.
30body() {
31 local prof="$OUT_DIR/$DOCNAME-$S_MODE.cpuprofile" out_f="$OUT_DIR/$DOCNAME-$S_MODE.stdout" time_f="$OUT_DIR/$DOCNAME-$S_MODE.time"
32 local load_before psi_before load_after psi_after io_before io_after
33 load_before="$(loadavg1)"; psi_before="$(psi_avg10 cpu)"; io_before="$(psi_avg10 io)"
34 systemd-run --user --scope --quiet -p MemoryMax=3G --slice=claude-rc.slice -- \
35 /usr/bin/time -v -o "$time_f" -- \
36 node "$HERE/cpuprof_runs.mjs" "--vendor=$AUST_VENDOR" "--doc=$DOC" "--mode=$S_MODE" \
37 "--out=$prof" "--edits=$EDITS" "--reps=$REPS" > "$out_f" 2> "$out_f.err"
38 local exit_code=$?
39 load_after="$(loadavg1)"; psi_after="$(psi_avg10 cpu)"; io_after="$(psi_avg10 io)"
40 local rss_kb
41 rss_kb="$(awk -F': ' '/Maximum resident set size/{print $2}' "$time_f")"
42 local inner; inner="$(tail -n1 "$out_f" 2>/dev/null)"
43 [[ -z "$inner" ]] && inner='{"ok":false,"error":"no stdout from cpuprof_runs.mjs"}'
44 local flagged=false
45 if awk -v a="$psi_before" -v b="$psi_after" -v t="$PSI_LIMIT" 'BEGIN{exit !(a>t || b>t)}'; then
46 flagged=true
47 fi
48 jq -nc --arg doc "$DOCNAME" --arg mode "$S_MODE" --argjson attempt "$S_ATTEMPT" \
49 --argjson exit_code "$exit_code" --argjson rss_kb "${rss_kb:-0}" \
50 --argjson load_before "$load_before" --argjson load_after "$load_after" \
51 --argjson psi_before "$psi_before" --argjson psi_after "$psi_after" \
52 --argjson io_before "$io_before" --argjson io_after "$io_after" \
53 --argjson flagged "$flagged" --arg ts "$(date -u +%FT%TZ)" --argjson inner "$inner" \
54 '$inner * {ts:$ts, doc:$doc, mode:$mode, attempt:$attempt, exit_code:$exit_code, rss_kb:$rss_kb,
55 load_before:$load_before, load_after:$load_after, psi_before:$psi_before, psi_after:$psi_after,
56 io_before:$io_before, io_after:$io_after, flagged:$flagged}' >> "$OUT_DIR/runs.jsonl"
57 [[ "$flagged" == true ]] && return 3
58 return 0
59}
60
61for S_MODE in "$@"; do
62 S_ATTEMPT=1
63 while :; do
64 with_quiet_host body
65 rc=$?
66 echo "[cpuprof] $DOCNAME $S_MODE attempt $S_ATTEMPT: $(tail -n1 "$OUT_DIR/runs.jsonl" | jq -c '{ok,exit_code,calls_s,psi_before,psi_after,flagged}')" >&2
67 if [[ $rc -eq 3 && $S_ATTEMPT -lt 2 ]]; then
68 S_ATTEMPT=$((S_ATTEMPT + 1)); sleep "$GAP_S"; continue
69 fi
70 break
71 done
72 sleep "$GAP_S"
73done