Skip to content

Instantly share code, notes, and snippets.

@shmerl
Last active March 3, 2024 23:10
Show Gist options
  • Save shmerl/63ffb070bfc61710447495a7b37202a7 to your computer and use it in GitHub Desktop.
Save shmerl/63ffb070bfc61710447495a7b37202a7 to your computer and use it in GitHub Desktop.
AMD GPU power cap
#!/bin/bash
# Note: make sure the card / hwmon indexes correspond to your GPU!
# They can differ depending on your set up.
gpu_hwmon="/sys/class/drm/card0/device/hwmon/hwmon2"
# Takes cap parameter in Watts. "max" sets it to max allowed.
cap_watts=$1
# Internal caps values are in microwatts
min_cap=$(cat "${gpu_hwmon}/power1_cap_min")
max_cap=$(cat "${gpu_hwmon}/power1_cap_max")
cur_cap=$(cat "${gpu_hwmon}/power1_cap")
function show_caps() {
echo "Current GPU power cap: ${cur_cap} ($((cur_cap / 1000000)) Watts)."
echo "Maximum GPU power cap: ${max_cap} ($((max_cap / 1000000)) Watts)."
echo "Minimum GPU power cap: ${min_cap} ($((min_cap / 1000000)) Watts)."
}
if (($# == 0)); then
show_caps
exit 0
fi
if [[ "$cap_watts" == "max" ]]; then
cap_microwatts=$max_cap
else
cap_microwatts="${cap_watts}000000"
fi
num_regex='^[0-9]+$'
if (($# > 1)) || ! [[ "$cap_microwatts" =~ $num_regex ]] || (( ($cap_microwatts < $min_cap) || ($cap_microwatts > $max_cap) )); then
echo "Invalid parameters! Set numeric cap between $((min_cap / 1000000)) and $((max_cap / 1000000)) Watts."
exit 1
fi
echo $cap_microwatts | sudo tee "${gpu_hwmon}/power1_cap" > /dev/null
cur_cap=$(cat "${gpu_hwmon}/power1_cap")
show_caps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment