oxedyne/fe2o3/fe2o3_austenite/tools/bench/cpuprof_rank.mjs
9.2 KiB, 1 run
created by r1870400018:66228, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #!/usr/bin/env node |
| 2 | // Ranks a V8 `.cpuprofile` (node `--cpu-prof`, or the inspector's Profiler domain, which |
| 3 | // cpuprof_runs.mjs drives) taken on the profiling wasm package: the top functions by self time, and the |
| 4 | // totals by module and by crate. |
| 5 | // |
| 6 | // Rust symbols in the wasm name section are legacy-mangled (`_ZN...17h<hash>E`) and are demangled here, so |
| 7 | // the output reads `oxedyne_fe2o3_austenite::flow::block::layout`. A frame's *crate* is the first path |
| 8 | // segment of its name; its *module* is the first two (`flow::block` within the crate), and its *area* the |
| 9 | // first one. Three views carry the totals: |
| 10 | // |
| 11 | // - self time by the function's own crate and module: where the instructions were; |
| 12 | // - self time attributed to the nearest `oxedyne_fe2o3_austenite` frame on the stack: which of |
| 13 | // Austenite's modules asked for the work, whoever executed it (alloc, hashbrown, memcpy); |
| 14 | // - inclusive time of named functions (`--incl=a,b`, substrings of the demangled name): the share of the |
| 15 | // samples that had one of them on the stack, counted once per sample whatever the recursion; |
| 16 | // - the callers of named functions (`--callers=a,b`): the self time of a function whose name contains the |
| 17 | // substring, split by the nearest frame above it that is not itself a match. |
| 18 | // |
| 19 | // A sample's duration is the delta to the next sample, as the DevTools profile view takes it. |
| 20 | // |
| 21 | // Usage: cpuprof_rank.mjs FILE.cpuprofile [--top=30] [--incl=name,name,...] [--callers=name,...] [--json] |
| 22 | import fs from 'node:fs'; |
| 23 | |
| 24 | const AUSTENITE = 'oxedyne_fe2o3_austenite'; |
| 25 | |
| 26 | const ESCAPES = { |
| 27 | SP: '@', BP: '*', RF: '&', LT: '<', GT: '>', LP: '(', RP: ')', C: ',', |
| 28 | }; |
| 29 | |
| 30 | // One identifier of a legacy-mangled path: `$LT$` and the rest, `$u20$` and the like, `..` for `::`. |
| 31 | function unescapeIdent(id) { |
| 32 | let s = id.startsWith('_$') ? id.slice(1) : id; |
| 33 | s = s.replace(/\$([A-Za-z]+)\$/g, (m, name) => (name in ESCAPES ? ESCAPES[name] : m)); |
| 34 | s = s.replace(/\$u([0-9a-f]+)\$/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))); |
| 35 | return s.replace(/\.\./g, '::'); |
| 36 | } |
| 37 | |
| 38 | // `_ZN<len><ident>...E` to a path; the trailing `h<16 hex>` hash segment is dropped. A name wasm-bindgen |
| 39 | // has already demangled loses its `::h<16 hex>` suffix. Anything else comes back as it was. |
| 40 | export function demangle(name) { |
| 41 | if (!name.startsWith('_ZN')) return name.replace(/::h[0-9a-f]{16}$/, ''); |
| 42 | let i = 3; |
| 43 | const parts = []; |
| 44 | while (i < name.length && name[i] !== 'E') { |
| 45 | let j = i; |
| 46 | while (j < name.length && name[j] >= '0' && name[j] <= '9') j++; |
| 47 | if (j === i) return name; |
| 48 | const len = Number(name.slice(i, j)); |
| 49 | parts.push(name.slice(j, j + len)); |
| 50 | i = j + len; |
| 51 | } |
| 52 | if (parts.length && /^h[0-9a-f]{16}$/.test(parts[parts.length - 1])) parts.pop(); |
| 53 | return parts.map(unescapeIdent).join('::'); |
| 54 | } |
| 55 | |
| 56 | const PATH = /^[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*/; |
| 57 | |
| 58 | // The path a name's crate and module come from: the implementing type's for `<T as Trait>::f`, else the |
| 59 | // function's own. |
| 60 | function ownerPath(dem) { |
| 61 | let s = dem; |
| 62 | while (s.startsWith('<')) s = s.slice(1); |
| 63 | const m = s.match(PATH); |
| 64 | if (m && m[0].includes('::')) return m[0]; |
| 65 | const as = dem.indexOf(' as '); |
| 66 | if (as >= 0) { |
| 67 | const t = dem.slice(as + 4).match(PATH); |
| 68 | if (t) return t[0]; |
| 69 | } |
| 70 | return m ? m[0] : null; |
| 71 | } |
| 72 | |
| 73 | // What a frame is: its demangled name and where it counts. |
| 74 | function classify(cf) { |
| 75 | const raw = cf.functionName || ''; |
| 76 | const url = cf.url || ''; |
| 77 | if (raw.startsWith('(')) return { name: raw, crate: raw, module: raw, area: raw, kind: 'runtime' }; |
| 78 | const wasm = url.startsWith('wasm://') || /^wasm-function\[/.test(raw) || raw.startsWith('_ZN') || /::h[0-9a-f]{16}$/.test(raw); |
| 79 | if (!wasm) { |
| 80 | const where = url ? url.split('/').pop() : ''; |
| 81 | const name = `${raw || '(anonymous)'}${where ? ` [${where}]` : ''}`; |
| 82 | return { name, crate: '(js)', module: '(js)', area: '(js)', kind: 'js' }; |
| 83 | } |
| 84 | const name = demangle(raw); |
| 85 | const path = ownerPath(name); |
| 86 | if (!path) return { name, crate: '(wasm)', module: '(wasm)', area: '(wasm)', kind: 'wasm' }; |
| 87 | const seg = path.split('::'); |
| 88 | return { |
| 89 | name, |
| 90 | crate: seg[0], |
| 91 | module: seg.length > 1 ? `${seg[0]}::${seg[1]}` : seg[0], |
| 92 | area: seg[0] === AUSTENITE && seg.length > 1 ? `${seg[0]}::${seg[1]}` : seg[0], |
| 93 | kind: 'wasm', |
| 94 | path: seg, |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | // The austenite module a frame belongs to, two segments deep: `flow::block`. |
| 99 | function austeniteModule(info) { |
| 100 | if (info.crate !== AUSTENITE || !info.path) return null; |
| 101 | return info.path.slice(1, 3).join('::') || '(root)'; |
| 102 | } |
| 103 | |
| 104 | function add(map, key, ms) { |
| 105 | map.set(key, (map.get(key) || 0) + ms); |
| 106 | } |
| 107 | |
| 108 | function table(map, total, top) { |
| 109 | return [...map.entries()].sort((a, b) => b[1] - a[1]).slice(0, top).map(([name, ms]) => ({ |
| 110 | name, ms: ms, pct: total > 0 ? (100 * ms) / total : 0, |
| 111 | })); |
| 112 | } |
| 113 | |
| 114 | export function rank(profile, opts = {}) { |
| 115 | const top = opts.top ?? 30; |
| 116 | const incl = opts.incl ?? []; |
| 117 | const callers = opts.callers ?? []; |
| 118 | const nodes = new Map(); |
| 119 | for (const n of profile.nodes) nodes.set(n.id, n); |
| 120 | const parent = new Map(); |
| 121 | for (const n of profile.nodes) for (const c of n.children || []) parent.set(c, n.id); |
| 122 | const info = new Map(); |
| 123 | for (const n of profile.nodes) info.set(n.id, classify(n.callFrame)); |
| 124 | |
| 125 | const samples = profile.samples; |
| 126 | const deltas = profile.timeDeltas; |
| 127 | const durMs = (i) => (i + 1 < deltas.length ? deltas[i + 1] : 0) / 1000; |
| 128 | |
| 129 | const self = new Map(); // function name -> ms |
| 130 | const byCrate = new Map(); |
| 131 | const byModule = new Map(); |
| 132 | const byArea = new Map(); |
| 133 | const byAskedBy = new Map(); // nearest austenite module on the stack |
| 134 | const byKind = new Map(); |
| 135 | const inclMs = new Map(incl.map((s) => [s, 0])); |
| 136 | const callerMs = new Map(callers.map((s) => [s, new Map()])); |
| 137 | let total = 0; |
| 138 | let idle = 0; |
| 139 | for (let i = 0; i < samples.length; i++) { |
| 140 | const ms = durMs(i); |
| 141 | const id = samples[i]; |
| 142 | const inf = info.get(id); |
| 143 | if (!inf) continue; |
| 144 | if (inf.name === '(idle)') { |
| 145 | idle += ms; |
| 146 | continue; |
| 147 | } |
| 148 | total += ms; |
| 149 | add(self, inf.name, ms); |
| 150 | add(byCrate, inf.crate, ms); |
| 151 | add(byModule, inf.module, ms); |
| 152 | add(byArea, inf.area, ms); |
| 153 | add(byKind, inf.kind, ms); |
| 154 | let asked = null; |
| 155 | const seen = new Set(); |
| 156 | for (let at = id; at !== undefined; at = parent.get(at)) { |
| 157 | const f = info.get(at); |
| 158 | if (!f) continue; |
| 159 | if (asked === null) asked = austeniteModule(f); |
| 160 | for (const s of incl) { |
| 161 | if (!seen.has(s) && f.name.includes(s)) { |
| 162 | seen.add(s); |
| 163 | inclMs.set(s, inclMs.get(s) + ms); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | add(byAskedBy, asked === null ? '(no austenite frame)' : `austenite::${asked}`, ms); |
| 168 | for (const s of callers) { |
| 169 | if (!inf.name.includes(s)) continue; |
| 170 | let at = parent.get(id); |
| 171 | while (at !== undefined && info.get(at) && info.get(at).name.includes(s)) at = parent.get(at); |
| 172 | add(callerMs.get(s), at === undefined || !info.get(at) ? '(root)' : info.get(at).name, ms); |
| 173 | } |
| 174 | } |
| 175 | return { |
| 176 | total_ms: total, |
| 177 | idle_ms: idle, |
| 178 | samples: samples.length, |
| 179 | span_ms: (profile.endTime - profile.startTime) / 1000, |
| 180 | top_self: table(self, total, top), |
| 181 | by_crate: table(byCrate, total, 40), |
| 182 | by_module: table(byModule, total, 40), |
| 183 | by_area: table(byArea, total, 40), |
| 184 | asked_by: table(byAskedBy, total, 40), |
| 185 | by_kind: table(byKind, total, 10), |
| 186 | inclusive: [...inclMs.entries()].map(([name, ms]) => ({ name, ms, pct: total > 0 ? (100 * ms) / total : 0 })), |
| 187 | callers: [...callerMs.entries()].map(([name, m]) => ({ name, rows: table(m, total, 8) })), |
| 188 | }; |
| 189 | } |
| 190 | |
| 191 | function fmtRows(rows, width = 9) { |
| 192 | return rows.map((r) => `${r.ms.toFixed(1).padStart(width)} ms ${r.pct.toFixed(1).padStart(5)}% ${r.name}`).join('\n'); |
| 193 | } |
| 194 | |
| 195 | export function render(r) { |
| 196 | const out = []; |
| 197 | out.push(`samples ${r.samples}, busy ${r.total_ms.toFixed(1)} ms (idle ${r.idle_ms.toFixed(1)} ms, span ${r.span_ms.toFixed(1)} ms)`); |
| 198 | out.push('\nTop functions by self time'); |
| 199 | out.push(fmtRows(r.top_self)); |
| 200 | out.push('\nSelf time by crate'); |
| 201 | out.push(fmtRows(r.by_crate)); |
| 202 | out.push('\nSelf time by module (crate::module)'); |
| 203 | out.push(fmtRows(r.by_module)); |
| 204 | out.push('\nSelf time attributed to the nearest austenite frame on the stack (module, two deep)'); |
| 205 | out.push(fmtRows(r.asked_by)); |
| 206 | out.push('\nSelf time by area (austenite::area, other crates whole)'); |
| 207 | out.push(fmtRows(r.by_area)); |
| 208 | out.push('\nSelf time by kind of frame'); |
| 209 | out.push(fmtRows(r.by_kind)); |
| 210 | if (r.inclusive.length) { |
| 211 | out.push('\nInclusive time of named functions'); |
| 212 | out.push(fmtRows(r.inclusive)); |
| 213 | } |
| 214 | for (const c of r.callers) { |
| 215 | out.push(`\nCallers of self time in ${c.name}`); |
| 216 | out.push(fmtRows(c.rows)); |
| 217 | } |
| 218 | return out.join('\n') + '\n'; |
| 219 | } |
| 220 | |
| 221 | function main() { |
| 222 | const args = { top: 30, incl: [], callers: [], json: false }; |
| 223 | let file = null; |
| 224 | for (const a of process.argv.slice(2)) { |
| 225 | let m; |
| 226 | if ((m = a.match(/^--top=(\d+)$/))) args.top = Number(m[1]); |
| 227 | else if ((m = a.match(/^--incl=(.*)$/))) args.incl = m[1].split(',').filter(Boolean); |
| 228 | else if ((m = a.match(/^--callers=(.*)$/))) args.callers = m[1].split(',').filter(Boolean); |
| 229 | else if (a === '--json') args.json = true; |
| 230 | else if (!a.startsWith('--')) file = a; |
| 231 | } |
| 232 | if (!file) { |
| 233 | process.stderr.write('usage: cpuprof_rank.mjs FILE.cpuprofile [--top=30] [--incl=name,name] [--callers=name,...] [--json]\n'); |
| 234 | process.exit(2); |
| 235 | } |
| 236 | const r = rank(JSON.parse(fs.readFileSync(file, 'utf8')), args); |
| 237 | process.stdout.write(args.json ? JSON.stringify(r) + '\n' : render(r)); |
| 238 | } |
| 239 | |
| 240 | import { fileURLToPath } from 'node:url'; |
| 241 | if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) main(); |