ostia 0.3.0 → 0.4.0
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/index.d.ts +127 -99
- package/package.json +1 -1
- package/src/ab/ab-runner.ts +57 -44
- package/src/ab/index.ts +186 -112
- package/src/baseline/index.ts +3 -9
- package/src/bench/index.ts +177 -123
- package/src/bench/projects.ts +14 -10
- package/src/bench/registry.ts +70 -68
- package/src/bench/run-tasks.ts +26 -23
- package/src/bench/run.ts +2 -1
- package/src/bench/runner.ts +20 -5
- package/src/bench/support.ts +26 -26
- package/src/bench/sweep.ts +7 -5
- package/src/cache/fingerprint.ts +9 -6
- package/src/cache/store.ts +17 -5
- package/src/capture/bunflags.ts +12 -11
- package/src/capture/cpu-parse.ts +36 -24
- package/src/capture/cpu.ts +7 -4
- package/src/capture/frames.ts +7 -1
- package/src/capture/heap-parse.ts +30 -5
- package/src/capture/heap.ts +9 -5
- package/src/capture/inspector.ts +8 -7
- package/src/capture/jsc-parse.ts +8 -8
- package/src/capture/jsc.ts +4 -4
- package/src/ci/index.ts +64 -40
- package/src/cli/flags.ts +42 -43
- package/src/cli/output.ts +29 -35
- package/src/cli/run.ts +95 -95
- package/src/compare/index.ts +39 -32
- package/src/config/env.ts +13 -0
- package/src/config/index.ts +31 -23
- package/src/config/validate.ts +33 -27
- package/src/format.ts +5 -6
- package/src/glob.ts +9 -12
- package/src/index.ts +19 -4
- package/src/ir/cpu.ts +2 -2
- package/src/ir/defined.ts +12 -0
- package/src/ir/document.ts +93 -73
- package/src/ir/fp.ts +7 -10
- package/src/ir/git.ts +4 -3
- package/src/ir/types.ts +3 -3
- package/src/measure/alloc.ts +7 -8
- package/src/measure/cpu.ts +12 -20
- package/src/measure/environment.ts +5 -8
- package/src/measure/inprocess.ts +15 -15
- package/src/measure/instructions.ts +22 -16
- package/src/measure/loop.ts +24 -17
- package/src/measure/noise.ts +8 -8
- package/src/measure/paired.ts +39 -47
- package/src/measure/peak.ts +20 -21
- package/src/measure/timing.ts +23 -23
- package/src/profile.ts +2 -1
- package/src/renderers/collapsed.ts +28 -21
- package/src/renderers/cpu-tree.ts +43 -35
- package/src/renderers/format.ts +16 -26
- package/src/renderers/index.ts +14 -4
- package/src/renderers/json.ts +2 -2
- package/src/renderers/jsonl.ts +11 -7
- package/src/renderers/markdown.ts +54 -36
- package/src/renderers/mermaid.ts +43 -31
- package/src/renderers/minimal.ts +15 -15
- package/src/renderers/paired.ts +31 -37
- package/src/renderers/relative.ts +10 -14
- package/src/renderers/select.ts +17 -39
- package/src/renderers/speedscope.ts +52 -48
- package/src/renderers/terminal.ts +37 -33
- package/src/spawn/index.ts +38 -47
- package/src/stats/bootstrap.ts +56 -58
- package/src/stats/index.ts +16 -12
- package/src/stats/mannwhitney.ts +11 -10
- package/src/time.ts +51 -39
package/src/cache/store.ts
CHANGED
|
@@ -4,21 +4,33 @@ function cachePath(outDir: string, key: string): string {
|
|
|
4
4
|
return `${outDir}/cache/${key}.json`;
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* is a miss.
|
|
10
|
-
*/
|
|
7
|
+
// A missing or unreadable entry (e.g. truncated by an interrupted write)
|
|
8
|
+
// is a miss, never an error.
|
|
11
9
|
export async function readCachedRun(
|
|
12
10
|
outDir: string,
|
|
13
11
|
key: string,
|
|
14
12
|
): Promise<Measurement | undefined> {
|
|
15
13
|
try {
|
|
16
|
-
|
|
14
|
+
const value: unknown = await Bun.file(cachePath(outDir, key)).json();
|
|
15
|
+
return isMeasurement(value) ? value : undefined;
|
|
17
16
|
} catch {
|
|
18
17
|
return undefined;
|
|
19
18
|
}
|
|
20
19
|
}
|
|
21
20
|
|
|
21
|
+
// The cache holds ostia's own output, so this checks only the fields
|
|
22
|
+
// readers reach first.
|
|
23
|
+
function isMeasurement(value: unknown): value is Measurement {
|
|
24
|
+
return (
|
|
25
|
+
typeof value === "object" &&
|
|
26
|
+
value !== null &&
|
|
27
|
+
"id" in value &&
|
|
28
|
+
typeof value.id === "string" &&
|
|
29
|
+
"warnings" in value &&
|
|
30
|
+
Array.isArray(value.warnings)
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
22
34
|
export async function writeCachedRun(
|
|
23
35
|
outDir: string,
|
|
24
36
|
key: string,
|
package/src/capture/bunflags.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { processEnv } from "../config/env.ts";
|
|
1
2
|
import type { Warning } from "../ir/types.ts";
|
|
2
3
|
|
|
3
4
|
// BUN_OPTIONS is split on whitespace; a backslash escapes a space or quote
|
|
@@ -18,7 +19,10 @@ export function withBunFlags(
|
|
|
18
19
|
}
|
|
19
20
|
// A wrapper script that eventually execs bun still picks the flags up.
|
|
20
21
|
// Ours go last so they win over any the caller already set.
|
|
21
|
-
const merged
|
|
22
|
+
const merged: Record<string, string> = {};
|
|
23
|
+
for (const [key, value] of Object.entries({ ...processEnv(), ...env })) {
|
|
24
|
+
if (value !== undefined) merged[key] = value;
|
|
25
|
+
}
|
|
22
26
|
const ours = flags.map(escapeBunOption).join(" ");
|
|
23
27
|
return {
|
|
24
28
|
argv,
|
|
@@ -37,29 +41,26 @@ export interface ProfiledRunOptions {
|
|
|
37
41
|
fileName: string;
|
|
38
42
|
}
|
|
39
43
|
|
|
40
|
-
|
|
44
|
+
interface ProfiledRun {
|
|
41
45
|
diagnosticWallNs: number;
|
|
42
46
|
exitCode: number;
|
|
43
47
|
artifactPath?: string;
|
|
44
|
-
raw?:
|
|
48
|
+
raw?: unknown;
|
|
45
49
|
warnings: Warning[];
|
|
46
50
|
}
|
|
47
51
|
|
|
48
|
-
|
|
49
|
-
* artifact it wrote; `raw` is absent (with an `artifact-missing` warning)
|
|
50
|
-
* when none appeared. */
|
|
51
|
-
export async function runProfiled<T>(
|
|
52
|
+
export async function runProfiled(
|
|
52
53
|
opts: ProfiledRunOptions,
|
|
53
54
|
flags: string[],
|
|
54
55
|
artifact: { what: string; kind: string },
|
|
55
|
-
): Promise<ProfiledRun
|
|
56
|
+
): Promise<ProfiledRun> {
|
|
56
57
|
const artifactPath = `${opts.artifactDir}/${opts.fileName}`;
|
|
57
58
|
const { argv, env } = withBunFlags(opts.argv, flags, opts.env);
|
|
58
59
|
|
|
59
60
|
const start = Bun.nanoseconds();
|
|
60
61
|
const proc = Bun.spawn(argv, {
|
|
61
|
-
cwd: opts.cwd,
|
|
62
|
-
env,
|
|
62
|
+
...(opts.cwd !== undefined && { cwd: opts.cwd }),
|
|
63
|
+
...(env !== undefined && { env }),
|
|
63
64
|
stdout: "ignore",
|
|
64
65
|
stderr: "ignore",
|
|
65
66
|
stdin: "ignore",
|
|
@@ -85,7 +86,7 @@ export async function runProfiled<T>(
|
|
|
85
86
|
diagnosticWallNs,
|
|
86
87
|
exitCode,
|
|
87
88
|
artifactPath,
|
|
88
|
-
raw:
|
|
89
|
+
raw: await file.json(),
|
|
89
90
|
warnings: [],
|
|
90
91
|
};
|
|
91
92
|
}
|
package/src/capture/cpu-parse.ts
CHANGED
|
@@ -24,8 +24,22 @@ export interface RawCpuProfile {
|
|
|
24
24
|
timeDeltas: number[];
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
//
|
|
28
|
-
|
|
27
|
+
// Checks only the arrays `parseCpuProfile` walks, not each node.
|
|
28
|
+
export function isRawCpuProfile(value: unknown): value is RawCpuProfile {
|
|
29
|
+
return (
|
|
30
|
+
typeof value === "object" &&
|
|
31
|
+
value !== null &&
|
|
32
|
+
"nodes" in value &&
|
|
33
|
+
Array.isArray(value.nodes) &&
|
|
34
|
+
"samples" in value &&
|
|
35
|
+
Array.isArray(value.samples) &&
|
|
36
|
+
"timeDeltas" in value &&
|
|
37
|
+
Array.isArray(value.timeDeltas)
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// spec: cpu-prof and the inspector report sourcemapped `file://` URLs and
|
|
42
|
+
// 0-based lines (jsc's are 1-based).
|
|
29
43
|
function normalizeUrl(url: string): string {
|
|
30
44
|
return url.startsWith("file://") ? url.slice("file://".length) : url;
|
|
31
45
|
}
|
|
@@ -40,10 +54,9 @@ export function parseCpuProfile(
|
|
|
40
54
|
|
|
41
55
|
const { frames, intern } = createFrameTable();
|
|
42
56
|
const indexById = new Map<number, number>();
|
|
43
|
-
const nodes
|
|
57
|
+
const nodes = new Array<CallNode>(count);
|
|
44
58
|
|
|
45
|
-
for (
|
|
46
|
-
const node = rawNodes[i]!;
|
|
59
|
+
for (const [i, node] of rawNodes.entries()) {
|
|
47
60
|
const cf = node.callFrame;
|
|
48
61
|
const frameIx = intern(
|
|
49
62
|
cf.functionName,
|
|
@@ -59,16 +72,16 @@ export function parseCpuProfile(
|
|
|
59
72
|
const sampleCount = new Float64Array(count);
|
|
60
73
|
const sampleIds = raw.samples;
|
|
61
74
|
const deltas = raw.timeDeltas;
|
|
62
|
-
for (
|
|
63
|
-
const ix = indexById.get(
|
|
75
|
+
for (const [i, id] of sampleIds.entries()) {
|
|
76
|
+
const ix = indexById.get(id);
|
|
64
77
|
if (ix === undefined) continue;
|
|
65
|
-
selfUs[ix]
|
|
66
|
-
sampleCount[ix]
|
|
78
|
+
selfUs[ix] = (selfUs[ix] ?? 0) + (deltas[i] ?? 0);
|
|
79
|
+
sampleCount[ix] = (sampleCount[ix] ?? 0) + 1;
|
|
67
80
|
}
|
|
68
81
|
|
|
69
82
|
const parentIx = new Int32Array(count).fill(-1);
|
|
70
|
-
for (
|
|
71
|
-
const children =
|
|
83
|
+
for (const [i, node] of rawNodes.entries()) {
|
|
84
|
+
const children = node.children;
|
|
72
85
|
if (!children) continue;
|
|
73
86
|
for (const childId of children) {
|
|
74
87
|
const c = indexById.get(childId);
|
|
@@ -79,10 +92,9 @@ export function parseCpuProfile(
|
|
|
79
92
|
const order: number[] = [];
|
|
80
93
|
const stack: number[] = [];
|
|
81
94
|
for (let i = count - 1; i >= 0; i--) if (parentIx[i] === -1) stack.push(i);
|
|
82
|
-
|
|
83
|
-
const i = stack.pop()!;
|
|
95
|
+
for (let i = stack.pop(); i !== undefined; i = stack.pop()) {
|
|
84
96
|
order.push(i);
|
|
85
|
-
const children = rawNodes[i]
|
|
97
|
+
const children = rawNodes[i]?.children;
|
|
86
98
|
if (!children) continue;
|
|
87
99
|
for (const childId of children) {
|
|
88
100
|
const c = indexById.get(childId);
|
|
@@ -90,21 +102,21 @@ export function parseCpuProfile(
|
|
|
90
102
|
}
|
|
91
103
|
}
|
|
92
104
|
const totalUs = new Float64Array(count);
|
|
93
|
-
for (
|
|
94
|
-
const
|
|
95
|
-
totalUs[i]
|
|
96
|
-
const p = parentIx[i]
|
|
97
|
-
if (p >= 0) totalUs[p]
|
|
105
|
+
for (const i of order.toReversed()) {
|
|
106
|
+
const total = (totalUs[i] ?? 0) + (selfUs[i] ?? 0);
|
|
107
|
+
totalUs[i] = total;
|
|
108
|
+
const p = parentIx[i] ?? -1;
|
|
109
|
+
if (p >= 0) totalUs[p] = (totalUs[p] ?? 0) + total;
|
|
98
110
|
}
|
|
99
111
|
|
|
100
112
|
const totals = new Map<number, FrameTotal>();
|
|
101
|
-
for (
|
|
113
|
+
for (const [i, node] of nodes.entries()) {
|
|
102
114
|
addFrameTotal(
|
|
103
115
|
totals,
|
|
104
|
-
|
|
105
|
-
selfUs[i]
|
|
106
|
-
totalUs[i]
|
|
107
|
-
sampleCount[i]
|
|
116
|
+
node.frameIx,
|
|
117
|
+
selfUs[i] ?? 0,
|
|
118
|
+
totalUs[i] ?? 0,
|
|
119
|
+
sampleCount[i] ?? 0,
|
|
108
120
|
);
|
|
109
121
|
}
|
|
110
122
|
|
package/src/capture/cpu.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { CpuEvidence, Warning } from "../ir/types.ts";
|
|
2
2
|
import { type ProfiledRunOptions, runProfiled } from "./bunflags.ts";
|
|
3
|
-
import {
|
|
3
|
+
import { isRawCpuProfile, parseCpuProfile } from "./cpu-parse.ts";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
interface CpuCaptureOptions extends ProfiledRunOptions {
|
|
6
6
|
intervalUs: number;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
interface CpuCaptureResult {
|
|
10
10
|
diagnosticWallNs: number;
|
|
11
11
|
exitCode: number;
|
|
12
12
|
artifactPath?: string;
|
|
@@ -19,7 +19,7 @@ export interface CpuCaptureResult {
|
|
|
19
19
|
export async function runCpuCapture(
|
|
20
20
|
opts: CpuCaptureOptions,
|
|
21
21
|
): Promise<CpuCaptureResult> {
|
|
22
|
-
const { raw, ...run } = await runProfiled
|
|
22
|
+
const { raw, ...run } = await runProfiled(
|
|
23
23
|
opts,
|
|
24
24
|
[
|
|
25
25
|
"--cpu-prof",
|
|
@@ -33,6 +33,9 @@ export async function runCpuCapture(
|
|
|
33
33
|
{ what: "a .cpuprofile", kind: "CPU" },
|
|
34
34
|
);
|
|
35
35
|
if (!raw) return run;
|
|
36
|
+
if (!isRawCpuProfile(raw)) {
|
|
37
|
+
throw new Error(`ostia: ${run.artifactPath} isn't a CPU profile`);
|
|
38
|
+
}
|
|
36
39
|
|
|
37
40
|
if (raw.samples.length === 0) {
|
|
38
41
|
run.warnings.push({
|
package/src/capture/frames.ts
CHANGED
|
@@ -25,7 +25,13 @@ export function createFrameTable(): {
|
|
|
25
25
|
if (ix === undefined) {
|
|
26
26
|
ix = frames.length;
|
|
27
27
|
byUrl.set(urlKey, ix);
|
|
28
|
-
frames.push({
|
|
28
|
+
frames.push({
|
|
29
|
+
key: fp("fr", name, urlKey),
|
|
30
|
+
name,
|
|
31
|
+
...(url !== undefined && { url }),
|
|
32
|
+
...(line !== undefined && { line }),
|
|
33
|
+
...(col !== undefined && { col }),
|
|
34
|
+
});
|
|
29
35
|
}
|
|
30
36
|
return ix;
|
|
31
37
|
},
|
|
@@ -12,6 +12,29 @@ export interface RawHeapSnapshot {
|
|
|
12
12
|
strings: string[];
|
|
13
13
|
}
|
|
14
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
|
+
|
|
15
38
|
const TOP_N = 20;
|
|
16
39
|
|
|
17
40
|
interface TypeBucket {
|
|
@@ -32,8 +55,10 @@ export function parseHeapSnapshot(raw: RawHeapSnapshot): HeapEvidence {
|
|
|
32
55
|
}
|
|
33
56
|
|
|
34
57
|
const typeCount = typeNames.length;
|
|
35
|
-
const buckets
|
|
36
|
-
|
|
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>();
|
|
37
62
|
const seen: TypeBucket[] = [];
|
|
38
63
|
const newBucket = (type: string): TypeBucket => {
|
|
39
64
|
const bucket = { type, count: 0, bytes: 0 };
|
|
@@ -45,12 +70,12 @@ export function parseHeapSnapshot(raw: RawHeapSnapshot): HeapEvidence {
|
|
|
45
70
|
const nodes = raw.nodes;
|
|
46
71
|
const len = nodes.length;
|
|
47
72
|
for (let offset = 0; offset < len; offset += fieldCount) {
|
|
48
|
-
const typeIdx = nodes[offset + typeIx]
|
|
73
|
+
const typeIdx = nodes[offset + typeIx];
|
|
49
74
|
const selfSize = nodes[offset + selfSizeIx] ?? 0;
|
|
50
75
|
heapSizeBytes += selfSize;
|
|
51
76
|
let bucket: TypeBucket | undefined;
|
|
52
|
-
if (typeIdx >= 0 && typeIdx < typeCount) {
|
|
53
|
-
bucket = buckets[typeIdx] ??= newBucket(typeNames[typeIdx]
|
|
77
|
+
if (typeIdx !== undefined && typeIdx >= 0 && typeIdx < typeCount) {
|
|
78
|
+
bucket = buckets[typeIdx] ??= newBucket(typeNames[typeIdx] ?? "");
|
|
54
79
|
} else {
|
|
55
80
|
bucket = unknownBuckets.get(typeIdx);
|
|
56
81
|
if (bucket === undefined) {
|
package/src/capture/heap.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { HeapEvidence, Warning } from "../ir/types.ts";
|
|
2
2
|
import { type ProfiledRunOptions, runProfiled } from "./bunflags.ts";
|
|
3
|
-
import {
|
|
3
|
+
import { isRawHeapSnapshot, parseHeapSnapshot } from "./heap-parse.ts";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
type HeapCaptureOptions = ProfiledRunOptions;
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
interface HeapCaptureResult {
|
|
8
8
|
diagnosticWallNs: number;
|
|
9
9
|
exitCode: number;
|
|
10
10
|
artifactPath?: string;
|
|
@@ -17,7 +17,7 @@ export interface HeapCaptureResult {
|
|
|
17
17
|
export async function runHeapCapture(
|
|
18
18
|
opts: HeapCaptureOptions,
|
|
19
19
|
): Promise<HeapCaptureResult> {
|
|
20
|
-
const { raw, ...run } = await runProfiled
|
|
20
|
+
const { raw, ...run } = await runProfiled(
|
|
21
21
|
opts,
|
|
22
22
|
[
|
|
23
23
|
"--heap-prof",
|
|
@@ -28,5 +28,9 @@ export async function runHeapCapture(
|
|
|
28
28
|
],
|
|
29
29
|
{ what: "a heap snapshot", kind: "heap" },
|
|
30
30
|
);
|
|
31
|
-
|
|
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) };
|
|
32
36
|
}
|
package/src/capture/inspector.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { CpuEvidence } from "../ir/types.ts";
|
|
2
|
-
import {
|
|
2
|
+
import { isRawCpuProfile, parseCpuProfile } from "./cpu-parse.ts";
|
|
3
3
|
import {
|
|
4
4
|
DEFAULT_SAMPLING_INTERVAL_US,
|
|
5
5
|
type SamplingOptions,
|
|
6
6
|
} from "./sampling.ts";
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
interface InspectorCaptureResult<T> {
|
|
9
9
|
result: T;
|
|
10
10
|
cpu: CpuEvidence;
|
|
11
11
|
diagnosticWallNs: number;
|
|
@@ -16,8 +16,8 @@ export async function captureInspectorProfile<T>(
|
|
|
16
16
|
opts: SamplingOptions = {},
|
|
17
17
|
): Promise<InspectorCaptureResult<T>> {
|
|
18
18
|
const intervalUs = opts.intervalUs ?? DEFAULT_SAMPLING_INTERVAL_US;
|
|
19
|
-
//
|
|
20
|
-
// invocation.
|
|
19
|
+
// perf: imported lazily, since importing node:inspector costs ~4ms of
|
|
20
|
+
// startup for every invocation.
|
|
21
21
|
const { Session } = await import("node:inspector/promises");
|
|
22
22
|
const session = new Session();
|
|
23
23
|
session.connect();
|
|
@@ -34,9 +34,10 @@ export async function captureInspectorProfile<T>(
|
|
|
34
34
|
|
|
35
35
|
const result = await fn();
|
|
36
36
|
|
|
37
|
-
const { profile } =
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
const { profile } = await session.post("Profiler.stop");
|
|
38
|
+
if (!isRawCpuProfile(profile)) {
|
|
39
|
+
throw new Error("ostia: the inspector returned no CPU profile");
|
|
40
|
+
}
|
|
40
41
|
const diagnosticWallNs = Bun.nanoseconds() - start;
|
|
41
42
|
const cpu = parseCpuProfile(profile, "inspector", intervalUs);
|
|
42
43
|
return { result, cpu, diagnosticWallNs };
|
package/src/capture/jsc-parse.ts
CHANGED
|
@@ -53,7 +53,8 @@ export function parseJscProfile(
|
|
|
53
53
|
const { frames, intern } = createFrameTable();
|
|
54
54
|
function frameIxFromRaw(rf: RawJscFrame): number {
|
|
55
55
|
const isSentinel = rf.line === UINT32_SENTINEL;
|
|
56
|
-
// jsc lines
|
|
56
|
+
// spec: jsc lines and columns are 1-based; stored 0-based so they match
|
|
57
|
+
// cpu-prof and the inspector.
|
|
57
58
|
const line = isSentinel ? undefined : rf.line - 1;
|
|
58
59
|
const col =
|
|
59
60
|
isSentinel || rf.column === UINT32_SENTINEL ? undefined : rf.column - 1;
|
|
@@ -84,8 +85,8 @@ export function parseJscProfile(
|
|
|
84
85
|
for (const trace of raw.traces) {
|
|
85
86
|
const traceFrames = trace.frames;
|
|
86
87
|
let current = rootNode;
|
|
87
|
-
for (
|
|
88
|
-
const frameIx = frameIxFromRaw(
|
|
88
|
+
for (const rf of traceFrames.toReversed()) {
|
|
89
|
+
const frameIx = frameIxFromRaw(rf);
|
|
89
90
|
let child = current.children.get(frameIx);
|
|
90
91
|
if (!child) {
|
|
91
92
|
child = newNode(frameIx);
|
|
@@ -150,11 +151,10 @@ export function parseJscProfile(
|
|
|
150
151
|
[...byFrame.entries()]
|
|
151
152
|
.sort((a, b) => b[1] - a[1])
|
|
152
153
|
.slice(0, 3)
|
|
153
|
-
.
|
|
154
|
-
|
|
155
|
-
frameKey:
|
|
156
|
-
|
|
157
|
-
})),
|
|
154
|
+
.flatMap(([frameIx, samples]) => {
|
|
155
|
+
const frame = frames[frameIx];
|
|
156
|
+
return frame ? [{ tier, frameKey: frame.key, samples }] : [];
|
|
157
|
+
}),
|
|
158
158
|
);
|
|
159
159
|
|
|
160
160
|
const jit: JitTierBreakdown = {
|
package/src/capture/jsc.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { profile as jscProfile } from "bun:jsc";
|
|
2
2
|
|
|
3
3
|
import type { CpuEvidence, JitTierBreakdown } from "../ir/types.ts";
|
|
4
|
-
import { parseJscProfile
|
|
4
|
+
import { parseJscProfile } from "./jsc-parse.ts";
|
|
5
5
|
import {
|
|
6
6
|
DEFAULT_SAMPLING_INTERVAL_US,
|
|
7
7
|
type SamplingOptions,
|
|
8
8
|
} from "./sampling.ts";
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
interface JscCaptureResult<T> {
|
|
11
11
|
result: T;
|
|
12
12
|
cpu: CpuEvidence;
|
|
13
13
|
jit: JitTierBreakdown;
|
|
@@ -24,10 +24,10 @@ export async function captureJscProfile<T>(
|
|
|
24
24
|
// Diagnostic window: profiler start through stop, nothing else. Matches
|
|
25
25
|
// the inspector capture, whose session setup is outside it.
|
|
26
26
|
const start = Bun.nanoseconds();
|
|
27
|
-
const raw =
|
|
27
|
+
const raw = await jscProfile(async () => {
|
|
28
28
|
result = await fn();
|
|
29
29
|
return result;
|
|
30
|
-
}, intervalUs)
|
|
30
|
+
}, intervalUs);
|
|
31
31
|
const diagnosticWallNs = Bun.nanoseconds() - start;
|
|
32
32
|
|
|
33
33
|
const { cpu, jit } = parseJscProfile(raw.stackTraces, intervalUs);
|