#!/bin/bash
pid=$(pgrep -f "python.*main.py" | head -1)
echo "PID: $pid"
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 -50
echo ""
echo "=== Thread states ==="
for tid in $(ls /proc/$pid/task/ 2>/dev/null); do
    state=$(cat /proc/$pid/task/$tid/stat 2>/dev/null | awk '{print $3}')
    ticks=$(cat /proc/$pid/task/$tid/stat 2>/dev/null | awk '{print $14+$15}')
    if [ "$state" = "R" ] || [ "$ticks" -gt 1000 ] 2>/dev/null; then
        echo "  TID $tid: state=$state ticks=$ticks"
    fi
done
echo ""
echo "=== Last 5 log ==="
tail -5 /home/fabian/comfyui8.log
