cputemp2maxfreq
This daemon sets the cpufreq scaling_max_freq setting and scales it down when the CPU reaches a given temperature.
Why this program?
I recently bought a new mainboard, an Asrock J5040-ITX.
This board has a passively cooled CPU which means heat can build up when you
give it a GPU or CPU intensive task. It will happily heat itself up to over
95°C, and since i'm a Gentoo user, that happens quite a lot :-). I thought
it's wasn't a good idea to leave it running for hours at 95°C. One
solution could be to add a small fan, but I hate fans and that's why i got a
fanless mainboard in the first place. The other solution is to reduce CPU heat
generation when the temperature is too high, and the best way to do that is to
reduce it's frequency as can be done with cpufreq within linux.
When I discovered there is no program to do that i deciced to write it myself.
What does this program do?
It's basic function is simple: It reads the CPU's temperature sensor and if the temperature is above a certain threshold it will lower scaling_max_freq to lower the CPU speed and allow it to cool down. When the temperature drops below the threshold it will increase scaling_max_freq again to allow it to reach it's full speed.
There is a lot you can tune and do with this basic function, these are the options you can pass to cputemp2maxfreq:
Scaling options:
- -d sets the transition latency (the time it takes the CPU to apply a new frequency). This is a value in microseconds or the keyword "auto" for autodetection.
- -f sets the fallback frequency. In case something goes wrong the program sets the CPU frequency to it's minimum. In case that fails too it will try the fallback frequency
- -g the cpufreq governor to use. Conservative seems to work nicely with this daemon's concept. Use the special keyword "keep" to prevent this daemon from changing the governor
- -i set the input for CPU temperature. This can be set in several ways:
- An absolute path (usually somewhere in /sys), this should start with a /
- The name of a sensor (for example "Package id 0" from coretemp) don't forget to quote when the name contains spaces
- The word "auto" triggers autodetection
- -k keep the last state instead of setting minimum speedi on exit
- -p set poll interval
- -P set the Physical CPU numer (socket numer) for multi CPU systems. Note that this is only needed for systems with more than one physical CPU's, it's not needed for multi Core CPU's.
- -s set the step size to increase/decrease CPU speed. Note that this value will be multiplied by the difference between the CPU temperature and the set temperature to calculate the actual change.
- -t set the target temperature
Logging options:
- -c or -C will log data to a CSV file which can be used for analysis later. See the paragraph below on how to use it with LibreOffice. -c will append data, -C will overwrite an existing file
- -l set the logging mechanism to use. Possible values:
- none: No logging at all
- kmsg: Log to the kernel log so you can see it with dmesg
- stdout: Log to stdout, especially useful when testing/tuning
- syslog: Log to syslog, it will use facility Daemon and level Critical
- -m log measurements (CPU temperature and frequency) via logger
- -u use unix timestamps in log and CSV outputs instead of YYYY-MM-DD HH:MM:SS
Other options
- -h displays a help text listing all the options
- -v displays version and license information.
To use the CSV file with libreOffice first stop the daemon to make sure all data has been written. Then use LibreOffice Calc to open the CSV file. In the text import dialog use the following options:
- Separated by Comma
- String delimited: "
- Other options: Only set Detect special numbers, other options should be disabled
The preview should already show a nicely formed table, so click Ok.
If the document is opened read-only, click the Edit Document button. Select the whole sheet, open the Insert menu and choose Chart...
Now select Chart Type XY (Scatter) and subtype Lines Only and click Finish.
You should now have a graph and depending on the number of data points it might beusefull to make it wider to give the graph some more space. Becaue the CPU temperatures are much lower numbers than the frequencies it's useful to add a second Y axis, to do so click on the background and choose Insert/Delete axes and enable the secondary Y axis. Now click the first temperature line (usually at the bottom of the chart) and choose Format Data Series, and set Allign Data Series to Secondary Y axis. Do the same for the second temperature line.
The resulting graph should look like this:
At the start of the graph the CPU Temperature (green line) was low enough to allow the Scaling Maximum Frequency (light blue line) to go up to the CPU's Maximum Frequency (red line). Then it got a heavy task to do, the CPU's Current Frequency (yellow line) was scaled up to almost maximum and that heated up the CPU. At around 12:37:26 the CPU Temperature has risen above the Target Temperature (black line) and the Scaling Maximum Frequency was lowered and the CPU Current Frequency followed. At around 12:40:19 the job was finished and the CPU Frequency was scaled down for lower power consumtion, it cooled down and that allowed the Scaling Maximum Frequency to be increased the CPU's Maximum Frequency again. The scaling between 12:37:26 and 12:40:19 was done by this daemon.
Also note that the temperature given to this program is not a maximum temperature but a target. As can be seen in the graph above the temperature will rise above this temperature before action is taken. Depending on the CPU load and poll interval this could be a significant difference, so keep the target temperature well below your CPU's maximum temperature.
Hardware requirements
In order to be able to use this program there are some hardware requirements:
- Your CPU should have a temperature sensor that is available in sysfs (most CPU's have this)
- Your CPU should be able to be set to any frequency in a range. Especially some older CPU's could only use a limited set of fixed frequencies, this is unusable.
How to install this program
Since i'm a Gentoo user i made an ebuild provided in the gentoo directory of the source code. Place it in your own overlay to use it, the suggested category is sys-power. The ebuild will install the binary in /usr/sbin and install an initscript so you can start the daemon on boot.
To install from source manually the best way is to git clone https://code.pa4wdh.nl.eu.org/tools/cputemp2maxfreq. Change to the source directory and type make to start the build proces. It shouldn't take long since this is a very small program. The result should be an executable called cputemp2maxfreq.
What can i do with this program?
The daemon has two uses: Get the maximum performance from your CPU when the temperature allows it, and protect the hardware from overheating.
This program is distributed as Free Software under the GPL version 3, see the COPYING file for details.
Since i'm a Gentoo user there is a topic about this program on the Gentoo forums.
Happy computing!