How to use CPU frequency scaling (cpufreq)

Learn what cpufreq means, how to enable it in the Linux kernel and on your system, how to install an userland frequency controller and how to monitor the results.

1. Introduction: what are CPU frequency scaling and cpufreq?

It has to do with powersaving. It is a method of “toning down” your CPU speed, on-the-fly, based on your system’s actual needs at a certain point in time. If the system needs to work hard, the CPU is ramped up. If it doesn’t, it simmers down and saves on power.

That’s the basic idea behind an entire set of features in the Linux kernel, grouped under the name “CPU frequency scaling”, or “cpufreq”, for short.

There are several methods for achieving this. The method I will be describing here is the simplest and has the most chances of working with most computers out there.

2. Checklist

This list presents the steps you’ll have to accomplish to achieve frequency scaling. You will need to read the rest of this document to figure it out, but this is a checklist that will help you keep things on track.

  1. Figure out what cpufreq governor you want (section 3.1). This document insists on userspace. If you don’t want it, you’re on your own, since I’m assuming you know what you’re doing.
  2. Figure out what cpufreq driver(s) you need. You will need to know what kind of CPU you have and perhaps the type of motherboard chipset. (see section 3.2)
  3. Check that the sysfs filesystem is mounted under /sys and keep a look on /sys/devices/system/cpu/cpu0/ for the cpufreq/ subdirectory and useful info in there (see section 4.2).
  4. Find the kernel modules for the governor and driver(s) you chose (see section 4.3) and ensure that they are loaded upon every machine startup (see section 4.6).
  5. Make sure the right governor is selected at startup (section 4.5).
  6. Install the userland frequency controller daemon (powernowd) –section 5.
  7. Install a graphical monitoring utility –section 6.

3. Advanced information about cpufreq

3.1. Cpufreq governors

The Linux kernel actually offers several methods of controlling CPU powersaving. These methods are called “governors”.

Each governor provides a distinct policy on how to deal with the CPU. In order to do so, they first examine the CPU characteristics (FSB frequency and multiplier) and determine a maximum and a minimum frequency that the processor can run at. Then they take action, as follows (the name in the brackets is the name of the corresponding kernel module):

Each governor can be built-in the kernel or as a separate module which can be loaded whenever you feel it’s convenient.

3.2. Cpufreq drivers

In addition to governors there are drivers, which determine the actual methods that are used to scale the CPU around.

For instance, one driver may use ACPI stats for this purpose, another may make use of the processor’s built-in features (PowerNow! for AMD or SpeedStep for Intel), while yet another may use nForce2 FSB scaling.

Each driver can be built-in the kernel or as a separate module which can be loaded whenever you feel it’s convenient.

Only one cpufreq driver module should be loaded at any given moment. Trying to load another while a driver is active will most likely be denied by the kernel. Remove the active one (with rmmod) first. Remember to restart the userland daemon if you switch drivers, and be advised that some of them may freeze if you do it.

Here’s a list of drivers, as the kernel module names, as of kernel 2.6.21:

4. Enabling cpufreq

4.1. Basic requirements

Technically, in order for cpufreq to work you need to at least cover the following:

However, on any modern distro aimed at desktop usage you can be fairly sure that the above has already been covered. So you only have to move on.

4.2. Checking the cpufreq sysfs directory

It is quite probable that your distro has provided the required governors and drivers in the form of kernel modules. Therefore, you will need to load at least one governor and one driver for cpufreq to work. And while it is possible that a default governor has been built in the kernel, it’s less likely that this is true for drivers.

Start by checking the contents of this directory:

/sys/devices/system/cpu/cpu0

If you have two processors or a multi-core processor, there may be additional cpu1/, cpu2/ and so on subdirectories to look for.

There should be a subdirectory called cpufreq. If there isn’t, you will need to load some modules to get it going.

4.3. Finding the necessary modules

Now, find out what modules are available by issuing this command in a terminal (careful, it’s a two-liner, select all of it):

ls /lib/modules/$(uname -r)/kernel/drivers/cpufreq/ \
/lib/modules/$(uname -r)/kernel\
/arch/i386/kernel/cpu/cpufreq/

The first part should list available drivers, while the second will list available governors. Speaking of governors, if either of userspace or performance is missing, it’s probably built-in the kernel as default.

For the purpose of this tutorial, you want the userspace governor. So if it’s not listed you can assume it’s the default and move on (you’ll see how we can check and enforce it in the next section). If it’s listed, the performance governor is probably the default; again, the next section will take care of things.

Now, drivers are important. You need at least one relevant driver loaded for cpufreq to work. If you haven’t done so already, go back to the previous section (2) and skim the available drivers, then pick one that’s likely to work for you. Obviously, it should be one that the above command has listed as available for your current kernel.

4.4. Enabling a cpufreq driver

Let’s say you have a NForce2 motherboard and that cpufreq-nforce2.ko is listed as available. You can enable it by running the following command as root (some distro’s, notably Ubuntu, will need to add sudo in front of the command):

modprobe cpufreq-nforce2

Note the missing .ko.

If it worked there should be no error messages, and you should see cpufreq/ appear under /sys/devices/system/cpu/cpu0/.

If it doesn’t work (ie. cpufreq/ does not appear, or there are error messages) then try to load another driver. Please try to make an informed choice –cat /proc/cpuinfo should give you hints about what kind of CPU you have– do not load drivers blindly, it won’t do any good.

4.5. Enabling the userspace governor

Now there’s a minor snag that may have to be taken care of. Either ondemand or userspace can be selected as the default governor for your CPU frequency scaling. If your distro is smart it will have selected userspace by default. But if not…

The powernowd daemon, which I cover below, will enable the userspace governor for you! So chances are you won’t need to do any of this and can skip right to the next main section.

To check, open a terminal and run the following commands to take a peek at two files:

cd /sys/devices/system/cpu/cpu0/cpufreq/
cat scaling_governor
userspace
cat scaling_available_governors
conservative ondemand userspace

If you get an error right after the first command, something is not yet right. Most likely, you will need to load at least one cpufreq module to get it to appear. A driver would be the best choice, so see the previous section. Or you can load any of the governor modules, to make the subdir appear, and switch to userspace later.

Once scaling_governor contains “userspace”, you’re all set and can move on.

If not, you need to check that scaling_available_governors contains “userspace” among the governors.

If it’s not there, it can mean that the appropriate module has not been loaded. You can load it by running modprobe cpufreq_userspace as root.

If you find that scaling_governor contains something other than “userspace”, decide what to do. Perhaps you’d actually like to keep whatever governor is in there by default (most likely “performance”). If not, do this command:

echo userspace > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

You can add this command to /etc/rc.local so you don’t have to do it manually whenever the system restarts. Or, if you have the sysfsutils package installed, you can edit /etc/sysfs.conf and add a line such as this:

devices/system/cpu/cpu0/cpufreq/scaling_governor = userspace

4.6. Loading kernel modules

You can load any kernel module, provided it’s available, by running the modprobe command as root. So in order to obtain the userspace governor and the nForce2 driver you would do this:

modprobe cpufreq_userspace
modprobe cpufreq_nforce2

All the module names for governors and drivers are listed in section 3. To find out which ones are actually available on your system, see section 4.3.

Also, you want to make sure this happens automatically when you start the machine. One way is to edit /etc/rc.local and add these commands to it.

A more elegant method is to edit /etc/modules and add just the module names (cpufreq_userspace and cpufreq_nforce2), each alone on a line. Feel free to use a cpufreq driver that’s actually appropriate for your hardware.

5. Install an userland frequency controller

OK, so I’m assuming you have sysfs support and that you managed to enable the userspace governor.

Now you need a daemon that will take care of throttling the CPU power on the fly for you.

5.1. powernowd

One cpufreq controller that I find very very nice and easy to use is powernowd. Despite the name, it works with most processors, not only AMD-made. It is light, fast and supports non-x86 and SMP systems.

All you need to do is look the package up in your distro’s package manager and install it. That’s it. Powernowd will start and get to work.

By default it works like this:

Immediately jumps to the highest frequency whenever CPU usage goes over 80%, and decreases by “step” Hz as usage drops below 20%.

As you can see, it’s a finer job than either the conservative or ondemand governors.

If you change the cpufreq driver (by removing the module and inserting another), you will need to restart powernowd: /etc/init.d/powernowd restart

5.2. CPUSpeed

CPUSpeed is another daemon which I’ve tried briefly. It seems even nicer than powernowd in some respects, since it offers a configuration file that will let you specify the cpufreq driver and it will load it for you.

On the downside, not all distributions carry a package for it, which may force you to compile a source package, if you’re up to it.

6. Install an utility that monitors the frequency changes

Now, the only thing left to do is install a nice graphical gizmo that will let us know how the whole thing works out.

CPU Scaling Frequency Monitor

I use Gnome and I am very pleased with a gnome-panel applet called “CPU Frequency Scaling Monitor”. On Debian and Ubuntu it’s in the gnome-applets package. Add it to your panel, customize its looks as you please, then stand back and admire it. You can see it in the screenshot, showing a 50% downscaling.

You can test it by doing something that occupies 100% of the CPU. The frequency should jump to 100% as well. But when you’re not using the CPU to the max, the frequency should drop.

How much it drops depends on /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq (you can echo values in there, as root!).

How smoothly it drops and rises depends on the cpufreq driver; some drivers allow more fine-grained control, whereas others only allow two positions (100% and 50%).

You’d be well advised to also install a CPU temperature monitoring utility, at least temporary, to see if there’s any improvement brought about by cpufreq.

7. How it works out in real life

Is it really useful? Yes, I believe so. Using the above, my Athlon XP 1800+ runs as much as 5° Celsius (about 8-10°F) cooler than usual, under normal usage conditions, which is no small feature considering the summer heat.

And while I don’t have the equipment to measure it, there’s probably a small amount of power saved as well. Save on power, fight global warming.