自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
电量统计信息 HAL
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
设备子系统的功耗通常是在实验室环境中针对各种稳态条件进行测量和记录的,例如当屏幕开启时,或设备处于闲置电源状态时。这对于具有恒定功耗的子系统,或在实验室环境中容易测量的条件下是可行的,但对于某些用例(如当屏幕显示视频时)就不适用。
IPower.hal 1.0
提供了一个接口,用于传递电量提示并报告子系统休眠状态指标的累积数据。在 Android 10 及更高版本中,累积统计信息报告函数位于 IPowerStats.hal
电源统计信息收集 API 中,并提供了检索设备耗电量数据的一种方式。这取代了 IPower.hal
接口的累积统计信息收集部分,实现了更明确的功能分离。
IPowerStats
服务的判读不是定期进行。它们发生在关键时刻,例如当电池电量下降 1% 时。当耗电量较低时,判读频率较低;当耗电量较高时,判读频率较高。数据可能会发回服务器,也可能会在 bug 报告中用到以进行分析和分类。这有助于持续降低功耗并延长电池续航时间。
IPower.hal 和 IPowerStats.hal
IPower.hal
和
IPowerStats.hal
接口在 Android 10 中均可用,但 IPower.hal
统计信息收集功能仅在 IPowerStats.hal
接口中可用。IPowerStats.hal
功能中包含的 API 可用于获取和使用在受支持的设备上通过功耗测量而收集到的数据:
以下客户端会使用 IPowerStats.hal
API:
Statsd
,目的是收集每条轨道的功耗指标。
Perfetto
,目的是将功耗与 CPU 活动相关联。
Batterystats
,目的是使用测得的数据优化电耗归因,而不是通过 power_profile.xml.
中的预定义常量来估计耗电量。
对于 Android 10 及更高版本,设备制造商可以在 IPower.hal
和 IPowerStats.hal
函数之间进行选择,但如果未实现 IPowerStats.hal
,所有客户端必须回退到 IPower.hal
。
IPowerStats.hal 实现选项
在 Android 7 到 Android 9 上,只有 IPower.hal
函数可用。升级到 Android 10 的设备必须有一个硬件功耗监控子系统,或者可以通过其他方式来监控和记录功耗统计信息。有些 SoC 会为您收集耗电量统计数据,或者您可以通过软件获取电源实体状态驻留信息。只有在支持 getRailInfo()
、getEnergyData()
和 streamEnergyData()
时才需要使用功耗监控硬件。
如果您在没有功耗监控硬件的情况下实现 IPowerStats.hal
,getRailInfo(), getEnergyData()
和 streamEnergyData()
会返回 NOT_SUPPORTED
。同样地,在这种情况下,getPowerEntityInfo(), getPowerEntityStateInfo()
和 getPowerEntityStateResidencyData()
也可能会返回 NOT_SUPPORTED
。
轨道监控 API 返回的数据示例包括:
- 屏幕的电源轨道使用了 X 微瓦 (μW)。
- 调制解调器的电源轨道使用了 Y 微瓦 (μW)。
子系统休眠状态 API 返回的数据示例包括:
- 调制解调器休眠了 X 毫秒 (ms)。
- SoC 处于掉电模式达 Y 毫秒 (ms)。
- GPU 处于挂起状态达 Z 毫秒 (ms)。
使用硬件电源监控子系统
如果您的设备设计有一个硬件功耗监控子系统,可通过创建单个 sysfs 节点(PowerStats.hal
可以从该节点解析数据)来实现 IPowerStats.hal
,或者通过进行一系列 ioctl 类型的系统调用来实现。
您必须以不会造成累加器溢出的方式实现内核驱动程序。所使用的算法取决于您独特的硬件功耗监控子系统设计,它必须同时提供瞬时和平均总线电压以及电流测量值。内核驱动程序必须以不清空功耗累加器的方式采集这些数据,并且必须以 64 位变量的形式来维护自启动以来每个子轨的累积功耗数据;该变量会随着每次累加器查询的能耗读数而递增。
某个给定组件(或者多个组件)的统计信息必须在单个节点中。虽然这并不是 sysfs 的常规用法(通常将每个节点限制为单个值),但可确保所有数据的一致性。
设计方案指导意见
- 从 sysfs 节点判读数据或进行系统调用时,保持较低的延迟(不超过 1msec)。
- 确保支持统计信息功能不会明显增加耗电量:
- 请勿通过增加接入点 (AP) 和/或子系统的唤醒次数来跟踪处于休眠模式的时间等参数。
- 如有可能,随同其他流量在应用处理器和固件之间适时地传输统计信息。
- 如有必要,子系统可以使用以下驱动程序函数:
- 在内部缓存数据,以略微降低数据时效性为代价来避免出现延迟/唤醒。
- 在子系统处于休眠状态时执行推断,以便在不唤醒子系统的情况下提供最新的休眠时间信息。
选择组件、子系统和统计信息
在选择要从哪些组件或子系统收集 IPowerStats.hal
数据时,请选择设备上会使用大量电流(5mA 或更多)或支持多种功耗模式的组件或子系统,示例如下:
- 各个 SoC 子系统。
- 部分或完全位于 SoC 之外的子系统,如 Wi-Fi、图像处理器或安全处理器。
- 外围设备,如大功率 LED 和相机。
- 使用不同模式的电源域,如整个 SoC 的电源域。
自定义
这个可选功能可进行自定义。您可以设计用例并自定义您的使用方式:
- 决定要测量哪些轨道,以及测量频率。
- 决定何时判读数据以及如何解读这些数据。
- 决定根据您的数据要执行的操作以及执行时间。
验证
VTS 测试可确保设备符合 Android 要求。IPowerStats.hal
中的注释用于验证设备是否合规。
例如,如果您调用 getRailInfo()
但它没有返回任何内容,则表示 VTS 测试失败了,原因在于您没有收到关于所监控轨道的信息,也没有收到 SUCCESS
这一返回状态。同样,如果您收到轨道信息,但随它一起返回的还有 NON_SUPPORTED
或 FILE_SYSTEM_ERROR
响应,那也表示测试失败了。VTS 会按照 IPower.hal 和 IPowerStats.hal 注释中的要求,验证是否符合 HAL 文件中的设备制造商规范。VTS 测试中使用的注释示例如下:
/**
* Rail information:
* Reports information related to the rails being monitored.
*
* @return rails Information about monitored rails.
* @return status SUCCESS on success or NOT_SUPPORTED if
* feature is not enabled or FILESYSTEM_ERROR on filesystem nodes
* access error.
*/
getRailInfo()
generates(vec<e;RailInfo>e; rails, Status status);
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Power stats HAL\n\nDevice subsystem power is often measured and recorded in a lab environment\nfor various steady-state conditions, such as when the screen is on, or the\ndevice is in an idle power state. This works for subsystems with a constant\npower draw, or under conditions that are easily measured in lab environments,\nbut not for certain use cases, such as when a screen displays a video.\n\n`IPower.hal 1.0` provides an interface for passing [power hints](https://android.googlesource.com/platform/hardware/interfaces/+/fd877b5479b1639b4ba57af5c1f78a7a463e6be1/power/1.0/IPower.hal#52) and reporting cumulative data on subsystem sleep-state metrics.\nIn Android 10 and higher, the cumulative statistics-reporting function\nresides in the `IPowerStats.hal` power-stat collection APIs, and\nprovides a way to retrieve on-device energy-usage data. This replaces the\ncumulative stats-gathering portion of the `IPower.hal` interface,\nfor a clearer separation of functionality.\n\nThe `IPowerStats` service readings aren't periodic. They occur at\nkey moments, such as when there's a 1% battery drop. Readings are less frequent\nwhen the battery drain is low, and more frequent when it's high. Data may be\nsent back to servers, and may be used in bug reports for analysis and triage.\nThis supports ongoing efforts to decrease power consumption and increase\nbattery life.\n\nIPower.hal and IPowerStats.hal\n------------------------------\n\nBoth the `IPower.hal` and [IPowerStats.hal](https://android.googlesource.com/platform/hardware/interfaces/+/fd877b5479b1639b4ba57af5c1f78a7a463e6be1/power/stats/1.0/IPowerStats.hal) interfaces are available on Android 10, but the\n`IPower.hal` **stats** collection functionality is only\navailable from the `IPowerStats.hal` interface. The\n`IPowerStats.hal` functionality includes APIs to acquire and use\ndata collected from on-device power measurements for supported devices:\n\n- Performs rail-level energy measurements for both low frequency ([getRailInfo](https://android.googlesource.com/platform/hardware/interfaces/+/fd877b5479b1639b4ba57af5c1f78a7a463e6be1/power/stats/1.0/IPowerStats.hal#29)) and high-frequency ([streamEnergyData](https://android.googlesource.com/platform/hardware/interfaces/+/fd877b5479b1639b4ba57af5c1f78a7a463e6be1/power/stats/1.0/IPowerStats.hal#47)) clients, and reports accumulated energy since boot.\n- Reports information related to each supported [PowerEntity](https://android.googlesource.com/platform/hardware/interfaces/+/fd877b5479b1639b4ba57af5c1f78a7a463e6be1/power/stats/1.0/IPowerStats.hal#101) for which data is available. A `PowerEntity` is a platform subsystem, peripheral, or power domain that impacts total device-power consumption.\n- Reports the set of power entity states ([getPowerEntityStateInfo](https://android.googlesource.com/platform/hardware/interfaces/+/fd877b5479b1639b4ba57af5c1f78a7a463e6be1/power/stats/1.0/IPowerStats.hal#116)) for which the specified entities provide residency data, then reports the accumulated data for each specified `PowerEntity`.\n\nThe `IPowerStats.hal` APIs are used by the following clients:\n\n- `Statsd`, to collect per-rail power consumption metrics.\n- `Perfetto`, to correlate power consumption with CPU activity.\n- `Batterystats`, to improve battery attribution by using measured data rather than estimating the battery consumption from predefined constants in `power_profile.xml.`\n\nWith Android 10 and higher, a device manufacturer may choose between\nthe `IPower.hal` and the `IPowerStats.hal` functions, but\nall clients must fall back to `IPower.hal` if\n`IPowerStats.hal` isn't implemented .\n\nIPowerStats.hal implementation options\n--------------------------------------\n\nOnly the `IPower.hal` functions are available on Android 7\nthrough Android 9. Devices that have been upgraded to Android 10 must\nhave a hardware power-monitoring subsystem, or other means available to monitor\nand record power statistics. Some SoCs gather\npower-usage statistics for you, or you may obtain power-entity state residency\ninformation through software. Power-monitoring hardware is only necessary to\nsupport `getRailInfo()`, `getEnergyData()`, and\n`streamEnergyData()`.\n\nIf you implement `IPowerStats.hal` without power-monitoring\nhardware, `getRailInfo(), getEnergyData()`, and\n`streamEnergyData()` return `NOT_SUPPORTED`. Similarly,\n`getPowerEntityInfo(), getPowerEntityStateInfo()`, and\n`getPowerEntityStateResidencyData()`may also return\n`NOT_SUPPORTED` if it isn't intended to be used.\n\n\nExamples of data returned by the rail-monitoring APIs include\n\n- The power rail for the display consumed X µW.\n- The power rail for the modem consumed Y µW.\n\n\nExamples of data returned by the subsystem sleep-state APIs include\n\n- The modem was asleep for X ms.\n- The SoC was in the power-collapse state for Y ms.\n- The GPU was in the suspend state for Z ms.\n\n### Use a hardware power-monitoring subsystem\n\nIf your device design has a hardware power-monitoring subsystem, implement\n`IPowerStats.hal` by creating a single **sysfs** node\nfrom which `PowerStats.hal` can parse data, or by making a\ncollection of **ioctl**-type system calls.\n\nYou must implement your kernel driver in a way that prevents accumulator\noverflow. The algorithm used depends on your unique hardware power-monitoring\nsubsystem design, which must provide both instantaneous and average bus voltage\nand current measurements. The kernel driver must capture this data in a manner\nthat doesn't clear the energy accumulators, and it must maintain the\naccumulated energy data for each subrail since boot, in the form of a 64-bit\nvariable that gets incremented with the energy reading from each accumulator\nquery.\n\nStats for a given component (or optionally, multiple components) must be in\na single node. While this isn't a conventional use of **sysfs**\n(which normally limits each node to a single value), it ensures all data is\nconsistent.\n\n### Design guidance\n\n- Keep latency low (1 msec, maximum) when reading from the **sysfs** node or making system calls.\n- Ensure that supporting stats functionality doesn't measurably increase the power drain:\n - Don't increase access point (AP) and/or subsystem wakeups to track parameters such as the time spent in sleep mode.\n - Transfer stats between the apps processor and firmware opportunistically with other traffic when possible.\n- If necessary, the subsystem may use the following driver functions:\n - Internally caching data to avoid latency/wakeups at the expense of slightly stale data.\n - Performing extrapolation when the subsystem is asleep, to provide updated sleep-time without waking the subsystem.\n\n### Choose components, subsystems, and stats\n\nWhen choosing which components or subsystems from which to gather\n`IPowerStats.hal` data, select anything on the device that consumes\nsignificant current (5 mA or more), or that supports\nmultiple power-consumption modes, such as the following:\n\n- Individual SoC subsystems.\n- Subsystems partially or completely outside of the SoC, such as WiFi, the image processor, or the security processor.\n- Peripherals such as high-power LEDs and cameras.\n- Power domains that use different modes (such as the power domain for the SoC as a whole).\n\nCustomization\n-------------\n\nThis optional feature is amenable to customization. Design use cases\nand customize your use:\n\n- Decide which rails to measure, and how frequently to measure them.\n- Decide when to read the data, and how to interpret it.\n- Decide what action to take and when to take it, based on your data.\n\nValidation\n----------\n\nVTS tests ensure Android requirements are met. The comments in\n`IPowerStats.hal` are used for verifying that a device is in\ncompliance.\n\nFor example, if you call `getRailInfo()` and it returns nothing,\nthe VTS test fails, because you didn't receive information about the monitored\nrails, or a returned status of `SUCCESS`. Similarly, if you received\nrail info, but it was accompanied by a `NON_SUPPORTED` or\n`FILE_SYSTEM_ERROR` response, that's also a failure. The VTS\nverifies the device manufacturer specification is adhered to in the HAL file,\nusing the requirements in the IPower.hal and IPowerStats.hal comments. An\nexample of comments used in VTS testing is shown below: \n\n```scilab\n/**\n* Rail information:\n* Reports information related to the rails being monitored.\n*\n* @return rails Information about monitored rails.\n* @return status SUCCESS on success or NOT_SUPPORTED if\n* feature is not enabled or FILESYSTEM_ERROR on filesystem nodes\n* access error.\n*/\ngetRailInfo()\ngenerates(vec<e;RailInfo>e; rails, Status status);\n```"]]