ostia 0.2.12 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +50 -358
- package/docs/agent-protocol.md +210 -0
- package/docs/cli.md +1196 -0
- package/docs/config.md +233 -0
- package/docs/document-schema.md +224 -0
- package/docs/library.md +421 -0
- package/docs/preload-recipes.md +169 -0
- package/docs/statistics.md +218 -0
- package/index.d.ts +923 -679
- package/package.json +16 -14
- package/src/ab/ab-runner.ts +487 -0
- package/src/ab/index.ts +1160 -0
- package/src/baseline/index.ts +69 -0
- package/src/bench/index.ts +516 -0
- package/src/bench/projects.ts +43 -0
- package/src/bench/range.ts +27 -0
- package/src/bench/registry.ts +426 -0
- package/src/bench/run-tasks.ts +179 -0
- package/src/bench/run.ts +52 -0
- package/src/bench/runner.ts +162 -0
- package/src/bench/support.ts +161 -0
- package/src/bench/sweep.ts +41 -0
- package/src/cache/fingerprint.ts +62 -0
- package/src/cache/store.ts +40 -0
- package/src/capture/bunflags.ts +92 -0
- package/src/capture/cpu-parse.ts +131 -0
- package/src/capture/cpu.ts +47 -0
- package/src/capture/frames.ts +60 -0
- package/src/capture/heap-parse.ts +111 -0
- package/src/capture/heap.ts +36 -0
- package/src/capture/inspector.ts +47 -0
- package/src/capture/jsc-parse.ts +167 -0
- package/src/capture/jsc.ts +35 -0
- package/src/capture/sampling.ts +5 -0
- package/src/ci/index.ts +440 -0
- package/src/cli/flags.ts +263 -0
- package/{chunk-9sx15b08.js → src/cli/help.ts} +23 -11
- package/src/cli/main.ts +18 -0
- package/src/cli/output.ts +179 -0
- package/src/cli/run.ts +1110 -0
- package/src/compare/index.ts +624 -0
- package/src/config/env.ts +13 -0
- package/src/config/index.ts +283 -0
- package/src/config/validate.ts +273 -0
- package/src/errors.ts +8 -0
- package/src/format.ts +35 -0
- package/src/glob.ts +45 -0
- package/src/index.ts +60 -0
- package/src/ir/cpu.ts +10 -0
- package/src/ir/defined.ts +12 -0
- package/src/ir/document.ts +491 -0
- package/src/ir/fp.ts +28 -0
- package/src/ir/git.ts +37 -0
- package/src/ir/types.ts +615 -0
- package/src/measure/alloc.ts +56 -0
- package/src/measure/cpu.ts +91 -0
- package/src/measure/environment.ts +38 -0
- package/src/measure/inprocess.ts +191 -0
- package/src/measure/instructions.ts +139 -0
- package/src/measure/loop.ts +64 -0
- package/src/measure/noise.ts +55 -0
- package/src/measure/paired.ts +304 -0
- package/src/measure/peak.ts +201 -0
- package/src/measure/timing.ts +263 -0
- package/src/profile.ts +110 -0
- package/src/renderers/collapsed.ts +42 -0
- package/src/renderers/cpu-tree.ts +191 -0
- package/src/renderers/cpuprofile.ts +43 -0
- package/src/renderers/format.ts +132 -0
- package/src/renderers/index.ts +37 -0
- package/src/renderers/json.ts +10 -0
- package/src/renderers/jsonl.ts +19 -0
- package/src/renderers/markdown.ts +543 -0
- package/src/renderers/mermaid.ts +90 -0
- package/src/renderers/minimal.ts +602 -0
- package/src/renderers/paired.ts +223 -0
- package/src/renderers/relative.ts +60 -0
- package/src/renderers/select.ts +264 -0
- package/src/renderers/speedscope.ts +73 -0
- package/src/renderers/terminal.ts +470 -0
- package/src/renderers/types.ts +27 -0
- package/src/spawn/index.ts +387 -0
- package/src/stats/bootstrap.ts +151 -0
- package/src/stats/index.ts +144 -0
- package/src/stats/mannwhitney.ts +82 -0
- package/src/time.ts +337 -0
- package/src/version.ts +3 -0
- package/ab-runner.ts +0 -8
- package/chunk-05jfst35.js +0 -28
- package/chunk-1g1x9zz2.js +0 -15
- package/chunk-23g2rwrn.js +0 -3
- package/chunk-468h5412.js +0 -25
- package/chunk-4esjfc2m.js +0 -2
- package/chunk-ct2xsezd.js +0 -14
- package/cli.js +0 -3
- package/index.js +0 -2
- package/runner.ts +0 -7
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { CpuEvidence, Warning } from "../ir/types.ts";
|
|
2
|
+
import { type ProfiledRunOptions, runProfiled } from "./bunflags.ts";
|
|
3
|
+
import { isRawCpuProfile, parseCpuProfile } from "./cpu-parse.ts";
|
|
4
|
+
|
|
5
|
+
interface CpuCaptureOptions extends ProfiledRunOptions {
|
|
6
|
+
intervalUs: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface CpuCaptureResult {
|
|
10
|
+
diagnosticWallNs: number;
|
|
11
|
+
exitCode: number;
|
|
12
|
+
artifactPath?: string;
|
|
13
|
+
cpu?: CpuEvidence;
|
|
14
|
+
warnings: Warning[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// BUN_CPU_PROFILE is documented but doesn't work on Bun 1.4.0, hence
|
|
18
|
+
// flags/BUN_OPTIONS.
|
|
19
|
+
export async function runCpuCapture(
|
|
20
|
+
opts: CpuCaptureOptions,
|
|
21
|
+
): Promise<CpuCaptureResult> {
|
|
22
|
+
const { raw, ...run } = await runProfiled(
|
|
23
|
+
opts,
|
|
24
|
+
[
|
|
25
|
+
"--cpu-prof",
|
|
26
|
+
"--cpu-prof-dir",
|
|
27
|
+
opts.artifactDir,
|
|
28
|
+
"--cpu-prof-name",
|
|
29
|
+
opts.fileName,
|
|
30
|
+
"--cpu-prof-interval",
|
|
31
|
+
String(opts.intervalUs),
|
|
32
|
+
],
|
|
33
|
+
{ what: "a .cpuprofile", kind: "CPU" },
|
|
34
|
+
);
|
|
35
|
+
if (!raw) return run;
|
|
36
|
+
if (!isRawCpuProfile(raw)) {
|
|
37
|
+
throw new Error(`ostia: ${run.artifactPath} isn't a CPU profile`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (raw.samples.length === 0) {
|
|
41
|
+
run.warnings.push({
|
|
42
|
+
code: "empty-profile",
|
|
43
|
+
message: "CPU capture produced zero samples.",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return { ...run, cpu: parseCpuProfile(raw, "cpu-prof", opts.intervalUs) };
|
|
47
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { fp } from "../ir/fp.ts";
|
|
2
|
+
import type { Frame, FrameTotal } from "../ir/types.ts";
|
|
3
|
+
|
|
4
|
+
export function createFrameTable(): {
|
|
5
|
+
frames: Frame[];
|
|
6
|
+
intern: (
|
|
7
|
+
name: string,
|
|
8
|
+
url: string | undefined,
|
|
9
|
+
line: number | undefined,
|
|
10
|
+
col: number | undefined,
|
|
11
|
+
) => number;
|
|
12
|
+
} {
|
|
13
|
+
const ixByName = new Map<string, Map<string, number>>();
|
|
14
|
+
const frames: Frame[] = [];
|
|
15
|
+
return {
|
|
16
|
+
frames,
|
|
17
|
+
intern(name, url, line, col) {
|
|
18
|
+
let byUrl = ixByName.get(name);
|
|
19
|
+
if (byUrl === undefined) {
|
|
20
|
+
byUrl = new Map();
|
|
21
|
+
ixByName.set(name, byUrl);
|
|
22
|
+
}
|
|
23
|
+
const urlKey = url ?? "";
|
|
24
|
+
let ix = byUrl.get(urlKey);
|
|
25
|
+
if (ix === undefined) {
|
|
26
|
+
ix = frames.length;
|
|
27
|
+
byUrl.set(urlKey, ix);
|
|
28
|
+
frames.push({
|
|
29
|
+
key: fp("fr", name, urlKey),
|
|
30
|
+
name,
|
|
31
|
+
...(url !== undefined && { url }),
|
|
32
|
+
...(line !== undefined && { line }),
|
|
33
|
+
...(col !== undefined && { col }),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return ix;
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function addFrameTotal(
|
|
42
|
+
totals: Map<number, FrameTotal>,
|
|
43
|
+
frameIx: number,
|
|
44
|
+
selfUs: number,
|
|
45
|
+
totalUs: number,
|
|
46
|
+
samples: number,
|
|
47
|
+
): void {
|
|
48
|
+
const existing = totals.get(frameIx);
|
|
49
|
+
if (existing) {
|
|
50
|
+
existing.selfUs += selfUs;
|
|
51
|
+
existing.totalUs += totalUs;
|
|
52
|
+
existing.samples += samples;
|
|
53
|
+
} else {
|
|
54
|
+
totals.set(frameIx, { frameIx, selfUs, totalUs, samples });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function sortedTotals(totals: Map<number, FrameTotal>): FrameTotal[] {
|
|
59
|
+
return [...totals.values()].sort((a, b) => b.selfUs - a.selfUs);
|
|
60
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { HeapEvidence } from "../ir/types.ts";
|
|
2
|
+
|
|
3
|
+
export interface RawHeapSnapshot {
|
|
4
|
+
snapshot: {
|
|
5
|
+
meta: {
|
|
6
|
+
node_fields: string[];
|
|
7
|
+
node_types: (string[] | string)[];
|
|
8
|
+
};
|
|
9
|
+
node_count: number;
|
|
10
|
+
};
|
|
11
|
+
nodes: number[];
|
|
12
|
+
strings: string[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Checks only the arrays `parseHeapSnapshot` walks and the count it
|
|
16
|
+
// reports.
|
|
17
|
+
export function isRawHeapSnapshot(value: unknown): value is RawHeapSnapshot {
|
|
18
|
+
if (typeof value !== "object" || value === null) return false;
|
|
19
|
+
if (!("nodes" in value) || !Array.isArray(value.nodes)) return false;
|
|
20
|
+
if (!("snapshot" in value)) return false;
|
|
21
|
+
const { snapshot } = value;
|
|
22
|
+
if (typeof snapshot !== "object" || snapshot === null) return false;
|
|
23
|
+
if (!("node_count" in snapshot) || typeof snapshot.node_count !== "number") {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (!("meta" in snapshot)) return false;
|
|
27
|
+
const { meta } = snapshot;
|
|
28
|
+
return (
|
|
29
|
+
typeof meta === "object" &&
|
|
30
|
+
meta !== null &&
|
|
31
|
+
"node_fields" in meta &&
|
|
32
|
+
Array.isArray(meta.node_fields) &&
|
|
33
|
+
"node_types" in meta &&
|
|
34
|
+
Array.isArray(meta.node_types)
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const TOP_N = 20;
|
|
39
|
+
|
|
40
|
+
interface TypeBucket {
|
|
41
|
+
type: string;
|
|
42
|
+
count: number;
|
|
43
|
+
bytes: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function parseHeapSnapshot(raw: RawHeapSnapshot): HeapEvidence {
|
|
47
|
+
const origin = "heap-prof";
|
|
48
|
+
const { node_fields, node_types } = raw.snapshot.meta;
|
|
49
|
+
const typeIx = node_fields.indexOf("type");
|
|
50
|
+
const selfSizeIx = node_fields.indexOf("self_size");
|
|
51
|
+
const fieldCount = node_fields.length;
|
|
52
|
+
const typeNames = node_types[0];
|
|
53
|
+
if (typeIx === -1 || selfSizeIx === -1 || !Array.isArray(typeNames)) {
|
|
54
|
+
return { origin, typeCounts: [], objectCount: raw.snapshot.node_count };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const typeCount = typeNames.length;
|
|
58
|
+
const buckets = new Array<TypeBucket | undefined>(typeCount);
|
|
59
|
+
// Keyed by `number | undefined` because a truncated `nodes` reads
|
|
60
|
+
// `undefined` for its last type.
|
|
61
|
+
const unknownBuckets = new Map<number | undefined, TypeBucket>();
|
|
62
|
+
const seen: TypeBucket[] = [];
|
|
63
|
+
const newBucket = (type: string): TypeBucket => {
|
|
64
|
+
const bucket = { type, count: 0, bytes: 0 };
|
|
65
|
+
seen.push(bucket);
|
|
66
|
+
return bucket;
|
|
67
|
+
};
|
|
68
|
+
let heapSizeBytes = 0;
|
|
69
|
+
|
|
70
|
+
const nodes = raw.nodes;
|
|
71
|
+
const len = nodes.length;
|
|
72
|
+
for (let offset = 0; offset < len; offset += fieldCount) {
|
|
73
|
+
const typeIdx = nodes[offset + typeIx];
|
|
74
|
+
const selfSize = nodes[offset + selfSizeIx] ?? 0;
|
|
75
|
+
heapSizeBytes += selfSize;
|
|
76
|
+
let bucket: TypeBucket | undefined;
|
|
77
|
+
if (typeIdx !== undefined && typeIdx >= 0 && typeIdx < typeCount) {
|
|
78
|
+
bucket = buckets[typeIdx] ??= newBucket(typeNames[typeIdx] ?? "");
|
|
79
|
+
} else {
|
|
80
|
+
bucket = unknownBuckets.get(typeIdx);
|
|
81
|
+
if (bucket === undefined) {
|
|
82
|
+
bucket = newBucket(`unknown(${typeIdx})`);
|
|
83
|
+
unknownBuckets.set(typeIdx, bucket);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
bucket.count++;
|
|
87
|
+
bucket.bytes += selfSize;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
seen.sort((a, b) => b.count - a.count);
|
|
91
|
+
const typeCounts = seen.slice(0, TOP_N).map(({ type, count, bytes }) => ({
|
|
92
|
+
type,
|
|
93
|
+
count,
|
|
94
|
+
retainedBytes: bytes,
|
|
95
|
+
}));
|
|
96
|
+
const rest = seen.slice(TOP_N);
|
|
97
|
+
if (rest.length > 0) {
|
|
98
|
+
typeCounts.push({
|
|
99
|
+
type: "other",
|
|
100
|
+
count: rest.reduce((n, b) => n + b.count, 0),
|
|
101
|
+
retainedBytes: rest.reduce((n, b) => n + b.bytes, 0),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
origin,
|
|
107
|
+
heapSizeBytes,
|
|
108
|
+
objectCount: raw.snapshot.node_count,
|
|
109
|
+
typeCounts,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { HeapEvidence, Warning } from "../ir/types.ts";
|
|
2
|
+
import { type ProfiledRunOptions, runProfiled } from "./bunflags.ts";
|
|
3
|
+
import { isRawHeapSnapshot, parseHeapSnapshot } from "./heap-parse.ts";
|
|
4
|
+
|
|
5
|
+
type HeapCaptureOptions = ProfiledRunOptions;
|
|
6
|
+
|
|
7
|
+
interface HeapCaptureResult {
|
|
8
|
+
diagnosticWallNs: number;
|
|
9
|
+
exitCode: number;
|
|
10
|
+
artifactPath?: string;
|
|
11
|
+
heap?: HeapEvidence;
|
|
12
|
+
warnings: Warning[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// `--heap-prof-md` must not accompany `--heap-prof`: md wins and the
|
|
16
|
+
// binary snapshot is skipped.
|
|
17
|
+
export async function runHeapCapture(
|
|
18
|
+
opts: HeapCaptureOptions,
|
|
19
|
+
): Promise<HeapCaptureResult> {
|
|
20
|
+
const { raw, ...run } = await runProfiled(
|
|
21
|
+
opts,
|
|
22
|
+
[
|
|
23
|
+
"--heap-prof",
|
|
24
|
+
"--heap-prof-dir",
|
|
25
|
+
opts.artifactDir,
|
|
26
|
+
"--heap-prof-name",
|
|
27
|
+
opts.fileName,
|
|
28
|
+
],
|
|
29
|
+
{ what: "a heap snapshot", kind: "heap" },
|
|
30
|
+
);
|
|
31
|
+
if (!raw) return run;
|
|
32
|
+
if (!isRawHeapSnapshot(raw)) {
|
|
33
|
+
throw new Error(`ostia: ${run.artifactPath} isn't a heap snapshot`);
|
|
34
|
+
}
|
|
35
|
+
return { ...run, heap: parseHeapSnapshot(raw) };
|
|
36
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { CpuEvidence } from "../ir/types.ts";
|
|
2
|
+
import { isRawCpuProfile, parseCpuProfile } from "./cpu-parse.ts";
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_SAMPLING_INTERVAL_US,
|
|
5
|
+
type SamplingOptions,
|
|
6
|
+
} from "./sampling.ts";
|
|
7
|
+
|
|
8
|
+
interface InspectorCaptureResult<T> {
|
|
9
|
+
result: T;
|
|
10
|
+
cpu: CpuEvidence;
|
|
11
|
+
diagnosticWallNs: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function captureInspectorProfile<T>(
|
|
15
|
+
fn: () => T | Promise<T>,
|
|
16
|
+
opts: SamplingOptions = {},
|
|
17
|
+
): Promise<InspectorCaptureResult<T>> {
|
|
18
|
+
const intervalUs = opts.intervalUs ?? DEFAULT_SAMPLING_INTERVAL_US;
|
|
19
|
+
// perf: imported lazily, since importing node:inspector costs ~4ms of
|
|
20
|
+
// startup for every invocation.
|
|
21
|
+
const { Session } = await import("node:inspector/promises");
|
|
22
|
+
const session = new Session();
|
|
23
|
+
session.connect();
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
await session.post("Profiler.enable");
|
|
27
|
+
await session.post("Profiler.setSamplingInterval", {
|
|
28
|
+
interval: intervalUs,
|
|
29
|
+
});
|
|
30
|
+
// Diagnostic window: profiler start through stop, nothing else. Matches
|
|
31
|
+
// the jsc capture, which can only time `profile()` as a whole.
|
|
32
|
+
const start = Bun.nanoseconds();
|
|
33
|
+
await session.post("Profiler.start");
|
|
34
|
+
|
|
35
|
+
const result = await fn();
|
|
36
|
+
|
|
37
|
+
const { profile } = await session.post("Profiler.stop");
|
|
38
|
+
if (!isRawCpuProfile(profile)) {
|
|
39
|
+
throw new Error("ostia: the inspector returned no CPU profile");
|
|
40
|
+
}
|
|
41
|
+
const diagnosticWallNs = Bun.nanoseconds() - start;
|
|
42
|
+
const cpu = parseCpuProfile(profile, "inspector", intervalUs);
|
|
43
|
+
return { result, cpu, diagnosticWallNs };
|
|
44
|
+
} finally {
|
|
45
|
+
session.disconnect();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CallNode,
|
|
3
|
+
CpuEvidence,
|
|
4
|
+
FrameTotal,
|
|
5
|
+
JitTierBreakdown,
|
|
6
|
+
} from "../ir/types.ts";
|
|
7
|
+
import { addFrameTotal, createFrameTable, sortedTotals } from "./frames.ts";
|
|
8
|
+
|
|
9
|
+
export interface RawJscFrame {
|
|
10
|
+
sourceID: number;
|
|
11
|
+
name: string;
|
|
12
|
+
location: string;
|
|
13
|
+
sourceURL?: string;
|
|
14
|
+
line: number;
|
|
15
|
+
column: number;
|
|
16
|
+
category: string;
|
|
17
|
+
flags: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface RawJscTrace {
|
|
21
|
+
timestamp: number;
|
|
22
|
+
frames: RawJscFrame[]; // leaf-first
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface RawStackTraces {
|
|
26
|
+
interval: number; // seconds
|
|
27
|
+
traces: RawJscTrace[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const TIER_BUCKET = new Map<string, keyof JitTierBreakdown["tiers"]>([
|
|
31
|
+
["LLInt", "llint"],
|
|
32
|
+
["Baseline", "baseline"],
|
|
33
|
+
["DFG", "dfg"],
|
|
34
|
+
["FTL", "ftl"],
|
|
35
|
+
]);
|
|
36
|
+
const UINT32_SENTINEL = 4294967295; // JSC's "no line/column" marker on synthetic/native frames
|
|
37
|
+
|
|
38
|
+
interface MutableNode {
|
|
39
|
+
id: number;
|
|
40
|
+
frameIx: number;
|
|
41
|
+
children: Map<number, MutableNode>;
|
|
42
|
+
selfUs: number;
|
|
43
|
+
samples: number;
|
|
44
|
+
totalUs: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function parseJscProfile(
|
|
48
|
+
raw: RawStackTraces,
|
|
49
|
+
intervalUsOverride?: number,
|
|
50
|
+
): { cpu: CpuEvidence; jit: JitTierBreakdown } {
|
|
51
|
+
const samplingIntervalUs = intervalUsOverride ?? raw.interval * 1e6;
|
|
52
|
+
|
|
53
|
+
const { frames, intern } = createFrameTable();
|
|
54
|
+
function frameIxFromRaw(rf: RawJscFrame): number {
|
|
55
|
+
const isSentinel = rf.line === UINT32_SENTINEL;
|
|
56
|
+
// spec: jsc lines and columns are 1-based; stored 0-based so they match
|
|
57
|
+
// cpu-prof and the inspector.
|
|
58
|
+
const line = isSentinel ? undefined : rf.line - 1;
|
|
59
|
+
const col =
|
|
60
|
+
isSentinel || rf.column === UINT32_SENTINEL ? undefined : rf.column - 1;
|
|
61
|
+
return intern(rf.name, rf.sourceURL, line, col);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const allNodes: MutableNode[] = [];
|
|
65
|
+
function newNode(frameIx: number): MutableNode {
|
|
66
|
+
const node: MutableNode = {
|
|
67
|
+
id: allNodes.length,
|
|
68
|
+
frameIx,
|
|
69
|
+
children: new Map(),
|
|
70
|
+
selfUs: 0,
|
|
71
|
+
samples: 0,
|
|
72
|
+
totalUs: 0,
|
|
73
|
+
};
|
|
74
|
+
allNodes.push(node);
|
|
75
|
+
return node;
|
|
76
|
+
}
|
|
77
|
+
const rootNode = newNode(intern("(root)", undefined, undefined, undefined));
|
|
78
|
+
|
|
79
|
+
const tiers = { llint: 0, baseline: 0, dfg: 0, ftl: 0 };
|
|
80
|
+
const tierFrameSamples = new Map<string, Map<number, number>>();
|
|
81
|
+
|
|
82
|
+
const nodeIds: number[] = [];
|
|
83
|
+
const timeDeltasUs: number[] = [];
|
|
84
|
+
|
|
85
|
+
for (const trace of raw.traces) {
|
|
86
|
+
const traceFrames = trace.frames;
|
|
87
|
+
let current = rootNode;
|
|
88
|
+
for (const rf of traceFrames.toReversed()) {
|
|
89
|
+
const frameIx = frameIxFromRaw(rf);
|
|
90
|
+
let child = current.children.get(frameIx);
|
|
91
|
+
if (!child) {
|
|
92
|
+
child = newNode(frameIx);
|
|
93
|
+
current.children.set(frameIx, child);
|
|
94
|
+
}
|
|
95
|
+
current = child;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
current.selfUs += samplingIntervalUs;
|
|
99
|
+
current.samples += 1;
|
|
100
|
+
nodeIds.push(current.id);
|
|
101
|
+
timeDeltasUs.push(samplingIntervalUs);
|
|
102
|
+
|
|
103
|
+
const leafRaw = traceFrames[0];
|
|
104
|
+
const bucket = leafRaw && TIER_BUCKET.get(leafRaw.category);
|
|
105
|
+
if (bucket) {
|
|
106
|
+
tiers[bucket]++;
|
|
107
|
+
const byFrame = tierFrameSamples.get(bucket) ?? new Map<number, number>();
|
|
108
|
+
byFrame.set(current.frameIx, (byFrame.get(current.frameIx) ?? 0) + 1);
|
|
109
|
+
tierFrameSamples.set(bucket, byFrame);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function computeTotalUs(node: MutableNode): number {
|
|
114
|
+
let total = node.selfUs;
|
|
115
|
+
for (const child of node.children.values()) total += computeTotalUs(child);
|
|
116
|
+
node.totalUs = total;
|
|
117
|
+
return total;
|
|
118
|
+
}
|
|
119
|
+
computeTotalUs(rootNode);
|
|
120
|
+
|
|
121
|
+
const totals = new Map<number, FrameTotal>();
|
|
122
|
+
function accumulate(node: MutableNode): void {
|
|
123
|
+
addFrameTotal(
|
|
124
|
+
totals,
|
|
125
|
+
node.frameIx,
|
|
126
|
+
node.selfUs,
|
|
127
|
+
node.totalUs,
|
|
128
|
+
node.samples,
|
|
129
|
+
);
|
|
130
|
+
for (const child of node.children.values()) accumulate(child);
|
|
131
|
+
}
|
|
132
|
+
accumulate(rootNode);
|
|
133
|
+
|
|
134
|
+
const nodes: CallNode[] = allNodes.map((n) => ({
|
|
135
|
+
id: n.id,
|
|
136
|
+
frameIx: n.frameIx,
|
|
137
|
+
children: [...n.children.values()].map((c) => c.id),
|
|
138
|
+
}));
|
|
139
|
+
|
|
140
|
+
const cpu: CpuEvidence = {
|
|
141
|
+
origin: "jsc-profile",
|
|
142
|
+
samplingIntervalUs,
|
|
143
|
+
frames,
|
|
144
|
+
nodes,
|
|
145
|
+
totals: sortedTotals(totals),
|
|
146
|
+
samples: { nodeIds, timeDeltasUs },
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const topFramesByTier = [...tierFrameSamples.entries()].flatMap(
|
|
150
|
+
([tier, byFrame]) =>
|
|
151
|
+
[...byFrame.entries()]
|
|
152
|
+
.sort((a, b) => b[1] - a[1])
|
|
153
|
+
.slice(0, 3)
|
|
154
|
+
.flatMap(([frameIx, samples]) => {
|
|
155
|
+
const frame = frames[frameIx];
|
|
156
|
+
return frame ? [{ tier, frameKey: frame.key, samples }] : [];
|
|
157
|
+
}),
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const jit: JitTierBreakdown = {
|
|
161
|
+
origin: "jsc-profile",
|
|
162
|
+
tiers,
|
|
163
|
+
topFramesByTier,
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
return { cpu, jit };
|
|
167
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { profile as jscProfile } from "bun:jsc";
|
|
2
|
+
|
|
3
|
+
import type { CpuEvidence, JitTierBreakdown } from "../ir/types.ts";
|
|
4
|
+
import { parseJscProfile } from "./jsc-parse.ts";
|
|
5
|
+
import {
|
|
6
|
+
DEFAULT_SAMPLING_INTERVAL_US,
|
|
7
|
+
type SamplingOptions,
|
|
8
|
+
} from "./sampling.ts";
|
|
9
|
+
|
|
10
|
+
interface JscCaptureResult<T> {
|
|
11
|
+
result: T;
|
|
12
|
+
cpu: CpuEvidence;
|
|
13
|
+
jit: JitTierBreakdown;
|
|
14
|
+
diagnosticWallNs: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function captureJscProfile<T>(
|
|
18
|
+
fn: () => T | Promise<T>,
|
|
19
|
+
opts: SamplingOptions = {},
|
|
20
|
+
): Promise<JscCaptureResult<T>> {
|
|
21
|
+
const intervalUs = opts.intervalUs ?? DEFAULT_SAMPLING_INTERVAL_US;
|
|
22
|
+
let result!: T; // `bun:jsc.profile` returns only the profile, not fn's value
|
|
23
|
+
|
|
24
|
+
// Diagnostic window: profiler start through stop, nothing else. Matches
|
|
25
|
+
// the inspector capture, whose session setup is outside it.
|
|
26
|
+
const start = Bun.nanoseconds();
|
|
27
|
+
const raw = await jscProfile(async () => {
|
|
28
|
+
result = await fn();
|
|
29
|
+
return result;
|
|
30
|
+
}, intervalUs);
|
|
31
|
+
const diagnosticWallNs = Bun.nanoseconds() - start;
|
|
32
|
+
|
|
33
|
+
const { cpu, jit } = parseJscProfile(raw.stackTraces, intervalUs);
|
|
34
|
+
return { result, cpu, jit, diagnosticWallNs };
|
|
35
|
+
}
|