Added Temp to Hz script and service file
authorAndrew <andrew@liquid.me.uk>
Thu, 17 Jun 2021 15:13:02 +0000 (16:13 +0100)
committerAndrew <andrew@liquid.me.uk>
Thu, 17 Jun 2021 15:13:02 +0000 (16:13 +0100)
Limits CPU max speed depending on cpu temp
For old laptops with lazy owners who won't take them apart and clean the heatsinks, get still insist on compiling locally.

README.md
temp-to-hz/Temp2Hz.service [new file with mode: 0644]
temp-to-hz/Temp2Hz.sh [new file with mode: 0755]

index 514a27d47e9e209cf9a697ab9592beeb18641133..0d05a705eafd8f5796618c83473b25e79a7c1513 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
 Just some stuff, might be useful, probably not though...
+This is mostly a backup of "things I use", if you use them and something breaks, you get to keep all the pieces.
diff --git a/temp-to-hz/Temp2Hz.service b/temp-to-hz/Temp2Hz.service
new file mode 100644 (file)
index 0000000..a4cc5b2
--- /dev/null
@@ -0,0 +1,10 @@
+[Unit]
+Description=Cap CPU HZ according to temperature
+
+[Service]
+Type=simple
+ExecStart=/root/bin/Temp2Hz.sh start
+ExecStop=/root/bin/Temp2Hz.sh stop
+
+[Install]
+WantedBy=multi-user.target
diff --git a/temp-to-hz/Temp2Hz.sh b/temp-to-hz/Temp2Hz.sh
new file mode 100755 (executable)
index 0000000..da3a765
--- /dev/null
@@ -0,0 +1,86 @@
+#!/bin/bash
+debug=0
+
+Exit(){
+       if ((${debug}));then echo "Restoring default speed and exiting.";fi
+       SetCPUSpeed ${DefaultMaxCPUSpeed}
+       rm /var/run/Temp2Hz.pid
+       exit
+}
+
+GetTemp(){
+       Temp=$(/usr/bin/sensors|sed -n 's/Package id 0:  +\(.*\)°C  .*/\1/p')
+       if ((${debug}));then echo -n "Current Condition: ${Temp}°C ";fi
+}
+
+CheckTemp(){
+for index in $(echo -e $(echo "${!CPUSpeeds[@]}"|sed 's/ /\\n/g')|sort -r);do
+       if (( $(echo ${Temp}$(echo $index|sed 's/over_/>/;s/under_/</')|bc -l) ));then
+               if [ ${CurMaxCPUSpeed} != ${CPUSpeeds[${index}]} ]; then
+                       SetCPUSpeed ${CPUSpeeds[${index}]}
+               fi
+       return
+       fi
+done
+}
+
+GetCurMaxSpeed(){
+CurMaxCPUSpeed=$(cat /sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq)
+if ((${debug}));then echo "@${CurMaxCPUSpeed}Hz";fi
+}
+
+SetCPUSpeed(){
+       for file in /sys/devices/system/cpu/cpufreq/policy*;do
+               echo $1 > ${file}/scaling_max_freq
+               if ((${debug}));then echo "$1 Requested: ${file}/scaling_max_freq set to $(cat ${file}/scaling_max_freq)";fi
+       done
+}
+
+
+Main(){
+trap Exit SIGINT SIGUSR1
+echo -n $$ >/var/run/Temp2Hz.pid
+GetCurMaxSpeed
+DefaultMaxCPUSpeed=${CurMaxCPUSpeed}
+if ((${debug}));then echo "Default scaling_max_freq recorded as ${DefaultMaxCPUSpeed}";fi
+
+declare -A CPUSpeeds
+
+################ Config Section ################
+CPUSpeeds[over_90]=800000
+CPUSpeeds[over_85]=1300000
+CPUSpeeds[over_70]=1800000
+CPUSpeeds[over_55]=2300000
+CPUSpeeds[over_45]=2800000
+CPUSpeeds[under_45]=${CurMaxCPUSpeed}
+TempPollPeriod=15
+################################################
+
+
+##Main loop
+while GetTemp;do
+       GetCurMaxSpeed
+       CheckTemp
+       a=1;until [ $a -gt ${TempPollPeriod} ] ;do sleep 1;((a++));done
+done
+}
+
+KillMain(){
+       MainPID=$(/bin/cat /var/run/Temp2Hz.pid)
+       if ps -A|awk '{print $1}'|grep -q ${MainPID};then /bin/kill -USR1 ${MainPID};fi
+       for a in {0..5};do if ps -A|awk '{print $1}'|grep -q ${MainPID};then sleep 1;fi;done
+       if ps -A|awk '{print $1}'|grep -q ${MainPID};then echo "Stopping forcefully...";/usr/bin/killall -SIGKILL Temp2Hz.sh;fi
+}
+
+case $1 in
+       start )
+               Main
+       ;;
+       stop )
+               KillMain
+       ;;
+       * )
+       echo "Usage: $0 [start|stop]"
+       ;;
+esac
+