jueves, 29 de mayo de 2014

controlling cpu temperature with bash scripting

As I said before in a previous post, my favourite linux flavour is gentoo. Gentoo is a good distribution but as many others have some problem implicit in their own philosophy.

All their packets have to be compiled and this is a problem in terms of heating. I mean, if you pc is a very old laptop with one of its fan broken, compiling could be a dangerous task.

Some time my cpu have reached 80 ºC, even though my cpu claim that the critical point is 95 ºC I dont want to test it.

Therefore, in order to keep my cpu cold I make a-very-easy-to-understand script that modulates the cpu frequency.

Firstly, you have to know if your cpu is giving its temperature to the operative system.

ls /sys/devices/virtual/thermal/thermal_zone0/

and you have to see something like this:


This means that you have at least one thermal_zone that normaly is given by ACPI as in my case it is. If you read the content of the file temp, you will be able to see the cpu's temperature in milidegrees.

Secondly, you have to know if you have an active linux module that let you modulate the cpu's frequency with the userspace governor. In my case I'm using p4-clockmod as a frequency modulator. As you can read in several places this driver do not change your cpu at all because speedstep technology is not available in old pentium 4 processor. However this driver halt the cpu several times per seconds in order to emulate a frequency modulation. There are diferents drivers like acpi-cpufreq, as you can imagine this driver let you use the acpi p-states that are available in most of the new cpu that you can buy nowadays.

Let's try to change the cpu speed

You have to know at what frequencys are your proccesor able to run.

sudo cpupower frequency-info


Now you can try to change the cpu frequency.




Now you can try to read my code:

#!/bin/bash

#licenced with gpl-v3
clear

while true
do
   sleep 5
   temp=`cat /sys/devices/virtual/thermal/thermal_zone0/temp`
   echo $temp
   if(($temp <= 70000)); then
      cpupower frequency-set -f 1600MHz
      echo "cpupower frequency-set -f 1600MHz"
   else
      cpupower frequency-set -f 200MHz
      echo "cpupower frequency-set -f 200MHz"
   fi

   
done


example of execution:



Yeah man but.... why are you doing this if there are a good driver called intel-thermal-daemon?

man, i don't know, in gentoo it is not available in the main portage so I want to try by myself.

If you are using ubuntu you can try this driver with
sudo apt-get install thermald

If i were you, I'll definitely try thermald before my shit.

There are a lots of miss spellings a grammar mistakes, I will correct them when I learn proper english.

See you in my next post. (maybe I'm am my only reader)