Advertisement
Guest User

temp_throttle

a guest
Oct 11th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.63 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Usage: temp_throttle.sh max_temp
  4. # USE CELSIUS TEMPERATURES.
  5. # version 2.20
  6.  
  7. cat << EOF
  8. Author: Sepero 2016 (sepero 111 @ gmx . com)
  9. URL: http://github.com/Sepero/temp-throttle/
  10.  
  11. EOF
  12.  
  13. # Additional Links
  14. # http://seperohacker.blogspot.com/2012/10/linux-keep-your-cpu-cool-with-frequency.html
  15.  
  16. # Additional Credits
  17. # Wolfgang Ocker <weo AT weo1 DOT de> - Patch for unspecified cpu frequencies.
  18.  
  19. # License: GNU GPL 2.0
  20.  
  21. # Generic  function for printing an error and exiting.
  22. err_exit () {
  23.     echo ""
  24.     echo "Error: $@" 1>&2
  25.     exit 128
  26. }
  27.  
  28. if [ $# -ne 1 ]; then
  29.     # If temperature wasn't given, then print a message and exit.
  30.     echo "Please supply a maximum desired temperature in Celsius." 1>&2
  31.     echo "For example:  ${0} 60" 1>&2
  32.     exit 2
  33. else
  34.     #Set the first argument as the maximum desired temperature.
  35.     MAX_TEMP=$1
  36. fi
  37.  
  38.  
  39. ### START Initialize Global variables.
  40.  
  41. # The frequency will increase when low temperature is reached.
  42. LOW_TEMP=$((MAX_TEMP - 5))
  43.  
  44. CORES=$(nproc) # Get number of CPU cores.
  45. echo -e "Number of CPU cores detected: $CORES\n"
  46. CORES=$((CORES - 1)) # Subtract 1 from $CORES for easier counting later.
  47.  
  48. # Temperatures internally are calculated to the thousandth.
  49. MAX_TEMP=${MAX_TEMP}000
  50. LOW_TEMP=${LOW_TEMP}000
  51.  
  52. FREQ_FILE="/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies"
  53. FREQ_MIN="/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"
  54. FREQ_MAX="/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
  55.  
  56. # Store available cpu frequencies in a space separated string FREQ_LIST.
  57. if [ -f $FREQ_FILE ]; then
  58.     # If $FREQ_FILE exists, get frequencies from it.
  59.     FREQ_LIST=$(cat $FREQ_FILE) || err_exit "Could not read available cpu frequencies from file $FREQ_FILE"
  60. elif [ -f $FREQ_MIN -a -f $FREQ_MAX ]; then
  61.     # Else if $FREQ_MIN and $FREQ_MAX exist, generate a list of frequencies between them.
  62.     FREQ_LIST=$(seq $(cat $FREQ_MAX) -100000 $(cat $FREQ_MIN)) || err_exit "Could not compute available cpu frequencies"
  63. else
  64.     err_exit "Could not determine available cpu frequencies"
  65. fi
  66.  
  67. FREQ_LIST_LEN=$(echo $FREQ_LIST | wc -w)
  68.  
  69. # CURRENT_FREQ will save the index of the currently used frequency in FREQ_LIST.
  70. CURRENT_FREQ=2
  71.  
  72. # This is a list of possible locations to read the current system temperature.
  73. TEMPERATURE_FILES="
  74. /sys/class/thermal/thermal_zone0/temp
  75. /sys/class/thermal/thermal_zone1/temp
  76. /sys/class/thermal/thermal_zone2/temp
  77. /sys/class/hwmon/hwmon0/temp1_input
  78. /sys/class/hwmon/hwmon1/temp1_input
  79. /sys/class/hwmon/hwmon2/temp1_input
  80. /sys/class/hwmon/hwmon0/device/temp1_input
  81. /sys/class/hwmon/hwmon1/device/temp1_input
  82. /sys/class/hwmon/hwmon2/device/temp1_input
  83. null
  84. "
  85.  
  86. # Store the first temperature location that exists in the variable TEMP_FILE.
  87. # The location stored in $TEMP_FILE will be used for temperature readings.
  88. for file in $TEMPERATURE_FILES; do
  89.     TEMP_FILE=$file
  90.     [ -f $TEMP_FILE ] && break
  91. done
  92.  
  93. [ $TEMP_FILE == "null" ] && err_exit "The location for temperature reading was not found."
  94.  
  95.  
  96. ### END Initialize Global variables.
  97.  
  98.  
  99. ### START define script functions.
  100.  
  101. # Set the maximum frequency for all cpu cores.
  102. set_freq () {
  103.     # From the string FREQ_LIST, we choose the item at index CURRENT_FREQ.
  104.     FREQ_TO_SET=$(echo $FREQ_LIST | cut -d " " -f $CURRENT_FREQ)
  105.     echo $FREQ_TO_SET
  106.     for i in $(seq 0 $CORES); do
  107.         # Try to set core frequency by writing to /sys/devices.
  108.         { echo $FREQ_TO_SET 2> /dev/null > /sys/devices/system/cpu/cpu$i/cpufreq/scaling_max_freq; } ||
  109.         # Else, try to set core frequency using command cpufreq-set.
  110.         { cpufreq-set -c $i --max $FREQ_TO_SET > /dev/null; } ||
  111.         # Else, return error message.
  112.         { err_exit "Failed to set frequency CPU core$i. Run script as Root user. Some systems may require to install the package cpufrequtils."; }
  113.     done
  114. }
  115.  
  116. # Will reduce the frequency of cpus if possible.
  117. throttle () {
  118.     if [ $CURRENT_FREQ -lt $FREQ_LIST_LEN ]; then
  119.         CURRENT_FREQ=$((CURRENT_FREQ + 1))
  120.         echo -n "throttle "
  121.         set_freq $CURRENT_FREQ
  122.     fi
  123. }
  124.  
  125. # Will increase the frequency of cpus if possible.
  126. unthrottle () {
  127.     if [ $CURRENT_FREQ -ne 1 ]; then
  128.         CURRENT_FREQ=$((CURRENT_FREQ - 1))
  129.         echo -n "unthrottle "
  130.         set_freq $CURRENT_FREQ
  131.     fi
  132. }
  133.  
  134. get_temp () {
  135.     # Get the system temperature.
  136.    
  137.     TEMP=$(cat $TEMP_FILE)
  138. }
  139.  
  140. ### END define script functions.
  141.  
  142. echo "Initialize to max CPU frequency"
  143. unthrottle
  144.  
  145.  
  146. # Main loop
  147. while true; do
  148.     get_temp # Gets the current temperature and set it to the variable TEMP.
  149.     if   [ $TEMP -gt $MAX_TEMP ]; then # Throttle if too hot.
  150.         throttle
  151.     elif [ $TEMP -le $LOW_TEMP ]; then # Unthrottle if cool.
  152.         unthrottle
  153.     fi
  154.     sleep 3 # The amount of time between checking temperatures.
  155. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement