win3-resources/scripts/tdp

317 lines
7.2 KiB
Plaintext
Raw Permalink Normal View History

2021-05-20 07:42:54 +00:00
#! /usr/bin/bash
2021-05-20 10:10:29 +00:00
# Global vars/settings
2021-05-20 07:42:54 +00:00
command_name=$(basename $0)
2021-05-20 13:40:59 +00:00
rapl_path="/sys/class/powercap/intel-rapl:0"
2021-05-20 10:40:52 +00:00
pl1_path="${rapl_path}/constraint_0_power_limit_uw"
pl2_path="${rapl_path}/constraint_1_power_limit_uw"
2021-05-22 13:08:41 +00:00
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
gui_name="GPD TDP Manager"
gui_options=""
tdp_min=5
tdp_max=35
2021-05-20 10:10:29 +00:00
# Set up help text
2021-05-20 07:42:54 +00:00
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:
2021-05-20 08:46:14 +00:00
check [ARGS] Checks current TDP and prints it in watts
2021-05-20 10:58:07 +00:00
c [ARGS] Shortened version of check
set WATTS [ARGS] Sets TDP to the requested number in watts
s WATTS [ARGS] Shortened version of set
2021-05-22 13:10:05 +00:00
gui Runs graphical user interface
2021-05-20 10:58:07 +00:00
2021-05-20 07:42:54 +00:00
help Prints this help text
2021-05-20 08:46:14 +00:00
COMMAND --help Prints help for specified command"
2021-05-20 07:42:54 +00:00
check_help_text="${command_name} check [ARG]
2021-05-21 08:37:13 +00:00
Short command: c
2021-05-20 07:42:54 +00:00
Example:
${command_name} check --detail
2021-05-20 08:46:14 +00:00
${command_name} c -d
2021-05-20 07:42:54 +00:00
Arguments:
--detail, -d Prints PL2 as well as PL1
--help Prints this help text"
2021-05-20 08:46:14 +00:00
set_help_text="${command_name} set WATT [ARGUMENTS]
2021-05-21 08:37:13 +00:00
Short command: s
2021-05-20 07:42:54 +00:00
Example:
2021-05-21 08:37:13 +00:00
${command_name} set 10 --detail --same
${command_name} s 10 -d -s
2021-05-20 07:42:54 +00:00
Arguments:
2021-05-20 08:46:14 +00:00
--detail, -d Prints PL2 as well as PL1
2021-05-21 08:37:13 +00:00
--same, -s PL2 will be set to the same as PL1 rather than 2W higher
2021-05-20 07:42:54 +00:00
--help Prints this help text"
2021-05-22 13:08:41 +00:00
gui_help_text="${command_name} gui
No help is available for the GUI.
If it does not work, ensure the \"zenity\" command is installed and available"
2021-05-20 07:42:54 +00:00
print_help () {
case $1 in
"check")
echo "$check_help_text"
;;
"set")
echo "$set_help_text"
;;
2021-05-22 13:10:05 +00:00
"gui")
2021-05-22 13:08:41 +00:00
echo "$gui_help_text"
;;
2021-05-20 07:42:54 +00:00
*)
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
2021-05-20 10:10:29 +00:00
# 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
}
2021-05-20 10:40:52 +00:00
# Common checks/tasks
is_detailed () {
2021-05-20 08:46:14 +00:00
if [[ "$@" == *"--detail"* ]] || [[ "$@" == *"-d"* ]]; then
2021-05-20 10:40:52 +00:00
exit 0
else
exit 1
2021-05-20 10:10:29 +00:00
fi
2021-05-20 10:40:52 +00:00
}
2021-05-20 07:42:54 +00:00
2021-05-22 13:08:41 +00:00
is_help () {
if [[ "$@" == *"--help"* ]] || [[ "$@" == *"-h"* ]]; then
exit 0
else
exit 1
fi
}
is_number () {
if ! [[ $1 =~ ^-?[0-9]+$ ]]; then
exit 1
else
exit 0
fi
}
gui_msg () {
zenity --info --title="$gui_name" $gui_options --text="$1" --no-wrap
}
gui_read () {
zenity --entry --title="$gui_name" $gui_options --text="$1:"
}
gui_ask () {
if $(zenity --question --title="$gui_name" $gui_options --text="$1" --no-wrap); then
exit 0
else
exit 1
fi
}
2021-05-20 10:40:52 +00:00
set_pl () {
if [ $1 -eq 1 ] || [ $1 -eq 2 ] || [ ! -z $2 ]; then
if [ $1 -eq 1 ]; then
pl_path=$pl1_path
elif [ $1 -eq 2 ]; then
pl_path=$pl2_path
fi
if [ "$EUID" -eq 0 ]; then
echo $(w_to_uw $2) > $pl_path
2021-05-22 21:33:08 +00:00
elif ! [ -z $gui_used ]; then
2021-05-22 17:01:02 +00:00
sudo -A bash -c "echo $(w_to_uw $2) > ${pl_path}"
else
sudo bash -c "echo $(w_to_uw $2) > ${pl_path}"
fi
2021-05-20 10:40:52 +00:00
else
exit 1
fi
}
get_pl () {
if [ $1 -eq 1 ]; then
cat $pl1_path
elif [ $1 -eq 2 ]; then
cat $pl2_path
fi
}
# Retrieves current TDP and prints it
check_tdp () {
if [ -z "$1" ] || ( is_detailed $@ ); then
local pl1=$(uw_to_w $(get_pl 1))
local pl2=$(uw_to_w $(get_pl 2))
2021-05-23 10:28:49 +00:00
echo "PL1 is ${pl1}W"
2021-05-20 10:40:52 +00:00
if ( is_detailed $@ ); then
2021-05-23 10:28:49 +00:00
echo "PL2 is ${pl2}W"
2021-05-20 10:40:52 +00:00
fi
2021-05-20 07:42:54 +00:00
2021-05-22 13:08:41 +00:00
elif ( is_help $@ ); then
2021-05-20 07:42:54 +00:00
print_help "check"
else
print_unknown
fi
}
2021-05-20 10:10:29 +00:00
# Sets PL1 to number provided as first argument, and PL2 2W higher
2021-05-20 07:42:54 +00:00
set_tdp () {
if [ -z "$1" ]; then
2021-05-20 08:46:14 +00:00
echo "Please specify wattage"
2021-05-20 07:42:54 +00:00
exit
2021-05-22 13:08:41 +00:00
elif ( is_help $@ ); then
2021-05-20 07:42:54 +00:00
print_help "set"
exit
2021-05-22 13:08:41 +00:00
elif ! (is_number $1); then
2021-05-20 08:46:14 +00:00
echo "TDP is not a number or argument unknown!)"
print_unknown
exit
2021-05-20 07:42:54 +00:00
fi
if [ $1 -lt $tdp_min ] || [ $1 -gt $tdp_max ]; then
echo "TDP too high or low, should be between ${tdp_min}W and ${tdp_max}W"
echo "This is mostly a sanity limit to prevent you from throttling to a near unusable state"
2021-05-20 07:42:54 +00:00
else
#PL1
local watts=$1
2021-05-20 10:40:52 +00:00
set_pl 1 $watts
2021-05-20 07:42:54 +00:00
#PL2
local watts2=$(expr $watts + 2)
if [[ "$@" == *"--same"* ]] || [[ "$@" == *"-s"* ]]; then
2021-05-21 08:28:20 +00:00
watts2=$watts
fi
2021-05-20 10:40:52 +00:00
set_pl 2 $watts2
2021-05-20 07:42:54 +00:00
echo "PL1 is now ${watts}W (Long-term)"
2021-05-20 10:40:52 +00:00
if ( is_detailed $@ ); then
2021-05-20 08:46:14 +00:00
echo "PL2 is now ${watts2}W (Short-term)"
fi
2021-05-20 07:42:54 +00:00
fi
}
2021-05-22 13:08:41 +00:00
# Basic GUI using Zenity
gui_handler() {
if [ -z $(which zenity 2>/dev/null) ]; then
echo "Zenity is not available, GUI will not work until it is installed"
elif (is_help $@); then
print_help "gui"
else
2021-05-22 17:17:13 +00:00
# Enables graphical sudo prompt
2021-05-22 13:08:41 +00:00
export SUDO_ASKPASS="${script_dir}/gpd-sudo-prompt"
2021-05-22 17:01:02 +00:00
export gui_used=1
2021-05-22 13:08:41 +00:00
while : ; do
2021-05-22 17:17:13 +00:00
# Clear variables
gui_tdp=""
2021-05-22 13:08:41 +00:00
# Contains some ugly hacks to "widen" the entries for the small GPD screen, might tweak later
gui_action=$(zenity --list --title="${gui_name}" --width=400 --height=350 $gui_options --text="Choose action to perform:" --hide-column=1 --column="" --column="Action"\
2021-05-22 13:08:41 +00:00
check "`printf "\n Check current TDP\n "`" \
2021-05-22 21:24:48 +00:00
preset "`printf "\n Change TDP (Will ask for password)\n "`" \
set "`printf "\n Set custom TDP (Will ask for password)\n "`" \
2021-05-22 13:08:41 +00:00
exit "`printf "\n Exit TDP Manager\n "`")
case $gui_action in
"check")
2021-05-23 10:28:49 +00:00
gui_msg "$($command_name check --detail)"
2021-05-22 13:08:41 +00:00
;;
2021-05-22 17:17:13 +00:00
"preset")
2021-05-23 10:28:49 +00:00
gui_tdp=$(
zenity --list --title "${gui_name}" --width=400 --height=350 $gui_options --text="Choose TDP to set:" --hide-column=1 --column="" --column="Wattage" \
2021-05-23 10:28:49 +00:00
8 "`printf "\n 8W (Ultra-low, for simple 2D games)\n "`" \
12 "`printf "\n 12W (Low power)\n "`" \
15 "`printf "\n 15W (Balanced)\n "`" \
20 "`printf "\n 20W (High power)\n "`" \
25 "`printf "\n 25W (Very-high power)\n "`" \
35 "`printf "\n 35W (Max power / Docked)\n "`"
2021-05-23 10:28:49 +00:00
)
if ! [ -z $gui_tdp ]; then
gui_msg "$(tdp set $gui_tdp --detail --same)"
2021-05-23 10:28:49 +00:00
fi
2021-05-22 17:17:13 +00:00
;;
2021-05-22 13:08:41 +00:00
"set")
2021-05-23 10:28:49 +00:00
gui_tdp=$(gui_read "Please enter TDP")
if ! [ -z $gui_tdp ]; then
if $(gui_ask "Should PL2 be set to the same wattage?\nIf unsure, answer No."); then
gui_msg "$(tdp set $gui_tdp --same --detail)"
else
gui_msg "$(tdp set $gui_tdp --detail)"
fi
gui_tdp=""
2021-05-22 13:08:41 +00:00
fi
;;
*)
break
;;
esac
done
fi
}
if ! ls $pl1_path > /dev/null 2>&1 || ! ls $pl2_path > /dev/null 2>&1; then
echo "Could not find intel_rapl endpoints, are you using a GPD Win 3?"
exit
fi
2021-05-20 07:42:54 +00:00
# Command handler
2021-05-22 17:17:13 +00:00
# This checks which command was entered and forwards all other arguments to it
2021-05-20 07:42:54 +00:00
case $1 in
2021-05-20 08:46:14 +00:00
"check" | "c")
2021-05-20 07:42:54 +00:00
check_tdp "${@:2}"
;;
2021-05-20 08:46:14 +00:00
"set" | "s")
2021-05-20 07:42:54 +00:00
set_tdp "${@:2}"
;;
2021-05-22 13:10:05 +00:00
"gui") # Pass to GUI handler
2021-05-22 13:08:41 +00:00
gui_handler "${@:2}"
;;
2021-05-20 07:42:54 +00:00
"help")
print_help
;;
*)
print_unknown
;;
esac