Saving power with PowerTOP

Better Sleep


Intel's PowerTOP analysis tool helps optimize power usage and shows you the power guzzlers hiding out on your operating system.

By Kristian Kißling

Daniela Benzler, Fotolia

If you dual-boot your laptop with Windows and Linux, you may have noticed a strange phenomenon - the  battery lasts longer on Windows than on Linux. When this happens, people normally start tweaking their systems - unloading modules for power guzzling hardware, changing the ACPI settings, and dimming the display.

Recently, kernel developers have started to put more thought into the power situation; thanks to new kernel features, you can reduce your system's power consumption. PowerTOP [1] is a tool that helps you manage and optimize power usage.

Power Save Mode

A CPU will normally switch to a power save mode if the user falls asleep in front of the keyboard - or at least in theory. What actually happens with legacy kernels is that an internal clock, the static timer tick, occurs about a thousand times per second.

The CPU uses this signal to measure the time; of course, if the CPU isn't actually doing anything, the tick just rudely wakes the CPU out of power save mode.

To change this, dynticks were introduced with kernel version 2.6.21, timer ticks now only occur if the kernel really needs them. On 64-bit systems, the dyntick feature will appearl in kernel 2.6.23.

PowerTOP reveals what dynticks do - and more. It displays values on the console that tell you how much time the kernel spent in power save mode (Figure 1). At the same time, the tool tells you which kernel software components are waking up the kernel and thus helps you optimize your system. Timer ticks are not the only thing that interrupt the kernel's siesta; non-optimized software components - Gksu, for example - wake the kernel more often than necessary.

Figure 1: PowerTOP tells you how often the CPU enters power save mode and which programs wake it up again.

Kernel Panic

Newcomers might be a bit worried about needing kernel 2.6.21 to run PowerTOP - the tool will only show the C states otherwise, and it will not say anything about the power consumption of individual system components.

The good news is that a couple of distros on the market already use kernel 2.6.21: Debian "Sid" and the Debian derivative Sidux - admittedly experimental Linux varients. OpenSUSE 10.2 and Ubuntu 7.04 "Feisty Fawn" don't currently have updates for the kernel.


GLOSSARY
C States: Idle states of ACPI-capable CPUs. Four phases range from C1 (idle) to C4 (stopped processing). The higher the number, the less power the CPU consumes. Core-Duo CPUs also have a deep state C4.

Build Your Own

Of course, you could always build a new kernel of your own, but if this sounds too much like rocket science, just wait for openSUSE 10.3 or Ubuntu 7.10 "Gutsy Gibbon." Both systems will have the new kernel and probably packages for PowerTOP.

If you do decide to build a brand new kernel, you can download the kernel from Kernel.org [2]. The following example explains how to build a kernel for Ubuntu. Your mileage will differ depending on your distribution. Just make sure you know what you are doing - if you slip up, you might not be able to boot the system.

The following HOWTO is designed for "Edgy Eft" and "Feisty Fawn," and it basically draws from the Ubuntu wiki.

Start by installing the build-essential, kernel-package, xmlto, libncursesw5-dev, and libncurses5-dev packages. Then download a current kernel with an extension of tar.bz2 from Kernel.org to a directory of your choice.

Change to the directory you chose and type sudo tar xvjf linux-2.6.21.5.tar.bz2 -C /usr/src to unpack the kernel, taking care to replace 2.6.21.5 with the version you are using.

Next, change directory to /usr/src and create a symbolic link called linux that points at the directory with the kernel you just unpacked,

sudo ln -s/usr/src/linux-2.6.21.5/usr/src/linux

then change directory to linux. The kernel typically parses a configuration file to find out which kernel options you will be using.

Template

It is a good idea to use the configuration file for the existing kernel as a template for the new file; you will find the file below config-CurrentKernel in the /boot directory. Give the sudo cp /boot/config-CurrentKernel .config command to copy your existing configuration, and save the configuration as a hidden file in the /usr/src/linux directory - typing ls -la should list the file (Figure 2).

Figure 2: After you copy the configuration file for the existing kernel to the /usr/src/linux directory, you can customize the kernel options in the .config file at a later stage.

Configuration

The new kernel is bound to include new configuration settings. sudo make oldconfig prompts you to decide what to do with each new option. The best approach is to press Enter in each case to accept the defaults, which are then automatically dropped into your .config file. You can edit the file later (you need root privileges to do so) to enable and disable various kernel options, as described by the FAQ on the PowerTOP page [3].

The "Kernel Options" box lists the options. For example, to enable the CONFIG_NO_HZ option, you need to change the # CONFIG_NO_HZ is not set to CONFIG_NO_HZ=y (Figure 3). Reverse this procedure to disable an option - and don't forget the pound sign!

Figure 3: You need to modify a number of options in your .config file before building the kernel.

After saving the modified .config file, build the kernel using the following command:

sudo make-kpkg --initrd--revision=i686ver1 binary

This step creates .deb packages, which you can install and uninstall in the normal way. The process is typically fairly slow - in fact, it can take hours to complete on a slow machine. When you are done, type sudo make-kpkg clean to remove unwanted files. Now change to the parent directory and install the new kernel by typing sudo dpkg -i kernel-image-version.deb. The command updates the Grub boot manager at the same time.

If the GUI does not appear after rebooting, you will need to change the nvidia entry below Section "Device" in your /etc/X11/xorg.conf to nv, or fglrx to ati, and then relaunch the display manager. The new kernel needs new drivers for 3D graphics acceleration support. You may need to build modules manually for your WLAN or graphics card and apply a couple of patches.

Even if your newly built kernel doesn't support all your hardware, you will at least find out which software is using up too much power. You can then make a note of the culprits and optimize them on the old kernel.

Kernel Options

According to the FAQ, you need to enable the following options in the configuration file:

  • CONFIG_NO_HZ
  • CONFIG_TIMER_STATS
  • CONFIG_HIGH_RES_TIMERS
  • CONFIG_HPET
  • CONFIG_CPU_FREQ_GOV_ONDEMAND
  • CONFIG_USB_SUSPEND
  • CONFIG_SND_AC97_POWER_SAVE

Disable the following features:

  • CONFIG_IRQBALANCE
  • CONFIG_ACPI_DEBUG

Installing PowerTOP

The next step is to download the latest version of PowerTOP. Unpack the archive, change to the new directory, and type make to build the software.

Then launch the software directly from the same directory simply by typing sudo ./powertop.

Power Hogs

If you believe what Intel claims, PowerTOP works best on laptops with Intel CPUs. C state information is only for mobile CPUs in laptops. With AMD systems, desktop users can at least measure the power consumption of individual programs.

PowerTOP displays various values (see Figure 1). At the top, you can see how long the CPU remains in a specific C state. This tells you how well your kernel options are working. Ideally, the CPU should spend 95 percent of the time in C3 or C4 state, with an average residency of 50 milliseconds, according to the PowerTOP website.

In the lower part of the display (see Figure 1) is an underlined red value next to Wakeups-from-idle; it tells you how often the CPU wakes up from idle state per second.

This value should be as low as possible - the website claims you can reduce a three-figure number to single figures. This might work in theory, but you would need a whole bunch of patches and workarounds.

Battery Life

The line below Wakeups tells you how long your laptop's batteries are likely to last - assuming you are working on battery power. The battery life of a portable computer is bound to improve the more power guzzlers you switch off.

Troublemakers

One of the most interesting features is the list of programs that PowerTOP displays down at the bottom of the screen. These programs are the main troublemakers, organized hierarchically by the frequency with which they interrupt the CPU. PowerTOP occasionally displays a third black bar in the footer line with a letter and the name of an active process causing a number of wakeups. If you press the letter, PowerTOP sends a kill signal to the process.

Minor edits to configuration files can drastically change the hacking order in the power guzzler charts.

Conclusions

Most of the time, you need to install special patches to reduce the power consumption of individual programs. For example, if you add a line for Option "NoDRI" to the Section "Device" part of your /etc/X11/xorg.conf, the <interrupt> i915@pci entry will disappear out of the charts.

Before you can tackle some candidates, you must find out which program is hiding behind the cryptic name, such as i8042. In this case, the mailing list [4] and the Tips & Tricks section [5] on the PowerTOP website can help. The FAQ [3] reveals that i8042 refers to the kernel driver for the mouse and keyboard - don't be surprised if you see this entry when you investigate your system's power consumption with PowerTOP.

INFO
[1] PowerTOP: http://www.linuxpowertop.org
[2] Latest Linux kernels: ftp://ftp.kernel.org/pub/linux/kernel/v2.6
[3] The PowerTOP FAQ: http://www.linuxpowertop.org/faq.php
[4] PowerTOP mailing list: http://www.bughost.org/mailman/listinfo/power
[5] PowerTOP tips and tricks: http://www.linuxpowertop.org/known.php
THE AUTHOR

Kristian officially studied German philology, history, and social science in Berlin but wasted a lot of his time with computers. He got hooked on Linux in the `90s and now works as an editor for LinuxUser.