|
| 1 | +# Perfetto profiling on Android |
| 2 | + |
| 3 | +This document describes how continuous profiling works on Android when the SDK |
| 4 | +captures traces through the OS-level [`android.os.ProfilingManager`](https://developer.android.com/reference/android/os/ProfilingManager) |
| 5 | +API (available on API 35+), and how a captured **profile chunk** flows all the way |
| 6 | +from the device to a downloadable profile in Sentry. |
| 7 | + |
| 8 | +## What Perfetto is |
| 9 | + |
| 10 | +[Perfetto](https://perfetto.dev/) is Google's tracing framework for Android and Linux, and |
| 11 | +the tooling Android itself is instrumented with. Its |
| 12 | +[callstack sampler](https://perfetto.dev/docs/getting-started/cpu-profiling) interrupts the |
| 13 | +app at a fixed frequency, records the native and Java call stacks of the running threads, |
| 14 | +and writes them to a binary `.pftrace` file (a serialized |
| 15 | +[Perfetto protobuf](https://perfetto.dev/docs/reference/trace-packet-proto)). |
| 16 | +Starting with Android 15, apps can request such traces at |
| 17 | +runtime via `ProfilingManager` without root or `adb`, which is what makes on-device |
| 18 | +continuous profiling possible. |
| 19 | + |
| 20 | +Useful Perfetto references: |
| 21 | + |
| 22 | +- Perfetto docs: https://perfetto.dev/docs/ |
| 23 | +- CPU profiling with Perfetto: https://perfetto.dev/docs/getting-started/cpu-profiling |
| 24 | +- Trace format (`TracePacket` proto): https://perfetto.dev/docs/reference/trace-packet-proto |
| 25 | +- Perfetto UI (to open a downloaded `.pftrace`): https://ui.perfetto.dev/ |
| 26 | + |
| 27 | +## Pipeline overview |
| 28 | + |
| 29 | +Profile chunks travel the standard ingestion path described in |
| 30 | +[general/pipeline.md](../../general/pipeline.md) — SDK envelope, |
| 31 | +[Relay](https://develop.sentry.dev/ingestion/relay/) (Sentry's ingestion proxy), Kafka, a |
| 32 | +monolith processing task, then storage and a read API. Read that first; the rest of this |
| 33 | +document covers only where Perfetto deviates from it. |
| 34 | + |
| 35 | +The deviations are: |
| 36 | + |
| 37 | +- The envelope item carries **JSON and raw binary in one payload**, subdivided by a |
| 38 | + `meta_length` header rather than base64-encoding the trace ([details](#envelope-format-and-the-meta_length-header)). |
| 39 | +- Relay **converts** the Perfetto trace into the existing Sample v2 profile format, and |
| 40 | + additionally **keeps the raw `.pftrace`** in the object store so it can be downloaded |
| 41 | + later ([details](#relay-getsentryrelay)). |
| 42 | + |
| 43 | +```mermaid |
| 44 | +flowchart TD |
| 45 | + subgraph device["Android device — sentry-java"] |
| 46 | + PM[android.os.ProfilingManager] |
| 47 | + PP[PerfettoProfiler] |
| 48 | + PCP[PerfettoContinuousProfiler] |
| 49 | + PC[ProfileChunk] |
| 50 | + ENV["Envelope item<br/>[JSON metadata][raw .pftrace]<br/>header: meta_length"] |
| 51 | + PM --> PP --> PCP --> PC --> ENV |
| 52 | + end |
| 53 | +
|
| 54 | + subgraph relay["Relay (processing mode)"] |
| 55 | + SPLIT[Split payload at meta_length] |
| 56 | + CONV[Convert Perfetto → Sample v2] |
| 57 | + OS1[Upload raw .pftrace to object store] |
| 58 | + KAFKA[["Kafka topic: profiles<br/>ProfileChunkKafkaMessage<br/>(Sample v2 + attachment stored_id)"]] |
| 59 | + SPLIT --> CONV --> KAFKA |
| 60 | + SPLIT --> OS1 |
| 61 | + end |
| 62 | +
|
| 63 | + subgraph monolith["Monolith — getsentry/sentry"] |
| 64 | + TASK[process_profile_task] |
| 65 | + SYM[Symbolicate / deobfuscate] |
| 66 | + VR[vroomrs: parse + normalize] |
| 67 | + OS2[(Object store)] |
| 68 | + SNUBA[(Snuba: function metrics)] |
| 69 | + DB[(ProfileChunkAttachment row)] |
| 70 | + TASK --> SYM --> VR |
| 71 | + VR --> OS2 |
| 72 | + VR --> SNUBA |
| 73 | + TASK --> DB |
| 74 | + end |
| 75 | +
|
| 76 | + ENV -->|envelope| relay |
| 77 | + KAFKA --> TASK |
| 78 | + OS1 -.stored_id.-> DB |
| 79 | + VROOM[getsentry/vroom<br/>serve + merge flamegraphs] |
| 80 | + OS2 --> VROOM |
| 81 | + SNUBA --> VROOM |
| 82 | +``` |
| 83 | + |
| 84 | +## SDK (getsentry/sentry-java) |
| 85 | + |
| 86 | +On API 35+, [`AndroidOptionsInitializer`](../../../sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java) |
| 87 | +wires up `PerfettoContinuousProfiler` automatically. On older devices the SDK falls back |
| 88 | +to the legacy `Debug`-based [`AndroidContinuousProfiler`](../../../sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java), |
| 89 | +gated by the `enableLegacyProfiling` option (manifest key |
| 90 | +`io.sentry.profiling.enable-legacy-profiling`, defaults to `true`). Only **continuous |
| 91 | +profiling** is supported on the Perfetto path — transaction-based and app-start profiling |
| 92 | +are not. |
| 93 | + |
| 94 | +### Capturing chunks |
| 95 | + |
| 96 | +Continuous profiling emits a stream of independent [`ProfileChunk`](../../../sentry/src/main/java/io/sentry/ProfileChunk.java)s |
| 97 | +rather than one profile per transaction. `PerfettoContinuousProfiler` drives a chained |
| 98 | +loop: each chunk runs for `MAX_CHUNK_DURATION_MILLIS` (60s) via `PerfettoProfiler`, which |
| 99 | +calls `ProfilingManager.requestProfiling(PROFILING_TYPE_STACK_SAMPLING, …)` at |
| 100 | +`PROFILING_FREQUENCY_HZ` (101 Hz). When a chunk's trace file is ready, a new chunk starts, |
| 101 | +so profiling runs continuously. |
| 102 | + |
| 103 | +A chunk keeps a stable `profilerId` across the session and a per-chunk `chunkId`. When the |
| 104 | +OS produces the trace file, the profiler builds a `ProfileChunk` tagged with the Perfetto |
| 105 | +content type: |
| 106 | + |
| 107 | +```kotlin |
| 108 | +ProfileChunk.Builder(profilerId, chunkId, measurements, traceFile, timestamp, ProfileChunk.PLATFORM_ANDROID) |
| 109 | + .setContentType(ProfileChunk.CONTENT_TYPE_PERFETTO) // "application/x-perfetto-trace" |
| 110 | + .build() |
| 111 | +``` |
| 112 | + |
| 113 | +The chunk is captured via `scopes.captureProfileChunk(...)` and sent as its own envelope |
| 114 | +with item type [`SentryItemType.ProfileChunk`](../../../sentry/src/main/java/io/sentry/SentryItemType.java) |
| 115 | +(wire name `profile_chunk`). |
| 116 | + |
| 117 | +### Envelope format and the `meta_length` header |
| 118 | + |
| 119 | +A legacy chunk base64-encodes its trace into the `ProfileChunk` JSON. A Perfetto chunk is |
| 120 | +much larger, so [`SentryClient`](../../../sentry/src/main/java/io/sentry/SentryClient.java) instead |
| 121 | +routes it through the new `SentryEnvelopeItem.fromPerfettoProfileChunk(...)` factory, which |
| 122 | +avoids base64 by sending the raw binary alongside the JSON. |
| 123 | + |
| 124 | +The trick is a single envelope **item** whose payload concatenates the JSON metadata and |
| 125 | +the raw `.pftrace` bytes with **no delimiter**: |
| 126 | + |
| 127 | +```text |
| 128 | +[ProfileChunk JSON bytes][raw .pftrace binary bytes] |
| 129 | +``` |
| 130 | + |
| 131 | +A new `meta_length` property on the [envelope item header](../../../sentry/src/main/java/io/sentry/SentryEnvelopeItemHeader.java) |
| 132 | +tells the server where the JSON ends and the binary begins. The standard envelope item |
| 133 | +structure (header line + newline + payload) is unchanged; `meta_length` simply subdivides |
| 134 | +the payload: |
| 135 | + |
| 136 | +```text |
| 137 | +{"type":"profile_chunk","content_type":"application/x-perfetto-trace","filename":"…","length":<total>,"meta_length":<json bytes>} |
| 138 | +<ProfileChunk JSON><raw perfetto binary> |
| 139 | +``` |
| 140 | + |
| 141 | +- `length` — total payload size (JSON + binary), as for any envelope item. |
| 142 | +- `meta_length` — byte length of the JSON prefix. It is only known after the payload is |
| 143 | + serialized, so the header computes it lazily (via a `Callable<Integer>`) and omits the |
| 144 | + field entirely for non-Perfetto items, keeping the change backward compatible. |
| 145 | + |
| 146 | +## Relay (getsentry/relay) |
| 147 | + |
| 148 | +In processing mode Relay: |
| 149 | + |
| 150 | +1. **Splits** the compound item payload at `meta_length` into `(metadata JSON, raw profile)` |
| 151 | + and reads `content_type: "perfetto"` from the metadata. |
| 152 | +2. **Converts** the binary Perfetto trace into the existing **Sample v2** profile JSON |
| 153 | + format (`relay_profiling::expand_perfetto(...)`, backed by a checked-in subset of the |
| 154 | + Perfetto protobuf definitions). |
| 155 | +3. **Uploads** the raw `.pftrace` blob to object store (usecase `profiles`, keyed per |
| 156 | + org/project, with an attachment-retention TTL). |
| 157 | +4. **Produces** a `ProfileChunkKafkaMessage` to the `profiles` Kafka topic. The message |
| 158 | + carries the expanded Sample v2 JSON as `payload` plus an `attachments` array, where each |
| 159 | + attachment records: |
| 160 | + - `name` (e.g. `profile.perfetto`), |
| 161 | + - `content_type` (e.g. `application/x-perfetto-trace`), |
| 162 | + - `stored_id` — the object store key of the uploaded raw blob. |
| 163 | + |
| 164 | +```json |
| 165 | +{ |
| 166 | + "organization_id": 1, |
| 167 | + "project_id": 42, |
| 168 | + "received": 1720000000, |
| 169 | + "retention_days": 30, |
| 170 | + "payload": "<expanded Sample v2 profile JSON>", |
| 171 | + "attachments": [ |
| 172 | + { |
| 173 | + "name": "profile.perfetto", |
| 174 | + "content_type": "application/x-perfetto-trace", |
| 175 | + "stored_id": "<object store key of the raw .pftrace blob>" |
| 176 | + } |
| 177 | + ] |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +The monolith later uses `stored_id` to fetch the raw trace back. |
| 182 | + |
| 183 | +## Monolith (getsentry/sentry) |
| 184 | + |
| 185 | +`process_profile_task` (in `src/sentry/profiles/task.py`) consumes the `profiles` topic. |
| 186 | +Because Relay already converted the trace to Sample v2, the task treats a Perfetto chunk |
| 187 | +like any other: deobfuscate, hand it to `vroomrs` to parse and normalize |
| 188 | +(`vroomrs.profile_chunk_from_json_str(...)`), compress and store it, and emit function |
| 189 | +metrics to Snuba. |
| 190 | + |
| 191 | +The Perfetto-specific step is the last one: for each attachment on the message the task |
| 192 | +persists a lightweight **`ProfileChunkAttachment`** row — `project_id`, `profiler_id`, |
| 193 | +`chunk_id`, `name`, `content_type`, and the `stored_id` object store key. The row exists so |
| 194 | +the raw trace can be downloaded by ID without exposing the `stored_id`. |
| 195 | + |
| 196 | +Flamegraphs themselves are served by `getsentry/vroom`, which reads the stored chunks and |
| 197 | +the Snuba-indexed metadata and merges several chunks into one flamegraph. The endpoint |
| 198 | +lives in the monolith and passes the request through. |
| 199 | + |
| 200 | +### Perfetto format dispatch (vroom / vroomrs) |
| 201 | + |
| 202 | +Older Android SDKs emit the legacy Android trace format tagged as a "faulty" `version=2`, |
| 203 | +and the pipeline historically keyed off the platform rather than the version. To |
| 204 | +distinguish legacy from Sample v2 chunks, `ProfileChunk` carries a dedicated `version` |
| 205 | +field, and both `vroom` and `vroomrs` now dispatch on it instead of the platform: |
| 206 | + |
| 207 | +- Version `""` or `2.android-trace` → legacy Android trace format. |
| 208 | +- Any other version → Sample v2. |
| 209 | + |
| 210 | +## Downloading a Perfetto profile |
| 211 | + |
| 212 | +The monolith exposes two feature-gated endpoints: |
| 213 | + |
| 214 | +- **List attachments** — `GET /organizations/{org}/profiling/chunk-attachments/` |
| 215 | + (`sentry-api-0-organization-profiling-chunk-attachments`). Requires a `project` and |
| 216 | + `profiler_id`; resolves the visible `chunk_id`s (same logic as the flamegraph) and returns |
| 217 | + the matching `ProfileChunkAttachment` metadata. |
| 218 | +- **Download** — `GET /projects/{org}/{project}/profiling/chunks/{profiler_id}/{chunk_id}/attachments/{attachment_id}/?download` |
| 219 | + (`sentry-api-0-project-profiling-chunk-attachment`). The `?download` param is required; it |
| 220 | + streams the raw blob back from object store via the stored `stored_id`. Access requires |
| 221 | + the org's configured attachments role, analogous to generic event attachments. |
| 222 | + |
| 223 | +In the flamegraph UI, a toolbar button (added for continuous profiles when the feature is |
| 224 | +enabled and at least one attachment exists) lists and provides a way to download these traces. |
| 225 | + |
| 226 | +## References |
| 227 | + |
| 228 | +- SDK: [sentry-java#5251](https://github.com/getsentry/sentry-java/pull/5251) — Android `ProfilingManager` (Perfetto) support |
| 229 | +- Relay: [#5659](https://github.com/getsentry/relay/pull/5659), [#5932](https://github.com/getsentry/relay/pull/5932), [#6099](https://github.com/getsentry/relay/pull/6099), [#6102](https://github.com/getsentry/relay/pull/6102) — Perfetto parsing, pipeline, and object-store routing |
| 230 | +- vroom: [#672](https://github.com/getsentry/vroom/pull/672) — version dispatch for Android trace profiles |
| 231 | +- vroomrs: [#93](https://github.com/getsentry/vroomrs/pull/93) — accept Android profiles in Sample v2 format |
| 232 | +- Monolith: [sentry#118029](https://github.com/getsentry/sentry/pull/118029) (chunk attachments + endpoints), [sentry#118071](https://github.com/getsentry/sentry/pull/118071) (flamegraph download button) |
0 commit comments