Adit
BLR Droid · Android performance & tooling

From Trace to Triage

Reading Perfetto, then handing it to an agent.

Adit Lal, CTO & co-founder, Travv.world Rivu Chakraborty, Android & Kotlin GDE, Sarvam ~40 min
Adit
Why now

More code ships than anyone reads.

Agents write a growing share of it. A slow frame doesn't fail a test, so it ships. The fix is boring: measure on a schedule.

Adit
What it is

Perfetto is a dashcam for your phone.

Press record, use the app, stop. Then scrub back to the moment it felt slow and see what every thread was doing.

The Perfetto UI with a trace open: one row per CPU core, coloured by which thread was running, with frequency tracks below.

Open source, from Google. The recorder already ships on your phone.

Adit
What you can ask it

Three complaints your users already have.

  • "It stutters when I scroll." That is jank.
  • "It takes ages to open." That is cold start.
  • "It froze for a second." That is a blocked main thread.

A profiler only sees your code. Logcat only prints what you logged. Perfetto records the whole system.

Adit
Compose, XML, Flutter, React Native

Same OS underneath.

  • Every app forks a process, runs Application.onCreate, hands buffers to SurfaceFlinger and has to hit vsync.
  • Startup, frame deadlines, binder and thread state read the same in all of them.
  • Slice names differ, so tracing a finding back to your code is the framework-specific part.
Adit
Four words for the next 40 minutes

One picture teaches all four.

TRACE: one .pftrace file, everything below is inside it SLICE: one named span, start + duration Main Thread doFrame binder_transact RenderThread DrawFrame TRACK 0ms 40ms TIMELINE: every track, same clock. Scroll to zoom, drag to pan.
Adit
Capture → load → read

One command in, one screen to learn.

# you run this one line: scripts/capture.sh com.example.app # what it does for you, in order: 1. writes a trace config pointed at your app (+ system_server, so binder calls and SurfaceFlinger show up too) 2. sends it to the device (adb shell perfetto ... -d). Recording starts NOW 3. force-stops your app, then relaunches it: a cold start, captured live 4. pulls the finished file back: ./traces/<pkg>-startup-<time>.pftrace # a flow you drive yourself: start, use the app, stop scripts/capture.sh com.example.app --start scripts/capture.sh com.example.app --stop # Google ships the same kind of wrapper: python3 record_android_trace -o trace.perfetto-trace -t 10s -b 32mb sched gfx view # perfetto.dev/docs/getting-started/system-tracing
Adit
Step 2: a trace, loaded

CPU Scheduling, CPU Frequency and the app's own launch, all on one clock.

Perfetto UI with a cold-start trace loaded: CPU Scheduling, CPU Frequency, Android App Startups tracks visible
Adit
The first wall

Hundreds of processes in one trace. Start with three tracks.

  • Find your app's process and pin its main thread.
  • Pin RenderThread.
  • Pin the frame timeline: Expected and Actual.

Then read slice names. Grep one in your code. No match means it's a framework span, and that tells you something too.

Adit
Part 2: a real app

An app I built, and ship.

Compose Multiplatform. A release build, on a phone from 2017 that runs Android 10. Debug builds run 2 to 5x slower. A release build can still mislead: check what it ran.

Three things we'll measure: what it draws when nobody touches it, cold start, and binder/IPC.

Adit
Metric 1: frames

603 frames in 10 seconds. Nobody touched the screen.

Frames drawn in 10 seconds with no input, one capture, three screens Home 603 frames A list screen 604 frames A browse screen: 13 frames while it loads, then none

An idle screen should draw nothing. Two of these draw 60 frames a second, and each frame costs about 11 ms on the RenderThread.

Adit
Metric 2: cold start

Every slice, ranked. The widest bar is where your time went.

bindApplication 414 ms Choreographer#doFrame 273 ms traversal 273 ms Startup 98 ms measure (3 calls) 95 ms AndroidOwner:onMeasure (3 calls) 95 ms Compose:recompose (2 calls) 68 ms binder transaction (174 calls) 62 ms

Release build, top 8 by total time (13_startup_top_slices). This run took 814 ms to first frame. The median of 10 is 684 ms.

Adit
Metric 3: binder / IPC

Queueing, not computing.

What the main thread was doing during its slow binder calls (total ms, this capture) S: sleeping, waiting for the reply 51 ms R: runnable, waiting for a CPU, 23 ms Running: 11 ms Main thread: 318 calls to system_server, 79 ms, worst single call 5.6 ms Release build. Source: 31_binder_why_slow.csv + 30_binder_main_thread.csv. Android 10 does not name the interface.

51 ms asleep against 11 ms running. The other process isn't slow. The main thread is parked, waiting for replies, 318 times.

Rivu
So how do you read this

Three findings. One method behind all of them.

This part works on any app.

Rivu
One annotated timeline

The method took 40ms. The code ran for 8.

0ms 40ms Running Sleeping, 32ms Running the method took 40ms; the code ran for 8
Rivu
The checklist

Four questions, in this order.

  • Is this trace trustworthy?
  • Was the main thread running or waiting?
  • Which deadline was missed, and by whom?
  • Where does cold start go?
Rivu
Question 1

Is this trace trustworthy?

  • Dropped data: the buffer overflowed mid-capture
  • Missing FrameTimeline: no jank attribution
  • Debuggable build: 2 to 5x slower, so the numbers are wrong
Rivu
Question 1, answered by the tool

The report opens with a verdict.

"health_status": { "trace_health": "pass", // 00_health: empty is the GOOD case "coverage": { "frametimeline": "missing", ... }, // a zero means "missing" "frames": "unsupported", // Android 10 records no frame deadlines "build_type": "release", // read from the trace, never assumed "startup_capture": "ok", // did a startup capture contain the launch? "verdict": "trusted" }

This is the app's cold-start capture. Trusted, and it says what it can't answer. unknown is never a pass. Below trusted, every finding is marked directional_only.

Rivu
Before you trust a number

The phone is measuring itself.

  • Tracing costs CPU while it runs. We treat anything under about 5% as noise.
  • Low memory or a hot device can drop data. Question 1 catches that.
  • OEMs differ. Keep one baseline device and use the others for deltas only.
  • Re-measure every couple of weeks, or after a big merge.
Rivu
Question 2: the one that changes the fix

Was the main thread running, or waiting?

Running means your code is slow. Waiting means it was blocked, and profiling your own code will find nothing.

Rivu
thread_state

Four states. Each implies a different fix.

Runningon CPU now
Runnableready, waiting for CPU
Sleepingblocked, interruptible
Duninterruptible I/O wait
Rivu
Live in Perfetto UI

Click the main thread track. Read left to right.

Perfetto UI with an app's main thread and RenderThread expanded, real capture

A real capture, with the app's name replaced. Go live only with a public trace.

Rivu
Main thread · cold start · 684 ms to first frame · release build, Android 10 phone

79% Running. This app is compute-bound.

MAIN THREAD, FROM LAUNCH TO FIRST FRAME Running 79% Not running 21% 79% Running: compute-bound median of 10 cold starts (range 58 to 81%) · not running = waiting for a CPU, asleep or blocked
Rivu
Question 3

Which deadline was missed, and by whom?

Every frame owns a deadline. Miss it, and Perfetto can tell you if it was the app or SurfaceFlinger.

Rivu
The budget depends on the screen

16.6ms is a 60Hz number. Name the refresh rate.

16.6ms60Hz
11.1ms90Hz
8.3ms120Hz
Rivu
FrameTimeline · expected vs actual

Frame 3 blew the deadline. FrameTimeline says it was the app.

16.6ms deadline 0 16.6 Frame 1 expected actual Frame 2 Frame 3 App Deadline Missed Frame 4 Frame 5

An illustration. The app in this talk ran on Android 10, which records no frame deadlines, so it has no chart like this one.

Rivu
Question 4

Where does cold start go?

Five phases from process fork to first frame.

TTID is the time to the first drawn frame. Even an empty loading screen counts, so it can be gamed. TTFD is the time to the frame after reportFullyDrawn(). The app has to say "the content is on screen now", so it is harder to fake.

Rivu
Startup breakdown · release build, Android 10 phone

TTID: 684 ms. TTFD never fires, because reportFullyDrawn() is never called.

0 ms first frame fork bindApplication, about half activity first frame, about a third TTID 684 ms, median of 10 TTFD never fires reportFullyDrawn() never called

Shares from one run: bindApplication 414 ms and the first frame 273 ms of 814 ms. The two small blocks are not measured separately.

Rivu
JIT · cold start · same APK, 10 runs against 10

speed-profile is a label. It isn't proof.

The phone said speed-profile, and the JIT still ran for 2 seconds. The profile wasn't stale. It had never been applied.

Median of 10As installedProfile appliedChangeVerdict
JIT compile2,169 ms118 ms-95%clear
Cold start TTID674 ms487 ms-28%clear
Main thread running80%50%-37%clear
Main-thread binder120 ms128 ms+7%noise

Installed with adb, the label is set before the app first runs. The profile is copied in on first launch and only counts after the next compile. Launch once, send the INSTALL_PROFILE broadcast, compile, then measure. The label read speed-profile on both sides.

Rivu
Perfetto or Macrobenchmark?

Use both.

Macrobenchmark
  • Runs a scenario many times
  • Gives you a number you can track
  • Belongs in CI and before/after checks
Perfetto
  • Records what every thread did
  • Shows where the time went
  • Belongs in the first look and the why

Macrobenchmark saves a Perfetto trace for every iteration, so one run gives you both.

Adit
The turn

That was a checklist.
Checklists are transferable.

Watch an agent run the same one.

Adit
Encoded, next

One skill, two phases.

Phase 1: Triage

Show the issue. The four questions, as SQL, ranked worst-first.

SKILL.md steps 0 to 4: capture, triage, map slices to code

Phase 2: Fix

Baseline, change one thing, re-measure, prove it.

SKILL.md steps 5 and 6: fix one thing, re-measure

Adit
Triage, on the capture

One command. Trace in, ranked findings out.

$ python3 scripts/triage.py \ com.example.app-startup-141631.pftrace \ --pkg com.example.app --app-repo ~/code/app --out triage-out # parses once into trace_processor # runs 00_health .. 42_frame_phases (21 queries) # writes report.md, findings.json and report.html, which opens by itself # with --app-repo: fix_plan.md and code_audit.md, what to change and where # the report opens with a trust verdict # each finding names the query it was measured in
Adit
Triage output · report.md

Findings, worst first. Each with a next action.

  • 🔴 high Cold start TTID is 678 ms
  • 🟠 medium The JIT compiler ran for 2,094 ms, 18% of the capture
  • 🟠 medium The app drew 539 frames after launch with no input
  • 🟠 medium Main-thread binder: calls to system_server cost 99 ms
  • 🟠 medium The app has no trace sections of its own
  • 🔵 info App never calls reportFullyDrawn()
  • 🔵 info Late frames cannot be measured on this phone (Android 10)

The top of the list from one cold start. The same run also writes fix_plan.md: these, turned into changes to review, in order.

Adit
Triage output · report.html

It also writes a page.

Two panels from the skill's report.html: the main thread's states as one coloured strip, and the ten worst frames as bars measured from their deadline

The skill drew this page. The charts are measured. Each What it means line is the skill's own reading of them, so check it like you would a colleague's.

Thread states and frames keep Perfetto's colours. Sample: the repo's debug-build fixture. Open it.

Adit
Triage: the catch

Most hot slices are framework spans.

# triage.py --app-repo checks every hot slice name against the source: Checked 25 slice names against 1,136 source files. None is named after a function or screen in the app, and the app declares no trace sections. # bindApplication, traversal, DrawFrame, Recomposer:animation ... # the app caused every one of them. It wrote none of them.

So the skill reads the code instead: the Application class, the launcher Activity, the first screen. It looks there for causes that match the findings, and lists them with file and line.

Adit
While we built this talk

Our own skill misled us five times.

What happenedWhat it said thenWhat it says now
Captured a debug build by mistake9 confident findings, no warningcapture refuses · report: directional_only
Capture missed the app launchno startup findings, which reads as "fine"blocker: startup capture missed the launch
Empty health vs zero coverageboth look like "no data"explicit pass / missing
Captured on an Android 10 phone528 frames, 100% on time, "trusted"late frames: not measured, and why
Installed the build with adbspeed-profile, so the profile is stalethe label is not proof: apply it, then measure

The first three are checks in the repo that replay the original data through today's code. Two pass. The third needs a debug capture we can publish. The last two are pinned by tests.

Adit
What it says now

Bad capture? It says so first.

Report section 1, Can this trace be trusted: Not trustworthy. Trace health fail, debuggable build, startup capture missed the launch. Slices, sched, frame timeline and binder recorded; blocked reasons missing.
A real run of my own app: not trustworthy.
Report section 5, What to fix, in order: fix the capture first. Record again without dropped data, capture a build that is not debuggable, capture the launch again. Each says where and how to check.
Fix plan: the capture comes before any code.
Rivu
Fix, step 1

Record the baseline before you touch anything.

684 mscold start, to first frame
104 msmain-thread binder
2.1 sJIT, in a 12 s capture

Medians of 10 cold starts. Release build, compiled speed-profile, same phone. Keep the before APK: the next build overwrites it.

Rivu
Fix, step 2

Change one thing.

The code audit read the startup path and pointed at notification channel set-up: 1 delete and 8 creates on every launch, on the main thread. Nine binder calls. The change: do it once per app version. Nothing else changed.

Rivu
Fix, step 3 · 10 runs against 10

Re-measured. It was noise.

Median of 10Before (range)After (range)ChangeVerdict
Cold start TTID683.7 ms (660 to 835)673.6 ms (651 to 683)-1%noise
Main-thread binder104.0 ms (95 to 172)119.6 ms (100 to 153)+15%noise
JIT compile2,115 ms2,169 ms+3%noise

Nine calls out of about 320. It was never going to clear a spread of 95 to 172 ms. The comparison said so before anyone called it a win.

Rivu
Beyond cold start: 3 screens, one capture

Tell it a screen. The agent drives, traces, and triages.

capture.sh --start → open a screen, wait 10 s, touch nothing → capture.sh --stop → triage.py
ScreenFrames in 10 s, untouchedReading
Home603keeps drawing
A list screen604keeps drawing
A browse screen13one burst while it loads, then idle

All three share the app's shell: the bottom bar, the scaffold. So it isn't the shell. It is something the first two screens have and the third doesn't.

Adit
The clue in the trace

It isn't recomposing. It's drawing.

539frames after launch
538with Compose's frame clock
28recompositions

An animated value that is read in composition recomposes on every frame. This one doesn't. So it is read in a draw block, and composition tracing can't name it. The way in is to switch one suspect off and count frames.

Where it stands: found, measured, narrowed to two screens. Not fixed yet. The check is one number: 603 frames, down to about 13.

Adit
Take these home

Perfetto skills you can install today.

Ours
  • perfetto-triage · aldefy/perfetto-android-skills
  • 21 queries for startup, frames and binder
  • Tells you first if the capture can be trusted
  • Draws the findings as a page with charts
  • Reads the code that ran, and lists fixes with file and line
  • Start and stop a capture around any flow
  • Fix one thing, then measure again
Others
  • Perfetto AI skill · google/perfetto, ai/skills. Any trace question. Strong on memory and GPU.
  • Android profiler skill · android/skills. Records, then follows the blocking chain.
  • premex-ab/claude-marketplace. Android skills as plugins.
Adit
The boundary

What automation can't do here.

Can't
  • Know which composable never stops animating. It counted 603 idle frames and narrowed them to two screens. Someone still has to read that code and switch suspects off
  • Compare across builds or devices
  • Find a bug a careful human wouldn't
Good at
  • Ranking by self time
  • Refusing a debug build, and saying what a phone can't measure
  • Median of 10, and calling noise noise
Adit
Query pack · prompt · repo

trace-to-triage.pages.dev

Everything in this talk, runnable today.

github.com/aldefy/perfetto-android-skills (the skill, installable)

Adit
Before you go build an agent for this

The skill is where you start.

Don't outsource judgment about your own codebase to a tool that has never seen it. Read enough traces yourself that you can tell when the agent's ranking is wrong. That is how it gets better.

Adit

Questions.

Adit

Thank you.

Adit Lal, CTO & co-founder, Travv.world
Rivu Chakraborty, Android & Kotlin GDE, Sarvam

Adit
Backup · Step 1: open ui.perfetto.dev

No install. No account. Just a browser tab.

ui.perfetto.dev landing page: Open trace file, Record new trace, keyboard shortcuts
Adit
Backup · What "app startup time" means in Perfetto

android_startups is a view. It reads atrace events after the fact.

# atrace = Android's userspace tracing tags. Enabling category "am" tells # the OS "label ActivityManager events (app launch, etc) as they happen". # it's what gives raw scheduling data a human-readable NAME. # the PerfettoSQL behind "check startup time": INCLUDE PERFETTO MODULE android.startup.startups; INCLUDE PERFETTO MODULE android.startup.time_to_display; SELECT startup_id, package, ts, dur FROM android_startups; SELECT * FROM android_startup_time_to_display; # TTID = time to first RenderThread.DrawFrame (first pixel drawn) # TTFD = time to the next DrawFrame after reportFullyDrawn() is called # capture.sh's sequence, fixed: sleep 1 → sleep 3 adb shell perfetto -c - -o "$DEV" -d # tracing starts sleep 3 # was 1: too tight, lost the race adb shell am force-stop "$PKG" adb shell monkey -p "$PKG" -c android.intent.category.LAUNCHER 1
Adit
Backup · Trace vs profiler

One timeline. Kernel, framework, SurfaceFlinger, binder.

A profiler tells you your function was slow. A trace tells you why the thread wasn't running at all.

Rivu
Backup · Two checks, run before trusting anything else

Two queries. One asks "did anything break?" The other asks "is the data there?"

00_health.sql: PASS

Checks trace_processor's own error/data-loss stats. Any row here means the buffer dropped data or a source failed, and every number downstream is wrong.

-- our result: (no rows) -- zero errors, zero data loss. Empty = good, here.
01_coverage.sql: PASS

Counts rows from every data source the trace needs. A 0 in any column means that whole CLASS of question is unanswerable with this capture.

slices: 174,960 sched_rows: 53,823 frametimeline_rows: 898 blocked_reason_rows: 4,794 binder_slices: 3,118 processes: 823
Rivu
Backup · jank_type

UI thread built it. RenderThread drew it.

UI thread

Layout, measure, draw commands. Slow here = your view hierarchy or your code.

RenderThread

Actual GL/Vulkan submission. Slow here = shader compiles, GPU work, driver.

Rivu
Backup · The seam

Showing the issue and fixing it are two different jobs.

The fix phase starts here, with a baseline.

Rivu
Backup · Someone else's codebase

It ran first try. Then it filed four bugs.

What worked
  • Capture, triage and report on the first attempt
  • It refused a debuggable build until told to go ahead
  • The report led with its verdict: directional only
What it reported
  • A "medium" finding on a trace with 0.3% janky frames
  • Phone on USB and Wi-Fi: adb saw two devices and gave up
  • adb pull progress spilling into the output
  • "No rows" where it meant "not applicable"

One run on a debuggable build. It shows the tool runs, nothing more.

Adit
Backup · Where the human stands

The checklist scales. Judgment about your codebase doesn't.

That is the division of labour.

P
1
2
3
4
A
1 / 48