| 
									
										
										
										
											2021-05-20 07:42:54 +00:00
										 |  |  | #! /usr/bin/bash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Set up help text | 
					
						
							|  |  |  | command_name=$(basename $0) | 
					
						
							|  |  |  | 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 | 
					
						
							|  |  |  |   set WATTS [ARGS] Setts TDP to the requested number in watts | 
					
						
							| 
									
										
										
										
											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] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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-20 07:42:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Example: | 
					
						
							| 
									
										
										
										
											2021-05-20 08:46:14 +00:00
										 |  |  |   ${command_name} set 10 --detail | 
					
						
							|  |  |  |   ${command_name} s 10 -d | 
					
						
							| 
									
										
										
										
											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-20 07:42:54 +00:00
										 |  |  |   --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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Retrieves current TDP and prints it | 
					
						
							|  |  |  | check_tdp () { | 
					
						
							| 
									
										
										
										
											2021-05-20 08:46:14 +00:00
										 |  |  |   if [[ "$@" == *"--detail"* ]] || [[ "$@" == *"-d"* ]]; then | 
					
						
							| 
									
										
										
										
											2021-05-20 07:42:54 +00:00
										 |  |  |       echo "Placeholder" | 
					
						
							|  |  |  |       echo "Extra placeholder" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   elif [ -z "$1" ]; then | 
					
						
							|  |  |  |       echo "Placeholder" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   elif [ "$1" == "--help" ]; then | 
					
						
							|  |  |  |     print_help "check" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     print_unknown | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Sets TDP to number provided as first argument | 
					
						
							|  |  |  | 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 | 
					
						
							|  |  |  |   elif [ "$1" == "--help" ]; then | 
					
						
							|  |  |  |     print_help "set" | 
					
						
							|  |  |  |     exit | 
					
						
							| 
									
										
										
										
											2021-05-20 08:46:14 +00:00
										 |  |  |   elif ! [[ $1 =~ ^-?[0-9]+$ ]]; then | 
					
						
							|  |  |  |     echo "TDP is not a number or argument unknown!)" | 
					
						
							|  |  |  |     print_unknown | 
					
						
							|  |  |  |     exit | 
					
						
							| 
									
										
										
										
											2021-05-20 07:42:54 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-20 08:46:14 +00:00
										 |  |  |   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" | 
					
						
							| 
									
										
										
										
											2021-05-20 07:42:54 +00:00
										 |  |  |   else | 
					
						
							|  |  |  |     #PL1 | 
					
						
							|  |  |  |     local watts=$1 | 
					
						
							|  |  |  |     local uwatts=$(expr $watts '*' 1000000) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     #PL2 | 
					
						
							|  |  |  |     local watts2=$(expr $watts + 2) | 
					
						
							|  |  |  |     local uwatts2=$(expr $watts2 '*' 1000000) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     echo $uwatts | 
					
						
							|  |  |  |     echo "PL1 is now ${watts}W (Long-term)" | 
					
						
							| 
									
										
										
										
											2021-05-20 08:46:14 +00:00
										 |  |  |     if [[ "$@" == *"--detail"* ]] || [[ "$@" == *"-d"* ]]; then | 
					
						
							|  |  |  |       echo "PL2 is now ${watts2}W (Short-term)" | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2021-05-20 07:42:54 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Command handler | 
					
						
							|  |  |  | 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}" | 
					
						
							|  |  |  |     ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   "help") | 
					
						
							|  |  |  |     print_help | 
					
						
							|  |  |  |     ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *) | 
					
						
							|  |  |  |     print_unknown | 
					
						
							|  |  |  |     ;; | 
					
						
							|  |  |  | esac |