【Android】性能优化:电量消耗统计

电量的消耗和使用对于移动设备非常重要,一项调查问卷显示,电池的容量和寿命是手机最重要的营销点:所谓“the one thing that you can't do without”。

硬件

从硬件的角度看,Android电量的消耗主要来自屏幕,CPU,网络设备和各样的传感器:指纹,亮度,温湿度,陀螺仪,加速器等等。通常情况下,屏幕是耗电量最大的模式。一般来讲,Android设备的屏幕主要分为两种:Liquid-crystal display (LCD)Light-emitting diode (LED):

  • LCD有很多个液晶分子来负责每个像素的显示,然后由一个背光将所有的液晶分子同时照亮。每个像素的能耗是相同的,与呈现的颜色无关。

  • LED则是自发光:每个像素点由红、蓝、绿三种颜色光源的发光二极管共同控制和呈现。黑色不使用任何颜色,所以可以认为是无能耗,而白色需要所有的光源,耗能最多。所以有些手机在省电模式下(比如华为的超级省电模式),手机的背景都会设置成黑色。

LEDLCD的功耗比大约为1:10,LED更节能。同时LED在刷新率,鲜艳度和饱和度方面效果也更好,可以制造出比LCD更薄、更亮、更清晰的显示器。所以现在市面上的智能机大部分为LED屏。

除了屏幕之外,比较明显的耗电硬件是wifi或者数据网络连接设备,以及GPS定位。一般情况,使用wifi的能耗要小于使用移动网络的能耗。

另外还有CPU。一定情况下,CPU的耗电量可能是最大的,比如我们玩游戏的时候会明显感觉手机发烫,电量急速下掉。

耗电统计

可以使用adb dump来得到设备的能耗统计(batterystats)。首先重置手机状态:

adb shell dumpsys batterystats --reset
adb shell dumpsys batterystats --enable full-wake-history 

拔掉usb连接(避免充电),随意进行一定操作之后,采集能耗数据:

adb shell dumpsys batterystats > battery.txt

可以得到每个进程(Uid)的WakeLock以及所有app的能耗清单。

Battery History (1% used, 6456 used of 512KB, 115 strings using 8706):
                    0 (10) RESET:TIME: 2018-12-02-21-59-12
                    0 (2) 100 status=discharging health=good plug=none temp=286 volt=4388 charge=3574 +running +wake_lock +screen phone_signal_strength=great brightness=medium +usb_data wifi_signal_strength=4 wifi_suppl=completed proc=u0a99:"com.google.android.apps.turbo"
                    0 (2) 100 proc=u0a83:"com.android.printspooler"
                    0 (2) 100 proc=u0a20:"com.google.android.setupwizard"
                    0 (2) 100 proc=1000:"com.google.SSRestartDetector"
                    0 (2) 100 proc=u0a167:"com.tencent.mm"
                    0 (2) 100 proc=u0a171:"com.touchtype.swiftkey"
...

Google的Battery Historian可以帮助我们更直观的分析这些数据。battery-historian/scripts/有Battery Historian的第一版,用起来比较简单(需要有Python环境):

python historian.py -a battery.txt> battery.html

打开生成的battery.html文件可以看到具体的耗电情况。

一些简单的示意:

  • 横坐标:时间
  • battery_level:剩余电量
  • plugged:设备是否连接电源
  • screen:屏幕开启时长
  • top:当前屏幕显示的进程,比如上图中的进程com.tencent.mm就是微信
  • wake_lock*:wake_lock模块的工作时长
  • running:界面状态,主要为是否处于idle的状态。可以协助统计应用
  • wake_lock_in: 模块开始工作的时间以及时长
  • job:后台job
  • conn:网络连接方式,wifi或者2G,3G等
  • power_save:应该是省电模式开始时长吧,可能Android P之后才有的

historian.py是Battery Historian的第一版,而用Go重写的第二版更为强大,可以将耗电统计精确到每个进程。Battery Historian 2.0在9999端口挂在了一个分析和转换数据格式的服务器,比较简单的方法是通过Docker容器引擎,需要先安装Docker。运行:

docker run hello-world

看到如下输出就证明安装成功啦:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
78445dd45222: Pull complete
Digest:
sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
Status: Downloaded newer image for hello-world:latest
...

启动Battery Historain镜像:

docker -- run -p <port>:9999 gcr.io/android-battery-historian/:3.0 --port 9999

其中<port>为你的端口号,比如:

docker -- run -p 2222:9999 gcr.io/android-battery-historian/:3.0 --port 9999

如果遇到下面的错误:

Unable to find image 'gcr.io/android-battery-historian:3.0' locally
docker: Error response from daemon: manifest for gcr.io/android-battery-historian:3.0 not found.
See 'docker run --help'.

可以尝试sudo或者stable版:

docker -- run -p 2222:9999 gcr.io/android-battery-historian/stable:3.0 --port 9999

运行成功后在http://localhost/2222 就可以上传你的bugreport来分析了。生成bugreport:

adb bugreport > bugreport.zip

将bugreport.zip上传到http://localhost/2222,就可以看到详细的耗电统计报告:

Historain V2非常强大,可以看到每个App的耗电情况,比如微信:

资料

  • https://developer.android.com/studio/profile/battery-historian
  • https://github.com/google/battery-historian
  • https://docs.docker.com/install/
  • https://www.phonearena.com/news/What-is-the-most-important-smartphone-feature-to-you_id103392
  • https://en.wikipedia.org/wiki/Light-emitting_diode
  • https://en.wikipedia.org/wiki/Liquid-crystal_display
     

(转载请注明作者和出处:http://blog.csdn.net/xiaowei_cqu 未经允许请勿用于商业用途)

扫描或搜索公众号:小魏的修行路,在手机端查看文章

猜你喜欢

转载自blog.csdn.net/xiaowei_cqu/article/details/84727349
今日推荐