Analyzing Android with Battery Historian

3 minute read

Battery Historian is a tool for inspecting battery related issues, it works on android 5.0 and above, this can also be used for analyzing performance since battery drain also means bad performance.

This screenshot shows how battery historian report may looks: battery historian

Installation

For using battery historian, you can build on your own or using prebuilt docker image, or using online battery historian service.

This section will demonstrate how to build it on your own, battery historian was written in go, so the first thing you need is to install it with brew install go or update with brew upgrade go.

java is also required for using this, if you have other version installed, update with below commands:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-13.0.1/bin/java 88
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-13.0.1/bin/javac 88

Then get the battery-historian with command:

$ go get -d -u github.com/google/battery-historian/...
$ battery-historian go run setup.go

After downloaded battery-historian and setup, you are ready to go and give it a try:

$ go run cmd/battery-historian/battery-historian.go [--port <default:9999>]

The above command should tell us below message:

2019/11/10 22:10:31 Listening on port:  9999

Now it’s time to take a bugreport and upload for test, take bugreport with adb bugreport bug.zip then go to localhost and click Browse then Submit the report, a few seconds later, we’ll see the final report similar to the above screenshot.

Note: if the following variables are not set, the battery-historian git repo will be cloned to $HOME/go/src/github.com/google/battery-historian/:

export GOPATH=$HOME/work
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN

Usage

There are five tabs in the battery historian report:

Historian V2 the main part
System Health memory info, performance statistics like long dvm_lock_samples, historical broadcasts
Event Log Statistics about logtags
Custom Show custom metrics
Historian Former version of Historian (I think)

Historian V2

The most informative tab would be the first tab Historian V2 provides valuable information about system activities related to power consumption, below is an example chart of battery historian: battery historian

We can see when the screen is turned on/off, screen time: screen

Apps in top app: top app

System Health

system health From system health tab, we know how system performs, such as how much time it takes to handle a broadcast, does the system suffers from low memory, and system performance.

If the broadcast takes too much time, broadcasts that takes more than 1s and 5s are marked with dark color and normal ones with lighter color:

broadcast

#155 is an bad example of broadcast that takes too much time between Enqueue and Dispatch: broadcast take too long

Another example of holding a lock: dvm lock sample

Event Log

This part comes from android logtags, with this tab, it is very convenient for inspecting cpu usages, memory info and more: Event Log

System realtime memory info:

meminfo

CPU usage:

cpu usage

Custom

If the above chart does not fit your needs, you can customize it for yourself, you can add metrics of your interest:

add metric

Historian

Historian

Generate Battery Historian Report

The very basic report can be generated with bugreport only, reset batterystats with before taking bugreport:

$ adb shell dumpsys batterystats --reset

Then do adb bugreport bug.zip, this may take several minutes, after finished, upload the zip file to: localhost.

You can also capture wakelock traces if the target device is rooted:

$ echo "power:wakeup_source_activate" >> /d/tracing/set_event
$ echo "power:wakeup_source_deactivate" >> /d/tracing/set_event
$ echo 8192 > /d/tracing/buffer_size_kb
$ echo 1 > /d/tracing/tracing_on

Then do some intended user activity such as connect to a wifi hotspot or play with camera.

Pull the kernel trace and upload to battery-historian along with bugreport:

$ echo 0 > /d/tracing/tracing_on
$ adb pull /d/tracing/trace trace.dat

Trouble Shooting

An error occurred when I doing setup with command go run setup.go:

/Users/fdbai/go/src/github.com/google/battery-historian/third_party/closure-library/closure/goog/ui/textarea.js:607: WARNING - Parse error. unknown @suppress parameter: strictMissingProperties
/Users/fdbai/go/src/github.com/google/battery-historian/third_party/closure-library/closure/goog/window/window.js:128: WARNING - Parse error. unknown @suppress parameter: strictMissingProperties
/Users/fdbai/go/src/github.com/google/battery-historian/third_party/closure-library/closure/goog/streams/full_test_cases.js:635: ERROR - Parse error. '(' expected

1 error(s), 343 warning(s)

This error happens, because the official battery-historian has not been updated for a very long time, but the third party libraries still under active development, as nreid260 commented in an open issue of closure-library:

Those tests (I think) use async functions and will need a parser that supports them.

I fixed this issue by updating closureCompilerVersion to 20191027, you can find it on github.

References