Home Android & Kotlin Books Android Debugging by Tutorials

9
Profile CPU Activity Written by Zac Lippard

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

You can unlock the rest of this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Efficiently using the CPU is critical when it comes to your Android app. It determines the difference between jittery and smooth UI and significantly impacts a device’s battery life.

It’s important to keep in mind your app’s impact on resources. Your app should run smoothly and feel fluid throughout. A choppy app won’t bring delight to your end-user.

Your app should also be efficient and not have unfettered access to system resources. Extensive use of system resources causes other applications to delay their operations. This also drains the battery and could lower the app’s overall performance.

This chapter covers the Android Studio CPU Profiler and how you can use this tool to track CPU usage.

You’ll learn:

  • All about CPU traces including system traces and method/function traces.
  • Recording traces.
  • Import and export traces.
  • Inspect traces using the many different charting formats available in the CPU Profiler.
  • Use the third-party tool Perfetto to inspect system traces.

The CPU Profiler

Android Studio provides the CPU Profiler as a tool to measure CPU usage in real-time as well as to monitor active threads of an Android process. If you’re noticing a performance issue with your app, the Profiler is there to help!

To start, open the Podplay starter project in Android Studio and run the application. Once the app is running, you can connect the Profiler to your app’s process. To do so, click View ▸ Tool Windows ▸ Profiler, or simply click the Profiler tab in the bottom toolbar.

In the Profiler window, you’ll notice there’s not really much happening there yet. You now need to connect the Profiler to your running Podplay app process.

  1. Click the + icon in the top-left of the Profiler window in the Sessions toolbar.
  2. Select your device.
  3. Choose the Podplay app process to connect to.

You’ll notice a new entry on the left in the Sessions column. On the right, the Profiler has started collecting data. The session will remain until you close Android Studio. There are sections for CPU, Memory, Networking, and Energy. For the purposes of this chapter, you’ll focus on the CPU section.

Double-click the CPU section, and the Profiler will update to show the CPU panel.

To the left of the CPU panel is a section to create trace recordings and configurations.

On the right-hand side, there’s a series of timelines.

Each timeline section provides unique details about how your app is interacting with the CPU:

  1. Event timeline: UI rendering and interaction data shows up here. Activity and fragment lifecycle calls will appear in this timeline. Any user interactions such as button taps or text input will also display in this section.
  2. CPU timeline: This timeline displays the CPU utilization as a percentage. The timeline will also show CPU utilization belonging to other processes to compare your app against.
  3. Thread activity timeline: Active threads that are running as part of the Podplay application process. A green section indicates the thread is active and running, or at least in a runnable state. Yellow represents that the thread is active but waiting for an IO operation. Gray shows threads that are sleeping and not consuming any CPU.

Standalone Profiler

Don’t want to run the entire Android Studio IDE just to launch the Profiler? No worries! There’s a way to launch just the Profiler as a standalone tool.

Traces

A trace is a set of data that details how a process and its threads interact with the CPU and other resources. Typically the trace data is extremely thorough and covers method/function calls, rendering data, lifecycle calls on the Activity or Fragment and user input. All of these combined helps to paint a picture of how your app performs and what resources it’s using.

Recording Traces

Run the Podplay project in Android Studio. Start a new CPU Profiler session by clicking on the Profiler window tab at the bottom of Android Studio. Click the + icon and select the device and Podplay process to launch the new session. Now, click the CPU timeline to open the CPU Panel.

Recording Configurations

As mentioned earlier in the chapter, the left column shows a number of report configurations.

Recording a Trace

To record a trace from the CPU Panel, click Record.

Recording at App Startup

You can create trace recordings during the app startup as well. This is useful for monitoring resources and code during the launch of the Application and initial Activity classes where you might not be quick enough to attach the Profiler manually.

Exporting a Recording

Now that you’ve acquired a number of recorded traces, it’s time to export them. Hover over one of the recordings, and you’ll notice a save icon that you can click.

Importing a Recording

You can also import recordings to Android Studio. This is useful if you want to share and access a recording from another workstation or with other developers.

Inspecting Method Traces

Within the CPU Profiler, there are a number of ways to be able to inspect the trace data. You’ll learn about the different charting approaches in this section and the benefits of using each one.

Call Chart

The first charting option is the Call Chart. It appears under the Threads section of the trace. This chart provides a visual aid of the method and function calls made during the timeframe of the recording.

Looking at Top Down Charts

The Top Down Chart provides similar details to the Call Chart mentioned earlier but with more fine-grained details about each method. To use this chart, switch to the Analysis section on the right-hand side.

Looking at Flame Charts

The Flame Chart option is an inverted Call Chart. This way, ancestor methods are at the bottom, and the descendants appear above them. This causes the visualization of the chart to appear like flames, hence the name.

Looking at Bottom Up Charts

As you may have guessed, the Bottom Up Chart is very similar to the Top Down Chart but inverted, in a way.

Events

The Events tab is useful to see all the calls for the thread selected in the Threads timeline.

Inspecting System Traces

As mentioned earlier, system traces provide fine-grained details on the system resources allocated to the app on a given device. This provides a different approach to understanding how performant your app is and how efficient it runs within the Android ecosystem.

UI Interaction

The Interaction timeline shows you what the user did during the time frame captured.

UI Jank

Jank refers to an unpleasant or unstable experience within an app. Specific to the user experience, UI jank is often referring to jittery UI that feels unnatural or jarring to the end-user.

CPU Cores

The CPU Cores timeline gives insight into how the work your app requires allocates to the device’s CPU. This section lists each CPU core and frequency.

Process Memory (RSS)

The Process Memory timeline provides details about the app’s memory footprint. This footprint, generally known as the Resident Set Size or RSS, describes how much memory the app uses in RAM.

System Trace Formats

There are two primary formats for system traces on Android: Perfetto and Systrace.

Inspection With Perfetto UI

A situation may arise where you have a system trace file but don’t have Android Studio installed. Fear not, for there is a third-party alternative that you can use!

Challenge

Remember the topic about UI jank? Well, it turns out there’s some jankiness in this chapter’s starter and final Podplay app as well. Open the Logcat window and run the Podplay app. Tap Search and search for “android”. When the list of podcasts appears, tap any. Then, tap on a podcast episode to launch the player. You’ll notice a delay before the loading spinner appears. During this time, you’ll see logs stating Skipped X frames!.

Key Points

  • System traces measure performance on system resources and the UI.
  • Method/function traces measure performance of targeted Java/Kotlin methods and C/C++ functions.
  • Traces can be imported and exported with Android Studio.
  • The CPU Profiler provides inspection on traces.
  • You can analyze traces with a number of charting options provided by the CPU Profiler.
  • Perfetto UI is an alternative to the CPU Profiler to analyze system traces.

Where to Go From Here?

You covered a lot of ground regarding CPU profiling. There’s still so much more to explore regarding this topic!

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.

© 2022 Razeware LLC

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Unlock Now

To highlight or take notes, you’ll need to own this book in a subscription or purchased by itself.