|
| 1 | +// Copyright (C) 2026 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 | + |
| 15 | +import m from 'mithril'; |
| 16 | +import type {Engine} from '../../../trace_processor/engine'; |
| 17 | +import type {SqlValue} from '../../../trace_processor/query_result'; |
| 18 | +import {DataGrid} from '../../../components/widgets/datagrid/datagrid'; |
| 19 | +import {SQLDataSource} from '../../../components/widgets/datagrid/sql_data_source'; |
| 20 | +import {createSimpleSchema} from '../../../components/widgets/datagrid/sql_schema'; |
| 21 | +import type {SchemaRegistry} from '../../../components/widgets/datagrid/datagrid_schema'; |
| 22 | +import {fmtHex} from '../format'; |
| 23 | +import { |
| 24 | + type NavFn, |
| 25 | + sizeRenderer, |
| 26 | + shortClassName, |
| 27 | + DOMINATOR_TREE_PREAMBLE, |
| 28 | +} from '../components'; |
| 29 | + |
| 30 | +interface FlamegraphObjectsViewAttrs { |
| 31 | + engine: Engine; |
| 32 | + navigate: NavFn; |
| 33 | + onBackToTimeline?: () => void; |
| 34 | + nodeName?: string; |
| 35 | + pathHashes?: string; |
| 36 | + isDominator?: boolean; |
| 37 | +} |
| 38 | + |
| 39 | +function flamegraphQuery(pathHashes: string, isDominator: boolean): string { |
| 40 | + const hashTable = isDominator |
| 41 | + ? '_heap_graph_dominator_path_hashes' |
| 42 | + : '_heap_graph_path_hashes'; |
| 43 | + const values = pathHashes |
| 44 | + .split(',') |
| 45 | + .map((v) => `(${v.trim()})`) |
| 46 | + .join(', '); |
| 47 | + return ` |
| 48 | + WITH _hde_sel(path_hash) AS (VALUES ${values}) |
| 49 | + SELECT |
| 50 | + o.id, |
| 51 | + ifnull(c.deobfuscated_name, c.name) AS cls, |
| 52 | + o.self_size, |
| 53 | + o.native_size, |
| 54 | + ifnull(d.dominated_size_bytes, o.self_size) AS retained, |
| 55 | + ifnull(d.dominated_native_size_bytes, o.native_size) AS retained_native, |
| 56 | + ifnull(o.heap_type, 'default') AS heap, |
| 57 | + od.value_string AS str |
| 58 | + FROM _hde_sel f |
| 59 | + JOIN ${hashTable} h ON h.path_hash = f.path_hash |
| 60 | + JOIN heap_graph_object o ON o.id = h.id |
| 61 | + JOIN heap_graph_class c ON o.type_id = c.id |
| 62 | + LEFT JOIN heap_graph_dominator_tree d ON d.id = o.id |
| 63 | + LEFT JOIN heap_graph_object_data od ON od.object_id = o.id |
| 64 | + `; |
| 65 | +} |
| 66 | + |
| 67 | +function makeUiSchema(navigate: NavFn): SchemaRegistry { |
| 68 | + return { |
| 69 | + query: { |
| 70 | + id: { |
| 71 | + title: 'Object', |
| 72 | + columnType: 'identifier', |
| 73 | + cellRenderer: (value: SqlValue, row) => { |
| 74 | + const id = Number(value); |
| 75 | + const cls = String(row.cls ?? ''); |
| 76 | + const display = `${shortClassName(cls)} ${fmtHex(id)}`; |
| 77 | + const str = row.str != null ? String(row.str) : null; |
| 78 | + return m('span', [ |
| 79 | + m( |
| 80 | + 'button', |
| 81 | + { |
| 82 | + class: 'ah-link', |
| 83 | + onclick: () => |
| 84 | + navigate('object', {id, label: str ? `"${str}"` : display}), |
| 85 | + }, |
| 86 | + display, |
| 87 | + ), |
| 88 | + str |
| 89 | + ? m( |
| 90 | + 'span', |
| 91 | + {class: 'ah-str-badge'}, |
| 92 | + ` "${str.length > 40 ? str.slice(0, 40) + '\u2026' : str}"`, |
| 93 | + ) |
| 94 | + : null, |
| 95 | + ]); |
| 96 | + }, |
| 97 | + }, |
| 98 | + self_size: { |
| 99 | + title: 'Shallow', |
| 100 | + columnType: 'quantitative', |
| 101 | + cellRenderer: sizeRenderer, |
| 102 | + }, |
| 103 | + native_size: { |
| 104 | + title: 'Native', |
| 105 | + columnType: 'quantitative', |
| 106 | + cellRenderer: sizeRenderer, |
| 107 | + }, |
| 108 | + retained: { |
| 109 | + title: 'Retained', |
| 110 | + columnType: 'quantitative', |
| 111 | + cellRenderer: sizeRenderer, |
| 112 | + }, |
| 113 | + retained_native: { |
| 114 | + title: 'Retained Native', |
| 115 | + columnType: 'quantitative', |
| 116 | + cellRenderer: sizeRenderer, |
| 117 | + }, |
| 118 | + heap: { |
| 119 | + title: 'Heap', |
| 120 | + columnType: 'text', |
| 121 | + }, |
| 122 | + cls: { |
| 123 | + title: 'Class', |
| 124 | + columnType: 'text', |
| 125 | + }, |
| 126 | + str: { |
| 127 | + title: 'String Value', |
| 128 | + columnType: 'text', |
| 129 | + }, |
| 130 | + }, |
| 131 | + }; |
| 132 | +} |
| 133 | + |
| 134 | +function FlamegraphObjectsView(): m.Component<FlamegraphObjectsViewAttrs> { |
| 135 | + let dataSource: SQLDataSource | null = null; |
| 136 | + let lastPathHashes: string | undefined; |
| 137 | + |
| 138 | + return { |
| 139 | + oninit(vnode) { |
| 140 | + const {pathHashes, isDominator, engine} = vnode.attrs; |
| 141 | + lastPathHashes = pathHashes; |
| 142 | + if (pathHashes) { |
| 143 | + dataSource = new SQLDataSource({ |
| 144 | + engine, |
| 145 | + sqlSchema: createSimpleSchema( |
| 146 | + flamegraphQuery(pathHashes, isDominator ?? true), |
| 147 | + ), |
| 148 | + rootSchemaName: 'query', |
| 149 | + preamble: DOMINATOR_TREE_PREAMBLE, |
| 150 | + }); |
| 151 | + } |
| 152 | + }, |
| 153 | + onupdate(vnode) { |
| 154 | + if (vnode.attrs.pathHashes !== lastPathHashes) { |
| 155 | + const {pathHashes, isDominator, engine} = vnode.attrs; |
| 156 | + lastPathHashes = pathHashes; |
| 157 | + if (pathHashes) { |
| 158 | + dataSource = new SQLDataSource({ |
| 159 | + engine, |
| 160 | + sqlSchema: createSimpleSchema( |
| 161 | + flamegraphQuery(pathHashes, isDominator ?? true), |
| 162 | + ), |
| 163 | + rootSchemaName: 'query', |
| 164 | + preamble: DOMINATOR_TREE_PREAMBLE, |
| 165 | + }); |
| 166 | + } else { |
| 167 | + dataSource = null; |
| 168 | + } |
| 169 | + } |
| 170 | + }, |
| 171 | + view(vnode) { |
| 172 | + const {navigate, nodeName, onBackToTimeline} = vnode.attrs; |
| 173 | + |
| 174 | + if (!dataSource) { |
| 175 | + return m('div', [ |
| 176 | + nodeName |
| 177 | + ? m( |
| 178 | + 'h2', |
| 179 | + {class: 'ah-view-heading'}, |
| 180 | + 'Flamegraph: ', |
| 181 | + m('span', {class: 'ah-mono'}, nodeName), |
| 182 | + ) |
| 183 | + : null, |
| 184 | + m( |
| 185 | + 'div', |
| 186 | + {class: 'ah-card ah-mb-3'}, |
| 187 | + m( |
| 188 | + 'p', |
| 189 | + 'No flamegraph selection found. Select a node in the ', |
| 190 | + 'flamegraph and choose "Open in Heapdump Explorer" to see objects here.', |
| 191 | + ), |
| 192 | + ), |
| 193 | + ]); |
| 194 | + } |
| 195 | + |
| 196 | + return m('div', {class: 'ah-view-content'}, [ |
| 197 | + m('div', {class: 'ah-heading-row'}, [ |
| 198 | + m( |
| 199 | + 'h2', |
| 200 | + {class: 'ah-view-heading'}, |
| 201 | + nodeName |
| 202 | + ? ['Flamegraph: ', m('span', {class: 'ah-mono'}, nodeName)] |
| 203 | + : 'Flamegraph Objects', |
| 204 | + ), |
| 205 | + onBackToTimeline |
| 206 | + ? m( |
| 207 | + 'button', |
| 208 | + {class: 'ah-download-link', onclick: onBackToTimeline}, |
| 209 | + 'Back to Timeline', |
| 210 | + ) |
| 211 | + : null, |
| 212 | + ]), |
| 213 | + m(DataGrid, { |
| 214 | + schema: makeUiSchema(navigate), |
| 215 | + rootSchema: 'query', |
| 216 | + data: dataSource, |
| 217 | + fillHeight: true, |
| 218 | + initialColumns: [ |
| 219 | + {id: 'self_size', field: 'self_size'}, |
| 220 | + {id: 'native_size', field: 'native_size'}, |
| 221 | + {id: 'retained', field: 'retained'}, |
| 222 | + {id: 'retained_native', field: 'retained_native'}, |
| 223 | + {id: 'heap', field: 'heap'}, |
| 224 | + {id: 'cls', field: 'cls'}, |
| 225 | + {id: 'id', field: 'id'}, |
| 226 | + ], |
| 227 | + showExportButton: true, |
| 228 | + }), |
| 229 | + ]); |
| 230 | + }, |
| 231 | + }; |
| 232 | +} |
| 233 | + |
| 234 | +export default FlamegraphObjectsView; |
0 commit comments