Perfetto

On this page 7

Perfetto is an open-source platform for system tracing, profiling and trace analysis, developed by Google and used as the default tracing system on Android since Android 9. In mobile performance work, it records what the kernel, the framework and individual apps are doing on a shared timeline, which lets engineers explain jank, slow app startup, ANRs, wake locks and battery drain with evidence rather than guesswork.

Perfetto replaced the older systrace workflow and extends far beyond it: it combines kernel ftrace events, userspace trace points, CPU and memory profiling and a SQL-based trace processor in one toolchain that also serves Chrome and Linux.

How Perfetto records a trace

Perfetto uses a producer and consumer architecture around a central tracing service. On Android, the traced daemon owns shared-memory buffers, producers such as traced_probes and instrumented apps write events into them, and a consumer such as the perfetto command-line tool starts the session and collects the result.

Each session is driven by a trace config, a protobuf text file that declares which data sources to enable, the buffer sizes, and the duration. The output is a single protobuf trace file, usually with the .perfetto-trace or .pftrace extension, that contains every event with nanosecond timestamps.

The main components and their roles are summarized below.

ComponentRoleWhere it runs
tracedCentral tracing service and buffer ownerDevice daemon
traced_probesKernel ftrace, process and power dataDevice daemon
perfetto CLIStarts sessions and saves trace filesDevice shell via adb
Track Event SDKCustom app and library instrumentationInside the app process
Trace ProcessorParses traces into SQL tablesHost machine or browser
Perfetto UITimeline visualization and queriesWeb browser, offline capable
Core Perfetto components

Data sources and profiling modes

The power of Perfetto comes from combining data sources in a single trace, so that a dropped frame can be correlated with CPU frequency, scheduler decisions, binder calls and garbage collection at the same instant. Choosing too many sources inflates the trace, so experienced engineers enable only what the question requires.

Data sourceTypical contentQuestions it answers
ftrace schedulingThread states, CPU placement, wakeupsWhy a thread was runnable but not running
atrace categoriesFramework slices such as view, gfx, amWhere main-thread time went
CPU frequency and idleClock changes and idle statesWhether thermal or power limits slowed work
heapprofdSampled native allocations with call stacksWhich code grows native memory
Java heap dumpObject graph of the managed heapWhat retains memory and causes leaks
Callstack samplingPeriodic CPU stack samplesWhich functions consume CPU time
Battery and power railsCounters from the power subsystemWhich component drains energy
Common data sources and what they reveal

Recording happens in two buffer modes. Ring buffer mode keeps the most recent events in memory and overwrites old data, which suits short reproductions. Long trace mode periodically writes buffers to a file, which allows traces lasting minutes to hours at the cost of more storage and I/O.

Analyzing traces with SQL

Perfetto treats a trace as a queryable database. The Trace Processor imports the raw file into tables such as slice, thread_state, counter and process, and engineers query them with PerfettoSQL, a SQL dialect with a standard library of performance metrics.

This approach turns one-off investigations into repeatable analysis. A team can write a query that measures time to first frame or main-thread blocking, run it across hundreds of traces from a lab, and track the result over releases. Python bindings let the same queries run inside continuous integration pipelines.

The UI and the query engine serve different stages of an investigation.

ApproachStrengthBest use
Timeline viewShows causality across threads at a glanceExploring a single jank or ANR
Ad hoc SQL queryPrecise numbers from one traceMeasuring durations and counts
Batch trace analysisStatistics across many tracesRegression detection and benchmarks
Visual inspection compared with SQL analysis

Perfetto compared with other mobile profiling tools

Perfetto is the system-wide tracing layer, while other tools focus on narrower views or other platforms. The Android Studio Profiler uses Perfetto data internally for its system trace view but offers a simplified, app-focused interface. On iOS, Xcode Instruments fills a comparable role, and MetricKit and Android vitals provide aggregated field data rather than detailed traces.

ToolPlatformScopeData origin
PerfettoAndroid, Chrome, LinuxWhole system, kernel to appLab or targeted field traces
Android Studio ProfilerAndroidOne debuggable appDeveloper machine sessions
Xcode InstrumentsiOS and macOSApp and system templatesDeveloper machine sessions
Android vitalsAndroidAggregated quality metricsProduction user devices
Positioning of Perfetto among related tools

In practice, field metrics reveal that a problem exists, and Perfetto traces explain why it happens. Mature teams connect the two by reproducing a vitals regression in the lab and capturing a focused trace.

Practical pitfalls when tracing

Trace quality depends heavily on setup discipline. Misleading traces usually come from configuration mistakes rather than from the tool itself.

  • Debug builds distort timing: profile a release-like build with R8 and Baseline Profiles applied.
  • Too small a buffer loses data: events overwritten in ring buffer mode leave gaps at the critical moment.
  • Tracing has overhead: heavy ftrace categories or high sampling rates can change the behavior being measured.
  • Missing app slices: without custom trace sections or the Track Event SDK, app code appears as anonymous main-thread time.
  • Thermal state matters: a warm device throttles the CPU and produces slower, unrepresentative results.

The simple explanation

Perfetto works like a flight recorder for a phone. While an app runs, it writes down what every part of the system was doing, moment by moment, including the processor, the screen drawing and the memory.

Afterward, engineers replay the recording on a detailed timeline. When an app stutters or starts slowly, they can see exactly which task was waiting and why. Instead of guessing, they fix the real cause.

How it is used in practice

A team maintaining a shopping app sees in Android vitals that cold startup time on mid-range phones has risen from about 1.1 seconds to 1.8 seconds after a release. An engineer reproduces the launch on a comparable device, records a Perfetto trace with scheduling, atrace and CPU frequency enabled, and opens it in the Perfetto UI.

The timeline shows the main thread blocked for roughly 400 milliseconds on disk reads while a newly added analytics library loads its configuration during application creation. A PerfettoSQL query confirms the same pattern across ten repeated traces. The team moves the initialization to a background thread and defers it until after the first frame, and the next release brings startup back to about 1.1 seconds, which the lab traces and field metrics both confirm.