xref: /aosp_15_r20/external/perfetto/ui/src/plugins/com.google.PixelCpmTrace/index.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2024 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 {createQueryCounterTrack} from '../../components/tracks/query_counter_track';
16import {PerfettoPlugin} from '../../public/plugin';
17import {Trace} from '../../public/trace';
18import {COUNTER_TRACK_KIND} from '../../public/track_kinds';
19import {TrackNode} from '../../public/workspace';
20import {NUM, STR} from '../../trace_processor/query_result';
21
22export default class implements PerfettoPlugin {
23  static readonly id = 'com.google.PixelCpmTrace';
24
25  async onTraceLoad(ctx: Trace): Promise<void> {
26    const group = new TrackNode({
27      title: 'Central Power Manager',
28      isSummary: true,
29    });
30
31    const {engine} = ctx;
32    const result = await engine.query(`
33      select
34        id AS trackId,
35        extract_arg(dimension_arg_set_id, 'name') AS trackName
36      FROM track
37      WHERE classification = 'pixel_cpm_trace'
38      ORDER BY trackName
39    `);
40
41    const it = result.iter({trackId: NUM, trackName: STR});
42    for (let groupAdded = false; it.valid(); it.next()) {
43      const {trackId, trackName} = it;
44      const uri = `/cpm_trace_${trackName}`;
45      const track = await createQueryCounterTrack({
46        trace: ctx,
47        uri,
48        data: {
49          sqlSource: `
50             select ts, value
51             from counter
52             where track_id = ${trackId}
53           `,
54          columns: ['ts', 'value'],
55        },
56        columns: {ts: 'ts', value: 'value'},
57      });
58      ctx.tracks.registerTrack({
59        uri,
60        title: trackName,
61        tags: {
62          kind: COUNTER_TRACK_KIND,
63          trackIds: [trackId],
64        },
65        track,
66      });
67      group.addChildInOrder(new TrackNode({uri, title: trackName}));
68      if (!groupAdded) {
69        ctx.workspace.addChildInOrder(group);
70        groupAdded = true;
71      }
72    }
73  }
74}
75