amd-monitor/amd-monitor.sh
mrkmntal 6a048792eb
All checks were successful
Generate Power Graph / plot (push) Successful in 2m12s
Hour power update
2025-11-07 13:09:17 -05:00

72 lines
2.1 KiB
Bash
Executable file

#!/bin/bash
# AMD Laptop APU + GPU Live Monitor for ASUS TUF (Debian 13)
# Author: MarkMental
# --- Configuration ---
APU_HWMON="/sys/class/hwmon/hwmon5" # APU sensor
GPU_HWMON="/sys/class/drm/card0/device/hwmon/hwmon4" # dGPU sensor
INTERVAL=1 # seconds between refreshes
LOGFILE="power_log.csv" # CSV output file
RETENTION_SECONDS=3600 # keep roughly 1 hour of samples
# --- Helpers ---
read_watts() {
local file=$1
if [ -f "$file" ]; then
awk '{printf "%.2f", $1/1000000}' "$file"
else
echo "0.00"
fi
}
read_temp() {
local file=$1
if [ -f "$file" ]; then
awk '{printf "%.1f", $1/1000}' "$file"
else
echo "N/A"
fi
}
# --- Ensure CSV exists and has header ---
if [ ! -f "$LOGFILE" ]; then
echo "timestamp,apu_w,gpu_w,total_w,apu_temp,gpu_temp" > "$LOGFILE"
fi
# --- Main loop ---
clear
echo "🧠 AMD APU / GPU Live Power Monitor (Debian 13 TUF)"
echo "--------------------------------------------------"
while true; do
APU_PWR=$(read_watts "$APU_HWMON/power1_input")
GPU_PWR=$(read_watts "$GPU_HWMON/power1_average")
APU_TEMP=$(read_temp "$APU_HWMON/temp1_input")
GPU_TEMP=$(read_temp "$GPU_HWMON/temp1_input")
TOTAL=$(awk -v a="$APU_PWR" -v g="$GPU_PWR" 'BEGIN {printf "%.2f", a+g}')
TIME=$(date -Iseconds)
# --- Print live readout ---
tput cup 2 0
echo "APU Power: ${APU_PWR} W | Temp: ${APU_TEMP} °C "
echo "GPU Power: ${GPU_PWR} W | Temp: ${GPU_TEMP} °C "
echo "-----------------------------------------------"
echo "Total Power: ${TOTAL} W"
echo "Press Ctrl+C to exit"
# --- Append to CSV ---
echo "$TIME,$APU_PWR,$GPU_PWR,$TOTAL,$APU_TEMP,$GPU_TEMP" >> "$LOGFILE"
# --- Keep roughly 1 hour of samples (plus header) ---
MAX_SAMPLES=$(( (RETENTION_SECONDS + INTERVAL - 1) / INTERVAL ))
MAX_LINES_WITH_HEADER=$(( MAX_SAMPLES + 1 ))
LINES=$(wc -l < "$LOGFILE")
if (( LINES > MAX_LINES_WITH_HEADER )); then
{
head -n 1 "$LOGFILE"
tail -n "$MAX_SAMPLES" "$LOGFILE"
} > "${LOGFILE}.tmp" && mv "${LOGFILE}.tmp" "$LOGFILE"
fi
sleep $INTERVAL
done