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, Sorting} from '../../public/aggregation'; 16import {AreaSelection, AreaSelectionAggregator} from '../../public/selection'; 17import {Dataset} from '../../trace_processor/dataset'; 18import {Engine} from '../../trace_processor/engine'; 19import {LONG, NUM, STR_NULL} from '../../trace_processor/query_result'; 20 21export class SliceSelectionAggregator implements AreaSelectionAggregator { 22 readonly id = 'slice_aggregation'; 23 24 readonly schema = { 25 id: NUM, 26 name: STR_NULL, 27 ts: LONG, 28 dur: LONG, 29 } as const; 30 31 async createAggregateView( 32 engine: Engine, 33 area: AreaSelection, 34 dataset?: Dataset, 35 ) { 36 if (!dataset) return false; 37 38 await engine.query(` 39 create or replace perfetto table ${this.id} as 40 select 41 name, 42 sum(dur) AS total_dur, 43 sum(dur)/count() as avg_dur, 44 count() as occurrences 45 from (${dataset.query()}) 46 where 47 ts + dur > ${area.start} 48 and ts < ${area.end} 49 group by name 50 `); 51 52 return true; 53 } 54 55 getTabName() { 56 return 'Slices'; 57 } 58 59 async getExtra() {} 60 61 getDefaultSorting(): Sorting { 62 return {column: 'total_dur', direction: 'DESC'}; 63 } 64 65 getColumnDefinitions(): ColumnDef[] { 66 return [ 67 { 68 title: 'Name', 69 kind: 'STRING', 70 columnConstructor: Uint32Array, 71 columnId: 'name', 72 }, 73 { 74 title: 'Wall duration (ms)', 75 kind: 'TIMESTAMP_NS', 76 columnConstructor: Float64Array, 77 columnId: 'total_dur', 78 sum: true, 79 }, 80 { 81 title: 'Avg Wall duration (ms)', 82 kind: 'TIMESTAMP_NS', 83 columnConstructor: Float64Array, 84 columnId: 'avg_dur', 85 }, 86 { 87 title: 'Occurrences', 88 kind: 'NUMBER', 89 columnConstructor: Uint32Array, 90 columnId: 'occurrences', 91 sum: true, 92 }, 93 ]; 94 } 95} 96