MeeGo 1.2 Harmattan Developer Documentation Develop for the Nokia N9

Optimising power consumption

To reduce the energy consumption of an application, you must identify what type of behaviour increases the power consumption and modify the code accordingly. This section describes the most common causes for excessive power consumption by an application and gives practical instructions for developing applications for Harmattan devices.

Avoid polling

Waiting for something to happen in a busyloop (regardless of the inserted sleeps) or with a timer consumes a lot of power. Instead of polling, ensure that the application waits for events.

Note: Using sleep or a timer reduces energy consumption only slightly.

Avoid unnecessary activities

To reduce power consumption of your application, make sure that any code is only executed if the execution is strictly necessary.

Note: This concerns all code, including the code that checks if something is unnecessary.

Activities that can consume an excessive amount of energy include:

  • The application recalculates data if the source data has not changed.
  • An application wakes up to check new messages from a server when the device is in flight mode (radios disabled).
  • Display is updated when the display is blanked.
  • A regular timer wakes up a clock application only to determine that the application is not visible, which consumes a lot of power. Instead, the application can disable the timer when the application is not visible and restart it when the application receives a notification that it is again visible.

Avoid unnecessary wake-ups

To maximise battery life, the device enters the deep sleep mode whenever possible. The deeper the sleep mode, the less energy is consumed while sleeping. However, the deeper the sleep, the more time and power the device consumes when entering and exiting the sleep mode. Thus, waking the device up from the sleep mode, in addition to the actual processing of data, consumes a lot of power.

To minimise power consumption of your application, it must wake the device up as infrequently as possible, particularly if your application is running on the background while the device is otherwise idle.

Note: From the point of view of power consumption, the most important factor is the number of wake-ups, not the number of timers.

Tips:

  • If you have multiple timers, consider reducing the number of wake-up events by merging some of the timers into a single timer.
  • Consider using system heartbeat to wake up the application at the same time as other applications. This reduces the number of global wake-up events.
  • Only use timers when it is absolutely necessary. In many applications, the actions can be triggered by only events such as user activity.
  • Adjust the timer intervals to be as long as possible.

Use efficient algorithms and data structures

To minimise power consumption, make sure you use efficient algorithms and data structures in your application. Inefficient application logic can be the dominant factor that causes excessive power consumption. When you are optimising your application for speed, you are also optimising the power consumption of your application.

Example: Searching in a long linked list is much slower than carrying out a binary search in a sorted array, but updating the head of a linked list is much faster. Consider the following:

  • Which operation do you have more often?
  • Would it be better to implement the functionality with a tree, a hash table or something else?

Avoid inefficient or unnecessary network accesses

Transmitting and receiving data over wireless interfaces uses energy. Even though the consumed amount of energy depends, to some extent, on the length of the message, it is more important to minimise the number of data transfer sessions in general. If possible, combine data transfer sessions carried out by the application. Most network interfaces have similar hidden wake-up/sleep cost as the system sleep has and, for some interfaces, this extra cost is extremely significant. Use system heartbeat to allow applications to synchronise their network accesses.

Power consumption depends on the connection type (WLAN or 3G). When using 3G, multiple separate data transfer sessions consume a considerable amount of power. To decrease power consumption, ensure that the application monitors the connection type and status. When the connection switches to 3G, the application can retrieve data more seldomly. If there is no network connection available, the application must behave accordingly.

Tip: Consider using system heartbeat to combine data transfer sessions for different applications.

Prefer dark colours

Colours shown on the display of the device affect power consumption. Dark colours consume less power than light colours.

Tip: Avoid large areas with light colours in the application, if possible.

Avoid activating sensors unnecessarily

Since sensors can consume a considerable amount of power, consider carefully when sensors are needed for your application. Sensors have various service levels, so think carefully which is the minimum service level that your application needs. The required service level may vary during the application lifecycle.

Tips:

  • Always consider which resources you need and when you need them. For example, a tracking application recording both GPS and accelerometer data can switch GPS off when the location has not changed for some time. The application can restart tracking only after the accelerometer indicates the device is accelerating (in other words, being moved again).
  • If the application is in the background, consider whether the sensor needs to be used during that time.
  • Make sure that the reporting threshold of the accelerometer is not too low; otherwise it will consume a lot of energy with even minimum impact.

Important APIs

The following table describes the available APIs that can be used in power optimisation.

APIs for power optimisation
API Technology Description
Qt Quick Components: platformWindow QML Provides information about the following properties of the application window:
  • visibility
  • active status (whether the application has focus)
  • view mode (full size or thumbnail)

For more information, see Application lifecycle.

Qt Quick: Qt.application.active QML Indicates whether the application window is active and can be interactived with.

For more information, see QML Qt Element.

QmSystem: QmDisplayState C++ Indicates whether the display is on or off.
QmSystem: QmActivity C++ Indicates whether the user of the device is active or not based on, for example, touch screen events.
Qt Mobility System Information: QSystemBatteryInfo C++ / QML Provides information about the battery changing state, allowing power saving behaviour when battery level is low or performing heavy updates when the device is connected to a wall charger.
QmSystem: QmBattery C++ Same as QSystemBatteryInfo.
Qt Mobility System Information: QSystemAlignedTimer C++ / QML System heartbeat timer, allows you to synchronise periodic updates and operations with other applications, minimizing the number of overall wakeup calls in the system. For more information, see Using heartbeat section.
QmSystem: QmHeartBeat C++ Same as QSystemAlignedTimer
Synchronization Framework C++ Runs scheduled background activities. Often a better and more battery-friendly option than implementing a daemon that is running all the time. See the Integrating event feed into applications section for an example of using the Synchronization Framework.

Tools

  • You can analyse the consumption of power and other resources on the device with the Energy Profiler tool. For instructions, see Using Energy Profiler.
  • You can track wake-ups and system states with the PowerTOP tool. For instructions, see Using PowerTOP.