xref: /aosp_15_r20/external/perfetto/docs/data-sources/battery-counters.md (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1# Power data sources
2
3On Android Perfetto bundles data sources to retrieve power
4counters from the device power management units (where supported).
5
6## Battery counters
7
8_This data source has been introduced in Android 10 (Q) and requires the
9presence of power-management hardware on the device. This is available on
10most Google Pixel smartphones._
11
12Modern smartphones are equipped with a power monitoring IC which is able to
13measure the charge flowing in and out of the battery. This allows Perfetto to
14observe the total and instantaneous charge drained from the battery by the
15overall device (the union of SoC, display, radios and all other hardware
16units).
17
18A simplified block diagram:
19
20![](/docs/images/battery-counters.png "Schematic diagram of battery counters")
21
22These counters report:
23
24* The remaining battery capacity in %.
25* The remaining battery charge in microampere-hours (µAh).
26* The instantaneous (typically the average over a small window of time) current
27  in microampere (µA)
28
29The presence and the resolution of these counters depends on the device
30manufacturer. At the platform level this data is obtained polling the
31Android [IHealth HAL][health-hal].
32For more details on HW specs and resolution see
33[Measuring Device Power](https://source.android.com/devices/tech/power/device).
34
35[health-hal]: https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/health/2.0/IHealth.hal?q=IHealth
36
37#### Measuring charge while plugged on USB
38
39Battery counters measure the charge flowing *in* and *out* of
40the battery. If the device is plugged to a USB cable, you will likely observe
41a positive instantaneous current and an increase of the total charge, denoting
42the fact that charge is flowing in the battery (i.e. charging it) rather
43than out.
44
45This can make measurements in lab settings problematic. The known workarounds
46for this are:
47
48* Using specialized USB hubs that allow to electrically disconnect the USB ports
49  from the host side. This allows to effectively disconnect the phone while the
50  tests are running.
51
52* On rooted phones the power management IC driver allows to disconnect the USB
53  charging while keeping the USB data link active. This feature is
54  SoC-specific, is undocumented and not exposed through any HAL.
55  For instance on a Pixel 2 this can be achieved running, as root:
56  `echo 1 > /sys/devices/soc/800f000.qcom,spmi/spmi-0/spmi0-02/800f000.qcom,spmi:qcom,pmi8998@2:qcom,qpnp-smb2/power_supply/battery/input_suspend`.
57  Note that in most devices the kernel USB driver holds a wakelock to keep the
58  USB data link active, so the device will never fully suspend even when turning
59  the screen off.
60
61### UI
62
63![](/docs/images/battery-counters-ui.png)
64
65### SQL
66
67```sql
68select ts, t.name, value from counter as c left join counter_track t on c.track_id = t.id
69```
70
71ts | name | value
72---|------|------
73338297039804951 | batt.charge_uah | 2085000
74338297039804951 | batt.capacity_pct | 75
75338297039804951 | batt.current_ua | -1469687
76338297145212097 | batt.charge_uah | 2085000
77338297145212097 | batt.capacity_pct | 75
78338297145212097 | batt.current_ua | -1434062
79
80### TraceConfig
81
82Trace proto:
83[BatteryCounters](/docs/reference/trace-packet-proto.autogen#BatteryCounters)
84
85Config proto:
86[AndroidPowerConfig](/docs/reference/trace-config-proto.autogen#AndroidPowerConfig)
87
88Sample config (Android):
89
90```protobuf
91data_sources: {
92    config {
93        name: "android.power"
94        android_power_config {
95            battery_poll_ms: 250
96            battery_counters: BATTERY_COUNTER_CAPACITY_PERCENT
97            battery_counters: BATTERY_COUNTER_CHARGE
98            battery_counters: BATTERY_COUNTER_CURRENT
99            battery_counters: BATTERY_COUNTER_VOLTAGE
100        }
101    }
102}
103```
104
105Sample Config (Chrome OS or Linux):
106
107```protobuf
108data_sources: {
109    config {
110        name: "linux.sysfs_power"
111    }
112}
113```
114
115## {#odpm} On-Device Power Rails Monitor (ODPM)
116
117_This data source has been introduced in Android 10 (Q) and requires the
118dedicated hardware on the device. This hardware is not yet available on
119most production phones._
120
121Recent version of Android introduced the support for more advanced power
122monitoring at the hardware subsystem level, known as
123"On-Device Power Rail Monitors" (ODPMs).
124These counters measure the energy drained by (groups of) hardware units.
125
126Unlike the battery counters, they are not affected by the charging/discharging
127state of the battery, because they measure power downstream of the battery.
128
129The presence and the resolution of power rail counters depends on the device
130manufacturer. At the platform level this data is obtained polling the
131Android [IPowerStats HAL][power-hal].
132
133Googlers: See [go/power-rails-internal-doc](http://go/power-rails-internal-doc)
134for instructions on how to change the default rail selection on Pixel devices.
135
136[power-hal]: https://cs.android.com/android/platform/superproject/main/+/main:hardware/interfaces/power/stats/1.0/IPowerStats.hal
137
138Simplified block diagram:
139
140![](/docs/images/power-rails.png "Block diagram of ODPMs")
141
142### TraceConfig
143
144Trace proto:
145[PowerRails](/docs/reference/trace-packet-proto.autogen#PowerRails)
146
147Config proto:
148[AndroidPowerConfig](/docs/reference/trace-config-proto.autogen#AndroidPowerConfig)
149
150Sample config:
151
152```protobuf
153data_sources: {
154    config {
155        name: "android.power"
156        android_power_config {
157            battery_poll_ms: 250
158            collect_power_rails: true
159            # Note: it is possible to specify both rails and battery counters
160            # in this section.
161        }
162    }
163}
164```
165
166## Related data sources
167
168See also the [CPU -> Frequency scaling](cpu-freq.md) data source.
169