APE: An Annotation Language and Middleware for Energy- Efficient Mobile Application Development Nima...

35
APE: An Annotation Language and Middleware for Energy-Efficient Mobile Application Development Nima Nikzad, Octav Chipara , and William G. Griswold [email protected] Dept. of Computer Science and Engineering, UC San Diego Dept. of Computer Science, University of Iowa

Transcript of APE: An Annotation Language and Middleware for Energy- Efficient Mobile Application Development Nima...

APE: An Annotation Language and Middleware for Energy-Efficient Mobile Application Development

Nima Nikzad, Octav Chipara†, and William G. Griswold

[email protected]. of Computer Science and Engineering, UC San Diego

Dept. of Computer Science, University of Iowa†

2

Growth of Continuously-Running Mobile Applications

• Runs in background, out of user’s sight• Periodic workloads• Often delay-tolerant

• e.g., health monitoring, sports and news feeds, social networking

How can we run many CRM applicationswhile minimizing impact on battery life?

3

Low- and Application-level Optimizations are Both Needed

• Low-level Optimizations can only do so much• DVFS, tickless kernels, radio scheduling, batched

writes to flash, etc…

• Application-level Optimizations are necessary• Workload shaping, filtering, piggy-backing radio

transmission, etc…• Take advantage of application specific behavior

4

Thread uploadThread = new Thread(new Runnable() { while(true) { try { Thread.sleep(1200000); // Sleep 20 min. } catch(InterruptedException e) { attemptUpload(); }});uploadThread.start();

Case Study: Naïve Upload in CitiSense

CO, NO2, O3

Alerts, Maps

5

Small Transmissions Big Impact

t

Pow

er

6 sec.

12 sec.

1 W

10 mW

6

t

Pow

er

Piggybacking is Effective for Amortizing Cost of Communication

1 W

10 mW

7

Thread uploadThread = new Thread(new Runnable() { while(true) { Intent batt = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int lvl = batt.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scl = batt.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = lvl / (float) scl; try { if(batteryPct > 70) { Thread.sleep(1200000); } else { Thread.sleep(3600000); } } catch(InterruptedException e) { attemptUpload(); }});uploadThread.start();

TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);TransListener transListener = new TransListener();teleManager.listen(transListener, PhoneStateListener.LISTEN_DATA_ACTIVITY);

private class TransListener extends PhoneStateListener { public void onDataActivity(int act) { if(act == TelephonyManager.DATA_ACTIVITY_IN || act == TelephonyManager.DATA_ACTIVITY_OUT || act == TelephonyManager.DATA_ACTIVITY_INOUT) { uploadThread.interrupt(); } }}

Code is Quickly Complicated by Power-Management Policy

Thread uploadThread = new Thread(new Runnable() { while(true) { try { Thread.sleep(1200000); } catch(InterruptedException e) { attemptUpload(); }});uploadThread.start();

Thread uploadThread = new Thread(new Runnable() { while(true) { try { Thread.sleep(1200000); } catch(InterruptedException e) { attemptUpload(); }});uploadThread.start();

TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);TransListener transListener = new TransListener();teleManager.listen(transListener, PhoneStateListener.LISTEN_DATA_ACTIVITY);

private class TransListener extends PhoneStateListener { public void onDataActivity(int act) { if(act == TelephonyManager.DATA_ACTIVITY_IN || act == TelephonyManager.DATA_ACTIVITY_OUT || act == TelephonyManager.DATA_ACTIVITY_INOUT) { uploadThread.interrupt(); } }}

8

Building Energy-Efficient Appsis Not Simple!

• Code for power-management tends to be complex• Highly application specific

• Trade-off: Energy vs Timeliness and/or Accuracy• Thread management, event and state change handling

• Optimizations should be postponed until application requirements are set• Use cases and ‘acceptable’ delay may change over time• But… Retrofitting Time + Bugs

9

What Do Energy-Management Policies Typically Look like?

• We examined the latest research, best-practice guides, documentation, and open-source projects• Android Best Practice Guide, AT&T Research, [Wang09],

[Nath12], [Nikzad12], [Musolesi10], Cyanogen Project

• Common thread: Timing and Device State!

• “Wait until _______ before executing _______”

10

APE is a Declarative Annotation Language and Runtime for Policies

Thread uploadThread = new Thread(new Runnable() { while(true) { Intent batt = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int lvl = batt.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scl = batt.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = lvl / (float) scl; try { if(batteryPct > 70) { Thread.sleep(120000); } else { Thread.sleep(360000); } } catch(InterruptedException e) { attemptUpload(); }});uploadThread.start();

TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);TransListener transListener = new TransListener();teleManager.listen(transListener, PhoneStateListener.LISTEN_DATA_ACTIVITY);

private class TransListener extends PhoneStateListener { public void onDataActivity(int act) { if(act == TelephonyManager.DATA_ACTIVITY_IN || act == TelephonyManager.DATA_ACTIVITY_OUT || act == TelephonyManager.DATA_ACTIVITY_INOUT) { uploadThread.interrupt(); } }}

Thread uploadThread = new Thread(new Runnable() { while(true) { @APE_If(“Battery.Level > 70%”) @APE_WaitUntil(“Network.Active”, MaxDelay=1200) @APE_Else() @APE_WaitUntil(“Network.Active”, MaxDelay=3600) attemptUpload(); }});uploadThread.start();

11

Rest of this Talk

cG ≥ 20 min : truestart

true : Net.Activity

while(true) { @APE_WaitUntil(“Net.Active”, MaxDelay=1200) uploadSensorData();}

OR

WiFi AND

ORNetAct

3G 4G

12

The APE Policy ModelEffectively Modeling Policies Using Timed Automata• Clarify semantics

• Drive design of language and runtime

13

Many Policies Can Be Expressed Using Simple Timed Automata

Wait up to 20 minutes for cellular network activity

cG ≥ 20 min : truestart

true : Net.Activity

14

P = ({(Accel.Move, ∞), (GPS.Drive, 30 sec)}, ∞)

More Complicated Policies CanFall Back to Earlier States

Wait for movement using the accelerometer, then wait up to thirty seconds for driving to be detected

true : Accel.Move

cAcc ≥ 30 sec : truetrue : GPS.Drivestart

15

Many Policies Can Be Expressed Using Very Simple Timed Automata

Wait up to 20 minutes for cellular network activity

P = ({(Net.Active, ∞)}, 20 min)

cG ≥ 20 min : truestart

true : Net.Activity

16

The APE Annotation LanguageRepresenting Power-management Policies as Annotations• Expressing simple timed automata using text

17

while(true) { @APE_WaitUntil(“Net.Active”, MaxDelay=1200 PreWait=“log(‘Started waiting for activity…’)”, PostWait=“log(‘Activity detected!’)”) uploadSensorData();}

while(true) { @APE_WaitUntil(“Net.Active”, MaxDelay=1200) uploadSensorData();}

APE_WaitUntil:Wait For Desired Device State

P = ({(Net.Active, ∞)}, 20 min)

Wait up to 20 minutes for cellular network activity

18

APE_WaitUntil:Wait For Desired Device State

P = ({(Accel.Move, ∞), (GPS.Drive, 30 sec)}, ∞)

while(true) { @APE_WaitUntil(“({accMove()},inf), ({gpsDrive()},30)”, MaxDelay=inf) downloadDriveData();}

Wait for movement using the accelerometer, then wait up to thirty seconds for driving to be detected

19

Other APE Constructs

• if-else constructs

• Creating new terms and saving policies

while(true) { @APE_If(“Battery.Level > 70%”) @APE_WaitUntil(“Net.Active”, MaxDelay=1800) @APE_Else() @APE_WaitUntil(“WiFi.Connected”, MaxDelay=3600) uploadSensorData();}

@APE_DefineTerm(“MyTerm”, “Battery.Charging AND (WiFi.Connected OR Cell.4G)”)

@APE_DefinePolicy(“MyPolicy”, “(Display.Off,inf),(MyTerm,10)”)

20

The APE Middleware ServiceRuntime Hardware Monitoring on Behalf of Client Apps• Minimizing overhead associated with monitoring

21

Annotations are Processed at Compile-time into Runtime Requests

APEAnnotated

Source

SourceIncluding

GeneratedAPE

Requests

APE AnnotationPreprocessor

22

Clients Start Up the APE Service and Send Requests at Runtime

Client Process A

Andr

oid

Syst

em

Bind me to the APE Service APE Service

Client Process BBind me to the APE Service

WaitUntil(1)

A.1:Net.Active

B.1:…

A.1:Net.Active

Register “Net.Active” as 1

23

APE Service

A.1:Net.Active

B.1:…

APE Returns Control to the Calling Thread Once the Request is Satisfied

Andr

oid

Syst

em

Client Process A

Client Process B

Tell me aboutchanges in

cellular state

Data outgoingon cellular radio

Return WaitUntil(1)

24

Boolean Expressions Are Transformed into Expression Trees

WiFi.Connected OR Network.Active AND (Cell.3G OR Cell.4G)OR

WiFi AND

ORNetAct

3G 4G

25

APE Service

The APE Service Performs Device State Monitoring on Behalf of Clients

OR

WiFi AND

ORNetAct

3G 4G

Network State Monitor

Android System

F

F F

F F

F F

F F T F

T

T

T

T

T

T

26

EvaluationA Case Study of Optimizing a CRM Application Using APE• How complicated are policies in practice?

• What are the potential benefits and overhead?

27

Thread uploadThread = new Thread(new Runnable() { while(true) { Intent batt = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int lvl = batt.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scl = batt.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = lvl / (float) scl; try { if(batteryPct > 70) { Thread.sleep(1200000); } else { Thread.sleep(3600000); } } catch(InterruptedException e) { attemptUpload(); }});uploadThread.start();

TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);TransListener transListener = new TransListener();teleManager.listen(transListener, PhoneStateListener.LISTEN_DATA_ACTIVITY);

private class TransListener extends PhoneStateListener { public void onDataActivity(int act) { if(act == TelephonyManager.DATA_ACTIVITY_IN || act == TelephonyManager.DATA_ACTIVITY_OUT || act == TelephonyManager.DATA_ACTIVITY_INOUT) { uploadThread.interrupt(); } }}

Thread uploadThread = new Thread(new Runnable() { while(true) { @APE_If(“Battery.Level > 70%”) @APE_WaitUntil(“Network.Active”, MaxDelay=1200) @APE_Else() @APE_WaitUntil(“Network.Active”, MaxDelay=3600) attemptUpload(); }});uploadThread.start();

Case Study: CitiSense

Thread uploadThread = new Thread(new Runnable() { while(true) { @APE_If(“Battery.Level > 70%”) @APE_WaitUntil(“WiFi.Connected OR Network.Active AND (Cell.3G OR Cell.4G)”, MaxDelay=1200) @APE_Else() @APE_WaitUntil(“WiFi.Connected OR Network.Active AND (Cell.3G OR Cell.4G)”, MaxDelay=3600) attemptUpload(); }};uploadThread.start();

Four APE annotations versus 45 lines of Java(thread suspension/waking, new class for handling callbacks, registering for callbacks)

28

Device Power Consumption

Background Apps Can Repeatedly Wake Resources

Six application instanceswith no piggybacking

29

The Simplest of Policies Can Have a Big Impact on Power-Consumption

Six application instanceswith @APE_WaitUntil(“Net.Active”, MaxDelay=120)

Device Power Consumption

30

0 1 2 3 4 50

100

200

300

400

500

600

APE Apps Non-APE Apps

Number of Additional Applications

Incr

ease

in P

ower

Con

sum

ption

(mW

)

Increase in power consumption after first instance

The Simplest of Policies Can Have a Big Impact on Power-Consumption

Increase of 13.49 mw (1.6%)

Increase of 551.06 mw (63.7%)

31

Overhead of Executing Each WaitUntil Call vs Hand-coded

1 3 7 15 31 63 127 191 255 383 5110

5

10

15

20

25

State Expression Length

Tim

e to

Res

olve

(ms)

Overhead of IPC measured to be 1.71 ms

Longest Expression from CitiSense

32

Limitations and Future Work

• Assumes developer is already aware of opportunities for energy savings• Under Development: Automatically identifying costly

operations and suggesting safe, effective policies

• User study to properly evaluate APE’s ease-of-use and sufficient expressibility of language

33

Conclusion• Use of timing is widely applicable for power-

management in mobile applications

• A simple annotation language and runtime can make this technique conveniently accessible to programmers

• Timed automata provide a semantic model that informs the design of language and runtime

• APE significantly eases the implementation of policies in CitiSense and achieves dramatic power-savings

34

Acknowledgement• This work was supported by the National Science

Foundation and the Roy J. Carver Charitable Trust

William GriswoldUC San Diego

Octav ChiparaUniv. of Iowa

35

Conclusion• Use of timing is widely applicable for power-

management in mobile applications

• A simple annotation language and runtime can make this technique conveniently accessible to programmers

• Timed automata provide a semantic model that informs the design of language and runtime

• APE significantly eases the implementation of policies in CitiSense and achieves dramatic power-savings