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} from '../../public/selection'; 17import {CPU_SLICE_TRACK_KIND} from '../../public/track_kinds'; 18import {Engine} from '../../trace_processor/engine'; 19import {AreaSelectionAggregator} from '../../public/selection'; 20import {LONG, NUM} from '../../trace_processor/query_result'; 21import {Dataset} from '../../trace_processor/dataset'; 22 23export class CpuSliceSelectionAggregator implements AreaSelectionAggregator { 24 readonly id = 'cpu_aggregation'; 25 readonly trackKind = CPU_SLICE_TRACK_KIND; 26 readonly schema = { 27 dur: LONG, 28 ts: LONG, 29 utid: NUM, 30 } as const; 31 32 async createAggregateView( 33 engine: Engine, 34 area: AreaSelection, 35 dataset?: Dataset, 36 ) { 37 if (!dataset) return false; 38 39 await engine.query(` 40 create or replace perfetto table ${this.id} as 41 select 42 process.name as process_name, 43 pid, 44 thread.name as thread_name, 45 tid, 46 sum(dur) AS total_dur, 47 sum(dur) / count() as avg_dur, 48 count() as occurrences 49 from process 50 join thread using (upid) 51 join (${dataset.query()}) as sched using (utid) 52 where 53 sched.ts + sched.dur > ${area.start} 54 and sched.ts < ${area.end} 55 group by utid 56 `); 57 return true; 58 } 59 60 getTabName() { 61 return 'CPU by thread'; 62 } 63 64 async getExtra() {} 65 66 getDefaultSorting(): Sorting { 67 return {column: 'total_dur', direction: 'DESC'}; 68 } 69 70 getColumnDefinitions(): ColumnDef[] { 71 return [ 72 { 73 title: 'Process', 74 kind: 'STRING', 75 columnConstructor: Uint16Array, 76 columnId: 'process_name', 77 }, 78 { 79 title: 'PID', 80 kind: 'NUMBER', 81 columnConstructor: Uint16Array, 82 columnId: 'pid', 83 }, 84 { 85 title: 'Thread', 86 kind: 'STRING', 87 columnConstructor: Uint16Array, 88 columnId: 'thread_name', 89 }, 90 { 91 title: 'TID', 92 kind: 'NUMBER', 93 columnConstructor: Uint16Array, 94 columnId: 'tid', 95 }, 96 { 97 title: 'Wall duration (ms)', 98 kind: 'TIMESTAMP_NS', 99 columnConstructor: Float64Array, 100 columnId: 'total_dur', 101 sum: true, 102 }, 103 { 104 title: 'Avg Wall duration (ms)', 105 kind: 'TIMESTAMP_NS', 106 columnConstructor: Float64Array, 107 columnId: 'avg_dur', 108 }, 109 { 110 title: 'Occurrences', 111 kind: 'NUMBER', 112 columnConstructor: Uint16Array, 113 columnId: 'occurrences', 114 sum: true, 115 }, 116 ]; 117 } 118} 119