Title: Add Perfetto trace output to profiling.sampling · Issue #150327 · python/cpython · GitHub
Open Graph Title: Add Perfetto trace output to profiling.sampling · Issue #150327 · python/cpython
X Title: Add Perfetto trace output to profiling.sampling · Issue #150327 · python/cpython
Description: Feature or enhancement Proposal: Feature Add a Perfetto trace output backend to profiling.sampling (target: 3.16), alongside the existing other output formats. This would let users open Python sampling profiles directly in the Perfetto U...
Open Graph Description: Feature or enhancement Proposal: Feature Add a Perfetto trace output backend to profiling.sampling (target: 3.16), alongside the existing other output formats. This would let users open Python samp...
X Description: Feature or enhancement Proposal: Feature Add a Perfetto trace output backend to profiling.sampling (target: 3.16), alongside the existing other output formats. This would let users open Python samp...
Opengraph URL: https://github.com/python/cpython/issues/150327
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add Perfetto trace output to profiling.sampling","articleBody":"# Feature or enhancement\n\n### Proposal:\n\n## Feature\n\nAdd a Perfetto trace output backend to `profiling.sampling` (target: 3.16), alongside the existing other output formats. This would let users open Python sampling profiles directly in the [Perfetto UI](https://ui.perfetto.dev) and analyse them with PerfettoSQL in the [trace processor](https://perfetto.dev/docs/analysis/trace-processor).\n\nOpening this for design sanity-check before sending a PR, as suggested by @pablogsal in offline discussion. The most load-bearing question (how to emit protobuf without a runtime dependency) is covered below.\n\n## Motivation\n\n- Perfetto is a trace viewer oriented systems-perf work, and being able to view Python samples there means a Python profile can sit on the same timeline as scheduler events, native CPU samples, and application instrumentation rather than living in a separate tool.\n- Once trace processor can read the output, users get SQL-driven analysis of Python samples (top-N functions, per-thread breakdowns, custom aggregations).\n\n## Proposed design\n\n### Output format\n\nEmit a Perfetto trace (`perfetto.protos.Trace`) containing:\n- A `ProcessDescriptor` / `ThreadDescriptor` per observed process/thread.\n- `InternedData` carrying `Frame`, `Mapping`, and `Callstack` entries\n- One `TracePacket` per sample containing a `StackSample` message that references the interned callstack.\n\n`StackSample` is part of a **new set of public profiling protos** I'm landing in Perfetto specifically so that producers like this one have a stable, transport-neutral surface to target (rather than reusing `PerfSample`, which is shaped by `perf_event_open` and leaks producer diagnostics into the data). The RFC is at https://github.com/google/perfetto/discussions/6027.\n\nConcretely, each Python sample maps to roughly:\n\n```\nTracePacket {\n timestamp: \u003cns>\n trusted_packet_sequence_id: \u003cseq>\n interned_data { ... } // first packet only, or as new frames appear\n stack_sample {\n task_context_iid: \u003cthread>\n execution_context_iid: \u003ccpu/mode> // optional\n callstack_iid: \u003cinterned callstack>\n primary_descriptor_iid: \u003c\"wall_time_ns\" counter>\n primary_weight: \u003cns since last sample>\n }\n}\n```\n### Proto serialisation without a runtime dependency\n\nThe stdlib can't depend on `protobuf`, so I'd hand-roll the wire format for the specific message types we emit. This is tractable because:\n\n- **Proto wire format is tiny.** It's varints + length-delimited + fixed32/64 + a tag byte per field. The whole encoder for the messages we need is on the order of a couple hundred lines of Python.\n- **We only encode, never decode.** Decoding protobuf is signifcantly harder than encode.\n- **The set of message types is small and stable.** I've specifically designated in the RFC upstream that these protos I'm adding are going to be \"eternally stabe\" protos which we won't change the wire format or semantics in a non-backwards compatible way.\n- **Precedent.** Perfetto itself ships an ad-hoc proto encoder/decoder (`protozero`) in C++ for similar reasons.\n\nSketch of the shape:\n\n```python\n# Hand-rolled, no runtime deps.\ndef _varint(buf, n): ...\ndef _tag(buf, field_no, wire_type): ...\ndef _string(buf, field_no, s): ...\ndef _message(buf, field_no, payload): ...\n\ndef encode_stack_sample(buf, sample):\n _tag(buf, 1, WIRE_VARINT); _varint(buf, sample.task_context_iid)\n _tag(buf, 6, WIRE_VARINT); _varint(buf, sample.callstack_iid)\n # ...\n```\n\nField numbers and wire types come straight from the `.proto` definitions in the Perfetto RFC. Any wire type constants would be inlined as Python integers.\n\n### CLI surface\n\nA new `--format perfetto` CLI flag to the profiling.sampling module in all the same places `--gecko` is allowed today.\n\n## Target\n\nPython 3.16.\n\n## Prerequisites\n\n- [Perfetto RFC-0027](https://github.com/google/perfetto/discussions/6027): public stack-sampling and heap-profiling protos. Required before this lands so we're targeting the stable protos.\n\ncc @pablogsal\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-154541\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/LalitMaganti","@type":"Person","name":"LalitMaganti"},"datePublished":"2026-05-23T23:18:08Z","url":"https://github.com/python/cpython/issues/150327"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d8a01e3b-1099-141c-63c0-244513a699c7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A01E:A70A9:1EE5A21:28AE7B3:6B023346 |
| html-safe-nonce | 1b9992514d85ee5ffadd6510a0ce81b6dfb940518027fc364ec1ef1c5596f8b2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMDFFOkE3MEE5OjFFRTVBMjE6MjhBRTdCMzo2QjAyMzM0NiIsInZpc2l0b3JfaWQiOiI1NDM5MzcwODI1NjkyMjMwNDcwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | ad79ca3fedcaa75fa2e0326109a6959d0133bb64273c93b9aa7df90a3ae6b2a1 |
| hovercard-subject-tag | issue:4509876655 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/150327/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8f524342d4c970d9bd94f17a5173522f388a95da33e64dfbee0e5acf7b0390f6/python/cpython/issues/150327 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8f524342d4c970d9bd94f17a5173522f388a95da33e64dfbee0e5acf7b0390f6/python/cpython/issues/150327 |
| og:image:alt | Feature or enhancement Proposal: Feature Add a Perfetto trace output backend to profiling.sampling (target: 3.16), alongside the existing other output formats. This would let users open Python samp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | LalitMaganti |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width