The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
CPU profile log: name the caller of each dependency's CPU, and each function's time with what it calls (#2368)
0xken15e08d53f96385
diff --git a/backend/src/lib/selfProfile.test.ts b/backend/src/lib/selfProfile.test.tsindex 58732566d..5c32ef7f5 100644--- a/backend/src/lib/selfProfile.test.ts+++ b/backend/src/lib/selfProfile.test.ts@@ -13,7 +13,33 @@ it('ranks files and functions by self time, with dependencies by package and nosamples: [2, 3, 3, 3, 2], timeDeltas: [0, 1000, 2000, 3000, 4000],})expect(top).toEqual({ sampledMs: 10, files: { 'node_modules/bson/lib/bson.cjs': 9, 'dist/read/changes.js': 1 },- functions: { 'deserialize node_modules/bson/lib/bson.cjs:42': 9, 'readChanges dist/read/changes.js:10': 1 } })+ functions: { 'deserialize node_modules/bson/lib/bson.cjs:42': 9, 'readChanges dist/read/changes.js:10': 1 },+ callers: {}, inclusive: { 'readChanges dist/read/changes.js:10': 1 } })+})++it('names the code of ours that a dependency or built-in spends its time for, and the time under each function of ours', () => {+ const app = (path: string) => `file://${process.cwd()}/${path}`+ const at = (id: number, functionName: string, url: string, children: number[] = [], lineNumber = 0) => ({ id, callFrame: { functionName, url, lineNumber }, children })+ const top = topSelfTime({+ nodes: [+ at(1, '(root)', '', [2, 6]),+ at(2, 'changes', app('dist/read/service.js'), [3, 4], 113),+ at(3, 'structuredClone', 'node:internal/worker/js_transferable', [], 111),+ at(4, 'visibleState', app('dist/superTabs/model.js'), [5, 7], 637),+ at(5, 'structuredClone', 'node:internal/worker/js_transferable', [], 111),+ at(6, '(garbage collector)', ''),+ // Recursion counts once per sample under a function of ours.+ at(7, 'visibleState', app('dist/superTabs/model.js'), [8], 637),+ at(8, 'parseEngineResponse', 'file:///app/node_modules/@prisma/client/runtime/library.js', [], 111),+ ],+ samples: [3, 5, 5, 4, 6, 8], timeDeltas: [0, 1000, 2000, 2000, 1000, 1000, 3000],+ })+ expect(top.callers).toEqual({+ 'structuredClone node:internal/worker/js_transferable:112 ← visibleState dist/superTabs/model.js:638': 4,+ 'parseEngineResponse node_modules/@prisma/client/runtime/library.js:112 ← visibleState dist/superTabs/model.js:638': 3,+ 'structuredClone node:internal/worker/js_transferable:112 ← changes dist/read/service.js:114': 1,+ })+ expect(top.inclusive).toEqual({ 'changes dist/read/service.js:114': 9, 'visibleState dist/superTabs/model.js:638': 8 })})it('counts the busy time of a named piece of work, and of what it awaits, under its name', async () => {diff --git a/backend/src/lib/selfProfile.ts b/backend/src/lib/selfProfile.tsindex ad8266df0..ce1bab12e 100644--- a/backend/src/lib/selfProfile.ts+++ b/backend/src/lib/selfProfile.ts@@ -108,11 +108,27 @@ export function startSelfProfile({ everyMs = 15 * 60_000, profileMs = 30_000, rereturn () => { enabled = false; hook.disable(); clearInterval(report); clearTimeout(first); clearInterval(every) }}-type Profile = { nodes: { id: number; callFrame: { functionName: string; url: string; lineNumber: number } }[]; samples?: number[]; timeDeltas?: number[] }+type Frame = { functionName: string; url: string; lineNumber: number }+type Profile = { nodes: { id: number; callFrame: Frame; children?: number[] }[]; samples?: number[]; timeDeltas?: number[] }-export function topSelfTime(profile: Profile): { sampledMs: number; files: Record<string, number>; functions: Record<string, number> } {+/**+ * The profile by self time (`files`, `functions`), and what the self time alone cannot tell, the stacks being dropped:+ * - `callers`: a dependency's or built-in's self time (Prisma's decoding, structuredClone) by the nearest function of ours+ * above it, as `<function> ← <our function>`;+ * - `inclusive`: each function of ours by the time spent in it and everything it called, counted once per sample however+ * deep it recurses.+ * Ours is what runs from `dist/` or `src/`; V8's own entries (idle, program, garbage collector) have no stack and count in+ * neither.+ */+export function topSelfTime(profile: Profile): { sampledMs: number; files: Record<string, number>; functions: Record<string, number>+ callers: Record<string, number>; inclusive: Record<string, number> } {const byId = new Map(profile.nodes.map(n => [n.id, n.callFrame]))- const files = new Map<string, number>(), functions = new Map<string, number>()+ const parent = new Map<number, number>()+ for (const node of profile.nodes) for (const child of node.children ?? []) parent.set(child, node.id)+ // Named once per node: a 30 s profile has tens of thousands of samples, each walking its stack.+ const ourName = new Map(profile.nodes.filter(n => isOurs(n.callFrame.url)).map(n => [n.id, name(n.callFrame)]))+ const files = new Map<string, number>(), functions = new Map<string, number>(), callers = new Map<string, number>(), inclusive = new Map<string, number>()+ const add = (m: Map<string, number>, key: string, us: number) => m.set(key, (m.get(key) ?? 0) + us)let total = 0const samples = profile.samples ?? [], deltas = profile.timeDeltas ?? []for (let i = 0; i < samples.length; i++) {@@ -120,14 +136,31 @@ export function topSelfTime(profile: Profile): { sampledMs: number; files: Recorif (!frame) continuetotal += usconst file = shortFile(frame.url) || `(${frame.functionName || 'native'})`- files.set(file, (files.get(file) ?? 0) + us)- const fn = `${frame.functionName || '(anonymous)'} ${file}${frame.url ? `:${frame.lineNumber + 1}` : ''}`- functions.set(fn, (functions.get(fn) ?? 0) + us)+ add(files, file, us)+ const fn = name(frame)+ add(functions, fn, us)+ if (!frame.url && frame.functionName.startsWith('(')) continue+ const ours = new Set<string>()+ let nearest: string | undefined+ for (let id: number | undefined = samples[i]; id !== undefined; id = parent.get(id)) {+ const key = ourName.get(id)+ if (!key) continue+ nearest ??= key+ ours.add(key)+ }+ if (nearest && !ourName.has(samples[i]!)) add(callers, `${fn} ← ${nearest}`, us)+ for (const key of ours) add(inclusive, key, us)}const top = (m: Map<string, number>, n: number) =>Object.fromEntries([...m].sort((a, b) => b[1] - a[1]).slice(0, n).map(([k, us]) => [k, Math.round(us / 1000)]))- return { sampledMs: Math.round(total / 1000), files: top(files, 20), functions: top(functions, 30) }+ return { sampledMs: Math.round(total / 1000), files: top(files, 20), functions: top(functions, 30), callers: top(callers, 30), inclusive: top(inclusive, 40) }+}++const name = (frame: Frame) => {+ const file = shortFile(frame.url) || `(${frame.functionName || 'native'})`+ return `${frame.functionName || '(anonymous)'} ${file}${frame.url ? `:${frame.lineNumber + 1}` : ''}`}+const isOurs = (url: string) => /^(dist|src)\//.test(shortFile(url))/** `file:///app/dist/lib/hub.js` → `dist/lib/hub.js`; a dependency → `node_modules/<package>/<file>`. */function shortFile(url: string): string {