Features PowerTOP Lead image: Elena Elisseeva, 123RF.com
Elena Elisseeva, 123RF.com
 

Identify troublesome energy consumers with PowerTOP

Energy Saver

Many applications interrupt the CPU when it tries to sleep. PowerTOP not only identifies these evildoers but also provides tips on how to get rid of them. By Tim Schürmann

When the CPU runs out of things to do, it automatically switches to one of several sleep states. Modern processes do this multiple times per second. The deeper the CPU sleeps, the less power it needs. For this reason, you should not wake it up until you really need to do so. Unfortunately, some arrogant processes and device drivers keep waking up the processor, thus adding to your electricity bill.

Detective Work

The PowerTOP tool identifies just these types of processes on Linux systems. It monitors all the active processes and device drivers, calculates their power consumption, and then shows you which ones are likely the biggest power wasters. Additionally, the internal database provides power-saving tips that PowerTOP will apply at the press of a button.

This feature gives laptop users in particular the ability to extend battery life, and it lets server operators reduce their energy costs, which is an important consideration if you happen to run a server farm. The tool also lets software developers test at an early stage to see whether or not their software interferes with the processor too often.

PowerTOP was created in 2007 by Intel and will run on AMD, ARM, and UltraSPARC processors with some restrictions. The tool is available under the free GPL v2 license and is now included by most major Linux distributions. However, older versions are typically in the repositories. More recent versions offer more power-saving tips, so you might want to download the current source code archive from the PowerTOP website [1] and build the tool yourself.

To do so, you'll need the C compiler, make, gettext, and the developer packages for libncursesw. Note that the w here is not a typo; it refers to the wide-character version of the popular ncurses library. This specification explains why Ubuntu users will need both the libncurses5-dev package and its sibling libncursesw5-dev. However, openSUSE, gives you everything you need in the ncurses-devel package bundle. To compile and install PowerTOP, use make and then sudo make install. Before you can launch the tool, though, you'll need to fulfill a couple of conditions.

Tick Tock

Older Linux kernels (up to version 2.6.20) use a fixed heartbeat of 1,000Hz. On each tick, the kernel wakes up the processor and checks for any work to do. This approach obviously limits power savings.

Starting with kernel 2.6.21, however, regular ticks are gone. Instead, Linux quickly determines when new tasks will have to be performed in the future and then sets the hardware alarm clock, also known as the High-Precision Event Timer (HPET). Thanks to these dynamic ticks (dynticks), the CPU can sleep for extended periods.

PowerTOP relies on a "tickless" kernel. Thus, a 32-bit kernel will need to be at least version 2.6.21, and a 64-bit kernel will need at least version 2.6.23.

To check your system, you can enter the command:

uname -r.

You'll be on the safe side as of openSUSE 11.0 and Ubuntu 8.04. Additionally, your kernel needs to enable support for dynamic ticks. This is the case for all the major distributions; if you're unsure, refer to the "A Question of Settings" box for more information.

Once all these conditions are fulfilled, you can launch PowerTOP with:

sudo powertop

You can also run the tool as a normal user, but then you will only see a fraction of the information, and you won't be able to apply any proposed improvements.

PowerTOP will now collect information for five seconds and then return the slightly cryptic message that you can see in Figure 1.

On a MacBook with Ubuntu 10.10, PowerTOP recommends disabling the unused Bluetooth interface and shows the commands to do so.
Figure 1: On a MacBook with Ubuntu 10.10, PowerTOP recommends disabling the unused Bluetooth interface and shows the commands to do so.

High C

On a laptop – and only there – PowerTOP shows on the top left how long the CPU slept on average during the five seconds and how deep the sleep was. The C indicator followed by a number stands for one of several power-saving states or C-states. The higher the number following C, the less energy the processor consumes, but the longer it will take to wake up again. The C0 state means that the processor is running at full speed and executing instruction. In C1, it is idle, and in the other states, it disables more and more areas and computational resources.

The processor saves the most energy in the C3 and C4 states, which is where you want it to spend the most time. Ideally, the CPU will sleep for 95 percent of the time in one of these two states. Which C-state is displayed will depend on your BIOS. Some BIOSs will hide the C3 and C4 states while the laptop is connected to the main power supply (Figure 2).

Desktops and servers do not have C-state information; while the laptop is connected to a power supply, you'll see only estimated power consumption.
Figure 2: Desktops and servers do not have C-state information; while the laptop is connected to a power supply, you'll see only estimated power consumption.

Modern CPUs can automatically reduce or increase their clock speeds as the need arises. Intel calls this technology SpeedStep. The Performance states, or P-states, on the right, show what frequencies your CPU uses and how long it uses them. In most cases, but not as a strict rule, you can assume that the more slowly the CPU is running, the less energy it will consume.

The Culprits

Below the state numbers, a bar shows how often the processor was awakened in one second (Wakeups-from-idle per second). The smaller the figure, the better. If you use the Gnome desktop, you can theoretically reduce this to three wake-ups per second, although realistically a value below 20 is pretty good. The color of the bar also indicates good and bad values: A bright red bar means that you really need to take some action; amber means an acceptable energy-saving situation.

If PowerTOP is running on a mobile device that is currently accessing its battery, the area below the bar tells you how much power the computer is currently using and how long the battery is likely to last.

The area below Top causes for wakeups lists the biggest culprits. The consumers in bold type are the ones PowerTOP thinks are the biggest power consumers. Besides processes, the list also contains devices and kernel functions.

For laptops, devices that disturb the CPU are listed above all the WLAN and Bluetooth components. You can quickly identify them by their port names. To disable these devices, you can typically remove the corresponding kernel module with

sudo modprobe -r modulname

and possibly shut down the daemon. Some laptops have special keys that, for example, disable WLAN and Bluetooth. PowerTOP shows whether they are working. USB adapters are particularly nasty because they keep the whole USB subsystem live.

If the X server (Xorg) appears in the list, that doesn't mean the server itself is to blame. Typically, some other program is sending requests to the X server, which wakes up the processor. The culprit here could be something as banal as a flashing cursor in a terminal. If an interrupt is to blame, the easiest way to find the evildoer is to unload the kernel modules it uses gradually. After each action, relaunch PowerTOP and check the changes.

Before you kill a problem-causing process or kernel module, however, you should think twice about whether you need it or whether a new version of it is available that is easier on resources.

Words of Wisdom

At the bottom of the screen, PowerTOP shows you energy-saving tips. You can apply a tip directly by pressing the corresponding key. For example, S enables SATA power-saving mode; B disables the Bluetooth interface, A enables the HD audio power-saving functions, and K stops repeated requests to the CD drive.

You should not blindly follow all of these suggestions. If the WLAN is your only way to access the outside world, you should probably keep WLAN enabled. If enabling hard disk power management on a server affects performance, doing this will be counterproductive.

Some measures are permanent (e.g., the udev rules); others only survive until you reboot. To prevent the latter case, you can add the proposed command to your start scripts – typically in /etc/rc.local. On laptops, you might want to collect the commands in a script and run it as needed – for example, when you switch from power supply to battery operation.

PowerTOP will continue to run and will refresh the display every five seconds. The tip shown at the bottom of the screen changes as the information is refreshed. You can press R to force a refresh and press Q or Ctrl+C to terminate the program.

Next Generation

By the time this issue of Admin magazine hits the newsstands, the completely revised PowerTOP version 2.0 should be available. It comes with a revamped user interface and supports the new power-saving functions in kernel 2.6.37. Future distributions of Ubuntu 11.04 and openSUSE 10.4 will use this version.

Additionally, PowerTOP 2.0 accesses the kernel's "perf" infrastructure to retrieve more precise data on the power consumption. After collecting sufficient data, the new version will also predict a device's power consumption, although the main developer, Arjan van de Ven, still classifies this function as experimental. To put PowerTOP 2.0 to meaningful use, you'll need at least kernel 2.6.36 and preferably 2.6.37. If you have an older kernel, stick with PowerTOP 1.x.

At the time of writing, I still had to download the source code for the beta version of PowerTOP 2.0 [2]. To compile the code, you need the GNU C++ compiler, along with the developer packages for libstdc++, pciutils, ncurses, zlib, and libnl. On openSUSE, these libraries are in the gcc-c++, pciutils-devel, ncurses-devel, zlib-devel, and libnl-devel packages, whereas Ubuntu users need to install g++, libpci-dev, libncurses5-dev, libncursesw5-dev, zlib1g-dev, and libnl-dev. To build PowerTOP, again, a simple make, followed by sudo make install will do just fine.

To start PowerTOP 2.0 and see the updated, slightly cleaner interface (Figure 3), enter

sudo powertop
PowerTOP 2.0 displays the C-states for each CPU core.
Figure 3: PowerTOP 2.0 displays the C-states for each CPU core.

This version distributes the information over multiple registers and lets you use the left and right arrows to toggle between them.

The Overview tab takes you to the number of wake-ups per second view, including a view of the power hogs. Idle stats lists the C-states, and Frequency stats shows the P-states – but in far more detail and individually for each CPU core.

Device stats lists misbehaving devices such as network cards and USB adapters. For the network cards, PowerTOP tells you how many data packets cross the network per second. Finally, in the Tunables tab, PowerTOP lists the potential savings. Bad tells you that you need to take some action, whereas Good tags the measures you have already implemented. With the arrow keys, you can access a power-saving tip and then press Enter to enable the action (Figure 4).

The Tunables tab contains a list of power-saving tips in PowerTOP 2.0. Note that VM in the first tip stands for virtual memory, not virtual machine.
Figure 4: The Tunables tab contains a list of power-saving tips in PowerTOP 2.0. Note that VM in the first tip stands for virtual memory, not virtual machine.

Conclusions

Depending on your Linux system, PowerTOP can help you to save up to 10 watts an hour. On your laptop, you'll be rewarded for your effort with longer battery life. With a server farm, you can lower its electricity bills. Using PowerTOP to check your system and discover the baddies that keep your CPU awake is definitely a good idea. PowerTOP is not a cure-all, however. If you want to save power, you should switch off any devices and interfaces you don't use and move to more energy-efficient hardware.