Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 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 | |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 15 | import {Duration, time} from '../base/time'; |
Steve Golton | 3067f8a | 2023-12-14 17:09:26 +0000 | [diff] [blame] | 16 | import {exists} from '../base/utils'; |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 17 | import {Actions} from '../common/actions'; |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 18 | import { |
Daniele Di Proietto | 8855123 | 2023-11-02 12:56:01 +0000 | [diff] [blame] | 19 | defaultViewingOption, |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 20 | expandCallsites, |
| 21 | findRootSize, |
| 22 | mergeCallsites, |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 23 | } from '../common/flamegraph_util'; |
Daniele Di Proietto | 8855123 | 2023-11-02 12:56:01 +0000 | [diff] [blame] | 24 | import { |
| 25 | CallsiteInfo, |
| 26 | FlamegraphState, |
| 27 | FlamegraphStateViewingOption, |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 28 | isHeapGraphDominatorTreeViewingOption, |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 29 | ProfileType, |
Daniele Di Proietto | 8855123 | 2023-11-02 12:56:01 +0000 | [diff] [blame] | 30 | } from '../common/state'; |
Hector Dearman | 6fbcabd | 2023-02-10 12:26:59 +0000 | [diff] [blame] | 31 | import {FlamegraphDetails, globals} from '../frontend/globals'; |
Tuchila Octavian | 5204758 | 2021-09-21 15:35:54 +0100 | [diff] [blame] | 32 | import {publishFlamegraphDetails} from '../frontend/publish'; |
Hector Dearman | ea4719d | 2023-10-31 09:11:09 +0000 | [diff] [blame] | 33 | import {Engine} from '../trace_processor/engine'; |
| 34 | import {NUM, STR} from '../trace_processor/query_result'; |
Steve Golton | 729115f | 2023-10-17 14:22:24 +0100 | [diff] [blame] | 35 | import {PERF_SAMPLES_PROFILE_TRACK_KIND} from '../tracks/perf_samples_profile'; |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 36 | |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 37 | import {AreaSelectionHandler} from './area_selection_handler'; |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 38 | import {Controller} from './controller'; |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 39 | |
Ioannis Ilkos | 40cc966 | 2022-07-15 15:35:09 +0100 | [diff] [blame] | 40 | export function profileType(s: string): ProfileType { |
| 41 | if (isProfileType(s)) { |
| 42 | return s; |
| 43 | } |
| 44 | if (s.startsWith('heap_profile')) { |
| 45 | return ProfileType.HEAP_PROFILE; |
| 46 | } |
| 47 | throw new Error('Unknown type ${s}'); |
| 48 | } |
| 49 | |
| 50 | function isProfileType(s: string): s is ProfileType { |
| 51 | return Object.values(ProfileType).includes(s as ProfileType); |
| 52 | } |
| 53 | |
| 54 | function getFlamegraphType(type: ProfileType) { |
| 55 | switch (type) { |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 56 | case ProfileType.HEAP_PROFILE: |
| 57 | case ProfileType.MIXED_HEAP_PROFILE: |
| 58 | case ProfileType.NATIVE_HEAP_PROFILE: |
| 59 | case ProfileType.JAVA_HEAP_SAMPLES: |
| 60 | return 'native'; |
| 61 | case ProfileType.JAVA_HEAP_GRAPH: |
| 62 | return 'graph'; |
| 63 | case ProfileType.PERF_SAMPLE: |
| 64 | return 'perf'; |
| 65 | default: |
| 66 | const exhaustiveCheck: never = type; |
| 67 | throw new Error(`Unhandled case: ${exhaustiveCheck}`); |
Ioannis Ilkos | 40cc966 | 2022-07-15 15:35:09 +0100 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Tuchila Octavian | 5204758 | 2021-09-21 15:35:54 +0100 | [diff] [blame] | 71 | export interface FlamegraphControllerArgs { |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 72 | engine: Engine; |
| 73 | } |
| 74 | const MIN_PIXEL_DISPLAYED = 1; |
| 75 | |
Hector Dearman | da988af | 2020-02-25 13:42:42 +0000 | [diff] [blame] | 76 | class TablesCache { |
| 77 | private engine: Engine; |
| 78 | private cache: Map<string, string>; |
| 79 | private prefix: string; |
| 80 | private tableId: number; |
| 81 | private cacheSizeLimit: number; |
| 82 | |
| 83 | constructor(engine: Engine, prefix: string) { |
| 84 | this.engine = engine; |
| 85 | this.cache = new Map<string, string>(); |
| 86 | this.prefix = prefix; |
| 87 | this.tableId = 0; |
| 88 | this.cacheSizeLimit = 10; |
| 89 | } |
| 90 | |
| 91 | async getTableName(query: string): Promise<string> { |
| 92 | let tableName = this.cache.get(query); |
| 93 | if (tableName === undefined) { |
| 94 | // TODO(hjd): This should be LRU. |
| 95 | if (this.cache.size > this.cacheSizeLimit) { |
| 96 | for (const name of this.cache.values()) { |
Hector Dearman | 7514f30 | 2021-08-19 18:21:52 +0100 | [diff] [blame] | 97 | await this.engine.query(`drop table ${name}`); |
Hector Dearman | da988af | 2020-02-25 13:42:42 +0000 | [diff] [blame] | 98 | } |
| 99 | this.cache.clear(); |
| 100 | } |
| 101 | tableName = `${this.prefix}_${this.tableId++}`; |
Hector Dearman | 7514f30 | 2021-08-19 18:21:52 +0100 | [diff] [blame] | 102 | await this.engine.query( |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 103 | `create temp table if not exists ${tableName} as ${query}`, |
| 104 | ); |
Hector Dearman | da988af | 2020-02-25 13:42:42 +0000 | [diff] [blame] | 105 | this.cache.set(query, tableName); |
| 106 | } |
| 107 | return tableName; |
| 108 | } |
| 109 | } |
| 110 | |
Tuchila Octavian | 5204758 | 2021-09-21 15:35:54 +0100 | [diff] [blame] | 111 | export class FlamegraphController extends Controller<'main'> { |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 112 | private flamegraphDatasets: Map<string, CallsiteInfo[]> = new Map(); |
Tuchila Octavian | 5204758 | 2021-09-21 15:35:54 +0100 | [diff] [blame] | 113 | private lastSelectedFlamegraphState?: FlamegraphState; |
Neda Topoljanac | 1743241 | 2019-11-29 16:50:44 +0000 | [diff] [blame] | 114 | private requestingData = false; |
| 115 | private queuedRequest = false; |
Tuchila Octavian | 5204758 | 2021-09-21 15:35:54 +0100 | [diff] [blame] | 116 | private flamegraphDetails: FlamegraphDetails = {}; |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 117 | private areaSelectionHandler: AreaSelectionHandler; |
Hector Dearman | da988af | 2020-02-25 13:42:42 +0000 | [diff] [blame] | 118 | private cache: TablesCache; |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 119 | |
Tuchila Octavian | 5204758 | 2021-09-21 15:35:54 +0100 | [diff] [blame] | 120 | constructor(private args: FlamegraphControllerArgs) { |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 121 | super('main'); |
Hector Dearman | da988af | 2020-02-25 13:42:42 +0000 | [diff] [blame] | 122 | this.cache = new TablesCache(args.engine, 'grouped_callsites'); |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 123 | this.areaSelectionHandler = new AreaSelectionHandler(); |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | run() { |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 127 | const [hasAreaChanged, area] = this.areaSelectionHandler.getAreaChange(); |
| 128 | if (hasAreaChanged) { |
| 129 | const upids = []; |
| 130 | if (!area) { |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 131 | this.checkCompletionAndPublishFlamegraph({ |
| 132 | ...globals.flamegraphDetails, |
| 133 | isInAreaSelection: false, |
| 134 | }); |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | for (const trackId of area.tracks) { |
Steve Golton | 729115f | 2023-10-17 14:22:24 +0100 | [diff] [blame] | 138 | const track = globals.state.tracks[trackId]; |
| 139 | if (track?.uri) { |
Steve Golton | 1ea8b9f | 2024-01-25 16:03:37 +0000 | [diff] [blame] | 140 | const trackInfo = globals.trackManager.resolveTrackInfo(track.uri); |
Steve Golton | 729115f | 2023-10-17 14:22:24 +0100 | [diff] [blame] | 141 | if (trackInfo?.kind === PERF_SAMPLES_PROFILE_TRACK_KIND) { |
Steve Golton | 3067f8a | 2023-12-14 17:09:26 +0000 | [diff] [blame] | 142 | exists(trackInfo.upid) && upids.push(trackInfo.upid); |
Steve Golton | 729115f | 2023-10-17 14:22:24 +0100 | [diff] [blame] | 143 | } |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 144 | } |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 145 | } |
| 146 | if (upids.length === 0) { |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 147 | this.checkCompletionAndPublishFlamegraph({ |
| 148 | ...globals.flamegraphDetails, |
| 149 | isInAreaSelection: false, |
| 150 | }); |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 151 | return; |
| 152 | } |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 153 | globals.dispatch( |
| 154 | Actions.openFlamegraph({ |
| 155 | upids, |
| 156 | start: area.start, |
| 157 | end: area.end, |
| 158 | type: ProfileType.PERF_SAMPLE, |
| 159 | viewingOption: defaultViewingOption(ProfileType.PERF_SAMPLE), |
| 160 | }), |
| 161 | ); |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 162 | } |
Hector Dearman | 6fbcabd | 2023-02-10 12:26:59 +0000 | [diff] [blame] | 163 | const selection = globals.state.currentFlamegraphState; |
Tuchila Octavian | 7eede88 | 2021-09-28 13:24:31 +0100 | [diff] [blame] | 164 | if (!selection || !this.shouldRequestData(selection)) { |
| 165 | return; |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 166 | } |
Tuchila Octavian | 7eede88 | 2021-09-28 13:24:31 +0100 | [diff] [blame] | 167 | if (this.requestingData) { |
| 168 | this.queuedRequest = true; |
| 169 | return; |
| 170 | } |
| 171 | this.requestingData = true; |
| 172 | |
Alexander Timin | 86d2e8a | 2022-06-28 17:14:14 +0000 | [diff] [blame] | 173 | this.assembleFlamegraphDetails(selection, area !== undefined); |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 176 | private async assembleFlamegraphDetails( |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 177 | selection: FlamegraphState, |
| 178 | isInAreaSelection: boolean, |
| 179 | ) { |
Tuchila Octavian | 7eede88 | 2021-09-28 13:24:31 +0100 | [diff] [blame] | 180 | const selectedFlamegraphState = {...selection}; |
| 181 | const flamegraphMetadata = await this.getFlamegraphMetadata( |
Steve Golton | 5dbacf5 | 2024-01-25 09:20:51 +0000 | [diff] [blame] | 182 | selection.type, |
| 183 | selectedFlamegraphState.start, |
| 184 | selectedFlamegraphState.end, |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 185 | selectedFlamegraphState.upids, |
| 186 | ); |
Tuchila Octavian | 7eede88 | 2021-09-28 13:24:31 +0100 | [diff] [blame] | 187 | if (flamegraphMetadata !== undefined) { |
| 188 | Object.assign(this.flamegraphDetails, flamegraphMetadata); |
| 189 | } |
| 190 | |
| 191 | // TODO(hjd): Clean this up. |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 192 | if ( |
| 193 | this.lastSelectedFlamegraphState && |
| 194 | this.lastSelectedFlamegraphState.focusRegex !== selection.focusRegex |
| 195 | ) { |
Tuchila Octavian | 7eede88 | 2021-09-28 13:24:31 +0100 | [diff] [blame] | 196 | this.flamegraphDatasets.clear(); |
| 197 | } |
| 198 | |
| 199 | this.lastSelectedFlamegraphState = {...selection}; |
| 200 | |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 201 | const expandedCallsite = |
| 202 | selectedFlamegraphState.expandedCallsiteByViewingOption[ |
| 203 | selectedFlamegraphState.viewingOption |
| 204 | ]; |
| 205 | const expandedId = expandedCallsite ? expandedCallsite.id : -1; |
| 206 | const rootSize = expandedCallsite?.totalSize; |
Tuchila Octavian | 7eede88 | 2021-09-28 13:24:31 +0100 | [diff] [blame] | 207 | |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 208 | const key = `${selectedFlamegraphState.upids};${selectedFlamegraphState.start};${selectedFlamegraphState.end}`; |
Tuchila Octavian | 7eede88 | 2021-09-28 13:24:31 +0100 | [diff] [blame] | 209 | |
| 210 | try { |
| 211 | const flamegraphData = await this.getFlamegraphData( |
Steve Golton | 5dbacf5 | 2024-01-25 09:20:51 +0000 | [diff] [blame] | 212 | key, |
| 213 | /* eslint-disable @typescript-eslint/strict-boolean-expressions */ |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 214 | selectedFlamegraphState.viewingOption /* eslint-enable */ |
| 215 | ? selectedFlamegraphState.viewingOption |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 216 | : defaultViewingOption(selectedFlamegraphState.type), |
Steve Golton | 5dbacf5 | 2024-01-25 09:20:51 +0000 | [diff] [blame] | 217 | selection.start, |
| 218 | selection.end, |
| 219 | selectedFlamegraphState.upids, |
| 220 | selectedFlamegraphState.type, |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 221 | selectedFlamegraphState.focusRegex, |
| 222 | ); |
| 223 | if ( |
| 224 | flamegraphData !== undefined && |
| 225 | // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions |
| 226 | selection && |
| 227 | selection.kind === selectedFlamegraphState.kind && |
| 228 | selection.start === selectedFlamegraphState.start && |
| 229 | selection.end === selectedFlamegraphState.end |
| 230 | ) { |
| 231 | const expandedFlamegraphData = expandCallsites( |
| 232 | flamegraphData, |
| 233 | expandedId, |
| 234 | ); |
Tuchila Octavian | 7eede88 | 2021-09-28 13:24:31 +0100 | [diff] [blame] | 235 | this.prepareAndMergeCallsites( |
Steve Golton | 5dbacf5 | 2024-01-25 09:20:51 +0000 | [diff] [blame] | 236 | expandedFlamegraphData, |
| 237 | this.lastSelectedFlamegraphState.viewingOption, |
| 238 | isInAreaSelection, |
| 239 | rootSize, |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 240 | expandedCallsite, |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 241 | ); |
Tuchila Octavian | 7eede88 | 2021-09-28 13:24:31 +0100 | [diff] [blame] | 242 | } |
| 243 | } finally { |
| 244 | this.requestingData = false; |
| 245 | if (this.queuedRequest) { |
| 246 | this.queuedRequest = false; |
| 247 | this.run(); |
| 248 | } |
| 249 | } |
Neda Topoljanac | 1743241 | 2019-11-29 16:50:44 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Tuchila Octavian | 5204758 | 2021-09-21 15:35:54 +0100 | [diff] [blame] | 252 | private shouldRequestData(selection: FlamegraphState) { |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 253 | return ( |
| 254 | selection.kind === 'FLAMEGRAPH_STATE' && |
| 255 | (this.lastSelectedFlamegraphState === undefined || |
| 256 | this.lastSelectedFlamegraphState.start !== selection.start || |
| 257 | this.lastSelectedFlamegraphState.end !== selection.end || |
| 258 | this.lastSelectedFlamegraphState.type !== selection.type || |
| 259 | !FlamegraphController.areArraysEqual( |
| 260 | this.lastSelectedFlamegraphState.upids, |
| 261 | selection.upids, |
| 262 | ) || |
| 263 | this.lastSelectedFlamegraphState.viewingOption !== |
| 264 | selection.viewingOption || |
| 265 | this.lastSelectedFlamegraphState.focusRegex !== selection.focusRegex || |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 266 | this.lastSelectedFlamegraphState.expandedCallsiteByViewingOption[ |
| 267 | selection.viewingOption |
| 268 | ] !== |
| 269 | selection.expandedCallsiteByViewingOption[selection.viewingOption]) |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 270 | ); |
Neda Topoljanac | 1743241 | 2019-11-29 16:50:44 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 273 | private prepareAndMergeCallsites( |
Steve Golton | 5dbacf5 | 2024-01-25 09:20:51 +0000 | [diff] [blame] | 274 | flamegraphData: CallsiteInfo[], |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 275 | viewingOption: FlamegraphStateViewingOption, |
| 276 | isInAreaSelection: boolean, |
| 277 | rootSize?: number, |
| 278 | expandedCallsite?: CallsiteInfo, |
| 279 | ) { |
Tuchila Octavian | 7eede88 | 2021-09-28 13:24:31 +0100 | [diff] [blame] | 280 | this.flamegraphDetails.flamegraph = mergeCallsites( |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 281 | flamegraphData, |
| 282 | this.getMinSizeDisplayed(flamegraphData, rootSize), |
| 283 | ); |
Tuchila Octavian | 5204758 | 2021-09-21 15:35:54 +0100 | [diff] [blame] | 284 | this.flamegraphDetails.expandedCallsite = expandedCallsite; |
| 285 | this.flamegraphDetails.viewingOption = viewingOption; |
Alexander Timin | 86d2e8a | 2022-06-28 17:14:14 +0000 | [diff] [blame] | 286 | this.flamegraphDetails.isInAreaSelection = isInAreaSelection; |
Tuchila Octavian | 0fbb7a2 | 2022-03-24 11:36:58 +0000 | [diff] [blame] | 287 | this.checkCompletionAndPublishFlamegraph(this.flamegraphDetails); |
| 288 | } |
| 289 | |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 290 | private async checkCompletionAndPublishFlamegraph( |
| 291 | flamegraphDetails: FlamegraphDetails, |
| 292 | ) { |
Tuchila Octavian | 0fbb7a2 | 2022-03-24 11:36:58 +0000 | [diff] [blame] | 293 | flamegraphDetails.graphIncomplete = |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 294 | ( |
| 295 | await this.args.engine.query(`select value from stats |
| 296 | where severity = 'error' and name = 'heap_graph_non_finalized_graph'`) |
| 297 | ).firstRow({value: NUM}).value > 0; |
Bingqian Liu | d4de9c1 | 2024-04-09 14:33:58 +0100 | [diff] [blame] | 298 | flamegraphDetails.graphLoading = false; |
Tuchila Octavian | 0fbb7a2 | 2022-03-24 11:36:58 +0000 | [diff] [blame] | 299 | publishFlamegraphDetails(flamegraphDetails); |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 302 | async getFlamegraphData( |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 303 | baseKey: string, |
| 304 | viewingOption: FlamegraphStateViewingOption, |
| 305 | start: time, |
| 306 | end: time, |
| 307 | upids: number[], |
| 308 | type: ProfileType, |
| 309 | focusRegex: string, |
| 310 | ): Promise<CallsiteInfo[]> { |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 311 | let currentData: CallsiteInfo[]; |
| 312 | const key = `${baseKey}-${viewingOption}`; |
| 313 | if (this.flamegraphDatasets.has(key)) { |
| 314 | currentData = this.flamegraphDatasets.get(key)!; |
| 315 | } else { |
Bingqian Liu | d4de9c1 | 2024-04-09 14:33:58 +0100 | [diff] [blame] | 316 | publishFlamegraphDetails({ |
| 317 | ...globals.flamegraphDetails, |
| 318 | graphLoading: true, |
| 319 | }); |
Tuchila Octavian | 5204758 | 2021-09-21 15:35:54 +0100 | [diff] [blame] | 320 | // Collecting data for drawing flamegraph for selected profile. |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 321 | // Data needs to be in following format: |
| 322 | // id, name, parent_id, depth, total_size |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 323 | const tableName = await this.prepareViewsAndTables( |
| 324 | start, |
| 325 | end, |
| 326 | upids, |
| 327 | type, |
| 328 | focusRegex, |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 329 | viewingOption, |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 330 | ); |
Isabelle Taylor | 8ee2583 | 2020-10-28 16:25:27 +0000 | [diff] [blame] | 331 | currentData = await this.getFlamegraphDataFromTables( |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 332 | tableName, |
| 333 | viewingOption, |
| 334 | focusRegex, |
| 335 | ); |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 336 | this.flamegraphDatasets.set(key, currentData); |
| 337 | } |
| 338 | return currentData; |
| 339 | } |
| 340 | |
| 341 | async getFlamegraphDataFromTables( |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 342 | tableName: string, |
| 343 | viewingOption: FlamegraphStateViewingOption, |
| 344 | focusRegex: string, |
| 345 | ) { |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 346 | let orderBy = ''; |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 347 | let totalColumnName: |
| 348 | | 'cumulativeSize' |
| 349 | | 'cumulativeAllocSize' |
| 350 | | 'cumulativeCount' |
| 351 | | 'cumulativeAllocCount' = 'cumulativeSize'; |
| 352 | let selfColumnName: 'size' | 'count' = 'size'; |
Florian Mayer | 68845bf | 2020-01-23 13:29:58 +0000 | [diff] [blame] | 353 | // TODO(fmayer): Improve performance so this is no longer necessary. |
| 354 | // Alternatively consider collapsing frames of the same label. |
| 355 | const maxDepth = 100; |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 356 | switch (viewingOption) { |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 357 | case FlamegraphStateViewingOption.ALLOC_SPACE_MEMORY_ALLOCATED_KEY: |
| 358 | orderBy = `where cumulative_alloc_size > 0 and depth < ${maxDepth} order by depth, parent_id, |
Florian Mayer | e13b68d | 2020-01-22 13:19:41 +0000 | [diff] [blame] | 359 | cumulative_alloc_size desc, name`; |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 360 | totalColumnName = 'cumulativeAllocSize'; |
| 361 | selfColumnName = 'size'; |
| 362 | break; |
| 363 | case FlamegraphStateViewingOption.OBJECTS_ALLOCATED_NOT_FREED_KEY: |
| 364 | orderBy = `where cumulative_count > 0 and depth < ${maxDepth} order by depth, parent_id, |
Florian Mayer | e13b68d | 2020-01-22 13:19:41 +0000 | [diff] [blame] | 365 | cumulative_count desc, name`; |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 366 | totalColumnName = 'cumulativeCount'; |
| 367 | selfColumnName = 'count'; |
| 368 | break; |
| 369 | case FlamegraphStateViewingOption.OBJECTS_ALLOCATED_KEY: |
| 370 | orderBy = `where cumulative_alloc_count > 0 and depth < ${maxDepth} order by depth, parent_id, |
Florian Mayer | e13b68d | 2020-01-22 13:19:41 +0000 | [diff] [blame] | 371 | cumulative_alloc_count desc, name`; |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 372 | totalColumnName = 'cumulativeAllocCount'; |
| 373 | selfColumnName = 'count'; |
| 374 | break; |
| 375 | case FlamegraphStateViewingOption.PERF_SAMPLES_KEY: |
| 376 | case FlamegraphStateViewingOption.SPACE_MEMORY_ALLOCATED_NOT_FREED_KEY: |
| 377 | orderBy = `where cumulative_size > 0 and depth < ${maxDepth} order by depth, parent_id, |
Tuchila Octavian | 3b31ed6 | 2021-09-14 12:27:26 +0100 | [diff] [blame] | 378 | cumulative_size desc, name`; |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 379 | totalColumnName = 'cumulativeSize'; |
| 380 | selfColumnName = 'size'; |
| 381 | break; |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 382 | case FlamegraphStateViewingOption.DOMINATOR_TREE_OBJ_COUNT_KEY: |
| 383 | orderBy = `where depth < ${maxDepth} order by depth, |
| 384 | cumulativeCount desc, name`; |
| 385 | totalColumnName = 'cumulativeCount'; |
| 386 | selfColumnName = 'count'; |
| 387 | break; |
| 388 | case FlamegraphStateViewingOption.DOMINATOR_TREE_OBJ_SIZE_KEY: |
| 389 | orderBy = `where depth < ${maxDepth} order by depth, |
| 390 | cumulativeSize desc, name`; |
| 391 | totalColumnName = 'cumulativeSize'; |
| 392 | selfColumnName = 'size'; |
| 393 | break; |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 394 | default: |
| 395 | const exhaustiveCheck: never = viewingOption; |
| 396 | throw new Error(`Unhandled case: ${exhaustiveCheck}`); |
| 397 | break; |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Hector Dearman | 7514f30 | 2021-08-19 18:21:52 +0100 | [diff] [blame] | 400 | const callsites = await this.args.engine.query(` |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 401 | SELECT |
| 402 | id as hash, |
| 403 | IFNULL(IFNULL(DEMANGLE(name), name), '[NULL]') as name, |
| 404 | IFNULL(parent_id, -1) as parentHash, |
| 405 | depth, |
| 406 | cumulative_size as cumulativeSize, |
| 407 | cumulative_alloc_size as cumulativeAllocSize, |
| 408 | cumulative_count as cumulativeCount, |
| 409 | cumulative_alloc_count as cumulativeAllocCount, |
| 410 | map_name as mapping, |
| 411 | size, |
| 412 | count, |
| 413 | IFNULL(source_file, '') as sourceFile, |
| 414 | IFNULL(line_number, -1) as lineNumber |
| 415 | from ${tableName} ${orderBy}`); |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 416 | |
Tuchila Octavian | 3b31ed6 | 2021-09-14 12:27:26 +0100 | [diff] [blame] | 417 | const flamegraphData: CallsiteInfo[] = []; |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 418 | const hashToindex: Map<number, number> = new Map(); |
Hector Dearman | 7b86465 | 2021-07-16 10:44:13 +0100 | [diff] [blame] | 419 | const it = callsites.iter({ |
| 420 | hash: NUM, |
| 421 | name: STR, |
| 422 | parentHash: NUM, |
| 423 | depth: NUM, |
| 424 | cumulativeSize: NUM, |
| 425 | cumulativeAllocSize: NUM, |
| 426 | cumulativeCount: NUM, |
| 427 | cumulativeAllocCount: NUM, |
| 428 | mapping: STR, |
| 429 | sourceFile: STR, |
| 430 | lineNumber: NUM, |
| 431 | size: NUM, |
| 432 | count: NUM, |
| 433 | }); |
| 434 | for (let i = 0; it.valid(); ++i, it.next()) { |
| 435 | const hash = it.hash; |
| 436 | let name = it.name; |
| 437 | const parentHash = it.parentHash; |
| 438 | const depth = it.depth; |
| 439 | const totalSize = it[totalColumnName]; |
| 440 | const selfSize = it[selfColumnName]; |
| 441 | const mapping = it.mapping; |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 442 | const highlighted = |
| 443 | focusRegex !== '' && |
| 444 | name.toLocaleLowerCase().includes(focusRegex.toLocaleLowerCase()); |
| 445 | const parentId = hashToindex.has(+parentHash) |
| 446 | ? hashToindex.get(+parentHash)! |
| 447 | : -1; |
Tuchila Octavian | 795f60b | 2021-05-24 16:39:38 +0100 | [diff] [blame] | 448 | |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 449 | let location: string | undefined; |
Hector Dearman | 7b86465 | 2021-07-16 10:44:13 +0100 | [diff] [blame] | 450 | if (/[a-zA-Z]/i.test(it.sourceFile)) { |
| 451 | location = it.sourceFile; |
| 452 | if (it.lineNumber !== -1) { |
| 453 | location += `:${it.lineNumber}`; |
Tuchila Octavian | 795f60b | 2021-05-24 16:39:38 +0100 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Florian Mayer | 68845bf | 2020-01-23 13:29:58 +0000 | [diff] [blame] | 457 | if (depth === maxDepth - 1) { |
| 458 | name += ' [tree truncated]'; |
| 459 | } |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 460 | // Instead of hash, we will store index of callsite in this original array |
| 461 | // as an id of callsite. That way, we have quicker access to parent and it |
Hector Dearman | 7b86465 | 2021-07-16 10:44:13 +0100 | [diff] [blame] | 462 | // will stay unique: |
| 463 | hashToindex.set(hash, i); |
| 464 | |
Florian Mayer | 697e0c2 | 2020-01-30 17:01:10 +0000 | [diff] [blame] | 465 | flamegraphData.push({ |
| 466 | id: i, |
| 467 | totalSize, |
| 468 | depth, |
| 469 | parentId, |
| 470 | name, |
| 471 | selfSize, |
| 472 | mapping, |
Isabelle Taylor | 8ee2583 | 2020-10-28 16:25:27 +0000 | [diff] [blame] | 473 | merged: false, |
Tuchila Octavian | 795f60b | 2021-05-24 16:39:38 +0100 | [diff] [blame] | 474 | highlighted, |
Hector Dearman | 23c078b | 2022-06-05 12:00:25 +0100 | [diff] [blame] | 475 | location, |
Florian Mayer | 697e0c2 | 2020-01-30 17:01:10 +0000 | [diff] [blame] | 476 | }); |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 477 | } |
| 478 | return flamegraphData; |
| 479 | } |
| 480 | |
Hector Dearman | da988af | 2020-02-25 13:42:42 +0000 | [diff] [blame] | 481 | private async prepareViewsAndTables( |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 482 | start: time, |
| 483 | end: time, |
| 484 | upids: number[], |
| 485 | type: ProfileType, |
| 486 | focusRegex: string, |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 487 | viewingOption: FlamegraphStateViewingOption, |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 488 | ): Promise<string> { |
Ioannis Ilkos | 40cc966 | 2022-07-15 15:35:09 +0100 | [diff] [blame] | 489 | const flamegraphType = getFlamegraphType(type); |
Ioannis Ilkos | 40cc966 | 2022-07-15 15:35:09 +0100 | [diff] [blame] | 490 | if (type === ProfileType.PERF_SAMPLE) { |
Lalit Maganti | c07333b | 2024-01-24 01:12:49 +0000 | [diff] [blame] | 491 | let upid: string; |
| 492 | let upidGroup: string; |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 493 | if (upids.length > 1) { |
Lalit Maganti | c07333b | 2024-01-24 01:12:49 +0000 | [diff] [blame] | 494 | upid = `NULL`; |
| 495 | upidGroup = `'${FlamegraphController.serializeUpidGroup(upids)}'`; |
| 496 | } else { |
| 497 | upid = `${upids[0]}`; |
| 498 | upidGroup = `NULL`; |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 499 | } |
Tuchila Octavian | c1e0f4c | 2021-09-23 17:35:09 +0100 | [diff] [blame] | 500 | return this.cache.getTableName( |
Steve Golton | 5dbacf5 | 2024-01-25 09:20:51 +0000 | [diff] [blame] | 501 | `select id, name, map_name, parent_id, depth, cumulative_size, |
Tuchila Octavian | c1e0f4c | 2021-09-23 17:35:09 +0100 | [diff] [blame] | 502 | cumulative_alloc_size, cumulative_count, cumulative_alloc_count, |
| 503 | size, alloc_size, count, alloc_count, source_file, line_number |
Lalit Maganti | c07333b | 2024-01-24 01:12:49 +0000 | [diff] [blame] | 504 | from experimental_flamegraph( |
| 505 | '${flamegraphType}', |
| 506 | NULL, |
| 507 | '>=${start},<=${end}', |
| 508 | ${upid}, |
| 509 | ${upidGroup}, |
| 510 | '${focusRegex}' |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 511 | )`, |
| 512 | ); |
Tuchila Octavian | c1e0f4c | 2021-09-23 17:35:09 +0100 | [diff] [blame] | 513 | } |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 514 | if ( |
| 515 | type === ProfileType.JAVA_HEAP_GRAPH && |
| 516 | isHeapGraphDominatorTreeViewingOption(viewingOption) |
| 517 | ) { |
| 518 | return this.cache.getTableName( |
| 519 | await this.loadHeapGraphDominatorTreeQuery(upids[0], end), |
| 520 | ); |
| 521 | } |
Hector Dearman | da988af | 2020-02-25 13:42:42 +0000 | [diff] [blame] | 522 | return this.cache.getTableName( |
Steve Golton | 5dbacf5 | 2024-01-25 09:20:51 +0000 | [diff] [blame] | 523 | `select id, name, map_name, parent_id, depth, cumulative_size, |
Florian Mayer | e13b68d | 2020-01-22 13:19:41 +0000 | [diff] [blame] | 524 | cumulative_alloc_size, cumulative_count, cumulative_alloc_count, |
Tuchila Octavian | 795f60b | 2021-05-24 16:39:38 +0100 | [diff] [blame] | 525 | size, alloc_size, count, alloc_count, source_file, line_number |
Lalit Maganti | c07333b | 2024-01-24 01:12:49 +0000 | [diff] [blame] | 526 | from experimental_flamegraph( |
| 527 | '${flamegraphType}', |
| 528 | ${end}, |
| 529 | NULL, |
| 530 | ${upids[0]}, |
| 531 | NULL, |
| 532 | '${focusRegex}' |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 533 | )`, |
| 534 | ); |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 537 | private async loadHeapGraphDominatorTreeQuery(upid: number, timestamp: time) { |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 538 | const outputTableName = `heap_graph_type_dominated_${upid}_${timestamp}`; |
| 539 | |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 540 | this.args.engine.query(` |
| 541 | INCLUDE PERFETTO MODULE memory.heap_graph_dominator_tree; |
| 542 | |
| 543 | -- heap graph dominator tree with objects as nodes and all relavant |
| 544 | -- object self stats and dominated stats |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 545 | CREATE PERFETTO TABLE _heap_graph_object_dominated AS |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 546 | SELECT |
| 547 | node.id, |
| 548 | node.idom_id, |
| 549 | node.dominated_obj_count, |
| 550 | node.dominated_size_bytes + node.dominated_native_size_bytes AS dominated_size, |
| 551 | node.depth, |
| 552 | obj.type_id, |
| 553 | obj.root_type, |
| 554 | obj.self_size + obj.native_size AS self_size |
| 555 | FROM memory_heap_graph_dominator_tree node |
| 556 | JOIN heap_graph_object obj USING(id) |
| 557 | WHERE obj.upid = ${upid} AND obj.graph_sample_ts = ${timestamp} |
| 558 | -- required to accelerate the recursive cte below |
| 559 | ORDER BY idom_id; |
| 560 | |
| 561 | -- calculate for each object node in the dominator tree the |
| 562 | -- HASH(path of type_id's from the super root to the object) |
| 563 | CREATE PERFETTO TABLE _dominator_tree_path_hash AS |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 564 | WITH RECURSIVE _tree_visitor(id, path_hash) AS ( |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 565 | SELECT |
| 566 | id, |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 567 | HASH( |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 568 | CAST(type_id AS TEXT) || '-' || IFNULL(root_type, '') |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 569 | ) AS path_hash |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 570 | FROM _heap_graph_object_dominated |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 571 | WHERE depth = 1 |
| 572 | UNION ALL |
| 573 | SELECT |
| 574 | child.id, |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 575 | HASH(CAST(parent.path_hash AS TEXT) || '/' || CAST(type_id AS TEXT)) AS path_hash |
| 576 | FROM _heap_graph_object_dominated child |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 577 | JOIN _tree_visitor parent ON child.idom_id = parent.id |
| 578 | ) |
| 579 | SELECT * from _tree_visitor |
| 580 | ORDER BY id; |
| 581 | |
| 582 | -- merge object nodes with the same path into one "class type node", so the |
| 583 | -- end result is a tree where nodes are identified by their types and the |
| 584 | -- dominator relationships are preserved. |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 585 | CREATE PERFETTO TABLE ${outputTableName} AS |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 586 | SELECT |
| 587 | map.path_hash as id, |
| 588 | COALESCE(cls.deobfuscated_name, cls.name, '[NULL]') || IIF( |
| 589 | node.root_type IS NOT NULL, |
| 590 | ' [' || node.root_type || ']', '' |
| 591 | ) AS name, |
| 592 | IFNULL(parent_map.path_hash, -1) AS parent_id, |
| 593 | node.depth - 1 AS depth, |
| 594 | sum(dominated_size) AS cumulative_size, |
| 595 | -1 AS cumulative_alloc_size, |
| 596 | sum(dominated_obj_count) AS cumulative_count, |
| 597 | -1 AS cumulative_alloc_count, |
| 598 | '' as map_name, |
| 599 | '' as source_file, |
| 600 | -1 as line_number, |
| 601 | sum(self_size) AS size, |
| 602 | count(*) AS count |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 603 | FROM _heap_graph_object_dominated node |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 604 | JOIN _dominator_tree_path_hash map USING(id) |
| 605 | LEFT JOIN _dominator_tree_path_hash parent_map ON node.idom_id = parent_map.id |
| 606 | JOIN heap_graph_class cls ON node.type_id = cls.id |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 607 | GROUP BY map.path_hash, name, parent_id, depth, map_name, source_file, line_number; |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 608 | |
Ioannis Ilkos | e03ca50 | 2024-04-23 18:57:23 +0100 | [diff] [blame^] | 609 | -- These are intermediates and not needed |
| 610 | DROP TABLE _heap_graph_object_dominated; |
| 611 | DROP TABLE _dominator_tree_path_hash; |
| 612 | `); |
| 613 | |
| 614 | return `SELECT * FROM ${outputTableName}`; |
Bingqian Liu | 7afd545 | 2024-03-27 18:42:47 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 617 | getMinSizeDisplayed( |
| 618 | flamegraphData: CallsiteInfo[], |
| 619 | rootSize?: number, |
| 620 | ): number { |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 621 | const timeState = globals.state.frontendLocalState.visibleState; |
Steve Golton | f3897e2 | 2023-05-11 14:18:30 +0100 | [diff] [blame] | 622 | const dur = globals.stateVisibleTime().duration; |
Steve Golton | b3a389d | 2023-07-10 11:03:17 +0100 | [diff] [blame] | 623 | // TODO(stevegolton): Does this actually do what we want??? |
| 624 | let width = Duration.toSeconds(dur / timeState.resolution); |
Hector Dearman | 77bf83f | 2020-09-09 15:54:07 +0100 | [diff] [blame] | 625 | // TODO(168048193): Remove screen size hack: |
| 626 | width = Math.max(width, 800); |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 627 | if (rootSize === undefined) { |
| 628 | rootSize = findRootSize(flamegraphData); |
| 629 | } |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 630 | return (MIN_PIXEL_DISPLAYED * rootSize) / width; |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 631 | } |
Neda Topoljanac | 1743241 | 2019-11-29 16:50:44 +0000 | [diff] [blame] | 632 | |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 633 | async getFlamegraphMetadata( |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 634 | type: ProfileType, |
| 635 | start: time, |
| 636 | end: time, |
| 637 | upids: number[], |
| 638 | ): Promise<FlamegraphDetails | undefined> { |
Neda Topoljanac | 1743241 | 2019-11-29 16:50:44 +0000 | [diff] [blame] | 639 | // Don't do anything if selection of the marker stayed the same. |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 640 | if ( |
| 641 | this.lastSelectedFlamegraphState !== undefined && |
| 642 | this.lastSelectedFlamegraphState.start === start && |
| 643 | this.lastSelectedFlamegraphState.end === end && |
| 644 | FlamegraphController.areArraysEqual( |
| 645 | this.lastSelectedFlamegraphState.upids, |
| 646 | upids, |
| 647 | ) |
| 648 | ) { |
Neda Topoljanac | 1743241 | 2019-11-29 16:50:44 +0000 | [diff] [blame] | 649 | return undefined; |
| 650 | } |
| 651 | |
Tuchila Octavian | 5204758 | 2021-09-21 15:35:54 +0100 | [diff] [blame] | 652 | // Collecting data for more information about profile, such as: |
Neda Topoljanac | 1743241 | 2019-11-29 16:50:44 +0000 | [diff] [blame] | 653 | // total memory allocated, memory that is allocated and not freed. |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 654 | const upidGroup = FlamegraphController.serializeUpidGroup(upids); |
| 655 | |
Hector Dearman | 7514f30 | 2021-08-19 18:21:52 +0100 | [diff] [blame] | 656 | const result = await this.args.engine.query( |
Steve Golton | 7e63f42 | 2024-03-18 14:17:32 +0000 | [diff] [blame] | 657 | `select pid from process where upid in (${upidGroup})`, |
| 658 | ); |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 659 | const it = result.iter({pid: NUM}); |
| 660 | const pids = []; |
| 661 | for (let i = 0; it.valid(); ++i, it.next()) { |
| 662 | pids.push(it.pid); |
| 663 | } |
Steve Golton | f3897e2 | 2023-05-11 14:18:30 +0100 | [diff] [blame] | 664 | return {start, dur: end - start, pids, upids, type}; |
Tuchila Octavian | d54b3cd | 2021-11-15 10:38:26 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | private static areArraysEqual(a: number[], b: number[]) { |
| 668 | if (a.length !== b.length) { |
| 669 | return false; |
| 670 | } |
| 671 | for (let i = 0; i < a.length; i++) { |
| 672 | if (a[i] !== b[i]) { |
| 673 | return false; |
| 674 | } |
| 675 | } |
| 676 | return true; |
| 677 | } |
| 678 | |
| 679 | private static serializeUpidGroup(upids: number[]) { |
| 680 | return new Array(upids).join(); |
Neda Topoljanac | 1743241 | 2019-11-29 16:50:44 +0000 | [diff] [blame] | 681 | } |
Neda Topoljanac | b62621c | 2019-11-26 12:41:58 +0000 | [diff] [blame] | 682 | } |