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 {toNs} from '../../common/time'; 19import {Config, CPU_SLICE_TRACK_KIND} from '../../tracks/cpu_slices/common'; 20import {globals} from '../globals'; 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 = []; 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, 40 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 > ${toNs(area.startSec)} AND 49 thread_state.ts < ${toNs(area.endSec)} group by upid`; 50 51 await engine.query(query); 52 return true; 53 } 54 55 getTabName() { 56 return 'CPU by process'; 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: 'Wall duration (ms)', 81 kind: 'TIMESTAMP_NS', 82 columnConstructor: Float64Array, 83 columnId: 'total_dur', 84 sum: true 85 }, 86 { 87 title: 'Avg Wall duration (ms)', 88 kind: 'TIMESTAMP_NS', 89 columnConstructor: Float64Array, 90 columnId: 'avg_dur' 91 }, 92 { 93 title: 'Occurrences', 94 kind: 'NUMBER', 95 columnConstructor: Uint16Array, 96 columnId: 'occurrences', 97 sum: true 98 } 99 ]; 100 } 101} 102