| name | perfetto-trace-analysis |
| description | Analyze Android performance traces using Perfetto/TraceProcessor. Supports Systrace, Perfetto traces, and Android Studio CPU/Memory profiles. Provides SQL-based trace analysis, flamegraph generation, and performance bottleneck identification. |
Perfetto Trace Analysis Skill
Overview
This skill enables deep analysis of Android performance traces using the Perfetto open-source trace analysis framework. It supports:
- Perfetto traces (.perfetto-trace, .ctrace)
- Systrace (.html, .json)
- Android Studio CPU profiles (.trace, .cpuprofile)
- Ftrace / atrace kernel traces
Core Concepts
TraceProcessor SQL Interface
Perfetto's TraceProcessor allows SQL queries against trace data:
SELECT process.name, thread.name, sum(dur)/1e9 as cpu_sec
FROM sched JOIN thread USING (utid) JOIN process USING (upid)
GROUP BY upid ORDER BY cpu_sec DESC LIMIT 20;
SELECT slice.name, ts, dur/1e6 as dur_ms
FROM slice JOIN track ON slice.track_id = track.id
WHERE track.name GLOB '*ui*'
AND dur > 16e6
ORDER BY dur DESC;
SELECT slice.name, count(*) as cnt, sum(dur)/1e6 as total_ms
FROM slice
WHERE slice.name LIKE '%lock%'
GROUP BY slice.name ORDER BY total_ms DESC;
SELECT slice.name, count(*) as allocs, sum(dur)/1e6 as total_ms
FROM slice
WHERE slice.name LIKE '%alloc%' OR slice.name LIKE '%malloc%'
GROUP BY slice.name ORDER BY total_ms DESC;
Key Tables
| Table | Description |
|---|
sched | CPU scheduling events |
slice | Trace events (begin/end) |
counter | Counter values (CPU freq, memory) |
thread_track | Thread-level tracks |
process_track | Process-level tracks |
actual_frame | Frame timing |
memory_snapshot | Heap snapshots |
heap_graph | Object graphs |
Analysis Dimensions
1. CPU Performance
- Per-thread CPU usage
- Scheduling latency (time from runnable to running)
- CPU frequency analysis
- Wakeup analysis
2. UI Performance (Jank Analysis)
- Frame timeline (expected vs actual)
- Choreographer frame drops
- Inflate/layout/measure/draw breakdown
- RecyclerView bind times
3. Memory Analysis
- Java heap growth patterns
- Native allocation patterns
- GC event frequency and duration
- OOM risk assessment
- Memory leak indicators
4. I/O Performance
- File read/write latency
- Disk I/O patterns
- SharedPreferences commit times
- Database query times
5. Network Performance
- Request latency breakdown
- Connection setup time
- TLS handshake duration
- Data transfer rates
6. Power/Battery
- Wakelock analysis
- Alarm frequency
- GPS usage patterns
- Network radio state transitions
Tools & Integration
trace_processor_shell
trace_processor_shell trace.perfetto-trace
trace_processor_shell -q query.sql trace.perfetto-trace
trace_processor_shell -q query.sql --json trace.perfetto-trace
Perfetto UI (ui.perfetto.dev)
- Upload traces for visual analysis
- Flamegraph views
- Counter tracks
- Slice analysis
Integration with AppSmartInspector
- Parse trace files and extract key metrics
- Generate structured analysis reports
- Identify top N bottlenecks
- Compare traces before/after optimization
- Automated regression detection
Analysis Workflow
- Collect trace -
adb shell perfetto -c - -o /data/misc/perfetto-traces/trace.pb
- Pull trace -
adb pull /data/misc/perfetto-traces/trace.pb
- Load into TraceProcessor - Run SQL queries
- Identify bottlenecks - Sort by impact (duration × frequency)
- Generate report - Structured markdown with metrics and recommendations
- Compare baselines - Track regressions across builds
Spec Document Template
When generating analysis specs, include:
- Executive Summary (top findings)
- Methodology (trace collection config)
- Detailed Analysis (per dimension)
- SQL Queries Used
- Metrics & Thresholds
- Recommendations (priority-sorted)
- Baseline for Regression Detection