1// Copyright (C) 2021 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 {exists} from '../../base/utils'; 16import {ColumnDef} from '../../common/aggregation_data'; 17import {Area, Sorting} from '../../common/state'; 18import {globals} from '../../frontend/globals'; 19import {Engine} from '../../trace_processor/engine'; 20import {CPU_SLICE_TRACK_KIND} from '../../core/track_kinds'; 21 22import {AggregationController} from './aggregation_controller'; 23 24export class CpuByProcessAggregationController extends AggregationController { 25 async createAggregateView(engine: Engine, area: Area) { 26 await engine.query(`drop view if exists ${this.kind};`); 27 28 const selectedCpus: number[] = []; 29 for (const trackKey of area.tracks) { 30 const track = globals.state.tracks[trackKey]; 31 if (track?.uri) { 32 const trackInfo = globals.trackManager.resolveTrackInfo(track.uri); 33 if (trackInfo?.kind === CPU_SLICE_TRACK_KIND) { 34 exists(trackInfo.cpu) && selectedCpus.push(trackInfo.cpu); 35 } 36 } 37 } 38 if (selectedCpus.length === 0) return false; 39 40 const query = `create view ${this.kind} as 41 SELECT process.name as process_name, pid, 42 sum(dur) AS total_dur, 43 sum(dur)/count(1) as avg_dur, 44 count(1) as occurrences 45 FROM process 46 JOIN thread USING(upid) 47 JOIN thread_state USING(utid) 48 WHERE cpu IN (${selectedCpus}) AND 49 state = "Running" AND 50 thread_state.ts + thread_state.dur > ${area.start} AND 51 thread_state.ts < ${area.end} group by upid`; 52 53 await engine.query(query); 54 return true; 55 } 56 57 getTabName() { 58 return 'CPU by process'; 59 } 60 61 async getExtra() {} 62 63 getDefaultSorting(): Sorting { 64 return {column: 'total_dur', direction: 'DESC'}; 65 } 66 67 getColumnDefinitions(): ColumnDef[] { 68 return [ 69 { 70 title: 'Process', 71 kind: 'STRING', 72 columnConstructor: Uint16Array, 73 columnId: 'process_name', 74 }, 75 { 76 title: 'PID', 77 kind: 'NUMBER', 78 columnConstructor: Uint16Array, 79 columnId: 'pid', 80 }, 81 { 82 title: 'Wall duration (ms)', 83 kind: 'TIMESTAMP_NS', 84 columnConstructor: Float64Array, 85 columnId: 'total_dur', 86 sum: true, 87 }, 88 { 89 title: 'Avg Wall duration (ms)', 90 kind: 'TIMESTAMP_NS', 91 columnConstructor: Float64Array, 92 columnId: 'avg_dur', 93 }, 94 { 95 title: 'Occurrences', 96 kind: 'NUMBER', 97 columnConstructor: Uint16Array, 98 columnId: 'occurrences', 99 sum: true, 100 }, 101 ]; 102 } 103} 104