1// Copyright (C) 2019 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 {assertTrue} from '../../base/logging'; 16import {RawQueryResult} from '../../common/protos'; 17import {slowlyCountRows} from '../../common/query_iterator'; 18import {fromNs, toNs} from '../../common/time'; 19import { 20 TrackController, 21 trackControllerRegistry 22} from '../../controller/track_controller'; 23 24import { 25 Config, 26 Data, 27 PROCESS_SCHEDULING_TRACK_KIND, 28} from './common'; 29 30// This summary is displayed for any processes that have CPU scheduling activity 31// associated with them. 32class ProcessSchedulingTrackController extends TrackController<Config, Data> { 33 static readonly kind = PROCESS_SCHEDULING_TRACK_KIND; 34 35 private maxCpu = 0; 36 private maxDurNs = 0; 37 private cachedBucketNs = Number.MAX_SAFE_INTEGER; 38 39 async onSetup() { 40 await this.createSchedView(); 41 42 const cpus = await this.engine.getCpus(); 43 44 // A process scheduling track should only exist in a trace that has cpus. 45 assertTrue(cpus.length > 0); 46 this.maxCpu = Math.max(...cpus) + 1; 47 48 const result = await this.query(` 49 select max(dur), count(1) 50 from ${this.tableName('process_sched')} 51 `); 52 this.maxDurNs = result.columns[0].longValues![0]; 53 54 const rowCount = result.columns[1].longValues![0]; 55 const bucketNs = this.cachedBucketSizeNs(rowCount); 56 if (bucketNs === undefined) { 57 return; 58 } 59 await this.query(` 60 create table ${this.tableName('process_sched_cached')} as 61 select 62 (ts + ${bucketNs / 2}) / ${bucketNs} * ${bucketNs} as cached_tsq, 63 ts, 64 max(dur) as dur, 65 cpu, 66 utid 67 from ${this.tableName('process_sched')} 68 group by cached_tsq, cpu 69 order by cached_tsq, cpu 70 `); 71 this.cachedBucketNs = bucketNs; 72 } 73 74 async onBoundsChange(start: number, end: number, resolution: number): 75 Promise<Data> { 76 assertTrue(this.config.upid !== null); 77 78 // The resolution should always be a power of two for the logic of this 79 // function to make sense. 80 const resolutionNs = toNs(resolution); 81 assertTrue(Math.log2(resolutionNs) % 1 === 0); 82 83 const startNs = toNs(start); 84 const endNs = toNs(end); 85 86 // ns per quantization bucket (i.e. ns per pixel). /2 * 2 is to force it to 87 // be an even number, so we can snap in the middle. 88 const bucketNs = 89 Math.max(Math.round(resolutionNs * this.pxSize() / 2) * 2, 1); 90 91 const rawResult = await this.queryData(startNs, endNs, bucketNs); 92 93 const numRows = slowlyCountRows(rawResult); 94 const slices: Data = { 95 kind: 'slice', 96 start, 97 end, 98 resolution, 99 length: numRows, 100 maxCpu: this.maxCpu, 101 starts: new Float64Array(numRows), 102 ends: new Float64Array(numRows), 103 cpus: new Uint32Array(numRows), 104 utids: new Uint32Array(numRows), 105 }; 106 107 const cols = rawResult.columns; 108 for (let row = 0; row < numRows; row++) { 109 const startNsQ = +cols[0].longValues![row]; 110 const startNs = +cols[1].longValues![row]; 111 const durNs = +cols[2].longValues![row]; 112 const endNs = startNs + durNs; 113 114 let endNsQ = Math.floor((endNs + bucketNs / 2 - 1) / bucketNs) * bucketNs; 115 endNsQ = Math.max(endNsQ, startNsQ + bucketNs); 116 117 if (startNsQ === endNsQ) { 118 throw new Error('Should never happen'); 119 } 120 121 slices.starts[row] = fromNs(startNsQ); 122 slices.ends[row] = fromNs(endNsQ); 123 slices.cpus[row] = +cols[3].longValues![row]; 124 slices.utids[row] = +cols[4].longValues![row]; 125 slices.end = Math.max(slices.ends[row], slices.end); 126 } 127 return slices; 128 } 129 130 private queryData(startNs: number, endNs: number, bucketNs: number): 131 Promise<RawQueryResult> { 132 const isCached = this.cachedBucketNs <= bucketNs; 133 const tsq = isCached ? `cached_tsq / ${bucketNs} * ${bucketNs}` : 134 `(ts + ${bucketNs / 2}) / ${bucketNs} * ${bucketNs}`; 135 const queryTable = isCached ? this.tableName('process_sched_cached') : 136 this.tableName('process_sched'); 137 const constainColumn = isCached ? 'cached_tsq' : 'ts'; 138 return this.query(` 139 select 140 ${tsq} as tsq, 141 ts, 142 max(dur) as dur, 143 cpu, 144 utid 145 from ${queryTable} 146 where 147 ${constainColumn} >= ${startNs - this.maxDurNs} and 148 ${constainColumn} <= ${endNs} 149 group by tsq, cpu 150 order by tsq, cpu 151 `); 152 } 153 154 private async createSchedView() { 155 await this.query(` 156 create view ${this.tableName('process_sched')} as 157 select ts, dur, cpu, utid 158 from experimental_sched_upid 159 where 160 utid != 0 and 161 upid = ${this.config.upid} 162 `); 163 } 164} 165 166trackControllerRegistry.register(ProcessSchedulingTrackController); 167