Developing Power Friendly Mobile Applications
Dale TaylorApplication Engineer
Intel Corporation
SSG – Software & Solutions Group
San Francisco
2005
February 8-9
Agenda
• 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
Battery 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
Optimizing 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 there’s a read error, time to correct without skipping
• Enhanced Intel(R)(R) SpeedStepTMTM Technology can’t 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
Being 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 API’s.
• Let’s look at some API’s and examples.
On 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
Power Settings IBM T41
Windows Power Management
New Power Scheme IBM T41
Medium is
Adaptive
System Power Status
• Indicates source of Power (AC vs. DC), battery life, and other information.
• GetSystemPowerStatus() function • Returns SYSTEM_POWER_STATUS {
BYTE ACLineStatus;BYTE BatteryFlag;BYTE BatteryLifePercent;BYTE Reserved1;DWORD BatteryLifeTime;DWORD BatteryFullLifeTime; }
• The system broadcasts PBT_APMPOWERSTATUSCHANGE to all applications when power status changes.
• Applications should also use GetDevicePowerState()
System 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
System 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!
How Do We Use This Knowledge?
• We’ve seen the API’s, let’s 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.
PeekMessage 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.
Wise PeekMessage UsageAnother 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 CWinThread::Run, 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()->OnIdle(lIdle++ ) ) ; // Perform some background processing here, call OnIdle}
Controlling 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 messageelse if (there is background processing to perform) // Perform background processingelse // No background processing, no messages - go to
sleep WaitMessage(...);
Case 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, we’ll need some tools.
PeekMessage vs. GetMessage (C-code)
Valuable 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
• Let’s take a look at PeekMessage and dive into optimizing an application that uses it.
Power 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).
Test 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
Evaluating PeekMessage
Power Evaluation Tool• Runs in CMD window• Produces a log file• Several options
• Look at Results
PeekMessage –vs- GetMessage Results #1
PeekMessage –vs- GetMessage Results #2
PeekMessage –vs- GetMessage Results #3
PeekMessage –vs- GetMessage Results #4
Summary
• 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.
Questions?
References
• 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?articleId=46200579
• DVD/CD Rendering: Optimizing for Power on Mobile Platforms http://www.devx.com/Intel/Article/21546/2217?pf=true
Intel 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_subscription/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
Intel’s Software & Solutions Focus
Deployment Intel® Solution Services Solution practices to accelerate deployment and share with the industry
Development Intel Developer Services On-line resource to accelerate development on IA
Software Development Products
Tools to optimize software application performance on Intel architecture
New Products and Technologies
Software Engineering Engineering efforts focused on emerging software trends (e.g. .NET, Linux, runtime environments)
Intel Labs Thought leadership and advancing computing technologies
• A worldwide consulting and services R&D 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
TechnologyAdoption Acceleration
INCUBATIONINCUBATION ADAPTATIONADAPTATION TAKE-UPTAKE-UP MATURITYMATURITY
Source Intel