tree: 5684ac6b297c08f8afccbbe2041633b545f59cb4 [search]
  1. src/
  2. Android.bp
  3. battery_drain_metrics_v2_spec.pbtx
  4. OWNERS
  5. perfetto_battery_drain.pbtx
  6. README.md
tests/PowerDrain/README.md

Tools for measuring power drain

This directory has some basic tools to aid in roughly measuring the power drain of a workload on an Android device. This tooling is meant to work across all ecosystem devices, and as such has a low common denominator and may still have poor results on certain devices.

For far more precise power drain measurement and debugging, devices with On Device Power Monitor (ODPM) are strongly recommended. There is other tooling built around this, like Android Studio Power Profiler. The tools in this directory do not use the more precise ODPMs.

Perfetto-based measurement

The pbtx files in here can be used on a device with an adb connection to measure the power drain of something running on the device.

Especially if the workload is continuous and self-running, it should be started prior to starting perfetto.

# Set up and run Perfetto trace
$ adb push perfetto_battery_drain.pbtx /data/misc/perfetto-configs/
$ adb shell /system/bin/perfetto --background-wait \
  --config /data/misc/perfetto-configs/perfetto_battery_drain.pbtx \
  --txt --out /data/misc/perfetto-traces/my_trace_file
# Note the PERFETTO_PID from the output

# Force refresh of Health HAL stats to populate our perfetto trace.
$ adb shell cmd battery get -f level

# Remove external power to the device (i.e. unplug the USB cable).
# Run device workload as long as desired.
# Reconnect external power to the device.

# Stop Perfetto
$ adb shell kill -TERM PERFETTO_PID

# Wait until Perfetto is finished wrapping up.
$ adb shell 'while ps -p PERFETTO_PID >/dev/null; do sleep 1; done'

# Pull trace and analyze
$ adb pull /data/misc/perfetto-traces/my_trace_file
$ ../../../../prebuilts/tools/linux-x86_64/perfetto/trace_processor_shell \
  --summary --summary-metrics-v2 all \
  --summary-spec battery_drain_metrics_v2_spec.pbtx my_trace_file

Looking at the output as text, the last three double_value are the important ones. They pair respectively with the last three column_names in the output. They are all expressed in microampere-hours (uAh).

The third last, calculated_charge_drain_uAh, is likely to be the best measurement for the device and should generally be used.

The last two, starting_battery_charge_uAh and ending_battery_charge_uAh, can be subtracted to get a different value for this. These measurements come exclusively from the reported charge of the device‘s battery. For many ecosystem devices, this is reported at coarse granularity and can be especially skewed when the device’s battery is at or near full.

Note that trace_processor_shell, per the given summary-spec, limits its analysis to when the device is not being powered, which is likely a subset of the Perfetto trace.

Cleanup

$ adb shell rm /data/misc/perfetto-configs/perfetto_battery_drain.pbtx /data/misc/perfetto-traces/my_trace_file
$ rm my_trace_file