fwt_software/tools/thermal-sim/cap-power.sh

32 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Cap the CPU package power, as a hot box or the 6 W TDP mode would.
#
# sudo tools/thermal-sim/cap-power.sh 6 # long-term limit 6 W
# sudo tools/thermal-sim/cap-power.sh restore
#
# Writes the RAPL long-term limit (constraint 0) of intel-rapl:0. The original is
# kept in /run/fgc-cap-power.saved. A reboot also clears it. Intel only.
set -euo pipefail
saved=/run/fgc-cap-power.saved
rapl=/sys/class/powercap/intel-rapl:0
limit=$rapl/constraint_0_power_limit_uw
usage() { echo "usage: $0 <watts> | restore" >&2; exit 2; }
[[ $# -eq 1 ]] || usage
[[ $EUID -eq 0 ]] || { echo "$0: needs root (sudo)" >&2; exit 1; }
[[ -w $limit ]] || { echo "$0: no writable RAPL limit at $limit" >&2; exit 1; }
if [[ $1 == restore ]]; then
[[ -f $saved ]] || { echo "nothing to restore (no cap set since boot)"; exit 0; }
cat "$saved" > "$limit"
rm -f "$saved"
echo "power limit restored to $(( $(cat "$limit") / 1000000 )) W"
exit 0
fi
[[ $1 =~ ^[0-9]+$ ]] || usage
[[ -f $saved ]] || cat "$limit" > "$saved"
echo $(( $1 * 1000000 )) > "$limit"
echo "package power capped at $1 W (was $(( $(cat "$saved") / 1000000 )) W; '$0 restore' to undo)"