152 lines
3.0 KiB
Bash
Executable File
152 lines
3.0 KiB
Bash
Executable File
#! /usr/bin/bash
|
|
|
|
# Global vars/settings
|
|
command_name=$(basename $0)
|
|
rapl_base="/sys/class/powercap/intel-rapl:0"
|
|
pl1_path="${rapl_base}/constraint_0_power_limit_uw"
|
|
pl2_path="${rapl_base}/constraint_1_power_limit_uw"
|
|
|
|
# Set up help text
|
|
help_text="${command_name}: GDP Win 3 TDP management script
|
|
|
|
This script checks and sets the TDP using intel_rapl
|
|
|
|
Usage:
|
|
${command_name} COMMAND [ARGUMENTS]
|
|
|
|
Command:
|
|
check [ARGS] Checks current TDP and prints it in watts
|
|
set WATTS [ARGS] Setts TDP to the requested number in watts
|
|
help Prints this help text
|
|
COMMAND --help Prints help for specified command"
|
|
|
|
check_help_text="${command_name} check [ARG]
|
|
|
|
Example:
|
|
${command_name} check --detail
|
|
${command_name} c -d
|
|
|
|
Arguments:
|
|
--detail, -d Prints PL2 as well as PL1
|
|
--help Prints this help text"
|
|
|
|
set_help_text="${command_name} set WATT [ARGUMENTS]
|
|
|
|
Example:
|
|
${command_name} set 10 --detail
|
|
${command_name} s 10 -d
|
|
|
|
Arguments:
|
|
--detail, -d Prints PL2 as well as PL1
|
|
--help Prints this help text"
|
|
|
|
print_help () {
|
|
case $1 in
|
|
"check")
|
|
echo "$check_help_text"
|
|
;;
|
|
"set")
|
|
echo "$set_help_text"
|
|
;;
|
|
*)
|
|
echo "$help_text"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
print_unknown () {
|
|
echo "Unknown command or incorrect arguments.
|
|
Try \"${command_name} help\" or \"${command_name} COMMAND --help\""
|
|
}
|
|
# End of help text
|
|
|
|
# Conversion tools
|
|
uw_to_w () {
|
|
if ! [[ $1 =~ ^-?[0-9]+$ ]]; then
|
|
exit
|
|
else
|
|
echo $(expr $1 / 1000000)
|
|
fi
|
|
}
|
|
|
|
w_to_uw () {
|
|
if ! [[ $1 =~ ^-?[0-9]+$ ]]; then
|
|
exit
|
|
else
|
|
echo $(expr $1 '*' 1000000)
|
|
fi
|
|
}
|
|
|
|
# Retrieves current TDP and prints it
|
|
check_tdp () {
|
|
if [[ "$@" == *"--detail"* ]] || [[ "$@" == *"-d"* ]]; then
|
|
local detailed=yes
|
|
fi
|
|
|
|
if [ -z "$1" ]; then
|
|
echo "PL1 is $(uw_to_w 10000000)W" # Placeholder
|
|
if [ $detailed == "yes" ]; then
|
|
echo "PL2 is $(uw_to_w 12000000)W" # Placeholder
|
|
|
|
elif [ "$1" == "--help" ]; then
|
|
print_help "check"
|
|
|
|
else
|
|
print_unknown
|
|
fi
|
|
}
|
|
|
|
# Sets PL1 to number provided as first argument, and PL2 2W higher
|
|
set_tdp () {
|
|
if [ -z "$1" ]; then
|
|
echo "Please specify wattage"
|
|
exit
|
|
elif [ "$1" == "--help" ]; then
|
|
print_help "set"
|
|
exit
|
|
elif ! [[ $1 =~ ^-?[0-9]+$ ]]; then
|
|
echo "TDP is not a number or argument unknown!)"
|
|
print_unknown
|
|
exit
|
|
fi
|
|
|
|
if [ $1 -lt 5 ] || [ $1 -gt 30 ]; then
|
|
echo "TDP too high or low, should be between 5W and 30W"
|
|
echo "This is a sanity limit to prevent you from throttling to a near unusable state"
|
|
else
|
|
#PL1
|
|
local watts=$1
|
|
local uwatts=$(w_to_uw $watts)
|
|
|
|
#PL2
|
|
local watts2=$(expr $watts + 2)
|
|
local uwatts2=$(w_to_uw $watts2)
|
|
|
|
echo $uwatts
|
|
echo "PL1 is now ${watts}W (Long-term)"
|
|
if [[ "$@" == *"--detail"* ]] || [[ "$@" == *"-d"* ]]; then
|
|
echo "PL2 is now ${watts2}W (Short-term)"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Command handler
|
|
case $1 in
|
|
|
|
"check" | "c")
|
|
check_tdp "${@:2}"
|
|
;;
|
|
|
|
"set" | "s")
|
|
set_tdp "${@:2}"
|
|
;;
|
|
|
|
"help")
|
|
print_help
|
|
;;
|
|
|
|
*)
|
|
print_unknown
|
|
;;
|
|
esac
|