strawberry/scripts/watch_cpu_capture.sh
trvon 7a7fa81c74
profiling: watch_cpu_capture.sh takes one Tracy capture when a process spins
Polls a profiled process's CPU every 5 s and, after sustained use above a
threshold, attaches scripts/profile_capture.sh for a fixed window. For bugs
that only appear sometimes (the tray spinning a core during the 2026-11-14
hang). Verified attach on the live profiled tray: zones tray.on_reply /
update_status / update_tray / poll arrive (~4 ms per 3 s poll when healthy).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 18:52:09 -07:00

45 lines
1.6 KiB
Bash
Executable file

#!/usr/bin/env bash
# Wait for a profiled process (built with -Denable_profiling=true) to burn CPU, then take one
# Tracy capture of it (scripts/profile_capture.sh attach). For bugs that only show up sometimes,
# like the tray spinning a core: leave this running and look at the capture afterwards.
#
# scripts/watch_cpu_capture.sh NAME OUT [THRESHOLD_PCT=50] [SUSTAIN_CHECKS=3] [CAPTURE_S=20] [MAX_HOURS=24]
#
# NAME is the process name (pgrep -x). Checks every 5 s; captures after SUSTAIN_CHECKS consecutive
# checks at or above THRESHOLD_PCT of one core, then exits. Exits 3 if nothing happened in MAX_HOURS.
set -euo pipefail
name=${1:?process name}
out=${2:?output prefix}
threshold=${3:-50}
sustain=${4:-3}
capture_s=${5:-20}
max_hours=${6:-24}
here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
hz=$(getconf CLK_TCK)
deadline=$(( $(date +%s) + max_hours * 3600 ))
hot=0
prev_pid="" prev_ticks=0
while [ "$(date +%s)" -lt "$deadline" ]; do
pid=$(pgrep -x "$name" | head -1 || true)
if [ -z "$pid" ]; then
hot=0 prev_pid=""
sleep 5
continue
fi
ticks=$(awk '{print $14 + $15}' "/proc/$pid/stat" 2>/dev/null || echo 0)
if [ "$pid" = "$prev_pid" ]; then
pct=$(( (ticks - prev_ticks) * 100 / (hz * 5) ))
if [ "$pct" -ge "$threshold" ]; then hot=$((hot + 1)); else hot=0; fi
if [ "$hot" -ge "$sustain" ]; then
echo "$(date -Is) $name (pid $pid) at ${pct}% for $((hot * 5)) s: capturing ${capture_s} s"
"$here/profile_capture.sh" attach "$out" "$capture_s"
exit 0
fi
fi
prev_pid=$pid prev_ticks=$ticks
sleep 5
done
echo "no sustained CPU in $max_hours h"
exit 3