#!/bin/bash
# Deep diagnostic: strace + py-spy + /proc analysis
pid=$(pgrep -f "python.*main.py" | head -1)
if [ -z "$pid" ]; then
    echo "NO PROCESS"
    exit 1
fi
echo "PID: $pid"

# What files does it have open? (compiler artifacts?)
echo "=== Open files (interesting ones) ==="
ls -la /proc/$pid/fd 2>/dev/null | wc -l
readlink /proc/$pid/fd/* 2>/dev/null | grep -iE "comgr|miopen|\.co|\.hsaco|\.hip|tmp|cache|rocm" | head -20

echo ""
echo "=== Check comgr cache ==="
find /home/fabian/.cache/ -name "*.co" -o -name "*.hsaco" 2>/dev/null | head -20
du -sh /home/fabian/.cache/comgr/ 2>/dev/null
du -sh /home/fabian/.cache/miopen/ 2>/dev/null

echo ""
echo "=== strace snapshot (2 seconds, top syscalls) ==="
timeout 3 strace -p $pid -c 2>&1 | tail -25

echo ""
echo "=== py-spy dump ==="
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope > /dev/null 2>&1
py-spy dump --pid $pid 2>&1 | head -50
