Skip to content

Perfetto - System-Wide Tracing & Analysis Cheatsheet

Perfetto - System-Wide Tracing & Analysis Cheatsheet

Perfetto is Google’s open-source, production-grade tracing and profiling stack for Linux, Android, and Chrome. It captures system-wide traces — CPU scheduling, syscalls, frequency changes, memory events, and custom application events — into a compact protobuf format, then lets you explore them in a sophisticated web UI or, uniquely, query them with SQL. Where perf samples stacks, Perfetto records a timeline of what every thread and CPU was doing, which is what you need for latency and concurrency problems.

Installation

ComponentHow
UI (no install)Open ui.perfetto.dev in a browser
CLI toolsDownload tracebox/perfetto from GitHub Releases
AndroidBuilt in (adb shell perfetto ...)
trace_processorDownload for local SQL analysis
Verify./tracebox --help

Capturing a Trace (Linux)

# Simple 10-second system-wide trace with ftrace scheduling data
./tracebox -o trace.pftrace --txt -c - <<EOF
buffers: { size_kb: 65536 }
data_sources: {
  config {
    name: "linux.ftrace"
    ftrace_config {
      ftrace_events: "sched/sched_switch"
      ftrace_events: "sched/sched_wakeup"
      ftrace_events: "power/cpu_frequency"
    }
  }
}
duration_ms: 10000
EOF
ElementPurpose
buffersRing buffer size for trace data
data_sourcesWhat to record (ftrace, procstats, heapprofd, …)
ftrace_eventsSpecific kernel tracepoints
duration_msCapture length
-oOutput trace file

Common Data Sources

SourceRecords
linux.ftraceScheduling, syscalls, IRQs, custom tracepoints
linux.process_statsPer-process CPU/memory over time
linux.sys_statsSystem-wide counters (meminfo, vmstat)
android.heapprofdNative heap profiling
android.java_hprofJava heap dumps
track_eventApplication-emitted spans/counters

The UI Workflow

StepAction
1Open ui.perfetto.dev and drag in the trace
2Timeline shows CPUs, threads, and tracks
3W/S zoom, A/D pan
4Click a slice to see its duration and args
5Select a range → summary of what ran
6Use the query box for SQL analysis

SQL Analysis (the superpower)

Perfetto’s trace processor exposes the trace as queryable tables.

-- Longest-running slices
SELECT name, dur/1e6 AS ms
FROM slice
ORDER BY dur DESC
LIMIT 20;

-- CPU time per process
SELECT p.name, SUM(s.dur)/1e6 AS cpu_ms
FROM sched s JOIN thread t USING(utid) JOIN process p USING(upid)
GROUP BY p.name ORDER BY cpu_ms DESC;

-- Why was a thread blocked?
SELECT ts, dur, state
FROM thread_state
WHERE utid = 42 AND state != 'Running'
ORDER BY dur DESC;
TableContains
sliceNamed, nested duration events
schedScheduling slices per CPU
thread_stateRunning/sleeping/blocked intervals
counterNumeric time series (freq, memory)
process, threadProcess/thread metadata

Local Analysis

# Interactive SQL shell over a trace
./trace_processor trace.pftrace

# Run a query and emit JSON for scripting
./trace_processor -q query.sql --query-output json trace.pftrace

What Perfetto Is Best At

ProblemWhy Perfetto helps
Latency spikesSee exactly what ran/blocked at that moment
Lock contentionthread_state shows blocked intervals and reasons
CPU migration/schedulingPer-CPU timeline of every thread
Frequency/thermal effectsCorrelate cpufreq counters with slowdowns
App + kernel correlationApplication spans on the same timeline as kernel events
AspectPerfettoperfbpftrace
ModelFull timeline traceSampling profilerAd-hoc eBPF probes
AnalysisUI + SQLReports/flame graphsCustom scripts
Best forLatency/concurrency/timelineCPU hot pathsTargeted kernel questions
OverheadModerate (configurable)LowLow

Complements perf (CPU hot spots) and bpftrace (targeted probes); Perfetto answers “what happened when.”

Resources