概述
在Battery系列(二)中分析道,当实例化BatteryStatsImpl完毕后,给BSI对象设置了一个PowerProfile
对象,这个对象的作用是什么呢?
在了解PowerProfile之前,需要了解下电源配置文件。电源配置文件定义了组件的电流消耗值以及该组件在一段时间内大概消耗的电量。
在Google给出的文档中指出:在电源配置文件中,功耗表示额定电压下的电流消耗量,单位为mA (mA),也可用微安 (uA) 表示,该值应代表电池上消耗的电流。
PowerProfile负责解析电源配置文件,获取功耗值,并将获取的值供给BatteryStatsService等类计算各个项的耗电量,因此,最终的电池使用信息是由BatteryStatsImpl中的统计时长和电源配置文件中的值的运算而来。
电源配置文件路径:/frameworks\base\core\res\res\xml\power_profile.xml
Google规定,设备制造厂商必须提供自己的power_profile.xml文件,因此,一般在device/xxx/overlay/framework/base/core/res/res/xml
目录下定义该文件。
解析power_profile.xml
下面开始从PowerProfile类的构造方法开始分析对电源配置文件的读取,其构造方法如下:
static final HashMap<String, Object> sPowerMap = new HashMap<>();
public PowerProfile(Context context) {
// Read the XML file for the given profile (normally only one per
// device)
synchronized (sLock) {
//sPowerMap是一个static Map<String,Object>,保存解析内容({name,value})
if (sPowerMap.size() == 0) {
//解析power_profile.xml文件
readPowerValuesFromXml(context);
}
//初始化cpu集群
initCpuClusters();
}
}
在构造方法中,首先进行了解析xml文件,将解析后的值存储在一个Map中。在power_profile.xml中,存在两种类型的数据,分别是<item>
元素和<array>
元素,如:
<item name="cpu.idle">3.5</item>
<item name="cpu.awake">37</item>
<array name="cpu.speeds.cluster1">
<value>1650000<