#!/bin/bash
# =============================================================
# BC-250 GPU Activity Monitor
# Since rocm-smi GPU utilization is broken (always 0%),
# we use alternative metrics to verify GPU compute usage.
# =============================================================

echo "=== BC-250 GPU Activity Monitor ==="
echo "NOTE: GPU % utilization is broken on this hardware."
echo "Using alternative metrics instead."
echo ""

while true; do
    TIMESTAMP=$(date '+%H:%M:%S')
    
    # 1. GPU Clock — high clock = GPU active
    SCLK=$(cat /sys/class/drm/card0/device/pp_dpm_sclk 2>/dev/null | grep '\*' | awk '{print $2}')
    
    # 2. GPU power draw (if available)
    POWER=$(cat /sys/class/drm/card0/device/hwmon/hwmon*/power1_average 2>/dev/null)
    if [ -n "$POWER" ]; then
        POWER_W=$(echo "scale=1; $POWER / 1000000" | bc 2>/dev/null || echo "N/A")
    else
        POWER_W="N/A"
    fi
    
    # 3. GPU VRAM usage (shared memory allocated by GPU)
    VRAM_USED=$(cat /sys/class/drm/card0/device/mem_info_vram_used 2>/dev/null)
    VRAM_TOTAL=$(cat /sys/class/drm/card0/device/mem_info_vram_total 2>/dev/null)
    if [ -n "$VRAM_USED" ] && [ -n "$VRAM_TOTAL" ]; then
        VRAM_MB=$(echo "scale=0; $VRAM_USED / 1048576" | bc 2>/dev/null || echo "N/A")
        VRAM_TOTAL_MB=$(echo "scale=0; $VRAM_TOTAL / 1048576" | bc 2>/dev/null || echo "N/A")
    else
        VRAM_MB="N/A"
        VRAM_TOTAL_MB="N/A"
    fi
    
    # 4. GPU temperature
    TEMP=$(cat /sys/class/drm/card0/device/hwmon/hwmon*/temp1_input 2>/dev/null)
    if [ -n "$TEMP" ]; then
        TEMP_C=$(echo "scale=0; $TEMP / 1000" | bc 2>/dev/null || echo "N/A")
    else
        TEMP_C="N/A"
    fi
    
    # 5. HIP processes using GPU
    HIP_PROCS=$(ls /proc/*/maps 2>/dev/null | xargs grep -l "libamdhip64\|libhsa-runtime" 2>/dev/null | wc -l)
    
    # 6. Kernel GPU activity (interrupts)
    GPU_IRQ=$(cat /proc/interrupts 2>/dev/null | grep amdgpu | awk '{sum=0; for(i=2;i<=NF-2;i++) sum+=$i; print sum}' | head -1)
    
    # 7. System RAM (since BC-250 shares system RAM as VRAM)
    RAM_INFO=$(free -m | grep Mem | awk '{printf "%dMB / %dMB (%.0f%%)", $3, $2, $3/$2*100}')
    
    echo "[$TIMESTAMP] Clock: ${SCLK:-N/A} | Power: ${POWER_W}W | Temp: ${TEMP_C}°C | VRAM: ${VRAM_MB}/${VRAM_TOTAL_MB}MB | HIP Procs: $HIP_PROCS | RAM: $RAM_INFO"
    
    sleep 2
done
