#!/bin/bash
pid=$(pgrep -f "python.*main.py" | head -1)
echo "PID: $pid"

# Check ALL threads for running state
echo "=== All thread states ==="
for tid in $(ls /proc/$pid/task/ 2>/dev/null); do
    stat=$(cat /proc/$pid/task/$tid/stat 2>/dev/null)
    state=$(echo "$stat" | awk '{print $3}')
    cpu=$(echo "$stat" | awk '{print $14+$15}')  # utime+stime
    wchan=$(cat /proc/$pid/task/$tid/wchan 2>/dev/null)
    if [ "$state" = "R" ] || [ "$cpu" -gt 1000 ] 2>/dev/null; then
        echo "  ** TID $tid: state=$state cpu=$cpu wchan=$wchan **"
    fi
done

echo ""
echo "=== Running threads only ==="
for tid in $(ls /proc/$pid/task/ 2>/dev/null); do
    state=$(cat /proc/$pid/task/$tid/stat 2>/dev/null | awk '{print $3}')
    if [ "$state" = "R" ]; then
        echo "  RUNNING: TID $tid"
        cat /proc/$pid/task/$tid/wchan 2>/dev/null
    fi
done

echo ""
echo "=== Top CPU threads (last column is cumulative CPU ticks) ==="
for tid in $(ls /proc/$pid/task/ 2>/dev/null); do
    stat=$(cat /proc/$pid/task/$tid/stat 2>/dev/null)
    utime=$(echo "$stat" | awk '{print $14}')
    stime=$(echo "$stat" | awk '{print $15}')
    total=$((utime + stime))
    state=$(echo "$stat" | awk '{print $3}')
    if [ "$total" -gt 100 ]; then
        echo "  TID $tid: state=$state total_ticks=$total"
    fi
done

echo ""
echo "=== py-spy dump (venv) ==="
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope > /dev/null 2>&1
/home/fabian/ComfyUI/venv/bin/py-spy dump --pid $pid 2>&1 | head -80

echo ""
echo "=== VRAM check ==="
rocm-smi --showmeminfo vram 2>/dev/null | grep -E "Used|Total"
