54 lines
3.6 KiB
Bash
54 lines
3.6 KiB
Bash
#!/usr/bin/env bash
|
|
# Safety sampler for a laptop GPU run of the product engine (`leleka up`, route A inside WSL2). Run it in the background for
|
|
# the length of one stretch; it samples the GPU, the VM memory and the engine log, and on a trip it stops the engine.
|
|
#
|
|
# lap_guard.sh OUT.tsv ENGINE_LOG LELEKA_BIN [INTERVAL_S=15] (stop it with: touch OUT.tsv.done)
|
|
#
|
|
# Trips (the owner's safety protocol):
|
|
# TEMP GPU temperature >= 85 C
|
|
# P5CAP performance state P5 with the SW power cap / SW thermal slowdown reasons (0x4 | 0x20) and the memory clock under 2 GHz
|
|
# (the OEM profile of the 0x19C blue screens); a plain P5 at idle is not a trip
|
|
# LIMIT power limit under 100 W while the GPU is busy
|
|
# MEM MemAvailable of the VM under 2,500 MiB
|
|
# STALL the engine log has not grown for 300 s while the GPU utilisation was >= 95 % in the last 8 samples
|
|
# TURBO the status file of turbo-watchdog.ps1 is fresh and says ok:false (turbo_status.sh; absent or stale is not a trip)
|
|
# GAP two samples more than 4 intervals apart (the machine stalled): all GPU work must stop
|
|
# A trip writes OUT.tsv.abort and runs `LELEKA_BIN down`, then TERMs leleka-inference. Columns of OUT.tsv: unix time, temperature,
|
|
# pstate, SM MHz, memory MHz, power W, limit W, throttle reasons, utilisation, VRAM MiB, MemAvailable MiB, engine log bytes,
|
|
# engine RSS MiB (leleka-inference).
|
|
set -u
|
|
out="$1"; log="$2"; bin="$3"; interval="${4:-15}"
|
|
rm -f "$out.abort" "$out.done"
|
|
printf 't\ttemp_c\tpstate\tsm_mhz\tmem_mhz\tpower_w\tlimit_w\tthrottle\tutil\tvram_mib\tavail_mib\tlog_bytes\n' > "$out"
|
|
trip() {
|
|
echo "$1 at $(date -u +%FT%TZ)" > "$out.abort"
|
|
echo "GUARD TRIP: $1" >&2
|
|
"$bin" down >> "$out.abort" 2>&1 || true
|
|
pkill -TERM -f bin/leleka-inference 2>/dev/null || true
|
|
exit 3
|
|
}
|
|
prev=$(date +%s); busy=0; log_size=0; log_changed=$prev
|
|
while [ ! -e "$out.done" ]; do
|
|
IFS=, read -r temp ps smc memc pw thr lim util vram < <(nvidia-smi \
|
|
--query-gpu=temperature.gpu,pstate,clocks.sm,clocks.mem,power.draw,clocks_throttle_reasons.active,power.limit,utilization.gpu,memory.used \
|
|
--format=csv,noheader,nounits 2>/dev/null | tr -d ' ')
|
|
# the query field power.limit reads [N/A] on this laptop: take the limit from the POWER section
|
|
lim=$(nvidia-smi -q -d POWER 2>/dev/null | awk -F: '/Current Power Limit/ && $2 !~ /N\/A/ {gsub(/[ W]/, "", $2); printf "%d", $2; exit}')
|
|
now=$(date +%s)
|
|
avail=$(awk '$1=="MemAvailable:" {printf "%d", $2/1024}' /proc/meminfo)
|
|
size=$(stat -c %s "$log" 2>/dev/null || echo 0)
|
|
if [ "$size" != "$log_size" ]; then log_size=$size; log_changed=$now; fi
|
|
printf '%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" "${temp:-0}" "${ps:-}" "${smc:-0}" "${memc:-0}" "${pw:-0}" "${lim:-0}" \
|
|
"${thr:-0x0}" "${util:-0}" "${vram:-0}" "$avail" "$size" "${rss:-0}" >> "$out"
|
|
if [ "${util:-0}" -ge 95 ] 2>/dev/null; then busy=$((busy + 1)); else busy=0; fi
|
|
[ $((now - prev)) -gt $((interval * 4)) ] && trip "GAP $((now - prev)) s between samples"
|
|
[ "${temp:-0}" -ge 85 ] 2>/dev/null && trip "TEMP ${temp} C"
|
|
[ "${ps:-}" = P5 ] && [ "${memc:-0}" -lt 2000 ] 2>/dev/null && [ $(( ${thr:-0x0} & 0x24 )) -ne 0 ] 2>/dev/null && trip "P5CAP reasons ${thr}"
|
|
[ "${util:-0}" -ge 50 ] 2>/dev/null && awk -v l="${lim:-0}" 'BEGIN {exit !(l+0 > 0 && l+0 < 100)}' && trip "LIMIT ${lim} W"
|
|
[ "$avail" -lt 2500 ] && trip "MEM ${avail} MiB"
|
|
turbo=$(bash "$(dirname "${BASH_SOURCE[0]}")/turbo_status.sh"); [ "${turbo%% *}" = BAD ] && trip "TURBO watchdog: $turbo"
|
|
[ "$busy" -ge 8 ] && [ $((now - log_changed)) -gt 300 ] && trip "STALL log idle $((now - log_changed)) s with the GPU busy"
|
|
prev=$now
|
|
sleep "$interval"
|
|
done
|