Energy efficiency

From Inforail
Jump to: navigation, search

Why does it matter?

Designing software with energy efficiency in mind is a noble thing to do.

  • Planetary scale
  • Keeping in mind the current rate of the development of the industrialized world, and our power consumption needs, it will be very difficult to keep up. On one hand, we can look for more sources of power, but from a developer's perspective
  • designing power efficient software is our shot at the same problem, from the other end
  • Personal scale
    • No one wants phones and laptops that quickly drain their power
    • Not many of us can afford, or are willing to carry bigger batteries that mitigate the problem

Improvements on this front make the software better

  • Thus you get more profit and popularity
  • You get to save the planet too!


Approaches

  • Planning - do I really need this feature?
  • Hardware - be aware of the features provided by the underlying platform or the hardware you're using
  • Software
    • Configuration - maybe the solution is as simple as ticking a checkbox
    • Programming - think hard and engineer it in a power-friendly way

Hardware aspects

Modern systems come with peripherals that can be disabled temporarily or set into sleep states. Make sure that these features are not disabled without a good reason.

  • ACPI - Advanced Configuration and Power Interface - specs that define power states for computer hardware.
  • CPU sleep states: C0, C1, C2, C3.
    • C0 - the processor is active and consumes a great amount of power
    • C3 - a sleep state, with a minimal power footprint
  • Other states
    • D for 'device'
    • P for 'performance'
    • V for 'vendetta'
    • 0 corresponds to the most power consuming state, N - the most 'sleepy' one


Methods

  • Think carefully about your background processes
    • Consider outsourcing the functionality to other tools, such as cron or the Task scheduler, thus removing the need of having a daemon
    • If you think you need a daemon, consider using a scheduled task instead
  • Avoid busy waiting
  • Take into account the system's state before performing certain actions
  • Review your hardware interactions, such as
    • HDD I/O - dumping a cache, writing data to a log ...
    • polling removable devices, cards in smart card readers ...
  • Rely on hardware accelerated features when possible
    • for generating random data, encrypting
    • encoding/decoding audio or video

Windows

Note that many of the features described here were introduced in Windows 7, a few were available in Vista. If you're writing code that is supposed to work on older platforms, make sure you won't rely on these features unless they are available.

Services
  • Startup
    • Delayed - for services that don't have to be up instantly
    • Manual - for on-demand activities, the service won't start until it is really needed
  • Benefits
  • Service triggers are events that services can use to enable or disable themselves. A service may be tied to
    • the availability of a network connection,
    • presence of a device
    • connection to an Active Directory domain
    • custom events can be developed as well
Scheduled tasks
  • The task scheduler can be used for actions such as update checks or maintenance.
  • Conditions function like service triggers, you can bind a task to things such as:
    • The system being idle, it is defined as "... if no keyboard or mouse activity has occurred during the previous 15 minutes and if there has been 0 percent CPU usage and 0 percent disk input or output for 90 percent of that same period of time."
    • availability of a network connection
    • the system is in a specific power state
Benefits
  • Reduced attack surface due to the fact that the process is not always running
  • Faster shutdown, sleep, hibernation and startup - a better user experience is a nice side-effect of power efficiency
Power availability requests

This is a mechanism that can tweak power consumption and adapt the system's behaviour to match a user's expectations. For example, you don't want the screen to dim or turn off when watching a movie or having a lengthy Skype session.

Another scenario - your automated backups application must not be interrupted while a data backup is in progress, otherwise this may result in incomplete copies; you will most likely want to prevent the system from going to sleep.

With these requests, you can:

  • Temporarily disable display power management.
  • Temporarily disable automatic sleep.
  • Enable away mode.

Note that the feature was introduced in Windows 7 and is not available in older versions.

Power events

These occur when the system changes, or is about to change a state. You can be notified when the battery is running out, or when the battery reached a critically low level.

Timers

The default setting on Windows is for timers to tick at 15.6 ms. Some applications reduce it 1 ms, on mobile systems this can decrease battery life by up to 25%.

Timers are used for actions that need to be performed periodically. Try to switch to an event-driven approach, and remove the use of a timer, otherwise the system will be brought out of a sleep state too often, or may not be able to enter such a state, because it is interrupted.

MSDN offers a timer-related guide that walks you through the basics of timers in the context of power.

  • Coalescing timers - adding a tolerance threshold that allows a timer to be fired a bit later, thus it can be grouped with other timers.
  • Resolution of a timer - how frequently it needs to fire. Resolution can be increased and decreased dynamically, make sure you set it back to a power-friendly value when you're done with the use case that requires maximum responsiveness.

Tools

  • Powertop - observe which processes caused the CPU to switch to C0
    • Success stories that illustrate how Powertop helped reveal some issues in certain applications.
  • Process Explorer is very feature rich, one of the options it to view the call stack of a given process or thread. Examine the stacks of your candidates, you can eliminate those who are currently at WaitForCriticalSection, WaitForMultipleObjects or WaitForSingleObject (see waiting functions below) - this means that they're sleeping, the power-consuming culprit must be elsewhere. While this doesn't help you detect the source of the power drain, it tells you where the source is not.
  • PowerCfg is a Windows tool that can observe the running processes and tell us about the changes they cause in power states; it can configure system settings too.
  • Intel battery life analyzer - Powertop-like tool for Windows.
  • Intel power checker - measure the performance of your process; does not run on desktop computers, because it requires the system to be powered by the battery when the test is run

Functions

Windows

There are multiple primitives related to waiting, you can rely on them to set your process to sleep until a certain condition occurs. MSDN documents the waiting functions.

  • WaitForSingleObject
  • WaitForMultipleObjects
  • PowerCreateRequest - see availability requests above.
    • must be accompanied by a string that is shown to people when the system's power settings are changed
    • PowerClearRequest - call this when you're done, to let the OS know it can go back to doing whatever it was doing prior to your intervention


  • select - sockets et al


Notes

  • I was kidding about V states
  • Screensavers on LCD screens will draw power without 'helping' the screen in any way.
  • Stories
    • PD and the ambiguous shutdown/hibernate message
    • Noteman's timers and reminders

References