In Android Studio 4.0, we overhauled the CPU Profiler UI to provide a more intuitive recording workflow. We continued to make improvements based on your feedback and added more features in 4.1.
In this article, we’ll highlight what’s new in Android Studio for System Trace, one of the infrequently-used yet powerful CPU recording configurations. Unlike Java Method or C/C++ Function Traces, System Trace collects device activities (e.g. CPU core scheduling) and Android system processes (e.g. SurfaceFlinger). In addition, you can instrument your code with custom events by calling the Trace API in your app. This way your custom events will be collected along with system events. All of this data combined can come in handy when you investigate performance issues such as UI jankiness or high power consumption.
All Threads at Once
CPU recordings are now separated from the main profiler timeline to allow for easier analysis. In this dedicated view, trace data is organized in sections on the left side of the Profiler window.
You can move sections up and down to reorganize the list, or individual items within a section by simply dragging-and-dropping.
We heard from you that selecting each thread to view its call chart (or trace events for System Trace) is cumbersome, so we combined all thread activities into one view, displaying thread states and call charts all at once. By default we sort the threads by how busy they are, but you can drag-and-drop any individual one to re-order them.
You can also collapse or expand each thread by single-clicking the triangle icon or double-clicking the thread name. Note that for a Java Method Trace or C/C++ Function Trace we collapse all threads by default due to deep call stacks, so that you can get a glance of all thread data up front.
For a System Trace, the trace events are now uniquely colored for easier differentiation.
More Intuitive Navigation
The new trace UI has a revamped timeline navigation scheme. We replaced the old horizontal scroll bar with a master-detail view.
On the top you can see a timeline that maps to only the duration of the trace, instead of the entire profiling session. Using the range selector you can quickly narrow down to a specific time range, for which the sections below display detailed data.
From there you can perform more precise navigation actions.
- Zoom in/out using Ctrl (or Cmd on Mac) + mouse wheel;
- Pan the view by dragging the mouse left and right while holding down the space key;
- Use the “WASD” keys to zoom and pan the view, just like the legacy SysTrace UI in Chrome (chrome://tracing).
Starting in Android Studio 4.1 Canary 9, you can drag your mouse in the Threads section to perform a box selection. Box selection lets you precisely select a rectangle area, which you can zoom into by clicking the Zoom to Selection button on the top right (“M” key). You can even select across multiple threads. This is useful when you drag-and-drop similar threads next to each other and inspect all of them at once. For example, you may want to perform analysis on multiple worker threads, often found in games.
The Analysis Panel
Speaking of analysis, we want to highlight the new Analysis Panel introduced in Android Studio 4.0, in the right-hand column of the Profiler window.
Here you can find analysis data derived from the recorded trace based on your selection. When you select a thread, stack frame or trace event from the left-hand column, the Analysis Panel will show information specific to your selection. For instance, when a thread is selected, its thread states are displayed, along with other useful info.
We want to make this Analysis Panel useful to you, so we’ve been exploring ways to surface helpful analysis data. In addition to the Top Down, Flame Chart and Bottom Up graphs already present in the CPU Profiler, we added a Summary tab in Android Studio 4.1 Canary 10 for things like thread state distribution, trace event statistics, etc. For example, oftentimes we want to find out more about a recurring trace event. The Summary tab displays basic statistics (count, min, max, etc.) as well as the longest running occurrences of the selected trace event. You can even navigate to another occurrence by selecting a row from the table.
Stability and Performance Improvements
Last but not least, we’ve been improving the stability and performance of CPU recording.
- We fixed several bugs that may result in recording failures.
- We built System Trace with Perfetto as the backend on Android API level 28 and later, taking advantage of Android’s latest instrumentation technology.
- We also optimized the profiler code to significantly reduce memory consumption (by up to 80%) when parsing a recorded trace, allowing you to open and analyze longer traces. Remember that you can increase the maximum heap size of Android Studio, which helps with very long traces.