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 {Sorting, TimestampedAreaSelection} from '../../common/state'; 18import {toNs} from '../../common/time'; 19import { 20 Config, 21 THREAD_STATE_TRACK_KIND 22} from '../../tracks/thread_state/common'; 23import {globals} from '../globals'; 24 25import {AggregationController} from './aggregation_controller'; 26 27export class ThreadAggregationController extends AggregationController { 28 async createAggregateView( 29 engine: Engine, selectedArea: TimestampedAreaSelection) { 30 await engine.query(`drop view if exists ${this.kind};`); 31 const area = selectedArea.area; 32 if (area === undefined) return false; 33 34 // TODO(taylori): Thread state tracks should have a real track id in the 35 // trace processor. 36 const utids = []; 37 for (const trackId of area.tracks) { 38 const track = globals.state.tracks[trackId]; 39 // Track will be undefined for track groups. 40 if (track !== undefined && track.kind === THREAD_STATE_TRACK_KIND) { 41 utids.push((track.config as Config).utid); 42 } 43 } 44 if (utids.length === 0) return false; 45 46 const query = `create view ${this.kind} as 47 SELECT process.name as process_name, pid, thread.name as thread_name, tid, 48 state, 49 sum(dur) AS total_dur, 50 sum(dur)/count(1) as avg_dur, 51 count(1) as occurrences 52 FROM process 53 JOIN thread USING(upid) 54 JOIN thread_state USING(utid) 55 WHERE utid IN (${utids}) AND 56 thread_state.ts + thread_state.dur > ${toNs(area.startSec)} AND 57 thread_state.ts < ${toNs(area.endSec)} 58 GROUP BY utid, state`; 59 60 await engine.query(query); 61 return true; 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: 'Thread', 80 kind: 'STRING', 81 columnConstructor: Uint16Array, 82 columnId: 'thread_name' 83 }, 84 { 85 title: 'TID', 86 kind: 'NUMBER', 87 columnConstructor: Uint16Array, 88 columnId: 'tid' 89 }, 90 { 91 title: 'State', 92 kind: 'STATE', 93 columnConstructor: Uint16Array, 94 columnId: 'state' 95 }, 96 { 97 title: 'Wall duration (ms)', 98 kind: 'TIMESTAMP_NS', 99 columnConstructor: Float64Array, 100 columnId: 'total_dur' 101 }, 102 { 103 title: 'Avg Wall duration (ms)', 104 kind: 'TIMESTAMP_NS', 105 columnConstructor: Float64Array, 106 columnId: 'avg_dur' 107 }, 108 { 109 title: 'Occurrences', 110 kind: 'NUMBER', 111 columnConstructor: Uint16Array, 112 columnId: 'occurrences' 113 } 114 ]; 115 } 116 117 getTabName() { 118 return 'Thread States'; 119 } 120 121 getDefaultSorting(): Sorting { 122 return {column: 'total_dur', direction: 'DESC'}; 123 } 124} 125