Download pptx - 18.35 15.97

Transcript
Page 1: 18.35 15.97
Page 2: 18.35 15.97

Designing Energy Efficient Applications

Matthew RobbenProgram Manager IIMicrosoft Corporation

Page 3: 18.35 15.97

Objectives

Explain how user-mode applications written for Windows client customers can affect energy efficiency Provide a basic understanding of the technology spaceIncrease awareness of potential application design tradeoffs and their impacts on system energy efficiency Empower you to make the best choices for customers

Page 4: 18.35 15.97

Agenda

Windows energy efficiency goalsEnergy efficiency conceptsApplication design principlesMeasurement tools and metricsCall to actionResources

Page 5: 18.35 15.97

Windows Energy Efficiency Goals

Maximize efficiency by defaultDeliver the right balance of performance and power for all Windows customers on all form factorsAchieve reliable transitions and consistent consumer experiences

Page 6: 18.35 15.97

The Best Case Scenario

Enable long battery life for customers

Deliver a great customer experience

Page 7: 18.35 15.97

The Worst Case Scenario

Bring a charger

Photo credit: Bryan Thomas Thanks to Alper Sarikaya

Buy a new PC

Page 8: 18.35 15.97

Form Factor and Power Source

Mobile (AC and DC power)NetbooksUltra-mobile PCs Tablet PCsCommodity midrange laptopsHigh-end desktop replacements

Desktop (AC power only)Graduated range from value PC up to high-end gaming and media rigs

Mobile PCs outsold desktop PCs for thefirst time in 20081

1. Gonsalves, Antone. “Laptop Shipments Exceed Desktops For The First Time.” Information Week.

Page 9: 18.35 15.97

Windows 7 Power Plans

Balanced (Default)Targeted at highest efficiency possible

High PerformanceFavors performance over power

Power SaverFavors power over performance

OEMs can override defaults or define additional plans

Page 10: 18.35 15.97

Windows System Power States

Active (ACPI G0/S0) In useIdleAway Mode (user not present)

Sleep (ACPI G1/S3)Hibernate (ACPI G1/S4)Off (ACPI G2/S5)State transitions can be invoked manually, programmatically, or by timeout

Page 11: 18.35 15.97

Windows Idle Timeouts

Save power by turning off components after a period of inactivity

For example, turn off the display after 5 minutes

Notable timeoutsDisplay and monitor blankingHard disk drive (HDD) spindownSleep or hibernate

Page 12: 18.35 15.97

Away Mode

Addresses media sharing scenariosPC is required to be running (S0) but appears off, and sound is off

Is not a new power stateSystem remains in S0, power consumption is the same as S0

When entering Away modeDisplay is turned off and audio is mutedKeyboard and mouse input are ignored

Page 13: 18.35 15.97

Resource Consumption

Windows 7 Mobile PC at Idle

CPU7%

Memory2%

Graphics11%

IO Con-troller4%

Network9%

Display20%

Disk6%

ODD1%

Other:Devices, Chipsets,

and Power Distribution

38%

Page 14: 18.35 15.97

Resource Consumption: CPU

CPU has the largest dynamic range of any componentVaries with utilization and processor stateGoverned by a squared law

CPUs have performance states (P-states)

Done using frequency and voltage throttlingShould match output to demandTransitions governed by OS or hardware platform

CPUs also have power states (C-states)

Very low power, but cannot execute code

Idle

10%

20%

30%

40%

50%

70%10

0%0

5

10

15

20

25

Notebook CPU Power vs. Utilization

Utilization

Pow

er

(Watt

s)

Page 15: 18.35 15.97

Average Power Consumption by State

0%

10%

20%

30%

40%

50%

60%

70%

80%

90%

100%

Average Power Consumption by State

Po

we

r (%

of

Ma

xim

um

)

P-States C-States

Pn

P0

Page 16: 18.35 15.97

CPU Power States (C-States)

Source: Intel whitepaper Energy Efficient Applications

From left to right:Less power

Clocks offCaches flushed

Higher overheadLonger wakeupLost cache context

Page 17: 18.35 15.97

Spinning

Up

Read/

Writ

e

Activ

e Idle

Low Id

le

Spun

Dow

n0

1

2

3

4

5

Example HDD Power

Resource Consumption: Disk

Pow

er

Consu

mpti

on

(W)

Disk and other devices also incorporate power states

States are defined in specifications, but support is specific to driver and device

Disks spin down after a period of idle time to save power

15-minute idle timeout in Windows 7

All I/O costs more than just device power – CPU, memory, and interconnectivity are also affected

Page 18: 18.35 15.97

Real World Example: DVD Playback

Reduced CPU/GPU utilization for the scenarioIntelligent scaling of the workload based on power policySmart caching to reduce optical disk drive power

Windo

ws Vi

sta

SP1

Windo

ws 7

0

4

8

12

16

20

Power Consumption

OthersDevicesICHGMCHMemoryCPU

Pow

er

Con

sum

pti

on

(Watt

s)

18.35

15.97

Page 19: 18.35 15.97

Application Design Principles

Reduce Execution Frequency

Coalesce Timer

Activity

Optimize Resource

Usage

React to PC State

Transitions

Respect System Idle

Page 20: 18.35 15.97

Optimize Resource Consumption

Concept: resources = power Example: CPU utilization vs. power Best practices

Performance optimizations = power optimizations (generally)Use event-driven designs

Don’t poll or spin in tight loops

Consider energy cost of resource tradeoffs

Idle

10%

20%

30%

40%

50%

70%10

0%0

5

10

15

20

25

Notebook CPU Power vs. Utilization

Utilization

Pow

er

(Watt

s)

Page 21: 18.35 15.97

CPU Usage Reduces Battery Life

Use WaitForSingleObjectEx() or SleepEx() APIs

Allows you to wait for many different types of objects, I/O Completions, or APCs

void EatBatteryLife(){

HANDLE sharedResource = NULL;

//spawn multiple threads, one of which does this:while (sharedResource == NULL){

waitTime++;}

}

DO NOT DO THIS

Page 22: 18.35 15.97

WaitForSingleObjectEx() API Usage

//thread 1's codevoid UpdateSharedResource() {

//set sharedResourcesharedResource = UpdateResource();

// Set sharedResourceIsReadyEvent to // signaled

SetEvent(sharedResourceIsReadyEvent);}

//thread 2's code

void ConsumeSharedResource() {

DWORD dwWaitResult;

dwWaitResult = WaitForSingleObjectEx( sharedResourceIsReadyEvent, INFINITE,

FALSE); // indefinite wait

switch (dwWaitResult) { case WAIT_OBJECT_0: // // TODO: use sharedResource // break;

default: return 0; }}

Page 23: 18.35 15.97

Reduce Execution Frequency

Concept: CPU idle-state transitions take time and have a costExample: 1 ms timer resolution Best practices

Use the default timer resolution whenever possible

Cancel resolution requests when finished doing high-resolution work

Maximize concurrency, minimize overhead of switching between threads, and consolidate work into batches

15.6 ms (de-fault)

1 ms0123456789

10111213

CPU PowerSystem Power

Po

we

r C

on

sum

pti

on

(W

)

Page 24: 18.35 15.97

1 Millisecond Timers Reduce Battery Life

timeBeginPeriod() controls system-wide timer resolution

Highest outstanding resolution request is honored

Default is 15.6ms – avoid reducing if possible

void InitBatteryLifeEaterApp(){

timeBeginPeriod(1);//run your whole app

}void CleanupBatteryLifeEaterApp(){

timeEndPeriod(1);}

DO NOT DO THIS

Page 25: 18.35 15.97

Coalesce Activity

Concept: system should do periodic work in batches if possibleExample: application timersBest practices

Use the timer coalescing APIs in Windows 7Engineer for timing tolerance if periodic work is required

Timer tick15.6 ms

Periodic timer events

Windows 7

Windows Vista

Page 26: 18.35 15.97

SetWaitableTimerEx() API

Replace calls to SetWaitableTimer() with calls to this API

It is more efficient than a purely periodic timerIt has a tolerance parameter that you need to scale with the timer period

BOOL WINAPI SetWaitableTimerEx( __in HANDLE hTimer, __in const LARGE_INTEGER *lpDueTime, __in LONG lPeriod, __in_opt PTIMERAPCROUTINE pfnCompletionRoutine, __in_opt LPVOID lpArgToCompletionRoutine, __in_opt PREASON_CONTEXT WakeContext, __in ULONG TolerableDelay );

Page 27: 18.35 15.97

SetWaitableTimerEx() API Usage

void CreateAndSetPeriodicTimer(){

myTimer = CreateWaitableTimerEx(NULL,TimerName, //string with chosen timer nameNULL,TIMER_MODIFY_STATE); //required security attribute

to call //SetWaitableTimerEx

bError = SetWaitableTimerEx(myTimer,DueTime, //UTC due time10000, //periodic timer duration is ten secondsCompletionRoutinePointer, //APC completion routineArgsToCompletionRoutine, //completion routine argumentsWakeContext, //only if waking the machine1000); //tolerable delay is one second

//DO WORK

bError = CancelWaitableTimer(myTimer); //be sure to cancel periodic timers!}

Page 28: 18.35 15.97

React to PC State Transitions

Concept: tailoring your application to PC state can save lots of energyExample: Media Foundation DVD playback scaling by power planBest practices

Register for state change notificationsUse the notification callback to trigger behavioral changes

DVD0.00

5.00

10.00

15.00

20.00

25.00

DVD Playback Power Consumption

High Perf

Balanced

Power Saver

Po

wer

Co

nsu

mp

tio

n (

Wat

ts)

Page 29: 18.35 15.97

RegisterPowerSettingNotification() API

Allows you to register for change notifications on power settings

Register within application initialization codeDeregister when your application is closed

Callback is a notification to change application behaviorIncludes new power setting value

void MyApp::OnInit(){

hACDCSource = RegisterPowerSettingNotification(m_hWnd, &GUID_ACDC_POWER_SOURCE,DEVICE_NOTIFY_WINDOW_HANDLE);

}void MyApp::OnDestroy(){

if (hACDCSource != 0)UnregisterPowerSettingNotification(hACDCSource);

}

Page 30: 18.35 15.97

Control PC State Transitions

Concept: Some applications require system or monitor availability Example: burning a DVD or recording a TV showBest practices:

Use only when necessaryCancel request when finished with your taskConsider using Away mode for media tasks

Page 31: 18.35 15.97

PowerSetRequest() API

PowerSetRequest APIsReplaces setThreadExecutionState()Allows you to issue availability requests for monitor, system, and away modeAllows you to create a custom, localized reason stringDoes not prevent user-initiated sleep transitions

Page 32: 18.35 15.97

PowerSetRequest() API Usage

void KeepSystemAwake(){

// This example uses a simple, non-localized availablity request diagnostic stringPOWER_REQUEST_CONTEXT SimpleRqContext;SimpleRqContext.Version = POWER_REQUEST_CONTEXT_VERSION; SimpleRqContext.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; SimpleRqContext.Reason.SimpleReasonString = L“System needed to burn a CD.";

HANDLE SimplePowerRequest = PowerCreateRequest(&SimpleRqContext);

// Set a system request to prevent automatic sleepPowerSetRequest(SimplePowerRequest,PowerRequestSystemRequired);

// // Do work here...//

// Clear the requestPowerClearRequest(SimplePowerRequest,PowerRequestSystemRequired);

}

Page 33: 18.35 15.97

Respect System Idle

Idle dominates almost all usage scenarios for client systems

Reducing idle power is essential for extending battery lifeWindows 7 made vast improvements

Clean Install

IT Image0%

1%

2%

3%

4%

5%

6%

7%

8%

CPU Utilization on Idle System

CP

U U

tili

za

tio

n (

%)

Page 34: 18.35 15.97

Measuring Battery Life – Microsoft’s Approach

Instrumented SystemsExtremely granular dataSpecialized measurement hardware with instrumented rails (does not scale)

System power consumption measurementsMeasuring power draw from AC adapter or batteryDependent on meter accuracy

Measurement Points

System Under TestMeasurement System

Page 35: 18.35 15.97

Measuring Battery Life – the Developer’s Approach

Battery rundown testsSpecific repeatable workload on battery powerMeasure time taken to drain battery completely reflecting potential user experience

Error proneBattery chemistry and degradation over timeExcessive time investment

System resource usageProcessor, disk, GPU and network activityPower state instrumentation Hard to validate small code changesNo insight into hardware specific problemsIt’s only a proxy

Page 36: 18.35 15.97

Tools for Measuring Battery Life

Powercfg /energy Xperf

Page 37: 18.35 15.97

Summary

Your code has a direct impact on energy efficiency and the customer experienceAll resources contribute to the overall system power budget

Most resources are becoming “power aware”

Remember the design principles

Reduce Execution Frequency

Coalesce Timer

Activity

Optimize Resource

Usage

React to PC State

Transitions

Respect System Idle

Page 38: 18.35 15.97

Call to Action

Turn energy efficiency into a competitive differentiator

PC running Application A: 1.2 hours of battery lifePC running Application B: 3.5 hours of battery life

Search through your code for any APIs or development approaches called out as inefficient and rewrite Measure before and after to verify the impact of design changes

Page 39: 18.35 15.97

Resources

Background Processes: Developing Efficient Background Processes for Windows

http://www.microsoft.com/whdc/system/pnppwr/powermgmt/backgroundprocs.mspx

Mobile Battery Life Solutions for Windows 7: A Guide for Mobile Platform Professionals

http://www.microsoft.com/whdc/system/pnppwr/mobile_bat_win7.mspx

Power Availability Requestshttp://www.microsoft.com/whdc/system/pnppwr/powermgmt/availabilityrequests.mspx

Optimizing Windows Vista Platforms for Energy Efficiencyhttp://www.microsoft.com/whdc/system/pnppwr/powermgmt/optimize_power.mspx

Page 40: 18.35 15.97

Resources

Windows Timer Coalescinghttp://www.microsoft.com/whdc/system/pnppwr/powermgmt/timercoal.mspx

Application Power Management Best Practices for Windows Vista

http://www.microsoft.com/whdc/system/pnppwr/powermgmt/pm_apps.mspx

Developing Power-Aware Applications for Windows Vistahttp://216.55.183.13/pdc2005/slides/fun319_stemen_miller.ppt

Integrating Drivers and Applications with Windows Power Management

http://download.microsoft.com/download/f/0/5/f05a42ce-575b-4c60-82d6-208d3754b2d6/integrating-with-windowspowermgmt.ppt

Page 42: 18.35 15.97

© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.

The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after

the date of this presentation.MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.


Recommended