53
APE: An Annota*on Language and Middleware for EnergyEfficient Mobile Applica*on 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

APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Embed Size (px)

Citation preview

Page 1: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

APE:  An  Annota*on  Language  and  Middleware  for  Energy-­‐Efficient  Mobile  Applica*on  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†  

Page 2: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Growth  of  Con*nuously-­‐Running  Mobile  Applica*ons

• Runs  in  background,    out  of  user’s  sight  • Periodic  workloads  • OIen  delay-­‐tolerant    •  e.g.,  health  monitoring,  sports  and  news  feeds,  social  networking  

2  

How  can  we  run  many  CRM  applica3ons  while  minimizing  impact  on  ba:ery  life?  

Page 3: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Low-­‐  and  Applica*on-­‐level  Op*miza*ons  are  Both  Needed

 •  Low-­‐level  Op2miza2ons  can  only  do  so  much  

•  DVFS,  Nckless  kernels,  radio  scheduling,  batched  writes  to  flash,  etc…  

• Applica2on-­‐level  Op2miza2ons  are  necessary  • Workload  shaping,  filtering,  piggy-­‐backing  radio  transmission,  etc…  •  Take  advantage  of  applica2on  specific  behavior  

3  

Page 4: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

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  Ci*Sense

4  

CO,  NO2,  O3  

Alerts,  Maps  

Page 5: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Small  Transmissions  à  Big  Impact

5  

t  

Power  

6  sec.  

12  sec.  

1  W  

10  mW  

Page 6: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

t  

Power  

Piggybacking  is  Effec*ve  for  Amor*zing  Cost  of  Communica*on

6  

1  W  

10  mW  

Page 7: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

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

7  

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();          }      }  }  

Page 8: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Building  Energy-­‐Efficient  Apps  is  Not  Simple!

• Code  for  power-­‐management  tends  to  be  complex  •  Highly  applicaNon  specific  

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

• OpNmizaNons  should  be  postponed  unNl  applicaNon  requirements  are  set  •  Use  cases  and  ‘acceptable’  delay  may  change  over  Nme  •  But…  Retrofiengà  Time  +  Bugs  

8  

Page 9: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

What  Do  Energy-­‐Management  Policies  Typically  Look  like?

• We  examined  the  latest  research,  best-­‐pracNce  guides,  documentaNon,  and  open-­‐source  projects  •  Android  Best  PracNce  Guide,  AT&T  Research,  [Wang09],  [Nath12],  [Nikzad12],  [Musolesi10],  Cyanogen  Project  

• Common  thread:  Timing  and  Device  State!  

•  “Wait  unNl  _______  before  execuNng  _______”  

9  

Page 10: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

APE  is  a  Declara*ve  Annota*on  Language  and  Run*me  for  Policies

10  

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();  

Page 11: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Rest  of  this  Talk

11  

cG  ≥  20  min  :  true  

start  

true  :  Net.Activity  

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

OR  

WiFi   AND  

OR  NetAct  

3G   4G  

Page 12: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

The  APE  Policy  Model EffecNvely  Modeling  Policies  Using  Timed  Automata  •  Clarify  semanNcs  

•  Drive  design  of  language  and  runNme  

12  

Page 13: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Many  Policies  Can  Be  Expressed  Using  Simple  Timed  Automata

13  

Wait  up  to  20  minutes  for  cellular  network  ac5vity  

cG  ≥  20  min  :  true  

start  

true  :  Net.Activity  

Page 14: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

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

More  Complicated  Policies  Can  Fall  Back  to  Earlier  States

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

14  

true  :  Accel.Move  

cAcc  ≥  30  sec  :  true  

true  :  GPS.Drive  start  

Page 15: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Many  Policies  Can  Be  Expressed  Using  Very  Simple  Timed  Automata

Wait  up  to  20  minutes  for  cellular  network  ac5vity  

15  

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

cG  ≥  20  min  :  true  

start  

true  :  Net.Activity  

Page 16: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

The  APE  Annota*on  Language RepresenNng  Power-­‐management  Policies  as  AnnotaNons  •  Expressing  simple  Nmed  automata  using  text  

16  

Page 17: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

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_WaitUn*l:  Wait  For  Desired  Device  State

17  

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

Wait  up  to  20  minutes  for  cellular  network  ac5vity  

Page 18: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

APE_WaitUn*l:  Wait  For  Desired  Device  State

18  

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  

Page 19: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Other  APE  Constructs   •  if-­‐else  constructs  

   

• CreaNng  new  terms  and  saving  policies  

19  

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)”)  

Page 20: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

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

20  

Page 21: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Annota*ons  are  Processed  at  Compile-­‐*me  into  Run*me  Requests

21  

APE  Annotated  Source  

Source  Including  Generated  

APE  Requests  

APE  AnnotaNon  Preprocessor  

Page 22: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Clients  Start  Up  the  APE  Service  and  Send  Requests  at  Run*me

22  

Client  Process  A  

Android  System

 Bind  me  to  the  APE  Service   APE  Service  

           Client  Process  B  

Bind  me  to  the  APE  Service  

WaitUnNl(1)  

A.1:  Net.AcNve  

 B.1:  …  

A.1:  Net.AcNve  

Register  “Net.AcNve”  as  1  

Page 23: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

APE  Service  

           

A.1:  Net.AcNve  

 B.1:  …  

APE  Returns  Control  to  the  Calling  Thread  Once  the  Request  is  Sa*sfied

23  

Android  System

 

Client  Process  A  

Client  Process  B  

Tell  me  about  changes  in  cellular  state  

Data  outgoing  on  cellular  radio  

Return  WaitUnNl(1)  

Page 24: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Boolean  Expressions  Are  Transformed  into  Expression  Trees

24  

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

WiFi   AND  

OR  NetAct  

3G   4G  

Page 25: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

APE  Service                              

The  APE  Service  Performs  Device  State  Monitoring  on  Behalf  of  Clients

25  

OR  

WiFi   AND  

OR  NetAct  

3G   4G  

Network  State  Monitor  

Android  System  

F  

F   F  

F   F  

F   F  

F   F   T   F  

T  

T  

T  

T  

T  

T  

Page 26: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Evalua*on A  Case  Study  of  OpNmizing  a  CRM  ApplicaNon  Using  APE  •  How  complicated  are  policies  in  pracNce?  

•  What  are  the  potenNal  benefits  and  overhead?  

26  

Page 27: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

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:  Ci*Sense

27  

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  annota2ons  versus  45  lines  of  Java  (thread  suspension/waking,  new  class  for  handling  callbacks,  registering  for  callbacks)  

Page 28: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

 Device  Power  ConsumpNon  

Background  Apps  Can  Repeatedly  Wake  Resources

28  

Six  applicaNon  instances  with  no  piggybacking  

Page 29: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

The  Simplest  of  Policies  Can  Have  a  Big  Impact  on  Power-­‐Consump*on

29  

Six  applicaNon  instances  with  @APE_WaitUntil(“Net.Active”,  MaxDelay=120)  

 Device  Power  ConsumpNon  

Page 30: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

0  

100  

200  

300  

400  

500  

600  

0   1   2   3   4   5  

Increase  in  Pow

er  Con

sump2

on  (m

W)  

Number  of  Addi2onal  Applica2ons  

APE  Apps   Non-­‐APE  Apps  

Increase  in  power  consumpNon  aIer  first  instance  

The  Simplest  of  Policies  Can  Have  a  Big  Impact  on  Power-­‐Consump*on

30  

Increase  of  13.49  mw  (1.6%)  

Increase  of  551.06  mw  (63.7%)  

Page 31: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Overhead  of  Execu*ng  Each  WaitUn*l  Call  vs  Hand-­‐coded

31  

0  

5  

10  

15  

20  

25  

1   3   7   15   31   63   127   191   255   383   511  

Time  to  Resolve  (m

s)  

State  Expression  Length  

Overhead  of  IPC  measured  to  be  1.71  ms  

Longest  Expression  from  CiNSense  

Page 32: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Limita*ons  and  Future  Work

• Assumes  developer  is  already  aware  of  opportuniNes  for  energy  savings  •  Under  Development:  AutomaNcally  idenNfying  costly  operaNons  and  suggesNng  safe,  effecNve  policies  

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

32  

Page 33: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Conclusion •  Use  of  2ming  is  widely  applicable  for  power-­‐management  in  mobile  applicaNons  

•  A  simple  annota2on  language  and  run2me  can  make  this  technique  conveniently  accessible  to  programmers  

•  Timed  automata  provide  a  seman2c  model  that  informs  the  design  of  language  and  runNme  

•  APE  significantly  eases  the  implementa2on  of  policies  in  CiNSense  and  achieves  drama2c  power-­‐savings  

33  

Page 34: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Acknowledgement •  This  work  was  supported  by  the  NaNonal  Science  FoundaNon  and  the  Roy  J.  Carver  Charitable  Trust  

34  

William  Griswold  UC  San  Diego  

Octav  Chipara  Univ.  of  Iowa  

Page 35: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Conclusion •  Use  of  2ming  is  widely  applicable  for  power-­‐management  in  mobile  applicaNons  

•  A  simple  annota2on  language  and  run2me  can  make  this  technique  conveniently  accessible  to  programmers  

•  Timed  automata  provide  a  seman2c  model  that  informs  the  design  of  language  and  runNme  

•  APE  significantly  eases  the  implementa2on  of  policies  in  CiNSense  and  achieves  drama2c  power-­‐savings  

35  

Page 36: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

DON’T  USE  -­‐  Anima*on  back  up

36  

t  

Power  

Page 37: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

DON’T  USE  -­‐  Anima*on  back  up

37  

t  

Power  

Page 38: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

38  

t  

Power  

Page 39: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Challenge:  Managing  Energy-­‐Consump*on  in  CRM  Apps

39  

•  Timing  plays  a  significant  role  in  energy-­‐consumpNon  

• Waking  the  CPU  •  Transmieng  data  •  Powering  the  display  

Limited  ba:ery!  

t  

Power  

Transmit  as  much  as  possible  at  this  Nme!  

Page 40: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

CO,  N

O2,  O3  

Alerts,  Maps  

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

Case  Study:  Ci*Sense

40  

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();  

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();  

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();          }      }  }  

Page 41: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

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();  

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();          }      }  }  

CO,  N

O2,  O3  

Alerts,  Maps  

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

Case  Study:  Ci*Sense

41  

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();  

Page 42: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

APE:  Annotated  Programming  for  Energy-­‐Efficiency

• A  small  and  declara2ve  annota2on  language  with  a  lightweight  middleware  run2me  for  Android  •  Demarcate  power-­‐hungry  code  using  annotaNons  •  ExecuNon  deferred  unNl  device  enters  state  that  minimizes  cost  of  operaNon  

•  Abstract  away  details  of  event/state  monitoring  

• Abstract  model  based  on  2med  automata  •  Guides  design  of  language  and  middleware  

42  

Page 43: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Outline •  IntroducNon  • Approaches  to  Energy-­‐Management  • APE:  Annotated  Programming  for  Energy-­‐efficiency  •  Policy  Model  •  AnnotaNon  Language  •  RunNme  Service  

•  EvaluaNon  •  Case  Study:  CiNSense  •  Overhead  and  Power  Savings  

• Conclusion  

43  

Page 44: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

The  APE  Policy  Model • Restricted  form  of  5med  automata:  

•  Σ  is  a  finite  set  of  events  •  𝑆  is  a  finite  set  of  states  •  𝑠0∈𝑆  is  the  start  state  •  𝑆𝐹⊆𝑆  is  a  set  of  accepNng  states  •  𝐶  is  a  finite  set  of  clocks,  and  •  𝐸  is  a  transiNon  funcNon  

44  

𝑇𝐴=(Σ,  𝑆,  𝑠0,  𝑆𝐹,  𝐶,  𝐸)  

Alur,  Rajeev,  and  David  L.  Dill.  "A  theory  of  Nmed  automata."  Theore5cal  computer  science  126.2  (1994):  183-­‐235.  

Page 45: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Typical  Automata  Can  Be  Expressed  Textually

•  𝜎i  is  a  Boolean  expression  consisNng  of  events  and  states  •  𝑡i  is  a  constraint  on  how  long  to  wait  for  𝜎i  to  be  true  •  𝑡𝑀𝑎𝑥𝐷𝑒𝑙𝑎𝑦  is  an  upper  bound  on  the  delay  of  O  

 •  Linear,  concise  nature  is  easy  to  express  in  wriNng  •  Drove  design  of  annotaNon  language  

45  

𝑃=({(𝜎1,𝑡1),  (𝜎2,𝑡2)…(𝜎𝑛,𝑡𝑛)},𝑡𝑀𝑎𝑥𝐷𝑒𝑙𝑎𝑦)  

Page 46: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

A  Policy  is  a  Sequence  of  Boolean  Expressions  and  Timing  Constraints • Defer  the  execuNon  of  an  operaNon  O  unNl  a  finite  sequence  of  states  holds:  

 •  𝜎i  is  a  Boolean  expression  consisNng  of  events  and  states  

•  𝑡i  is  a  constraint  on  how  long  to  wait  for  𝜎i  to  be  true  •  𝑡𝑀𝑎𝑥𝐷𝑒𝑙𝑎𝑦  is  an  upper  bound  on  the  delay  of  O  

•  If  𝜎𝑖  is  saNsfied  before  𝑡𝑖,  begin  monitoring  for  𝜎𝑖+1  •  Else,  begin  monitoring  for  𝜎𝑖-­‐1  again  •  Execute  O  once  𝜎𝑛  is  saNsfied  or  𝑡𝑀𝑎𝑥𝐷𝑒𝑙𝑎𝑦  has  passed  

46  

𝑃=({(𝜎1,𝑡1),  (𝜎2,𝑡2)…(𝜎𝑛,𝑡𝑛)},𝑡𝑀𝑎𝑥𝐷𝑒𝑙𝑎𝑦)  

Page 47: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Simplified  Model:  Limits  Power  for  Conciseness

• Wait  for  driving  to  be  detected  a@er  30  seconds  of  con5nuous  accelerometer  movement  

47  

Not  expressible  in  our  model!  

Page 48: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Allow  for  dynamic  selecNon  of  policies  at  runNme  

APE_If,  APE_ElseIf,  APE_Else:  Condi*onals  for  Policy  Selec*on

48  

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

Page 49: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

@APE_WaitUntil(“MyPolicy,      ({dataReady()},30)”,  MaxDelay=3600)  @APE_WaitUntil(“MyPolicy(inf,60),      ({dataReady()},30)”,  MaxDelay=3600)  

APE_DefineTerm,  APE_DefinePolicy:  Enabling  Reuse

49  

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

@APE_WaitUntil(“{requestPending()}  AND  MyTerm”,      MaxDelay=3600)  @APE_DefinePolicy(“MyPolicy”,  “(Display.Off,inf),(MyTerm,10)”)  

Page 50: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

while(true)  {      APEService.wait_until(1,  1800);      uploadSensorData();  }  

while(true)  {      @APE_WaitUntil(“WiFi.Connected”,  MaxDelay=1800)      uploadSensorData();  }  

void  onCreate(Bundle  savedInstanceState)  {      super(savedInstanceState);      …  }    

void  onCreate(Bundle  savedInstanceState)  {      super(savedInstanceState);      APEService.register_expr(1,  “WiFi.Connected”);      …  }  

Annota*ons  are  Processed  at  Compile-­‐*me  into  Run*me  Requests

50  

Page 51: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

APE  Run2me  Service  

The  APE  Service  Performs  Device  State  Monitoring  on  Behalf  of  Clients

51  

Client  App  A  

Android  APIs  

Client  App  B   Client  App  C  

Page 52: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Conclusion •  A  small  and  declara2ve  annota2on  language  with  a  lightweight  middleware  run2me  for  Android  •  Demarcate  power-­‐hungry  code  using  annotaNons  •  ExecuNon  deferred  unNl  device  enters  state  that  minimizes  cost  of  operaNon  

•  Abstract  away  details  of  event/state  monitoring  

• Manage  trade-­‐off  between  2ming  and  energy    

•  In  a  case  study,  a  policy  implemented  with  only  six  annota2ons  dras2cally  improved  efficiency  •  86%  reducNon  in  lines  of  power-­‐management  code  

52  

Page 53: APE:AnAnnota*onLanguageand% …ochipara/presentations/ICSE2014.pdf · NetAct" OR 3G 4G. TheAPEPolicyModel ... 1 3 7 15 31 63 127 191 255 383 511 % StateExpressionLength Overhead"of"IPC"measured"to"be"1.71"ms"

Two  Categories  of  Approaches  to  Energy-­‐Management

 •  Low-­‐level  Op2miza2ons:  DVFS,  Nckless  kernels,  radio  scheduling,  batched  writes  to  flash,  etc…  •  Implemented  in  kernel,  driver,  or  firmware  •  Responsibility  of  the  device  and  OS  vendors  

•  System-­‐level  Op2miza2ons:  workload  shaping,  filtering,  piggy-­‐backing  radio  transmission,  etc…  •  Implemented  at  applicaNon  level  •  Responsibility  of  the  applica2on  developers  

53