Getting the longest battery life on portable Linux machines is yet another moving target as kernels and standards change and vendors continue to snuggle up to Microsoft at the expense of non-Windows users. There was a bit of controversy at the release of the 3.x kernel because it contained a power regression (or not a power regression but something else that behaved like a power regression depending on who was talking) and the result was that Linux got considerably less battery life than Windows on the same machines. This was especially obvious to dual-boot users. This is a long complex story, so if you’re interested in the details see the links at the end.
Whatever the state of your kernel, follow along to learn how to hunt down power hogs and tweak your system for best battery life with the latest release of PowerTOP.
PowerTOP was developed by Intel to track down programs that waste power, and to gather useful data for optimizing hardware. In this article we’re not using our granny’s PowerTOP, but the overhauled and improved 2.x version updated earlier this year. This has a whole lot of improvements you can read about in the release announcement.
PowerTOP is supposed to support Intel, AMD, ARM, and UltraSparc processors. For me, it’s been inconsistent with AMD processors and sometimes won’t report idle statistics, and it’s always reliable on Intel CPUs. It gives a lot of information on your other hardware components, applications, and processes regardless of which CPU you’re using.
Starting with version 2.6.21, the 32-bit Linux kernel became a tickless kernel. 64-bit kernels became tickless in version 2.6.23. Before the tickless kernel, idle kernels woke themselves up at a rate of 100 to 1000Hz, depending on how they were configured, to look for something to do, so they were never really idle. The tickless kernel is more of a slacker, and when it’s idle it stays that way until some external event wakes it up.
Check your /boot/config-* file to see if your kernel is tickless. To do this first get your kernel version, then
grep the config file like this:
$ uname -r 3.2.0-29-generic $ grep NO_HZ /boot/config-3.2.0-29-generic CONFIG_NO_HZ=y
CONFIG_NO_HZ=y means yes, this is a tickless kernel.
Measuring Power Use
In theory this should result in lower power use. But the nicest theories are often spoiled by rude reality, and misbehaving and unnecessary applications waste power. PowerTOP reports how much power each process uses when you’re on battery, and not plugged in. Figure 1 shows what a moderately busy laptop on battery looks like.
This shows the discharge rate for your laptop battery, and how much power everything on your system is using in order from the most to the least. It also shows how many times each one sends a CPU interrupt.
When you’re plugged in it sorts by numbers of interrupts. This is a useful view that shows who is pestering your CPU the most. Figure 2 shows that Web browsers are busy and pesky, and that streaming audio is a heavy CPU user.
To get more accurate results, run PowerTOP in calibration mode:
$ sudo powertop –calibrate
This takes a few minutes, so you might as well go for a healthy walk. It will test different workloads, USB, and change screen brightness several times, so don’t be alarmed. Just leave it alone until it’s finished.
powertop --html=filename to generate a nicely-formatted HTML page of your results, replacing filename with the filename of your choice. You can create a useful baseline by running this after a cold startup, without opening any applications, and then run it a few more times throughout the day to get a comparison of different workloads. (Using a different filename each time, of course.)
Most distributions do a nice job of configuring power management, but they don’t catch everything, and there are often services running that you don’t need. For example, Bluetooth is usually enabled by default, which is fine if you need it, but if you don’t it’s a small steady power waste. If you install and test a lot of software you’re going to have cruft. That cruft adds up. It seems like everything wants a MySQL backend these days, whether it’s really necessary or not, so I’m always having unnecessary MySQL processes running. And whole LAMP stacks, and nameservers, and all kinds of things. Skype is a notorious power hog, and even though its Linux support is lukewarm, don’t feel too badly because it causes power and heat problems on Windows, too. Firefox and Chrome suck up the watts, especially when you have a lot of tabs open or multimedia running. This example is Firefox open with multiple tabs, and playing some online streaming audio:
986.9 mw 3.0 Process /usr/lib/firefox/firefox
That’s almost as much as the display backlight. Compare to a fresh instance of Firefox open to the about: page, which uses a mere 96.0 mw. firefox/plugin-container is measured separately, and that also sucks up a fair bit of power.
You can also use PowerTOP to profile and tweak hardware configurations, but this is more for developers than end users. Editor willing and the creek don’t rise we’ll take a look at hardware tweaks in a future installment.
Regression or Feature?
Check out these articles for more information on the power regression/not a regression in recent Linux kernels, and a lot of useful information on the complexities of power management on computers.