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