在测量功耗的时候,通常是使用Power Monitor去拆分,或者对单一模块进行测量。
今天突然在网上发现一个神奇的工具BatteryHistorian,跟Systrace很像,能很好的展示记录的过程中系统中各模块的耗电状态。
Android电池功耗BatteryHistorian数据分析
操作步骤:
adb shell dumpsys batterystats --reset
adb shell dumpsys batterystats --enable full-wake-history
在问题场景复现
adb bugreport > bugreport.txt
adb shell dumpsys batterystats > batterystats.txt
bugreport.txt中记录了从系统中拉出来zip文件名称
/data/user_de/0/com.android.shell/files/bugreports/bugreport-x.zip: 1 file pulled, 0 skipped. 67.9 MB/s (9751279 bytes in 0.137s)
Bug report copied to C:\Users\lenovo\bugreport-x.zip
接下来就是要使用BatteryHistorian,这个网上有很多教程如何搭建,这边找到了一个公开的网站能更省事一点,如下
Upload Bugreport in BatteryHistorian
CPU runing: cpu运行的状态
Kernel only uptime: 只有kernell运行
Userspace wakelock: 用户空间申请的锁
Screen: 屏幕是否点亮
Top app: 当前在内存中的应用,按内存占用率排序
Activity Manager Proc: 活跃的用户进程
Crashes(logcat): 某个时间点出现crash的应用
Doze: 是否进入doze模式
Device active: 和Doze相反
JobScheduler: 异步作业调度
SyncManager: 同步操作
Temp White List: 电量优化白名单
Phone call: 是否打电话
GPS: 是否使用GPS
Network connectivity: 网络连接状态(wifi、mobile是否连接)
Mobile signal strength: 移动信号强度(great\good\moderate\poor)
Wifi scan: 是否在扫描wifi信号
Wifi supplicant: 是否有wifi请求
Wifi radio: 是否正在通过wifi传输数据
Wifi signal strength: wifi信号强度
Wifi running: wifi组件是否在工作(未传输数据)
Wifi on: 同上
Audio: 音频子系统?
Camera: 相机是否在工作
Video:是否在播放视频
Foreground process: 前台进程
Package install: 是否在进行包安装
Package active: 包管理在工作
Battery level: 电池当前电量
Temperature: 电池温度
Plugged: 连接usb或者充电
Charging on: 在充电
Logcat misc: 是否在导出日志
batterystats.txt 中记录的东西会比BatteryHistorian更加详细,列举部分,里面信息还是很全的,充电时的状态,不充电时的状态等等
uptime: 未待机运行时间,cpu工作时间
realtime: 系统运行时间包含休眠时间
doze模式:延缓推迟后台的动作,off light deep(full)
power-save-off:不在省点状态
device-idle-on:在空闲状态
cell standby:射频待机的耗电量
RESET:Time:执行adb shell dumpsys batterystats --reset的开始时间
Per-PID Stats: 每个进程唤醒工作的时间
Discharge step durations: 每掉一隔电的时间点和设备的状态
Daily stats: 以天为单位展示每掉一隔电的时间点和设备状态
Statistics since last charge: 从上次充电以来的统计详情,包含很多子板块
Battery History: 耗电统计的历史记录,每一条记录以HistoryItem的形式存在
Cellular Statistics:移动数据网络状态和使用情况
Wifi Statistics: WIFI的网络状态和使用情况
Bluetooth: 蓝牙在不同工作状态下的使用情况
Estimated power use (mAh): 近似计算出的各个用户(uid)的耗电量
All kernel wake locks: 内核锁的使用统计
All partial wake locks: 应用锁的使用统计
All wakeup reasons: 所有的唤醒原因
Statistics by uid: 每一个uid的耗电细节
Battery History (7% used, 323KB used of 4096KB, 560 strings using 65KB):
Per-PID Stats:
PID 1512 wake time: +3m28s201ms
Discharge step durations:
#0: +1h29m59s999ms to 61 (screen-off, power-save-off, device-idle-on)
Daily stats:
Current start time: 2022-06-15-03-43-28
Next min deadline: 2022-06-16-01-00-00
Next max deadline: 2022-06-16-03-00-00
Current daily discharge step durations:
Daily from 2022-06-14-03-52-30 to 2022-06-15-03-43-28:
Discharge step durations:
#0: +1h46m27s123ms to 69 (screen-off, power-save-off, device-idle-on)
#1: +1h43m48s84ms to 70 (screen-off, power-save-off, device-idle-on)
Discharge total time: 5d 23h 11m 29s 600ms (from 14 steps)
Discharge screen off time: 6d 10h 12m 8s 400ms (from 12 steps)
Discharge screen off device idle time: 7d 0h 24m 49s 300ms (from 9 steps)
Charge step durations:
#0: +51m12s103ms to 77 (screen-on, power-save-off, device-idle-off)
#1: +1m29s651ms to 78 (screen-off, power-save-off, device-idle-off)
Charge total time: 2d 13h 46m 10s 200ms (from 6 steps)
Charge screen off time: 2h 29m 25s 100ms (from 1 steps)
Charge screen on time: 3d 13h 20m 10s 300ms (from 1 steps)
Package changes:
Update com.android.chrome vers=500509833
Update com.android.chrome vers=500509833
Update com.google.android.gms vers=221820039
Update com.google.android.gms vers=221820039
Statistics since last charge:
System starts: 0, currently on battery: false
Estimated battery capacity: 4000 mAh
Min learned battery capacity: 3288 mAh
Max learned battery capacity: 3288 mAh
Time on battery: 23h 55m 11s 793ms (99.8%) realtime, 1h 8m 46s 578ms (4.8%) uptime
Wifi Statistics:
Wifi kernel active time: 4h 55m 40s 779ms (20.6%)
WiFi Scan time: 20m 20s 606ms (1.4%)
WiFi Sleep time: 23h 8m 54s 844ms (96.8%)
WiFi Idle time: 44m 18s 427ms (3.1%)
WiFi Rx time: 1m 25s 214ms (0.1%)
WiFi Tx time: 33s 308ms (0.0%)
Wifi data received: 43.52MB
Wifi data sent: 3.34MB
Wifi packets received: 52277
Wifi packets sent: 28911
Wifi states:
sta 23h 55m 11s 793ms (100.0%)
All kernel wake locks:
Kernel Wake lock PowerManagerService.WakeLocks: 8m 6s 360ms (268 times) realtime
All partial wake locks:
Wake lock u0a225 *job*/com.android.vending/com.google.android.finsky.scheduler.process.backgroundimpl...
电量统计日志
BatteryHistorian中的平均电流计算来源于电池容量的估计值,在batterystats.txt中可以得知Estimated battery capacity: 4000 mAh
frameworks/base/core/java/com/android/internal/os/BatteryStatsImpl.java
mEstimatedBatteryCapacityMah = (int) mPowerProfile.getBatteryCapacity();
framework/base/core/java/com/android/internal/os/PowerProfile.java
readPowerValuesFromXml
framework/base/core/res/res/xml/power_profile.xml
<item name="battery.capacity">4000</item>
最后想说的是GMS版本连接wifi后台google service一直有在运行,造成wifi功耗高,其实不是wifi本身的问题。