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 {globals} from '../../frontend/globals'; 19import {Config, CPU_SLICE_TRACK_KIND} from '../../tracks/cpu_slices'; 20 21import {AggregationController} from './aggregation_controller'; 22 23 24export class CpuAggregationController extends AggregationController { 25 async createAggregateView(engine: Engine, area: Area) { 26 await engine.query(`drop view if exists ${this.kind};`); 27 28 const selectedCpus = []; 29 for (const trackId of area.tracks) { 30 const track = globals.state.tracks[trackId]; 31 // Track will be undefined for track groups. 32 if (track !== undefined && track.kind === CPU_SLICE_TRACK_KIND) { 33 selectedCpus.push((track.config as Config).cpu); 34 } 35 } 36 if (selectedCpus.length === 0) return false; 37 38 const query = `create view ${this.kind} as 39 SELECT process.name as process_name, pid, thread.name as thread_name, 40 tid, sum(dur) AS total_dur, 41 sum(dur)/count(1) as avg_dur, 42 count(1) as occurrences 43 FROM process 44 JOIN thread USING(upid) 45 JOIN thread_state USING(utid) 46 WHERE cpu IN (${selectedCpus}) AND 47 state = "Running" AND 48 thread_state.ts + thread_state.dur > ${area.start} AND 49 thread_state.ts < ${area.end} group by utid`; 50 51 await engine.query(query); 52 return true; 53 } 54 55 getTabName() { 56 return 'CPU by thread'; 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: 'Process', 69 kind: 'STRING', 70 columnConstructor: Uint16Array, 71 columnId: 'process_name', 72 }, 73 { 74 title: 'PID', 75 kind: 'NUMBER', 76 columnConstructor: Uint16Array, 77 columnId: 'pid', 78 }, 79 { 80 title: 'Thread', 81 kind: 'STRING', 82 columnConstructor: Uint16Array, 83 columnId: 'thread_name', 84 }, 85 { 86 title: 'TID', 87 kind: 'NUMBER', 88 columnConstructor: Uint16Array, 89 columnId: 'tid', 90 }, 91 { 92 title: 'Wall duration (ms)', 93 kind: 'TIMESTAMP_NS', 94 columnConstructor: Float64Array, 95 columnId: 'total_dur', 96 sum: true, 97 }, 98 { 99 title: 'Avg Wall duration (ms)', 100 kind: 'TIMESTAMP_NS', 101 columnConstructor: Float64Array, 102 columnId: 'avg_dur', 103 }, 104 { 105 title: 'Occurrences', 106 kind: 'NUMBER', 107 columnConstructor: Uint16Array, 108 columnId: 'occurrences', 109 sum: true, 110 }, 111 ]; 112 } 113} 114