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