perfetto icon indicating copy to clipboard operation
perfetto copied to clipboard

FR: allow including UI macros/startup commands in trace files

Open anakryiko opened this issue 8 months ago • 13 comments

It would be amazing to be able to produce Protobuf-based Perfetto trace, in which some of the tracks are actually derived using pre-defined SQL query based on data in another tracks.

E.g., having custom per-thread tracks where track events are tagged (using debug annotations) with associated CPU on which some event happened, it would be nice to have pre-defined per-CPU tracks that combine events across many threads/processes (or whatever custom track scope is) into a "per-CPU" tracks based on debug annotation with CPU number.

Of course, per-CPU is just a one straightforward use case, having full power of SQL at trace emitting stage would be amazingly powerful for producing all sorts of pre-canned views without bloating trace with duplicated data.

anakryiko avatar Apr 22 '25 18:04 anakryiko

Is this not just https://perfetto.dev/docs/analysis/debug-tracks?

LalitMaganti avatar Apr 22 '25 18:04 LalitMaganti

@LalitMaganti if I understand correctly, debug tracks can be only created from UI, manually, is that right? The feature request here is basically to be able to produce such debug tracks programmatically inside the trace file itself. But not through duplicating data, but rather deriving debug tracks with SQL, that will come together with protobuf trace file.

anakryiko avatar Apr 22 '25 18:04 anakryiko

This is an intentional design choice we made with Perfetto to separate the visualization of data from the contents of traces. As in, the visualization of traces are independent of how those traces are recorded/written.

This is a learning we took from chrome://tracing which baked the whole viewer itself into the trace but then made it impossible to actually develop due to needing to keep backcompat at the wrong level forever.

As such I don't think we'd want to support something something to the level of "bake SQL queries into the trace". Plugins, the newly added settings and a "initrc" for the UI are IMO far more "Perfetto" ways of dealing with your problem.

LalitMaganti avatar Apr 22 '25 18:04 LalitMaganti

@LalitMaganti I understand, thanks for entertaining the idea anyways!

Do you have some links for those added settings and "initrc" for the UI? I'd like to learn about them. Thanks!

anakryiko avatar Apr 22 '25 18:04 anakryiko

Do you have some links for those added settings and "initrc" for the UI?

Settings were added last week so are incredibly new. There's no link I can point you to beyond https://github.com/google/perfetto/commit/b1fc8d6adbb813b7f98b3eabcd6cf7485567a76b

initrc is not actually implemented (as it had a prerequisite on the settings page) but the idea is basically "a bunch of commands which run on startup and are user configurable" similar to a init.rc file in Linux/Android. The basic premise is that you could have a command which is "run all the queries and create debug tracks for the SQL stored in a given setting" and that would be executed on startup, setting up the UI in a user defined way without needing to write a formal plugin and/or bake anything into the file.

LalitMaganti avatar Apr 22 '25 19:04 LalitMaganti

@LalitMaganti oh, that's great! Yep, that initrc stuff is exactly what I'd like. Any ETAs for that?

anakryiko avatar Apr 22 '25 20:04 anakryiko

No formal timelines I'm afraid but I think @stevegolton might look at it at some point.

LalitMaganti avatar Apr 22 '25 20:04 LalitMaganti

It's definitely on the list. I think we need to work though some unanswered questions first about the syntax, but it's definitely planned.

stevegolton avatar Apr 26 '25 16:04 stevegolton

I'm going to start looking into this in the coming week.

LalitMaganti avatar Aug 19 '25 10:08 LalitMaganti

@LalitMaganti great, thank you, please keep us posted so we can try it when it's available!

anakryiko avatar Aug 19 '25 20:08 anakryiko

How we're planning on implementing this:

  1. UI macros (just being implemented in https://github.com/google/perfetto/pull/2649): a user defined setting which allows users to specify new "macros" which can be used to compose existing UI commands (including potentially other user defined macros; macros themselves become commands).
  2. A user defined setting to specify a list of commands to run on UI startup on every trace that user opens
  3. The ability to specify using URL parameters specific commands which should be run on startup (allowing customization on a per-trace basis, especially powerful once we solve trace sharing)
  4. (I need to check if this is implemented already or not): the ability to define a debug track from a command

LalitMaganti avatar Aug 20 '25 19:08 LalitMaganti

  1. is now implemented in https://github.com/google/perfetto/pull/2654 and I think once it lands, we can probably close out this issue once you confirm it does what you need.

I verified 4): there is a command to create debug tracks available at https://github.com/google/perfetto/blob/main/ui/src/plugins/dev.perfetto.DebugTracks/index.ts#L29

LalitMaganti avatar Aug 21 '25 00:08 LalitMaganti

Update on this since there was a discussion on another bug which I didn't capture here: I think I'm now convinced that we need to have some way to add some mechanism to define within the trace file itself to "run the following commands on UI startup": it will tie to the same automation commands feature that works through settings.

See more discussion in https://github.com/google/perfetto/pull/2654

LalitMaganti avatar Oct 31 '25 19:10 LalitMaganti