Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
51 lines
3.9 KiB
Bash
51 lines
3.9 KiB
Bash
#!/usr/bin/env bash
|
|
# Sample one process's memory and the GPU once a second until it exits (laptop profile benches), and enforce the laptop
|
|
# safeguards: the monitored process is killed (TERM, then KILL after 20 s) and ABORT is written to OUT.tsv.abort when
|
|
# * the GPU is in P5 with the memory clock below 2 GHz AND the SW power cap / SW thermal slowdown reasons (0x4 | 0x20) active: the OEM
|
|
# profile (a plain P5 with reason 0x1 "idle" while the model is being loaded into VRAM is a clock ramp, seen in T1_short, and is not
|
|
# aborted), or the power limit is below 100 W,
|
|
# or the GPU temperature exceeds 90 C (the OEM silent profile and the thermal state that preceded the BSODs of 2026-10-16/23);
|
|
# * two consecutive samples are more than 10 s apart (the machine stalled): STALL is written, all GPU work must stop.
|
|
# monitor.sh PID OUT.tsv
|
|
# Columns: unix time, VRAM used MiB (nvidia-smi, whole GPU), VmRSS, RssAnon, RssFile, RssShmem, VmLck, VmPin (MiB),
|
|
# MemAvailable (MiB, whole VM), VmSwap of the process (MiB), swap used by the VM (MiB), GPU SM and memory clock (MHz),
|
|
# board power (W), performance state, clocks_throttle_reasons.active (hex bitmask), power limit (W), temperature (C), utilisation (%).
|
|
# MON_INTERVAL (seconds, default 1) sets the sampling period. Prints the peaks and the mean clocks when the process is gone.
|
|
set -u
|
|
pid="$1"; out="$2"
|
|
interval="${MON_INTERVAL:-1}"
|
|
printf 't\tvram\trss\tanon\tfile\tshmem\tlck\tpin\tavail\tvmswap\tswapused\tsm_mhz\tmem_mhz\tpower_w\tpstate\tthrottle\tlimit_w\ttemp_c\tutil\n' > "$out"
|
|
rm -f "$out.abort"
|
|
field() { awk -v k="$1:" '$1==k {printf "%d", $2/1024}' "/proc/$pid/status" 2>/dev/null; }
|
|
abort() {
|
|
echo "$1" > "$out.abort"; echo "MONITOR ABORT: $1" >&2
|
|
pkill -TERM -P "$pid" 2>/dev/null; kill -TERM "$pid" 2>/dev/null
|
|
( sleep 20; pkill -KILL -P "$pid" 2>/dev/null; kill -KILL "$pid" 2>/dev/null ) &
|
|
}
|
|
prev=$(date +%s); aborted=0
|
|
while kill -0 "$pid" 2>/dev/null; do
|
|
gpu=$(nvidia-smi --query-gpu=memory.used,clocks.sm,clocks.mem,power.draw,pstate,clocks_throttle_reasons.active,power.limit,temperature.gpu,utilization.gpu --format=csv,noheader,nounits 2>/dev/null | head -1 | tr -d ' ')
|
|
IFS=, read -r vram smc memc pw ps thr lim temp util <<< "$gpu"
|
|
swapused=$(awk '$1=="SwapTotal:" {t=$2} $1=="SwapFree:" {f=$2} END {printf "%d", (t-f)/1024}' /proc/meminfo)
|
|
avail=$(awk '$1=="MemAvailable:" {printf "%d", $2/1024}' /proc/meminfo)
|
|
now=$(date +%s)
|
|
printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$now" "${vram:-0}" "$(field VmRSS)" "$(field RssAnon)" \
|
|
"$(field RssFile)" "$(field RssShmem)" "$(field VmLck)" "$(field VmPin)" "$avail" "$(field VmSwap)" "$swapused" \
|
|
"${smc:-0}" "${memc:-0}" "${pw:-0}" "${ps:-}" "${thr:-}" "${lim:-0}" "${temp:-0}" "${util:-0}" >> "$out"
|
|
if [ "$aborted" = 0 ]; then
|
|
if [ $((now - prev)) -gt 10 ]; then abort "STALL: $((now - prev)) s between samples"; aborted=1
|
|
elif awk -v l="${lim:-0}" 'BEGIN {exit !(l+0 > 0 && l+0 < 100)}'; then abort "power limit ${lim} W"; aborted=1
|
|
elif [ "${temp:-0}" -gt 90 ] 2>/dev/null; then abort "temperature ${temp} C"; aborted=1
|
|
elif [ "${ps:-}" = P5 ] && [ "${memc:-0}" -lt 2000 ] 2>/dev/null && [ $(( ${thr:-0x0} & 0x24 )) -ne 0 ] 2>/dev/null; then
|
|
abort "P5, memory ${memc} MHz, SW power cap / thermal slowdown active (reasons ${thr}): the OEM profile"; aborted=1
|
|
fi
|
|
fi
|
|
prev=$now
|
|
sleep "$interval"
|
|
done
|
|
awk -F'\t' 'NR>1 {for (i=2;i<=11;i++) if ($i+0>m[i]) m[i]=$i+0; if (min==""||$9+0<min) min=$9+0; if ($18+0>tm) tm=$18+0;
|
|
if ($12+0>0) {n++; sm+=$12; mm+=$13; pw+=$14}}
|
|
END {printf "peak MiB: vram=%d rss=%d anon=%d file=%d shmem=%d lck=%d pin=%d; min MemAvailable=%d; peak VmSwap=%d, VM swap used=%d; mean clocks sm=%d mem=%d MHz power=%.0f W; max temp=%d C\n",
|
|
m[2],m[3],m[4],m[5],m[6],m[7],m[8],min,m[10],m[11],(n?sm/n:0),(n?mm/n:0),(n?pw/n:0),tm}' "$out"
|
|
[ -f "$out.abort" ] && cat "$out.abort"
|