• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2023 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 {uuidv4} from '../../base/uuid';
16import {Actions} from '../../common/actions';
17import {SCROLLING_TRACK_GROUP} from '../../common/state';
18import {globals} from '../../frontend/globals';
19import {
20  Plugin,
21  PluginContextTrace,
22  PluginDescriptor,
23  PrimaryTrackSortKey,
24} from '../../public';
25
26import {ActiveCPUCountTrack, CPUType} from './active_cpu_count';
27import {RunnableThreadCountTrack} from './runnable_thread_count';
28
29class SchedPlugin implements Plugin {
30  async onTraceLoad(ctx: PluginContextTrace) {
31    ctx.registerTrack({
32      uri: RunnableThreadCountTrack.kind,
33      trackFactory: (trackCtx) =>
34        new RunnableThreadCountTrack({
35          engine: ctx.engine,
36          trackKey: trackCtx.trackKey,
37        }),
38    });
39    ctx.registerCommand({
40      id: 'dev.perfetto.Sched.AddRunnableThreadCountTrackCommand',
41      name: 'Add track: runnable thread count',
42      callback: () =>
43        addPinnedTrack(RunnableThreadCountTrack.kind, 'Runnable thread count'),
44    });
45
46    const uri = uriForActiveCPUCountTrack();
47    const title = 'Active ${cpuType} CPU count';
48    ctx.registerTrack({
49      uri,
50      displayName: title,
51      trackFactory: (trackCtx) => new ActiveCPUCountTrack(trackCtx, ctx.engine),
52    });
53    ctx.registerCommand({
54      id: 'dev.perfetto.Sched.AddActiveCPUCountTrackCommand',
55      name: 'Add track: active CPU count',
56      callback: () => addPinnedTrack(uri, title),
57    });
58
59    for (const cpuType of Object.values(CPUType)) {
60      const uri = uriForActiveCPUCountTrack(cpuType);
61      const title = `Active ${cpuType} CPU count`;
62      ctx.registerTrack({
63        uri,
64        displayName: title,
65        trackFactory: (trackCtx) =>
66          new ActiveCPUCountTrack(trackCtx, ctx.engine, cpuType),
67      });
68
69      ctx.registerCommand({
70        id: `dev.perfetto.Sched.AddActiveCPUCountTrackCommand.${cpuType}`,
71        name: `Add track: active ${cpuType} CPU count`,
72        callback: () => addPinnedTrack(uri, title),
73      });
74    }
75  }
76}
77
78function uriForActiveCPUCountTrack(cpuType?: CPUType): string {
79  const prefix = `perfetto.sched#ActiveCPUCount`;
80  if (cpuType) {
81    return `${prefix}.${cpuType}`;
82  } else {
83    return prefix;
84  }
85}
86
87function addPinnedTrack(uri: string, title: string) {
88  const key = uuidv4();
89  globals.dispatchMultiple([
90    Actions.addTrack({
91      key,
92      uri,
93      name: title,
94      trackSortKey: PrimaryTrackSortKey.DEBUG_TRACK,
95      trackGroup: SCROLLING_TRACK_GROUP,
96    }),
97    Actions.toggleTrackPinned({trackKey: key}),
98  ]);
99}
100
101export const plugin: PluginDescriptor = {
102  pluginId: 'perfetto.Sched',
103  plugin: SchedPlugin,
104};
105