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 {ColumnDef} from '../../common/aggregation_data';
16import {Engine} from '../../common/engine';
17import {Area, Sorting} from '../../common/state';
18import {toNs} from '../../common/time';
19import {Config, CPU_SLICE_TRACK_KIND} from '../../tracks/cpu_slices/common';
20import {globals} from '../globals';
21
22import {AggregationController} from './aggregation_controller';
23
24
25export class CpuAggregationController extends AggregationController {
26  async createAggregateView(engine: Engine, area: Area) {
27    await engine.query(`drop view if exists ${this.kind};`);
28
29    const selectedCpus = [];
30    for (const trackId of area.tracks) {
31      const track = globals.state.tracks[trackId];
32      // Track will be undefined for track groups.
33      if (track !== undefined && track.kind === CPU_SLICE_TRACK_KIND) {
34        selectedCpus.push((track.config as Config).cpu);
35      }
36    }
37    if (selectedCpus.length === 0) return false;
38
39    const query = `create view ${this.kind} as
40        SELECT process.name as process_name, pid, thread.name as thread_name,
41        tid, sum(dur) AS total_dur,
42        sum(dur)/count(1) as avg_dur,
43        count(1) as occurrences
44        FROM process
45        JOIN thread USING(upid)
46        JOIN thread_state USING(utid)
47        WHERE cpu IN (${selectedCpus}) AND
48        state = "Running" AND
49        thread_state.ts + thread_state.dur > ${toNs(area.startSec)} AND
50        thread_state.ts < ${toNs(area.endSec)} group by utid`;
51
52    await engine.query(query);
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