143
These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson OS X Performance Mercenary

Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

These are confidential sessions—please refrain from streaming, blogging, or taking pictures

Session 712

Energy Best Practices

Matt JacobsonOS X Performance Mercenary

Page 2: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 3: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 4: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 5: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 6: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 7: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 8: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 9: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Topics

Page 10: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

• Recognizing energy issues

Topics

Page 11: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

• Recognizing energy issues•Diagnosing issues

Topics

Page 12: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

• Recognizing energy issues•Diagnosing issues•Avoiding common pitfalls

Topics

Page 13: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

• Recognizing energy issues•Diagnosing issues•Avoiding common pitfalls•Adopting best practices and new APIs

Topics

Page 14: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

What Uses Energy?

Page 15: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

What Uses Energy?

CPU

Page 16: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

What Uses Energy?

I/OCPU

Page 17: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

What Uses Energy?

I/OCPU Graphics

Page 18: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

What Uses Energy?

I/OCPU Graphics

Page 19: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

CPU Usage

Page 20: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

How to Use the CPU

Page 21: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

How to Use the CPU

•Modern CPUs have many cores, fast clocks, etc.

Page 22: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

How to Use the CPU

•Modern CPUs have many cores, fast clocks, etc.• Still limited by the size of the battery

Page 23: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

How to Use the CPU

•Modern CPUs have many cores, fast clocks, etc.• Still limited by the size of the battery• If you ask to run, you’ll run

Page 24: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

How to Use the CPU

•Modern CPUs have many cores, fast clocks, etc.• Still limited by the size of the battery• If you ask to run, you’ll run• Single app can impact battery life

Page 25: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

CPU Power DrawVs. idle

Page 26: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

CPU Power DrawVs. idle

1% CPU

10% CPU

100% CPU

10% higher power draw

2x power draw

10x power draw

Page 27: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

CPU Power DrawVs. idle

1% CPU

10% CPU

100% CPU

10% higher power draw

2x power draw

10x power draw

Page 28: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

CPU Power DrawVs. idle

1% CPU

10% CPU

100% CPU

10% higher power draw

2x power draw

10x power draw

Page 29: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

How Much Is Too Much?

• Be absolutely idle when app is not in use■ Eliminate nonuser-driven work■ Wait quietly for user input

Page 30: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

How Much Is Too Much?

• Be absolutely idle when app is not in use■ Eliminate nonuser-driven work■ Wait quietly for user input

• Be efficient when user requests action■ Performance = power■ Efficient, multithreaded algorithms win (use GCD)■ Then race back to idle

Page 31: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Recognizing High CPU UsageActivity Monitor

Page 32: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Recognizing High CPU UsageActivity Monitor

Page 33: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 34: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 35: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 36: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 37: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 38: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 39: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

top(1)Recognizing High CPU Usage

$ top -pid <pid>PID COMMAND %CPU TIME #TH #WQ #PORT #MREG MEM23106 StockWatcher 2.2 00:13.55 7/1 4/1 132 168 6340K

Page 40: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

top(1)Recognizing High CPU Usage

$ top -pid <pid>PID COMMAND %CPU TIME #TH #WQ #PORT #MREG MEM23106 StockWatcher 2.2 00:13.55 7/1 4/1 132 168 6340K

Page 41: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

top(1)Recognizing High CPU Usage

$ top -pid <pid> -aPID COMMAND %CPU TIME #TH #WQ #PORT #MREG MEM23106 StockWatcher 2.2 00:00.20 7/1 4/1 132 168 6340K

Page 42: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

top(1)Recognizing High CPU Usage

$ top -pid <pid> -aPID COMMAND %CPU TIME #TH #WQ #PORT #MREG MEM23106 StockWatcher 2.2 00:00.20 7/1 4/1 132 168 6340K

Page 43: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 44: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 45: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 46: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageResource exceptions

Page 47: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageResource exceptions

kernel[0] <Debug>: process SomeApp[202] thread 1989 caught burning CPU! It used more than 50% CPU (Actual recent usage: 97%) over 180 seconds ...

Page 48: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageResource exceptions

kernel[0] <Debug>: process SomeApp[202] thread 1989 caught burning CPU! It used more than 50% CPU (Actual recent usage: 97%) over 180 seconds ...

/Library/Logs/DiagnosticReports/SomeApp_2013-06-13.cpu_resource.spin

Page 49: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageResource exceptions

kernel[0] <Debug>: process SomeApp[202] thread 1989 caught burning CPU! It used more than 50% CPU (Actual recent usage: 97%) over 180 seconds ...

/Library/Logs/DiagnosticReports/SomeApp_2013-06-13.cpu_resource.spinProcess: SomeApp [202]Architecture: x86_64Parent: launchd [137]

Powerstats for: SomeAppMicrostackshots: 64 samples 64 thread_start + 13 (libsystem_pthread.dylib) [0x7fff922b2fe9] 64 _pthread_start + 131 (libsystem_pthread.dylib) [0x7fff922ae7a0] 64 _pthread_body + 138 (libsystem_pthread.dylib) [0x7fff922ae90f] 64 run_thread + 17 (SomeApp) [0x10996bf11] 64 do_stuff + 9 (SomeApp) [0x10996bef9]

Page 50: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageSecondary indicators

I/OCPU Graphics

Page 51: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageSecondary indicators

I/OCPU Graphics

Page 52: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

fs_usageDiagnosing High CPU Usage

$ sudo fs_usage MyApp

Page 53: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

fs_usageDiagnosing High CPU Usage

$ sudo fs_usage MyApp20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:27 open ../Downloads.plist 0.000086 MyApp 20:29:27 write 0.000017 MyApp 20:29:27 close 0.000048 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp

Page 54: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

fs_usageDiagnosing High CPU Usage

$ sudo fs_usage MyApp20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:27 open ../Downloads.plist 0.000086 MyApp 20:29:27 write 0.000017 MyApp 20:29:27 close 0.000048 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp

Page 55: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

fs_usageDiagnosing High CPU Usage

$ sudo fs_usage MyApp20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:27 open ../Downloads.plist 0.000086 MyApp 20:29:27 write 0.000017 MyApp 20:29:27 close 0.000048 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp

Page 56: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

fs_usageDiagnosing High CPU Usage

$ sudo fs_usage MyApp20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:26 stat64 MyIcon.png 0.000002 MyApp 20:29:27 open ../Downloads.plist 0.000086 MyApp 20:29:27 write 0.000017 MyApp 20:29:27 close 0.000048 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp 20:29:27 stat64 MyIcon.png 0.000002 MyApp

Building Efficient OS X Apps Russian HillTuesday 2:00PM

Page 57: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageSecondary indicators

I/OCPU Graphics

Page 58: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageSecondary indicators

I/OCPU Graphics

Page 59: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageQuartz Debug

Page 60: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageQuartz Debug

Page 61: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageQuartz Debug

Page 62: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageQuartz Debug

Page 63: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Diagnosing High CPU UsageQuartz Debug

Graphics Tools for Xcodehttp://developer.apple.com

Page 64: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

•A little is a lot• Ensure absolute idle•Watch with Activity Monitor

■ Especially when idle in the background

•Diagnose with Time Profiler

CPU Usage

Page 65: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Timers

Page 66: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Timers

Page 67: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Timers

•Used to■ Schedule future/periodic events■ Drive animations

Page 68: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Timers

•Used to■ Schedule future/periodic events■ Drive animations

• Keep apps out of idle

Page 69: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Timers

•Used to■ Schedule future/periodic events■ Drive animations

• Keep apps out of idle• Can have outsize energy effect

Page 70: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Timers

Power Consumption

Memory Controller/Caches

RAM, Buses, Clocks…

Baseline Idle Power

CPU PowerTime

Page 71: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Timers

Power Consumption

Memory Controller/Caches

RAM, Buses, Clocks…

Timers Firing

Baseline Idle Power

CPU PowerTime

Page 72: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Timers

Power Consumption

Memory Controller/Caches

RAM, Buses, Clocks…

Timers Firing

Baseline Idle Power

CPU Power

Useful work vastly outweighed bywake-up costs

Time

Page 73: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Timers

Power Consumption

Memory Controller/Caches

RAM, Buses, Clocks…

Timers Firing

Baseline Idle Power

CPU Power

High frequencies inhibit platform idle

Useful work vastly outweighed bywake-up costs

Time

Page 74: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

What Is a Timer?

• Timer APIs■ Grand Central Dispatch timers■ CFRunLoopTimer■ NSTimer■ CFRunLoopRunInMode with timeout

Page 75: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

What Is a Timer?

• Low-level APIs■ sleep(), usleep(), nanosleep()

•APIs with timeouts■ pthread_cond_timedwait()■ dispatch_semaphore_wait()■ select(), poll(), kevent(), mach_msg()

Page 76: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

What Is a Timer?

•High-level APIs■ dispatch_after()■ -[NSObject performSelector:withObject:afterDelay:]■ NSProgressIndicator■ CVDisplayLink (OS X)/CADisplayLink (iOS)■ And more…

Page 77: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Recognizing Timer IssuesActivity Monitor

Page 78: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Recognizing Timer IssuesActivity Monitor

Page 79: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Recognizing Timer IssuesActivity Monitor

Page 80: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 81: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 82: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 83: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 84: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 85: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 86: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

timerfiresDiagnosing Timer Issues

$ sudo timerfires -p <pid> -s

Page 87: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

timerfiresDiagnosing Timer Issues

$ sudo timerfires -p <pid> -sTIME(ms) PID PROCESS TYPE TIMER ROUTINE 435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 933 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 1055 1603 MyApp dispatch MyApp`-[AppModel updateWidgets:] 1435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 1935 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 2055 1603 MyApp dispatch MyApp`-[AppModel updateWidgets:] 2435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 3004 1603 MyApp sleep libsystem_kernel.dylib`__semwait_signal+0xa libsystem_c.dylib`usleep+0x36 MyApp`-[AppModel pollForChange]+0x1a libdispatch.dylib`_dispatch_call_block_and_release+0xc

Page 88: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

timerfiresDiagnosing Timer Issues

$ sudo timerfires -p <pid> -sTIME(ms) PID PROCESS TYPE TIMER ROUTINE 435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 933 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 1055 1603 MyApp dispatch MyApp`-[AppModel updateWidgets:] 1435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 1935 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 2055 1603 MyApp dispatch MyApp`-[AppModel updateWidgets:] 2435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 3004 1603 MyApp sleep libsystem_kernel.dylib`__semwait_signal+0xa libsystem_c.dylib`usleep+0x36 MyApp`-[AppModel pollForChange]+0x1a libdispatch.dylib`_dispatch_call_block_and_release+0xc

Page 89: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

timerfiresDiagnosing Timer Issues

$ sudo timerfires -p <pid> -sTIME(ms) PID PROCESS TYPE TIMER ROUTINE 435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 933 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 1055 1603 MyApp dispatch MyApp`-[AppModel updateWidgets:] 1435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 1935 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 2055 1603 MyApp dispatch MyApp`-[AppModel updateWidgets:] 2435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 3004 1603 MyApp sleep libsystem_kernel.dylib`__semwait_signal+0xa libsystem_c.dylib`usleep+0x36 MyApp`-[AppModel pollForChange]+0x1a libdispatch.dylib`_dispatch_call_block_and_release+0xc

Page 90: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

timerfiresDiagnosing Timer Issues

$ sudo timerfires -p <pid> -sTIME(ms) PID PROCESS TYPE TIMER ROUTINE 435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 933 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 1055 1603 MyApp dispatch MyApp`-[AppModel updateWidgets:] 1435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 1935 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 2055 1603 MyApp dispatch MyApp`-[AppModel updateWidgets:] 2435 1603 MyApp CF MyApp`-[AppDelegate timerFired:] 3004 1603 MyApp sleep libsystem_kernel.dylib`__semwait_signal+0xa libsystem_c.dylib`usleep+0x36 MyApp`-[AppModel pollForChange]+0x1a libdispatch.dylib`_dispatch_call_block_and_release+0xc

Page 91: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Don’t Use Timers for… Synchronization

Page 92: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Don’t Use Timers for… Synchronization

BOOL workIsDone = NO;

/* thread one */void doWork(void) { /* wait for network ... */ workIsDone = YES;}

Page 93: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Don’t Use Timers for… Synchronization

BOOL workIsDone = NO;

/* thread one */void doWork(void) { /* wait for network ... */ workIsDone = YES;}

/* thread two */void waitForWorkToFinish(void) { while (!workIsDone) { usleep(100000); /* 100 ms */ } [WorkController workDidFinish];}

Page 94: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

• Serial dispatch queues•Dispatch semaphores• Pthread condition variables

Use Built-in Synchronization Instead

Page 95: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Use Built-in Synchronization Instead

BOOL workIsDone = NO;

/* thread one */void doWork(void) { /* wait for network ... */ workIsDone = YES;}

/* thread two */void waitForWorkToFinish(void) { while (!workIsDone) { usleep(100000); /* 100 ms */ } [WorkController workDidFinish];}

Page 96: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Use Built-in Synchronization Instead

my_queue = dispatch_queue_create(“com.myapp.myq”, DISPATCH_QUEUE_SERIAL);

/* thread one */void doWork(void) { dispatch_sync(my_queue, ^{ /* wait for network ... */ });}

/* thread two */void waitForWorkToFinish(void) { dispatch_sync(my_queue, ^{ [WorkController workDidFinish]; });}

Page 97: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Don’t Use Timers for… Polling

NSTimer *myTimer = [[NSTimer alloc] initWithFireDate:date interval:0.5 target:self selector:@selector(checkForFile:) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];

Page 98: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Use Event Streams Instead

• File system updates■ DISPATCH_SOURCE_TYPE_VNODE for small-scale updates■ FSEvents framework (OS X) for filesystem-wide updates

• Inter-process notifications■ XPC■ NSDistributedNotificationCenter■ notify(3)

Page 99: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Use Event Streams Instead

•Disk Arbitration notifications• I/O Kit matching notifications•Network events

■ Apple Push Notification Service■ Bonjour■ Network reachability

Page 100: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Use Event Streams Instead

NSTimer *myTimer = [[NSTimer alloc] initWithFireDate:date interval:0.5 target:self selector:@selector(checkForFile:) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];

Page 101: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Use Event Streams Instead

const uint64_t flags = DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE;dispatch_source_t my_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fd, flags, queue);

dispatch_source_set_event_handler(my_source, ^{ [self checkForFile];});

dispatch_resume(my_source);

Page 102: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Specify Suitable Timeouts

Page 103: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Specify Suitable Timeouts

for (;;) { long rv; dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, 500 * NSEC_PER_MSEC);

rv = dispatch_semaphore_wait(my_sema, timeout); if (havePendingWork) { [self doPendingWork]; }}

Page 104: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Specify Suitable Timeouts

for (;;) { long rv; dispatch_time_t timeout = DISPATCH_TIME_FOREVER;

rv = dispatch_semaphore_wait(my_sema, timeout); if (havePendingWork) { [self doPendingWork]; }}

Page 105: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Manage Repeating Timers

NSTimer *myTimer = [[NSTimer alloc] initWithFireDate:date interval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

/* ... */

/* myTimer not invalidated! */

Page 106: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Manage Repeating Timers

NSTimer *myTimer = [[NSTimer alloc] initWithFireDate:date interval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

/* ... */

[myTimer invalidate];

Page 107: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Specify Timer Tolerance

•Hint to the system of timer flexibility•Allows system to batch work

Page 108: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Specify Timer Tolerance

• For dispatch timers

dispatch_source_t my_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);dispatch_source_set_timer(my_timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, NSEC_PER_SEC / 10);

dispatch_source_set_event_handler(my_timer, ^{ [self timerFired];});dispatch_resume(my_timer);

Page 109: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Specify Timer Tolerance

• For CFRunLoopTimers

CFRunLoopTimerRef myTimer = CFRunLoopTimerCreate(kCFAllocatorDefault, fireDate, 1.0, 0, 0, &timerFired, NULL);CFRunLoopTimerSetTolerance(myTimer, 0.1);

CFRunLoopAddTimer(CFRunLoopGetCurrent(), myTimer, kCFRunLoopDefaultMode);

Page 110: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Specify Timer Tolerance

• For NSTimers

NSTimer *myTimer = [[NSTimer alloc] initWithFireDate:date interval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];[myTimer setTolerance:0.1];

[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];

Page 111: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

DemoPutting it all together

Page 112: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

• Be mindful of wakeup overhead•Monitor for wakeups in Activity Monitor•Debug with timerfires• Specify timer tolerance

Responsible Timer Use

Page 113: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Smart Background Work

Page 114: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Smart Background Work

• Be idle when you can, eliminating…■ extraneous CPU usage■ unnecessary timers

• Be smart when you can’t

Page 115: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Periodic Animations

• Stop driving UI the user isn’t watching•Apple apps work like this

■ Photo Booth■ FaceTime■ App Store

Page 116: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Frontmost App Notification

•App became frontmost/non-frontmost■ Implement -applicationDidResignActive: and -applicationDidBecomeActive: in your app delegate

■ Or checkif ([NSApp isActive]) ...

Page 117: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 118: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson
Page 119: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Occlusion Notifications

Page 120: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Occlusion Notifications

•App occluded■ Implement -applicationDidChangeOcclusionState: in your app delegate■ Or check

if ([NSApp occlusionState] & NSApplicationOcclusionStateVisible) ...

Page 121: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Occlusion Notifications

•App occluded■ Implement -applicationDidChangeOcclusionState: in your app delegate■ Or check

if ([NSApp occlusionState] & NSApplicationOcclusionStateVisible) ...

•Window occluded■ Implement -windowDidChangeOcclusionState: in your window delegate■ Or check

if ([window occlusionState] & NSWindowOcclusionStateVisible) ...

Page 122: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Occlusion Notifications

•App occluded■ Implement -applicationDidChangeOcclusionState: in your app delegate■ Or check

if ([NSApp occlusionState] & NSApplicationOcclusionStateVisible) ...

•Window occluded■ Implement -windowDidChangeOcclusionState: in your window delegate■ Or check

if ([window occlusionState] & NSWindowOcclusionStateVisible) ...

Improving Power Efficiency with App Nap Pacific HeightsWednesday 10:15AM

Page 123: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Batching Periodic Work

•Group maintenance operations together•Do them later with user-requested activity• Replaces timer-driven “cleanup” or “timeout” work

■ Causes wakeups■ Hurts your race to idle

Page 124: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Batching Periodic Work

- (void)cacheImage:(NSImage *)anImage { /* ... */

[self performSelector:@selector(removeImageIfStale:) withObject:anImage afterDelay:5.0];}

- (void)removeImageIfStale:(NSImage *)anImage { if ([self imageIsStale:anImage]) [self removeImage:anImage]; else [self performSelector:@selector(removeImageIfStale:) withObject:anImage afterDelay:5.0];}

Page 125: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Batching Periodic Work

- (void)cacheImage:(NSImage *)anImage { /* ... */

[imageArray addObject:anImage];}

- (void)handleUserEvent { dispatch_async(..., ^{ for (NSImage *image in imageArray) if ([self imageIsStale:anImage]) [self removeImage:anImage]; });

/* ... */}

Page 126: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Centralized Task Scheduling

• Periodic work that can wait for wall power■ Clean-up work■ Syncing■ Indexing■ Downloading new content

•New XPC API

Page 127: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Centralized Task Scheduling

xpc_object_t criteria = xpc_dictionary_create(NULL, NULL, 0);xpc_dictionary_set_bool(criteria, XPC_ACTIVITY_REPEATING, FALSE);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_DELAY, 3600);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_GRACE_PERIOD, 6 * 3600);xpc_dictionary_set_string(criteria, XPC_ACTIVITY_PRIORITY, XPC_ACTIVITY_PRIORITY_MAINTENANCE);

xpc_activity_register("com.myapp.MySimpleActivity", criteria, ^(xpc_activity_t activity) { /* do background work here */ });

Page 128: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Centralized Task Scheduling

xpc_object_t criteria = xpc_dictionary_create(NULL, NULL, 0);xpc_dictionary_set_bool(criteria, XPC_ACTIVITY_REPEATING, FALSE);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_DELAY, 3600);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_GRACE_PERIOD, 6 * 3600);xpc_dictionary_set_string(criteria, XPC_ACTIVITY_PRIORITY, XPC_ACTIVITY_PRIORITY_MAINTENANCE);

xpc_activity_register("com.myapp.MySimpleActivity", criteria, ^(xpc_activity_t activity) { /* do background work here */ });

Page 129: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Centralized Task Scheduling

xpc_object_t criteria = xpc_dictionary_create(NULL, NULL, 0);xpc_dictionary_set_bool(criteria, XPC_ACTIVITY_REPEATING, FALSE);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_DELAY, 3600);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_GRACE_PERIOD, 6 * 3600);xpc_dictionary_set_string(criteria, XPC_ACTIVITY_PRIORITY, XPC_ACTIVITY_PRIORITY_MAINTENANCE);

xpc_activity_register("com.myapp.MySimpleActivity", criteria, ^(xpc_activity_t activity) { /* do background work here */ });

Page 130: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Centralized Task Scheduling

xpc_object_t criteria = xpc_dictionary_create(NULL, NULL, 0);xpc_dictionary_set_bool(criteria, XPC_ACTIVITY_REPEATING, FALSE);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_DELAY, 3600);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_GRACE_PERIOD, 6 * 3600);xpc_dictionary_set_string(criteria, XPC_ACTIVITY_PRIORITY, XPC_ACTIVITY_PRIORITY_MAINTENANCE);

xpc_activity_register("com.myapp.MySimpleActivity", criteria, ^(xpc_activity_t activity) { /* do background work here */ });

Page 131: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Centralized Task Scheduling

xpc_object_t criteria = xpc_dictionary_create(NULL, NULL, 0);xpc_dictionary_set_bool(criteria, XPC_ACTIVITY_REPEATING, FALSE);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_DELAY, 3600);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_GRACE_PERIOD, 6 * 3600);xpc_dictionary_set_string(criteria, XPC_ACTIVITY_PRIORITY, XPC_ACTIVITY_PRIORITY_MAINTENANCE);

xpc_activity_register("com.myapp.MySimpleActivity", criteria, ^(xpc_activity_t activity) { /* do background work here */ });

Page 132: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Centralized Task Scheduling

xpc_object_t criteria = xpc_dictionary_create(NULL, NULL, 0);xpc_dictionary_set_bool(criteria, XPC_ACTIVITY_REPEATING, FALSE);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_DELAY, 3600);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_GRACE_PERIOD, 6 * 3600);xpc_dictionary_set_string(criteria, XPC_ACTIVITY_PRIORITY, XPC_ACTIVITY_PRIORITY_MAINTENANCE);

xpc_activity_register("com.myapp.MySimpleActivity", criteria, ^(xpc_activity_t activity) { /* do background work here */ });

Page 133: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Centralized Task Scheduling

xpc_object_t criteria = xpc_dictionary_create(NULL, NULL, 0);xpc_dictionary_set_bool(criteria, XPC_ACTIVITY_REPEATING, FALSE);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_DELAY, 3600);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_GRACE_PERIOD, 6 * 3600);xpc_dictionary_set_string(criteria, XPC_ACTIVITY_PRIORITY, XPC_ACTIVITY_PRIORITY_MAINTENANCE);

xpc_activity_register("com.myapp.MySimpleActivity", criteria, ^(xpc_activity_t activity) { /* do background work here */ });

Page 134: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Centralized Task Scheduling

xpc_object_t criteria = xpc_dictionary_create(NULL, NULL, 0);xpc_dictionary_set_bool(criteria, XPC_ACTIVITY_REPEATING, FALSE);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_DELAY, 3600);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_GRACE_PERIOD, 6 * 3600);xpc_dictionary_set_string(criteria, XPC_ACTIVITY_PRIORITY, XPC_ACTIVITY_PRIORITY_MAINTENANCE);

xpc_activity_register("com.myapp.MySimpleActivity", criteria, ^(xpc_activity_t activity) { /* do background work here */ });

Page 135: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Centralized Task Scheduling

Efficient Design with XPC Russian HillTuesday 2:00PM

xpc_object_t criteria = xpc_dictionary_create(NULL, NULL, 0);xpc_dictionary_set_bool(criteria, XPC_ACTIVITY_REPEATING, FALSE);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_DELAY, 3600);xpc_dictionary_set_int64(criteria, XPC_ACTIVITY_GRACE_PERIOD, 6 * 3600);xpc_dictionary_set_string(criteria, XPC_ACTIVITY_PRIORITY, XPC_ACTIVITY_PRIORITY_MAINTENANCE);

xpc_activity_register("com.myapp.MySimpleActivity", criteria, ^(xpc_activity_t activity) { /* do background work here */ });

Page 136: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Summary

Page 137: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Summary

•A little CPU costs a lot

Page 138: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Summary

•A little CPU costs a lot• So achieve absolute idle

Page 139: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Summary

•A little CPU costs a lot• So achieve absolute idle• Test early, test often

Page 140: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

More Information

Paul DanboldCore OS Technologies [email protected]

Dave DeLongApp Frameworks [email protected]

DocumentationInstruments User Guidehttp://developer.apple.com

Apple Developer Forumshttp://devforums.apple.com

Page 141: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Related Sessions

Improving Power Efficiency with App Nap Pacific HeightsWednesday 10:15AM

Maximizing Battery Life on OS X MissionTuesday 11:30AM

Efficient Design with XPC Russian HillTuesday 2:00PM

Building Efficient OS X Apps Nob HillTuesday 4:30PM

Power and Performance: Optimizing Your Website for Great Battery Life and Responsive Scrolling

Russian HillWednesday 9:00AM

Page 142: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson

Labs

Power and Performance for OS X Apps Core OS Lab AWednesday 10:15AM

Page 143: Energy Best Practices - Apple Inc.€¦ · These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 712 Energy Best Practices Matt Jacobson