1// Copyright (C) 2020 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 {exists} from '../../base/utils';
16import {ColumnDef, Sorting} from '../../public/aggregation';
17import {AreaSelection} from '../../public/selection';
18import {CPU_SLICE_TRACK_KIND} from '../../public/track_kinds';
19import {Engine} from '../../trace_processor/engine';
20import {AreaSelectionAggregator} from '../../public/selection';
21
22export class CpuSliceSelectionAggregator implements AreaSelectionAggregator {
23  readonly id = 'cpu_aggregation';
24
25  async createAggregateView(engine: Engine, area: AreaSelection) {
26    const selectedCpus: number[] = [];
27    for (const trackInfo of area.tracks) {
28      if (trackInfo?.tags?.kind === CPU_SLICE_TRACK_KIND) {
29        exists(trackInfo.tags.cpu) && selectedCpus.push(trackInfo.tags.cpu);
30      }
31    }
32    if (selectedCpus.length === 0) return false;
33
34    await engine.query(`
35      create or replace perfetto table ${this.id} as
36      select
37        process.name as process_name,
38        pid,
39        thread.name as thread_name,
40        tid,
41        sum(dur) AS total_dur,
42        sum(dur) / count() as avg_dur,
43        count() as occurrences
44      from process
45      join thread using (upid)
46      join sched using (utid)
47      where cpu in (${selectedCpus})
48        and sched.ts + sched.dur > ${area.start}
49        and sched.ts < ${area.end}
50        and utid != 0
51      group by utid
52    `);
53    return true;
54  }
55
56  getTabName() {
57    return 'CPU by thread';
58  }
59
60  async getExtra() {}
61
62  getDefaultSorting(): Sorting {
63    return {column: 'total_dur', direction: 'DESC'};
64  }
65
66  getColumnDefinitions(): ColumnDef[] {
67    return [
68      {
69        title: 'Process',
70        kind: 'STRING',
71        columnConstructor: Uint16Array,
72        columnId: 'process_name',
73      },
74      {
75        title: 'PID',
76        kind: 'NUMBER',
77        columnConstructor: Uint16Array,
78        columnId: 'pid',
79      },
80      {
81        title: 'Thread',
82        kind: 'STRING',
83        columnConstructor: Uint16Array,
84        columnId: 'thread_name',
85      },
86      {
87        title: 'TID',
88        kind: 'NUMBER',
89        columnConstructor: Uint16Array,
90        columnId: 'tid',
91      },
92      {
93        title: 'Wall duration (ms)',
94        kind: 'TIMESTAMP_NS',
95        columnConstructor: Float64Array,
96        columnId: 'total_dur',
97        sum: true,
98      },
99      {
100        title: 'Avg Wall duration (ms)',
101        kind: 'TIMESTAMP_NS',
102        columnConstructor: Float64Array,
103        columnId: 'avg_dur',
104      },
105      {
106        title: 'Occurrences',
107        kind: 'NUMBER',
108        columnConstructor: Uint16Array,
109        columnId: 'occurrences',
110        sum: true,
111      },
112    ];
113  }
114}
115