The previous article Android Perfetto Series 1: Introduction to Perfetto introduced what Perfetto is. This article will provide a brief guide on how to capture Perfetto traces.
Table of Contents
- Perfetto Series Catalog
- Main Content
- 1. Capturing via CLI (Recommended)
- 2. Capturing via Official Scripts
- 3. Using On-Device System Tracing
- 4. Using the Web Interface
- Reference Documents
Paul Graham once said: “Either provide something most people want a little, or something a few people want a lot.“ Perfetto is exactly what a dedicated minority “wants a lot.” Let’s begin.
This series aims to provide a fresh perspective on Android system operation through Perfetto. It offers a unique angle to study App, Framework, and Linux internals. Through graphical tools like Perfetto, you may gain much deeper insights.
Perfetto Series Catalog
- Android Perfetto Series Catalog
- Android Perfetto Series 1: Introduction to Perfetto
- Android Perfetto Series 2: Capturing Perfetto Traces
- Android Perfetto Series 3: Familiarizing with the Perfetto View
- Android Perfetto Series 4: Opening Large Traces via Command Line
- Android Perfetto Series 5: Choreographer-based Rendering Flow
- Android Perfetto Series 6: Why 120Hz? Advantages and Challenges
- Android Perfetto Series 7: MainThread and RenderThread Deep Dive
- Android Perfetto Series 8: Understanding Vsync and Performance Analysis
- Android Perfetto Series 9: Interpreting CPU Information
- Video (Bilibili) - Android Perfetto Basics and Case Studies
Main Content
The workflow for analyzing issues with Perfetto is identical to Systrace:
- Capture the Perfetto trace file.
- Open it in ui.perfetto.dev or analyze it via CLI.
This article introduces several capture methods. I strongly recommend the command-line approach for its flexibility and reliability.
1. Capturing via CLI (Recommended)
Basic Command - adb shell perfetto
For developers transition from Systrace, the CLI is very familiar. You can directly invoke /system/bin/perfetto on your Android device.
1 | # 1. Start the trace |
This records a 20-second trace with specified data sources and saves it to the local device storage. You can then open the resulting file in the web UI.
Advanced: Using Config Files
Unlike Systrace, Perfetto can capture an enormous amount of data. Passing all tags via CLI every time is cumbersome. Instead, you can use a Config file and point Perfetto to it.
Android 12 and Newer
On Android 12+, you can store configs in /data/misc/perfetto-configs/.
1 | adb push config.pbtx /data/misc/perfetto-configs/config.pbtx |
Older than Android 12
Due to SELinux restrictions on non-root devices, you must pass the config via standard input (stdin).
1 | adb push config.pbtx /data/local/tmp/config.pbtx |
Where do I get a Config?
I recommend using the Record new trace wizard on the web UI to visually select your sources, then copy the result to a local file. This allows you to maintain different configs for different scenarios (e.g., startup vs. jank).
2. Using Official Scripts (Highly Recommended)
The Perfetto team provides a helper script record_android_trace that handles path management, pulls the file automatically upon completion, and opens it in your browser.
Linux and Mac:
1 | curl -O https://raw.githubusercontent.com/google/perfetto/master/tools/record_android_trace |
Windows:
1 | curl -O https://raw.githubusercontent.com/google/perfetto/master/tools/record_android_trace |
You can also use -c to specify a config file with this script.

3. Using On-Device System Tracing
If you can’t connect to a PC, use the built-in System Tracing App in Developer Options.
- Enable Developer Options: Tap “Build Number” 7 times in “About Phone.”
- Open System Tracing: Find “System Tracing” in Developer Options.
- Configure & Record: Set the duration and buffer size, then tap “Record Trace.” Use the notification toggle to stop recording.
Once finished, you can share or pull the file to a PC for analysis.
4. Using the Web Interface
Note: Capturing directly from the browser can be finicky due to ADB connection issues. I primarily use the web interface to generate configs rather than for the actual capture.
Visit ui.perfetto.dev/#!/record, select “Add ADB device,” and configure your data sources. Under Android apps, ensure “Atrace userspace annotations,” “Event log (logcat),” and “Frame timeline” are checked.
If you are debugging a debuggable process on a recent Android version, you can also enable Callstack sampling under Stack Samples to view function callstacks in the trace.
Transitioning from Web to CLI
After selecting your sources in the UI, click “Recording command” to see the generated Config. You can save this text to a .pbtx file for use with the CLI methods mentioned above.
Reference Documents
- Perfetto Quickstart: Android Tracing
- Perfetto Concepts: Configuration
- Android Platform Tools Release Notes
“If you want to go fast, go alone. If you want to go far, go together.”
