• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {PerfettoPlugin} from '../../public/plugin';
16import {TrackNode, Workspace} from '../../public/workspace';
17
18// Type indicating the standard groups supported by this plugin.
19export type StandardGroup =
20  | 'USER_INTERACTION'
21  | 'THERMALS'
22  | 'POWER'
23  | 'IO'
24  | 'MEMORY'
25  | 'HARDWARE'
26  | 'CPU'
27  | 'GPU'
28  | 'NETWORK'
29  | 'SYSTEM';
30
31export default class implements PerfettoPlugin {
32  static readonly id = 'dev.perfetto.StandardGroups';
33
34  private readonly groups: Record<StandardGroup, TrackNode> = {
35    // Expand this group by default
36    USER_INTERACTION: makeGroupNode('User Interaction', false),
37    THERMALS: makeGroupNode('Thermals'),
38    POWER: makeGroupNode('Power'),
39    CPU: makeGroupNode('CPU'),
40    GPU: makeGroupNode('GPU'),
41    HARDWARE: makeGroupNode('Hardware'),
42    IO: makeGroupNode('IO'),
43    MEMORY: makeGroupNode('Memory'),
44    NETWORK: makeGroupNode('Network'),
45    SYSTEM: makeGroupNode('System'),
46  };
47
48  async onTraceLoad() {}
49
50  /**
51   * Gets or creates a standard group to place tracks into.
52   *
53   * @param workspace - The workspace on which to create the group.
54   */
55  getOrCreateStandardGroup(
56    workspace: Workspace,
57    group: StandardGroup,
58  ): TrackNode {
59    const node = this.groups[group];
60
61    // Only add the group if it's not already been added
62    if (node.parent === undefined) {
63      workspace.addChildInOrder(node);
64    }
65
66    return node;
67  }
68}
69
70function makeGroupNode(title: string, collapsed = true) {
71  return new TrackNode({title, isSummary: true, collapsed});
72}
73