Friday, October 8, 2010

CPU downvoltage, downclock and monitoring on gentoo linux

Hey everybody,

since I assembled my laptop starting from spare parts I had a serious problem: heat. I don't mean my laptop went on fire in idle, but when doing some serious computing form my MRI jobs, it happens to work for several minutes with both CPU at 100%.  That should not scare anybody, but I did force my barebone specs plugging a T9800 CPU, 35W, when the suggested CPU was of downvolted family (25W). Hehe :]. Obviously you can solve many overheating troubles, but the first solution should always be heatsink cleaning and fan check.

Keeping in mind that I always run my pc with the best airflow possible (no pc-bag under it or cover while sitting on the bed etc.), this is how I solved my problem, handling the CPU management in the same time, and how I tested the final configuration.

The first step is to get prepare the kernel. You should check for ACPI Processor P-state or AMD Opteron/Athlon64 PowerNow or the required driver for your architecture. Let's take Intel Core 2 Duo as example: build ACPI P-state as a module that will be called acpi-cpufreq. If you'll follow to the end this guide we are going to replace this module, so be sure not to build it in kernel. Also take a look at the active governors (few lines upper).
Obviously you should make the module load at startup, by adding the name in /etc/conf.d/modules or in the proper file if you don't have OpenRC.

Then we need to install a reliable hardware monitor. I suggest GKrell or conky. As I enjoy minimalist environments, I prefer conky ;].
Conky is a very handy system monitor and can do any sort of thing (yeah, almost any), even run scripts and plot their output. In our case what we need is to see CPUs clocks and temperatures (if you care usage too).
emerge -av conky
Even if default configs are available I'll paste here a short line for monitoring those values we need. You can customize your configuration by editing ~/.conkyrc and following man conky instruction.
${color0}CPU$color  ${freq_g 1}G ${cpubar cpu1 4,45} ${hwmon 0 temp 1}C

We are quite ready to begin. In the first part of my cpu experiments (;]) I used cpufrequtils to be able to set governor and clock myself. I suggest to begin here, to see what your CPU can really do:
emerge -av cpufrequtils
It is divided in a couple of function: cpufreq-info gets info from the CPU, cpufreq-set can set clock or governor etc... Their helps can explain how they work better than me. What you need is just to play a little, aiming to see the highest and the lowest clock etc. I used this tool to write some scripts to manually set clock or to change governor by keyboard shortcuts.

This is all the way deprecated as you'll put your eyes on cpufreqd. It relies on a set of profiles and rules, based on acpi information (AC plug, battery level, open apps, CPU request), to decide what is the best governor and what is the right clock in that moment. Let's emerge it
emerge -av cpufreqd
and let's add it to default runlevel
sudo rc-update add cpufreqd default
Now you should customize your config. Look at default:
sudo nano /etc/cpufreqd.conf
and decide what is the best for you. In my case I created several profiles and rules, one of them was handling overheating, here it comes the profile and the rule:

#cpu too hot
name=CPU hot
This can do the job quite  good. The reduction of clock can lower very fast the temperature of the CPU, bringing it back to full speed as soon as it can.

This is good, but not best. Googling around I found phc project, which works on cpu management. In their stuff that there is a cpu driver and coupled application that can handle CPU more in detail: you can change the core voltage of every frequency step. This can lead to two main consequences: you'll use less energy to run the core (less battery consumption) but you'll generate less heat too. ;]
This time we have to set a USE flag to let user run the main application and change values of cpu voltage.
sudo nano /etc/portage/package.use
add the following line
sys-power/phctool sudo
then let's emerge it with its driver:
emerge -av phctool phc-intel (or phc-k8)
Once you've set correctly permissions (see portage output), let's move to the new driver:
sudo modprobe -r acpi-cpufreq (or your arch old driver)
sudo modprobe phc-intel (or phc-k8)
to launch the application start it from command line:
and customize your voltages. Be careful not to exaggerate, make small steps and small modifies sequentially, testing results each time. Remember to set the option "restore VIDs on load" so it will get your last config and load it automatically.

Last tool is very dangerous, so if you don't know what you're doing just stop here. Following these steps you might broke your system, I don't feel liable for any trouble. Testing thermal behavior is  simple-stupid with cpuburn.
emerge -av cpuburn
It will install some "burn**" binaries. They are created each for a specific kind of cpu. Intel users should chose burnP* family while AMD burnK*. The command generates a single-core stress, so let's workaround it (run burn** & one time for each core). Remember to be ready with killall burn** if the temperature rises too fast or too high.

The final result of downvolting by 0,15V my CPU is that it never goes over 80°C even on full load; this keeping my pcufreqd rules and advantages. It's a pity that on idle I already have the lowest voltage available (at least it seems so as I can't get it lower), so my batteries won't improve lasting ;P...



  1. Hi there! I'm having a great time using this straight forward tutorial, it's awesome, it's Gentoo too! actually I've been performing intense calculation on my i7-3630qm laptop the temp and the temperature reaches the sky almost 96-97 c°, 3 points to hit the die temperature wall, cpufreqd gave me more control on this keeping the max temp below 71 c°- great thanks to the blogger. If you wanna monitor real time cores temperature install xsensors on Gentoo:

    sudo emerge x11-misc/xsensors

    run it in terminal:
    $ xsensors

  2. Thanks, since you run a IvyBridge cpu have a look also at the customization of the frequency steps...