cooler-447488_1920

Keeping Your Linux Laptop Cool with cpufreqd


“But why?”, you might ask. Well, it may happen that when running a CPU intensive program, like encoding a film, your laptop gets so hot that it decides to give up and shut down in the middle of the process.  The following is the method I use, so far with great success, to lower my laptop’s CPU temperature.

 

 Install cpufreqd

$ sudo apt-get install cpufreqd

cpufreqd is a daemon that monitors and manipulates CPU frequency. One of the main reasons your laptop overheats is that the CPU overheats. To solve this, you will lower the CPU frequency when things get too hot. This, of course, comes at the expense of performance, but hey, I for one, prefer a slow(er) job finished than a fast job interrupted.

After the install, you can check that the cpufreqd daemon is up by running the following:

$ ps -ef | grep cpufreqd

The first thing you want to do is to modify the configuration file, /etc/cpufreqd end uncomment the “enable_remote” option (you will need sudo to edit this file). The “General” section will now look like this:

[General]
pidfile=/var/run/cpufreqd.pid
poll_interval=2
verbosity=4
enable_remote=1
#remote_group=root
[/General]

This will make enable the creation of a UNIX socket, through which commands can be sent. You need to do that to be able to use the cpufreqd-get and cpufreqd-set utilities, to check which profile is active at a particular moment and, respectively to manually enable a profile, if you want.

After each update of the configuration file, the cpufreqd service needs to be restarted so that the changes take effect.

$ sudo service cpufreqd restart

You can nou check which profiles are defined in your /etc/cpufreqd.conf file and which are active, by running cpufreqd-get. Note that you need sudo for that:

$ sudo cpufreqd-get
Name (#1):      Performance High
Active on CPU#: 0, 1, 2, 3
Governor:       performance
Min freq:       2400000
Max freq:       2400000
Name (#2):      Performance Low
Governor:       performance
Min freq:       1866000
Max freq:       1866000
Name (#3):      Powersave High
Governor:       powersave
Min freq:       1466000
Max freq:       1466000
Name (#4):      Powersave Low
Governor:       powersave
Min freq:       1199000
Max freq:       1199000

To list only the applied profiles, run

$ sudo cpufreqd-get -l
CPU#0: "Performance High" performance 2400000-2400000
CPU#1: "Performance High" performance 2400000-2400000
CPU#2: "Performance High" performance 2400000-2400000
CPU#3: "Performance High" performance 2400000-2400000

Configure your Daemon

cpufreqd is rule-based. If some conditions are met, then a profile is applied. The profiles and rules are defined in the configuration file. There are a few profiles already defined that should suit your needs: Performance High, Performance Low, Powersave High and Powersave Low. Depending on how “serious” your heating problem is, you can choose two of these four and change between them, depending on the temperature. I, for instance, go between Performance High and Powersave High.

Each rule is assigned a score, calculated as the percentage of entries that match the system state plus the number of entries that match. So, if your rule is made up by two entries that both match, your rule score will be of (100+2)%=102%. If only one out of two matches, the score will be of 51%. The profile mentioned in the highest scoring rule is finally applied.

ACPI

To control the CPU frequency based on the temperature you need to use the ACPI plugin. Uncomment the following lines in the /etc/cpufreqd.conf.

[acpi]
acpid_socket=/var/run/acpid.socket
[/acpi]

Because of the scoring system, it is better to remove already existent rules from the configuration file. Rules with higher number of matching entries than the ones you are about to add will take precedence. Be careful not to remove the profiles as well, just the [Rule]…[/Rule] entries.

Add the following three new rules to your configuration file.

[Rule]
name=Normal Functioning
ac=on
acpi_temperature=thermal_zone0:20-75
profile=Performance High
[/Rule]

[Rule]
name=On Battery
ac=off
profile=Powersave High
[/Rule]

[Rule]
name=CPU Too Hot
ac=on
acpi_temperature=thermal_zone0:80-105
profile=Powersave High
[/Rule]

 

After adding the rules, remember to restart the cpufreqd service.

$ sudo service cpufreqd restart

So let’s take a look at what these rules do. The first one, Normal Functioning, will apply the Performance High profile, which should be defined above, when the AC is on (i.e. your laptop is plugged in) and the temperature is between 20 and 75 degrees (Celsius).

The On Battery rule is optional for the purpose of cooling your CPU. It just applies the Powersave High profile when your laptop runs on battery, to preserve power, regardless of the temperature.

The third rule, CPU Too Hot, is the one that will cool down you CPU. When the temperature is above 80 Celsius, it applies the Powersave High profile.

The thermal_zone0 label specifies which sensor to look at when reading the temp. You can choose any of the ones found here:

$ ls -d /sys/class/thermal/thermal*

These thresholds and applied profiles are, of course, of my own choosing. You can use whatever values suit your cooling needs. You may decide, for instance, that Powersave High is too restrictive for the CPU Too Hot rule and go for Performance Low instead. Or that you want the threshold to be around 70 C.

Notice also that there is a gap between the 75 C specified by the first rule and the 80 C in the third rule. When the temperature goes up, the third rule will come into effect when it reaches 80 C; when it goes down, the High Performance profile will be applied at 75 C.

Debugging

The debug level of the daemon is controlled by the verbosity value in the General section:

[General]
pidfile=/var/run/cpufreqd.pid
poll_interval=2
verbosity=4
enable_remote=1
#remote_group=root
[/General]

Choose a value of 6 here, and the daemon will report the rule scores; 7 will also say which rules matched and which did not. Where will it report this? In the /var/log/syslog file.

$ tail -f /var/log/syslog

 

Manual Control

You may choose to control the CPU frequency yourself and not rely on the daemon’s rule-based judgment. To switch to manual drive, just run:

$ sudo cpufreqd-set manual

You can then manually select the profile which you want to be applied, e.g.

$ sudo cpufreqd-set 3

You get the profile number like in the example above: sudo cpufreqd-get. In that case, profile #3 was Powersave High.

To get back to automatic control, run:

$ sudo cpufreqd-set dynamic

That’s it! You can now lower your laptop’s temperature by software. But remember, while this approach works in most cases, it should probably be a temporary workaround to your overheating problem. A further, hardware solution would be to dust off your cooler and check that the thermal paste between your CPU and cooler still does its job.  

 

References

[1] man cpufreqd
[2] man cpufreqd.conf
[3] man acpi
Subscribe to my mailing list
100% Privacy. I don't spam.

Leave a Reply

Your email address will not be published. Required fields are marked *