blob: 704add75d5a1cee9c94e52cd677956df54682633 [file] [log] [blame]
Isabelle Taylor4f93ced2020-07-03 18:26:43 +12001// 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
Steve Golton278b7f02023-09-06 16:26:23 +010015import {Duration} from '../../base/time';
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120016import {ColumnDef} from '../../common/aggregation_data';
Isabelle Taylor797f4832020-09-08 17:52:59 +010017import {Area, Sorting} from '../../common/state';
Hector Dearman6fbcabd2023-02-10 12:26:59 +000018import {globals} from '../../frontend/globals';
Hector Dearmanea4719d2023-10-31 09:11:09 +000019import {Engine} from '../../trace_processor/engine';
Steve Golton11c0c982023-10-03 08:30:15 +010020import {COUNTER_TRACK_KIND} from '../../tracks/counter';
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120021
22import {AggregationController} from './aggregation_controller';
23
24export class CounterAggregationController extends AggregationController {
Isabelle Taylor797f4832020-09-08 17:52:59 +010025 async createAggregateView(engine: Engine, area: Area) {
Hector Dearman7514f302021-08-19 18:21:52 +010026 await engine.query(`drop view if exists ${this.kind};`);
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120027
Steve Golton7e63f422024-03-18 14:17:32 +000028 const trackIds: (string | number)[] = [];
Steve Goltoncfe3e3d2023-10-26 16:24:31 +010029 for (const trackKey of area.tracks) {
30 const track = globals.state.tracks[trackKey];
Steve Golton11c0c982023-10-03 08:30:15 +010031 if (track?.uri) {
Steve Golton1ea8b9f2024-01-25 16:03:37 +000032 const trackInfo = globals.trackManager.resolveTrackInfo(track.uri);
Steve Golton11c0c982023-10-03 08:30:15 +010033 if (trackInfo?.kind === COUNTER_TRACK_KIND) {
34 trackInfo.trackIds && trackIds.push(...trackInfo.trackIds);
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120035 }
36 }
37 }
Steve Golton11c0c982023-10-03 08:30:15 +010038 if (trackIds.length === 0) return false;
Steve Goltonf3897e22023-05-11 14:18:30 +010039 const duration = area.end - area.start;
Steve Goltonb3a389d2023-07-10 11:03:17 +010040 const durationSec = Duration.toSeconds(duration);
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120041
42 const query = `create view ${this.kind} as select
43 name,
Isabelle Taylor9d335da2020-07-14 15:51:55 +010044 count(1) as count,
Steve Goltonf3897e22023-05-11 14:18:30 +010045 round(sum(weighted_value)/${duration}, 2) as avg_value,
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120046 last as last_value,
47 first as first_value,
48 max(last) - min(first) as delta_value,
Steve Goltonf3897e22023-05-11 14:18:30 +010049 round((max(last) - min(first))/${durationSec}, 2) as rate,
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120050 min(value) as min_value,
51 max(value) as max_value
52 from
53 (select *,
Steve Goltonf3897e22023-05-11 14:18:30 +010054 (min(ts + dur, ${area.end}) - max(ts,${area.start}))
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120055 * value as weighted_value,
56 first_value(value) over
57 (partition by track_id order by ts) as first,
58 last_value(value) over
59 (partition by track_id order by ts
60 range between unbounded preceding and unbounded following) as last
61 from experimental_counter_dur
Steve Golton11c0c982023-10-03 08:30:15 +010062 where track_id in (${trackIds})
Steve Goltonf3897e22023-05-11 14:18:30 +010063 and ts + dur >= ${area.start} and
64 ts <= ${area.end})
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120065 join counter_track
66 on track_id = counter_track.id
67 group by track_id`;
68
Hector Dearman7514f302021-08-19 18:21:52 +010069 await engine.query(query);
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120070 return true;
71 }
72
73 getColumnDefinitions(): ColumnDef[] {
74 return [
75 {
76 title: 'Name',
77 kind: 'STRING',
78 columnConstructor: Uint16Array,
79 columnId: 'name',
80 },
81 {
82 title: 'Delta value',
83 kind: 'NUMBER',
84 columnConstructor: Float64Array,
Hector Dearman23c078b2022-06-05 12:00:25 +010085 columnId: 'delta_value',
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120086 },
87 {
88 title: 'Rate /s',
89 kind: 'Number',
90 columnConstructor: Float64Array,
Hector Dearman23c078b2022-06-05 12:00:25 +010091 columnId: 'rate',
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120092 },
93 {
94 title: 'Weighted avg value',
95 kind: 'Number',
96 columnConstructor: Float64Array,
Hector Dearman23c078b2022-06-05 12:00:25 +010097 columnId: 'avg_value',
Isabelle Taylor4f93ced2020-07-03 18:26:43 +120098 },
99 {
Isabelle Taylor9d335da2020-07-14 15:51:55 +0100100 title: 'Count',
101 kind: 'Number',
102 columnConstructor: Float64Array,
Isabelle Taylor9e7b44e2020-09-15 11:51:30 +0100103 columnId: 'count',
104 sum: true,
Isabelle Taylor9d335da2020-07-14 15:51:55 +0100105 },
106 {
Isabelle Taylor4f93ced2020-07-03 18:26:43 +1200107 title: 'First value',
108 kind: 'NUMBER',
109 columnConstructor: Float64Array,
Hector Dearman23c078b2022-06-05 12:00:25 +0100110 columnId: 'first_value',
Isabelle Taylor4f93ced2020-07-03 18:26:43 +1200111 },
112 {
113 title: 'Last value',
114 kind: 'NUMBER',
115 columnConstructor: Float64Array,
Hector Dearman23c078b2022-06-05 12:00:25 +0100116 columnId: 'last_value',
Isabelle Taylor4f93ced2020-07-03 18:26:43 +1200117 },
118 {
119 title: 'Min value',
120 kind: 'NUMBER',
121 columnConstructor: Float64Array,
Hector Dearman23c078b2022-06-05 12:00:25 +0100122 columnId: 'min_value',
Isabelle Taylor4f93ced2020-07-03 18:26:43 +1200123 },
124 {
125 title: 'Max value',
126 kind: 'NUMBER',
127 columnConstructor: Float64Array,
Hector Dearman23c078b2022-06-05 12:00:25 +0100128 columnId: 'max_value',
Isabelle Taylor4f93ced2020-07-03 18:26:43 +1200129 },
Isabelle Taylor4f93ced2020-07-03 18:26:43 +1200130 ];
131 }
132
Isabelle Taylor6c836a52020-07-31 12:45:20 +0100133 async getExtra() {}
134
Isabelle Taylor4f93ced2020-07-03 18:26:43 +1200135 getTabName() {
136 return 'Counters';
137 }
138
139 getDefaultSorting(): Sorting {
140 return {column: 'name', direction: 'DESC'};
141 }
142}