April 30, 2009

Power Saving with Linux

Regardless of the state of the economy, price of electricity, or proximity to Earth Day, saving power is always a good idea. Recent advances in the kernel have brought energy-saving to a new level for Linux systems, enabling better control over power usage at the processor and on the motherboard. Other tools allow you to audit which applications wake up the processor unnecessarily, so you can tune your system accordingly.

Less Power!

Linux can conserve processor power in two ways: throttling back the frequency at which the processor runs when active, and better managing the idle state of the processor when it isn't. The first technique is called CPU frequency scaling, and the second takes advantage of a feature called "tickless idle" in recent kernels.

Frequency scaling adjusts the speed at which the processor's clock runs (also known as the processor's P-states). For many day-to-day computing tasks, running at a lower processor speed will have no discernible effect, and on portable devices can greatly increase battery life. CPU scaling uses the cpufreq module, which is provided by default in most consumer Linux distributions. You can tell cpufreq to use different strategies for selecting processor speed, such as always using the highest speed available (the "performance" policy) or the lowest (the "powersave" policy).

More flexible policies exist, too, such as "ondemand" which increases CPU speed when the load increases, and "userspace" which defers the decision to an interactive application. There are several applications to help you make use of cpufreq, including panel applets for KDE and GNOME and command-line utilities. Check your distribution's documentation to see what is included.

Even running at its lowest frequency the processor consumes more power than it does in an idle state. Modern CPUs have multiple sleep states (also known as C-states), each of which uses successively less power than the one before. Since the kernel determines when to put the processor to sleep and when to wake it up, good idle state management can make a tremendous difference in overall power consumption. Linux made a big step forward in this area with the introduction of the "tickless idle" feature of kernel 2.6.21 in 2007.

Enabling the tickless idle feature prevents the kernel (and thus the processor) from waking up at regular intervals to increment counters and check for application events waiting to be processed. Instead, the kernel will only awaken the processor when an event is scheduled, and will try to schedule multiple events to process together--thus allowing the processor to remain in its sleep state for longer intervals and consume less power.

Linux also supports the two most widespread standards for system-level power management--the older Advanced Power Management (APM) and more recent Advanced Configuration and Power Interface (ACPI)--that enable features like suspending and hibernating the entire computer. These reduced power states are common in laptops and portable devices, allowing you to suspend the computer after a specified idle interval or simply by closing the lid. Support for suspend and hibernate is built into both major Linux desktop environments, and can be configured through their respective control panels.

Taking Advantage

Even though the kernel can save power by aggressively controlling processor P-states and C-states, that capability is wasted if the applications running on the system do not take advantage of it--waking up processes too frequently, polling hardware unnecessarily, and so on. Intel sponsors the LessWatts.org Web site to provide Linux users and developers with information and tools to help better take advantage of power-saving principles on a typical machine.

powertop.jpgThe first tool to consider is PowerTop, a utility that performs an audit of what applications and events wake up the processor. You run PowerTop from the command line, usually as root to collect the most accurate data. It then monitors the system for a period of seconds, keeping track of hardware interrupts, wake-ups, and other resource consumption. Once it has collected sufficient data to profile your system, PowerTop will present a a chart depicting how much time your processor spends (on average) in each available C-state, along with a table of the biggest power draining problems it has found.

PowerTop also gives you the option to correct each power-draining problem. For example, if you keep your Bluetooth adapter switched on 24/7, PowerTop will not only tell you that you can save power by switching it off when not in use, but you can do so from within PowerTop. At lower levels, PowerTop will detect less visible culprits, from a rogue database daemon waking up multiple times every second to check for new connections, to a misbehaving video card driver.

Sometimes, the fixes for power-consuming applications are easy to implement, such as a command-line option that prevents xterm processes from waking up once every second when idle. Many others require patches to the source code of the application in question, though, and the PowerTop project regularly releases patches for common applications on its Known issues, tip and tricks page. In most cases, developers of the patched application pick up on the change and incorporate it into the mainline code, but you can also download and apply the PowerTop patched yourself if you can't afford to wait.

The site also makes resources available for software developers to help them avoid power-wasting designs like unnecessary interrupts or high-speed loops. Check out the Applications Power Management section to learn more. The LessWatts.org site hosts many other projects on the subject of reducing power consumption, including bus power management, graphics adapter power reduction, and power performance monitoring for the kernel itself.

Risk and Reward

Basic processor power management and hardware suspend behavior are extremely unlikely to harm your system, but you should take care when using PowerTop to adjust system parameters. Some of PowerTop's recommended adjustments, such as automatically suspending the USB controller during idle, could have unexpected results. Furthermore, it is usually best to let patches to a third-party application work their way through the application's bug triage and QA process. Always check to see whether the patches from LessWatts.org have already been accepted by the application or by your Linux distribution--if they have, it is much easier to stay up-to-date by using those official software sources than by patching and manually compiling from source.

Enterprise Power Saving Through Virtualization

Businesses that maintain racks and rooms full of equipment have the opportunity to save power on an even greater scale than desktop Linux users by simply combining multiple physical servers into one Linux box with virtualization. Virtualization is a broad and complex topic worthy of in-depth treatment on its own, but if you have legacy servers (particularly if they operate at low loads), don't overlook virtualization's cost savings to your company based on the reduced power consumption of a single machine. There are many platform virtualization options for Linux as the host or guest OS, both open source and proprietary.