#!/bin/bash

# GPU monitoring script for testing concurrent processing
# Usage: ./monitor_gpu.sh [output_file]

OUTPUT_FILE="${1:-gpu_metrics.log}"

echo "Starting GPU monitoring - output to $OUTPUT_FILE"
echo "Press Ctrl+C to stop"
echo "================================"

# Clear previous log
> "$OUTPUT_FILE"

# Add header
echo "Timestamp, Power(W), Temp(C), SM%, Mem%, FB(MB), PCIe-RX(MB/s), PCIe-TX(MB/s)" | tee "$OUTPUT_FILE"

# Monitor continuously with 1 second intervals
while true; do
    TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

    # Get GPU metrics (skip header lines)
    METRICS=$(nvidia-smi dmon -s pucvmet -c 1 | tail -1)

    # Parse metrics (columns: gpu pwr gtemp mtemp sm mem enc dec jpg ofa mclk pclk pviol tviol fb bar1 ccpm sbecc dbecc pci rxpci txpci)
    PWR=$(echo "$METRICS" | awk '{print $2}')
    TEMP=$(echo "$METRICS" | awk '{print $3}')
    SM=$(echo "$METRICS" | awk '{print $5}')
    MEM=$(echo "$METRICS" | awk '{print $6}')
    FB=$(echo "$METRICS" | awk '{print $15}')
    RXPCI=$(echo "$METRICS" | awk '{print $21}')
    TXPCI=$(echo "$METRICS" | awk '{print $22}')

    # Output to console and file
    echo "$TIMESTAMP, $PWR, $TEMP, $SM, $MEM, $FB, $RXPCI, $TXPCI" | tee -a "$OUTPUT_FILE"

    sleep 1
done
