Title: Developing Power Friendly Mobile Applications
1Developing Power Friendly Mobile Applications
San Francisco 2005 February 8-9
- Dale Taylor
- Application Engineer
- Intel Corporation
- SSG Software Solutions Group
2Agenda
- Battery Awareness
- Optimizing for performance and battery life
- Being a good Power Citizen
- On Now power management
- System power status
- System power management events
- System sleep criteria
- PeekMessage and Windows
- Windows concerns
- Alternatives code structure
- Alternatives - GetMessage
- Power Evaluation Tool
- Understanding the tool
- Test plan and usage considerations
- How you can get the tool
- Case study PeekMessage vs. GetMessage
- Summary
3Battery Awareness
- Mobile users expect performance and demand longer
battery life. - This is not just a hardware issue.
- We as application developers must be good power
citizens. - Application decisions effect power consumption on
a platform level. - Next Some examples
4Optimizing for Battery Life
- CD drive playing music
- When reading a song, read it all, caching it
then the drive can spin down - Battery savings, and other benefits!
- If theres a read error, time to correct without
skipping - Enhanced Intel(R) SpeedStepTM Technology cant
lower processor frequency if your app is keeping
the system unnecessarily busy - Avoid spin wait loops (PeekMessage)
- Optimizing for performance and battery life are
mutually beneficial - Tune for performance first
5Being a Good Power Citizen
- Understand you are a part of the whole.
- Insight into your applications power consumption
and performance is your first step. - Know how your application affects the system,
devices used, and hardware requirements. - Pay attention to System messages.
- Standby/Hibernation the lid is going down
- No time to ask the user, make the best choice
- Provide user options if necessary, not in
real-time - Use available APIs.
- Lets look at some APIs and examples.
6On Now Power Management
- Supported by most current hardware
- Always on (sleeping), ready for use
- Instantly available
- Controlled by user power policy
- Benefits
- Reduced power consumption
- Fast startup performance - no boot up
- Enables automated tasks, even when off
- Manages power on a per-device basis
7Power Settings IBM T41
8Windows Power Management
9New Power Scheme IBM T41
Medium is Adaptive
10System Power Status
- Indicates source of Power (AC vs. DC), battery
life, and other information. - GetSystemPowerStatus() function
- Returns SYSTEM_POWER_STATUS BYTE
ACLineStatusBYTE BatteryFlagBYTE
BatteryLifePercentBYTE Reserved1DWORD
BatteryLifeTimeDWORD BatteryFullLifeTime - The system broadcasts
- PBT_APMPOWERSTATUSCHANGE
- to all applications when power status changes.
- Applications should also use GetDevicePowerState()
11System Power Management Events
- WM_POWERBROADCAST Message
- Used to broadcast to an application notification
of power-management events - Change in power supply or battery status
- System is suspending
- Power events can help you prepare for changes in
mode and not lose data by saving and closing
files. - Similar notification happens at restore
12System Sleep Criteria
- When the system determines there is no activity,
it will go to sleep. - Certain activities cannot be detected
Presentations but you do not want the screen to
go blank. - SetThreadExecutionState() notifies the system
that your application is busy. - You must remember to clear the flag when done!
13How Do We Use This Knowledge?
- Weve seen the APIs, lets apply them to a real
application. - As program developers, we all need to check for
messages. - Message loops are primary methods of
communicating with Windows. - PeekMessage has been around since Windows 3.1.
14PeekMessage and Windows Concerns
- MSDN has the following discussion
- An application should use a PeekMessage() loop
for as little time as possible. To be compatible
with battery-powered computers and to optimize
system performance, every Windows-based
application should inform Windows that it is idle
as soon and as often as possible. An application
is idle when the GetMessage() or WaitMessage()
function is called and no messages are waiting in
the application's message queue. - Common message pump method
- A power-friendly PeekMessage() loop exits when
background processing is complete because, while
an application is in a PeekMessage() loop,
Windows cannot go idle.
15Wise PeekMessage Usage
- Another method for performing idle processing in
an application involves embedding a message loop
in one of your functions. This message loop is
very similar to MFC's main message loop, found in
CWinThreadRun, thus with MFC it must perform
many of the same functions as the main message
loop. - while ( bDoingBackgroundProcessing )
- MSG msg
- while ( PeekMessage( msg, NULL, 0, 0,
PM_NOREMOVE ) ) if ( !PumpMessage( ) ) - bDoingBackgroundProcessing FALSE
PostQuitMessage( ) - break
-
- // Let MFC do its idle processing
- LONG lIdle 0
- while ( AfxGetApp()-gtOnIdle(lIdle ) )
-
- // Perform some background processing here,
call OnIdle -
16Controlling Sleep Time Directly
- The WaitMessage() function is one of the simplest
Windows functions. It simply puts the current
task to sleep. It is normally used by
applications that perform background processing
to control precisely when their application goes
to sleep. Most applications that do background
processing implement a PeekMessage() loop in the
following manner - if (PeekMessage(...) ! NULL)
- // Translate dispatch the message
- else if (there is background processing to
perform) - // Perform background processing
- else
- // No background processing, no messages - go
to sleep - WaitMessage(...)
17Case Study PeekMessage vs. GetMessage
- A simple application that can be compiled to run
using PeekMessage or GetMessage. - PeeksCalled TRUE for peeks, FALSE for gets
- The next slide has the source code for both the
PeekMessage and GetMessage loops. - Other than the actual Message call, the loops are
identical. - I left adding the ability to vary the percentage
of time spent using each call as an exercise for
the curious. - Next To evaluate the real differences, well
need some tools.
18PeekMessage vs. GetMessage (C-code)
19Valuable Tools and Looking at PeekMessage
- Useful Tools when evaluating an application
- Intel Vtune Performance Analyzer
- Microsoft Performance Monitor (Perfmon)
- Intel Mobile Computing Technology Kit
- Power Evaluation Tool
- Battery Power Information Tool
- Lets take a look at PeekMessage and dive into
optimizing an application that uses it.
20Power Evaluation Tool
- Command line tool that automates the process of
determining the total power consumption and net
power consumption of an application running on
battery-operated systems. - Can be used to compare
- Two versions of an application on the same
system. - The average power consumption of the same
workload on two different platforms. - Two different power management settings (same
workload and platform).
21Test Plan Considerations
- Perform a baseline test to identify the update
granularity of the battery power information
check power management options. - Extensive testing has shown large variations
between models and makes (many within 1 to 30
sec, others longer than 100sec) - The execution time of the workload needs to be
long enough for the power evaluation tool to
generate results. - Each test should be performed at least three
times. - Consider running tests of different length by
concatenating multiple copies of the original
test. - Increased accuracy of average power consumption
for longer tests
22Evaluating PeekMessage
- Power Evaluation Tool
- Runs in CMD window
- Produces a log file
- Several options
- Look at Results
23PeekMessage vs- GetMessage Results 1
24PeekMessage vs- GetMessage Results 2
25PeekMessage vs- GetMessage Results 3
26PeekMessage vs- GetMessage Results 4
27Summary
- Be battery-aware.
- Be a good software citizen.
- Use the available APIs and tools.
- Optimize your application for performance and
battery usage. - Use GetMessage and WaitMessage
- Plan for and handle system messages.
- Low power situations
- Hibernation
- Power Evaluation Tools are available to help.
28Questions?
29References
- KB93915 - MFC Application Idle Processing and
Power Consumption - KB40669 - Howto Post Frequent Messages Within an
Application - http//intelmktg.com/sdd/ for access to the
Mobilized Software Resource Kit and the Power
Evaluation Tool - Not Off the Hook Why App developers are an
important link in the power-management chain.
http//www.mobilizedsoftware.com/showArticle.jhtml
?articleId46200579 - DVD/CD Rendering Optimizing for Power on Mobile
Platforms http//www.devx.com/Intel/Article/21546/
2217?pftrue
30Intel Software Developer DispatchMobilized
Software Resource Kit
- MSI R2 Kit Content Highlight
- Best-practices content and code samples to plan
and design mobile applications (39 collateral) - Offline Capability
- Application Connectivity
- Power performance
- Multiple Platform support
- Mobile Security Manageability
- Intel Mobile Computing Technology Kit (MCTK) 6
tools, utilities and APIs for Intel Centrino
mobile technology-based systems - Performance optimization techniques for Intel
XScale technology-based mobile devices
- Intel Software Development Products for Windows
Evaluation licenses - How to Subscribe and Order
- Developers visit ISDD web page to subscribe
- ASMO http//intelmktg.com/sdd/
- Russia http//apps.apnet.ru/sites/ids_subscriptio
n/msi2/web_1D.htm - APAC http//www.softwaredispatch.net/site/cn/reg.
jsp - EMEA Using ASMO site
- While At this conference, visit the Intel booth
and sign up
31Intels Software Solutions Focus
- A worldwide consulting and services RD
organization - Specializing in distributed solutions and
enterprise infrastructure - Focus Accelerating technology adoption
- Development of practices
- Proving thru end user deployments
- Sharing best known methods with industry