Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Add battery debugging doc #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/android_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ If you like staying up-to-date on the latest Android topics, members of our team
## Intermediate topics
Comfortable with the basics? Here are a few of our favorite resources to get started on intermediate topics.
- [Android style tips](http://blog.danlew.net/2014/11/19/styles-on-android/) (circa 2014)
- [Battery debugging](battery_debug.md)

[udacity course]: https://www.udacity.com/course/new-android-fundamentals--ud851
[devdocs]: https://developer.android.com/docs/
35 changes: 35 additions & 0 deletions android/battery_debug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Android Performance Profiling can be quite tricky in spite of the wealth of good tools available. This doc is to explain how to track down battery and performance issues.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a title, e.g.

# Battery Debugging


## Battery Historian

Whenever battery issues arise on Android, the first Google suggestion is to use the Battery Historian tool to parse an Android bug report and show what caused the battery drain.

While this is a good option for most Android apps, it proves next to useless for analyzing most battery issues in Focus and Klar. This is because Focus and Klar uses a foreground service that remains running as long as your web pages are open. The service is required to keep the app in the foreground so data can remain in memory rather than being persisted to disk. This keeps the app from being killed by the OS and sending all your precious web session and page data to the garbage collector.

Battery Historian starts from the premise that most battery problems are caused by excessive wake locks. It helps the programmer by showing wake locks, when the WiFi or cellular radio is being kept awake, and the rate of battery drain at different times. Focus and Klar do not require wake locks and they wouldn't be interesting, since the apps remain in the foreground most of the time the app is running.

Analyzing Battery Historian results can still prove interesting. [Here are the instructions](https://developer.android.com/studio/profile/battery-historian) to try.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can they still prove interesting? Perhaps you can provide an example?


## Android Studio Profiler

If Battery Historian does not point at the radio or screen being kept alive as the issue, the next thing is to look for excessive CPU activity. There are several tools to look into this, but the easiest to get started is the profiler built into Android Studio.

Open the project in Android Studio. If there is no Android Profiler tab on the bottom of the screen, open it from View -> Tool Windows -> Android Profiler.

Make sure you're running a debug version of the app and choose the device and debuggable process from the dropdowns at the top of the Profiler.

Click the CPU graph to get additional CPU detail. Choose instrumented to increase the resolution of the data. If the battery drain is happening while the phone is idle, you can easily check to find out what is causing the CPU activity, since there shouldn't be much at all.

[Here's more](https://developer.android.com/studio/profile/android-profiler) on how to use the Android Profiler in general. [Here's more on the CPU Profiler](https://developer.android.com/studio/profile/cpu-profiler) specifically.

## Systrace
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are Systrace and Traceview useful to diagnosing battery issues because they also help you find high CPU usage? I wonder if these + Android Profiler should fit under a higher category of debugging CPU usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally wrote this document for Focus, since Focus has a uniquely different situation than most apps on Android. Using a foreground service most of the time means we must consider CPU usage while the app is backgrounded and idle.


Systrace does not add that much to what Android Studio's Profiler already provides and the interface is a clunky webpage. You can still try it if you like. [Here are the instructions](https://developer.android.com/studio/command-line/systrace).

## Traceview

While you can get a lot of useful and interesting information from the Android Profiler and Systrace, it's helpful to get full call stacks. The easiest way to do this is to use the older Android profiling tool, Traceview. You can select from the code when to start and stop tracing with "Debug.startMethodTracing("name")" and "Debug.stopMethodTracing()"

When you run the code, it'll generate a trace file in the files folder on the SDcard. You can read the trace file either by double-clicking it in Android Studio's Device File Explorer or even better by opening it in Android Device Monitor. You can start Android Device Monitor in recent Android Studio versions by running $ANDROID_SDK/tools/monitor.

[Here's more detail on using TraceView](https://developer.android.com/studio/profile/traceview).