• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2025 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {DatasetSliceTrack} from '../../components/tracks/dataset_slice_track';
16import {PerfettoPlugin} from '../../public/plugin';
17import {Trace} from '../../public/trace';
18import {TrackNode} from '../../public/workspace';
19import {SourceDataset} from '../../trace_processor/dataset';
20import {LONG, NUM, STR} from '../../trace_processor/query_result';
21
22export default class implements PerfettoPlugin {
23  static readonly id = 'com.example.ExampleSimpleTrack';
24
25  async onTraceLoad(ctx: Trace): Promise<void> {
26    // Choose a title for the track.
27    const title = 'Slices that begin with "a"';
28
29    // Choose a URI for the track - the URI should be unique within the scope of
30    // the trace, but consistent so that it's the same between trace loads.
31    const uri = `com.example.ExampleSimpleTrack#SlicesThatBeginWithA`;
32
33    // Create & register the track.
34    ctx.tracks.registerTrack({
35      uri,
36      title,
37      track: new DatasetSliceTrack({
38        trace: ctx,
39        uri,
40        dataset: new SourceDataset({
41          // This is where we choose the SQL expression that describes the
42          // events that appear on the track.
43          src: `
44            select
45              id,
46              ts,
47              dur,
48              name
49            from slice
50            where name glob 'a*'
51          `,
52
53          // Tell the track that these fields are available in the SQL we
54          // provided. `id` and `ts` are required, `dur` tells the track to draw
55          // slices instead of instants, and `name` tells the track the text to
56          // display on the slices.
57          schema: {
58            id: NUM,
59            ts: LONG,
60            dur: LONG,
61            name: STR,
62          },
63        }),
64      }),
65    });
66
67    // Add the track to the default workspace, right at the top by providing a
68    // large negative sort order.
69    //
70    // Note, if you want your plugin to be enabled by default on
71    // ui.perfetto.dev, please don't do this. Be respectful to other uses and
72    // organize your tracks neatly.
73    const trackNode = new TrackNode({uri, title, sortOrder: -100});
74    ctx.workspace.addChildInOrder(trackNode);
75  }
76}
77