KEMBAR78
Power optimization for Android apps | PDF
PoweroptimizationforAndroidApps
Xavier Hallade, Developer Evangelist, Intel Corporation
@ph0b – ph0b.com – +XavierHallade
Power optimization for Android - Agenda
Introduction
Understanding power consumption
Improving your app’s behavior
Measuring power consumption
Q&A
Understandingpowerconsumption
Android devices
Complex systems running on battery
– Display, Input devices, Sensors, Connectivity, Camera, CPU, GPU, RAM, Storage,
A/V decoder, power subsystem
Battery life is one of the main complaints from users
Demand for more performance or less power consumption is driven by new
use cases
 No one could apply Moore’s law to battery capacity yet…
Power Management – by the OS
Specific applications lifecycle
 E.g. pausing applications not in foreground.
 Project Volta
 Doze Mode
 …
but applications can always easily ruin the battery life of Android devices.
What is using power ?
Network calls, data transfers
 Getting big chunks of data at random times, for data sync,
analytics, ad networks…
Location and sensors
 always-on, highest accuracy, maximum rate
Inefficient code
 useless work, bad data structures…
Display
45%
Network
30%
CPU
9%
Other
16%
ImprovingyourAPPSBehavior
1–NetworkUsage
Optimizing network calls
Allow for OS timer coalescing
Cache and prefetch
Use HTTP persistent connections (HTTP 1.1+)
Use compression
Avoid polling. Don’t forget to close TCP sockets.
Letting the OS batch network calls
AlarmManager.set()/setInexactRepeating() + a pool of network requests
SyncAdapter
android.app.job.JobScheduler/JobInfo.Builder (21+) or GCMNetworkManager
- setRequiresCharging, setRequiresIdle, setRequiredNetwork, setExecutionWindow…
- setBackoffCriteria…
use GCM push to trigger syncs only when actually needed
Prefetching and Caching
Prefetch data…
but minimize the size of your data transfers
 Adapt to the type of connection
Cache data:
 android.net.HttpResponseCache
 OkHttp
 …
Analyzing Network traffic from your application
Android Network Monitor
Charles Proxy
AT&T Application Resource Optimizer
AT&T Application Resource Optimizer
2–CPUandothers
Optimizing CPU usage
Race to idle
Batch work
Maximize data locality
use efficient algorithms
hardware acceleration
Profile first !
Analyzing CPU usage - Traceview
DDMS Method profiling or Debug.startMethodTracing("mytrace")
Intel® VTune Amplifier / Energy Profiler
• Gives exact information on CPU and
GPU states and hardware interruptions
• Part of Intel® System Studio
• Needs a kernel module
• Only possible on Intel development
devices and Intel based consumer
devices which support open source
builds
https://software.intel.com/en-us/intel-energy-profiler
Avoid useless work
 Disable receivers when not needed:
 Don’t forget to release Wakelocks
 Don’t trigger GPS fix, Bluetooth scan, etc “just in case”
ComponentName receiver = new ComponentName(context, myReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP)
Optimizing sensors and other data use
 Sensors: set sampling to SENSOR_DELAY_NORMAL (200ms) or SENSOR_DELAY_UI
(60ms).
 Prefer low power sensors when possible: accelerometer and no gyroscope
 GPS: use low accuracy (LocationManager.NETWORK_PROVIDER instead of
LocationManager.GPS_PROVIDER)
– Use high accuracy only once really needed.
Measuringpowerconsumption
With Battery Historian
Battery Historian 1.0
A simple python script generating a HTML report, no readme.
adb shell dumpsys batterystats --enable full-wake-history
adb shell dumpsys batterystats --reset
# use your device for some time
adb bugreport > bugreport.txt
./historian.py bugreport.txt > historian_report.html
Battery Historian 2.0
Dependency on: Go, Python, Git, Closure compiler, internet connection…
local webserver
Guide to install it on Windows:
http://ph0b.com/battery-historian-2-0-windows/
adb shell dumpsys batterystats --enable full-wake-history
adb shell dumpsys batterystats --reset
# use your device for some time
adb bugreport > bugreport.txt
Battery Historian 2.0
Battery Historian 2.0
Summary
Summary
Power optimization on typical Android devices requires understanding of
power consumption of all components
Batch, cache, avoid network requests and CPU work, optimize and schedule it
properly.
Measure and analyze your app using software tools:
 https://github.com/google/battery-historian
 http://intel.ly/system-studio
Additional Resources: Android Performance Patterns Videos
 http://bit.ly/1KDLqUu
Thankyou!
xavier.hallade@intel.com
@ph0b – ph0b.com – +XavierHallade

Power optimization for Android apps

  • 1.
    PoweroptimizationforAndroidApps Xavier Hallade, DeveloperEvangelist, Intel Corporation @ph0b – ph0b.com – +XavierHallade
  • 2.
    Power optimization forAndroid - Agenda Introduction Understanding power consumption Improving your app’s behavior Measuring power consumption Q&A
  • 3.
  • 4.
    Android devices Complex systemsrunning on battery – Display, Input devices, Sensors, Connectivity, Camera, CPU, GPU, RAM, Storage, A/V decoder, power subsystem Battery life is one of the main complaints from users Demand for more performance or less power consumption is driven by new use cases  No one could apply Moore’s law to battery capacity yet…
  • 5.
    Power Management –by the OS Specific applications lifecycle  E.g. pausing applications not in foreground.  Project Volta  Doze Mode  … but applications can always easily ruin the battery life of Android devices.
  • 6.
    What is usingpower ? Network calls, data transfers  Getting big chunks of data at random times, for data sync, analytics, ad networks… Location and sensors  always-on, highest accuracy, maximum rate Inefficient code  useless work, bad data structures… Display 45% Network 30% CPU 9% Other 16%
  • 7.
  • 8.
  • 9.
    Optimizing network calls Allowfor OS timer coalescing Cache and prefetch Use HTTP persistent connections (HTTP 1.1+) Use compression Avoid polling. Don’t forget to close TCP sockets.
  • 10.
    Letting the OSbatch network calls AlarmManager.set()/setInexactRepeating() + a pool of network requests SyncAdapter android.app.job.JobScheduler/JobInfo.Builder (21+) or GCMNetworkManager - setRequiresCharging, setRequiresIdle, setRequiredNetwork, setExecutionWindow… - setBackoffCriteria… use GCM push to trigger syncs only when actually needed
  • 11.
    Prefetching and Caching Prefetchdata… but minimize the size of your data transfers  Adapt to the type of connection Cache data:  android.net.HttpResponseCache  OkHttp  …
  • 12.
    Analyzing Network trafficfrom your application Android Network Monitor Charles Proxy AT&T Application Resource Optimizer
  • 13.
  • 14.
  • 15.
    Optimizing CPU usage Raceto idle Batch work Maximize data locality use efficient algorithms hardware acceleration Profile first !
  • 16.
    Analyzing CPU usage- Traceview DDMS Method profiling or Debug.startMethodTracing("mytrace")
  • 17.
    Intel® VTune Amplifier/ Energy Profiler • Gives exact information on CPU and GPU states and hardware interruptions • Part of Intel® System Studio • Needs a kernel module • Only possible on Intel development devices and Intel based consumer devices which support open source builds https://software.intel.com/en-us/intel-energy-profiler
  • 18.
    Avoid useless work Disable receivers when not needed:  Don’t forget to release Wakelocks  Don’t trigger GPS fix, Bluetooth scan, etc “just in case” ComponentName receiver = new ComponentName(context, myReceiver.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP)
  • 19.
    Optimizing sensors andother data use  Sensors: set sampling to SENSOR_DELAY_NORMAL (200ms) or SENSOR_DELAY_UI (60ms).  Prefer low power sensors when possible: accelerometer and no gyroscope  GPS: use low accuracy (LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER) – Use high accuracy only once really needed.
  • 20.
  • 21.
    Battery Historian 1.0 Asimple python script generating a HTML report, no readme. adb shell dumpsys batterystats --enable full-wake-history adb shell dumpsys batterystats --reset # use your device for some time adb bugreport > bugreport.txt ./historian.py bugreport.txt > historian_report.html
  • 22.
    Battery Historian 2.0 Dependencyon: Go, Python, Git, Closure compiler, internet connection… local webserver Guide to install it on Windows: http://ph0b.com/battery-historian-2-0-windows/ adb shell dumpsys batterystats --enable full-wake-history adb shell dumpsys batterystats --reset # use your device for some time adb bugreport > bugreport.txt
  • 23.
  • 24.
  • 25.
  • 26.
    Summary Power optimization ontypical Android devices requires understanding of power consumption of all components Batch, cache, avoid network requests and CPU work, optimize and schedule it properly. Measure and analyze your app using software tools:  https://github.com/google/battery-historian  http://intel.ly/system-studio Additional Resources: Android Performance Patterns Videos  http://bit.ly/1KDLqUu
  • 27.