dotfiles/.config/waybar/gpu-usage.sh

25 lines
939 B
Bash
Executable file

#!/bin/sh
declare -i hwmon_nr_amd=$(( $(cat -n /sys/class/hwmon/hwmon*/name | grep 'amdgpu' | grep -oP '\d+') - 1))
if (( $hwmon_nr_amd >= 0 )); then
hwmon_path="/sys/class/hwmon/hwmon$hwmon_nr_amd"
usage=$(cat "$hwmon_path/device/gpu_busy_percent")
vram=$(awk '{ print int($1/1024/1024) }' "$hwmon_path/device/mem_info_vram_used")
power=$(head -c -7 "$hwmon_path/power1_average")
elif which nvidia-smi &> /dev/null; then
usage=$(nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits)
vram=$(nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits)
power=$(nvidia-smi --query-gpu=power.draw --format=csv,noheader,nounits | awk '{ print int($1) }')
fi
if [[ $vram -ge 1024 ]]; then
vram=$vram/1024
vram_unit="GiB"
else
vram_unit="MiB"
fi
cat << EOF
{"text": "$usage%", "alt": "", "tooltip": "GPU usage: $usage%\nVRAM usage: ${vram}$vram_unit\nPower draw: ${power}W", "class": "", "percentage": ""}
EOF