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 {
20  ASYNC_SLICE_TRACK_KIND,
21  Config as AsyncSliceConfig
22} from '../../tracks/async_slices/common';
23import {
24  Config as SliceConfig,
25  SLICE_TRACK_KIND
26} from '../../tracks/chrome_slices/common';
27import {globals} from '../globals';
28
29import {AggregationController} from './aggregation_controller';
30
31export class SliceAggregationController extends AggregationController {
32  async createAggregateView(engine: Engine, area: Area) {
33    await engine.query(`drop view if exists ${this.kind};`);
34
35    const selectedTrackIds = [];
36    for (const trackId of area.tracks) {
37      const track = globals.state.tracks[trackId];
38      // Track will be undefined for track groups.
39      if (track !== undefined) {
40        if (track.kind === SLICE_TRACK_KIND) {
41          selectedTrackIds.push((track.config as SliceConfig).trackId);
42        }
43        if (track.kind === ASYNC_SLICE_TRACK_KIND) {
44          const config = track.config as AsyncSliceConfig;
45          for (const id of config.trackIds) {
46            selectedTrackIds.push(id);
47          }
48        }
49      }
50    }
51    if (selectedTrackIds.length === 0) return false;
52
53    const query = `create view ${this.kind} as
54        SELECT
55        name,
56        sum(dur) AS total_dur,
57        sum(dur)/count(1) as avg_dur,
58        count(1) as occurrences
59        FROM slices
60        WHERE track_id IN (${selectedTrackIds}) AND
61        ts + dur > ${toNs(area.startSec)} AND
62        ts < ${toNs(area.endSec)} group by name`;
63
64    await engine.query(query);
65    return true;
66  }
67
68  getTabName() {
69    return 'Slices';
70  }
71
72  async getExtra() {}
73
74  getDefaultSorting(): Sorting {
75    return {column: 'total_dur', direction: 'DESC'};
76  }
77
78  getColumnDefinitions(): ColumnDef[] {
79    return [
80      {
81        title: 'Name',
82        kind: 'STRING',
83        columnConstructor: Uint16Array,
84        columnId: 'name',
85      },
86      {
87        title: 'Wall duration (ms)',
88        kind: 'TIMESTAMP_NS',
89        columnConstructor: Float64Array,
90        columnId: 'total_dur',
91        sum: true
92      },
93      {
94        title: 'Avg Wall duration (ms)',
95        kind: 'TIMESTAMP_NS',
96        columnConstructor: Float64Array,
97        columnId: 'avg_dur'
98      },
99      {
100        title: 'Occurrences',
101        kind: 'NUMBER',
102        columnConstructor: Uint16Array,
103        columnId: 'occurrences',
104        sum: true
105      }
106    ];
107  }
108}
109