ckb-cycle-tools 1.2.0 → 1.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.
@@ -0,0 +1,100 @@
1
+ import { type Folded } from "./flame.js";
2
+ import type { MockTransaction } from "./mock.js";
3
+ import { type DebuggerOutput } from "./replay.js";
4
+ /** A spawned script's own profile (`--vm-id`), as `flameTransaction` and `profileSpawns` record it. */
5
+ export interface FlameChild {
6
+ /** Its VM id (`--vm-id`), and the VM that spawned it (0: the group's script). */
7
+ vm: number;
8
+ parent: number;
9
+ /** Where its binary came from, as ckb-debugger prints it: `cell_dep[1][0..11552]`. */
10
+ locator: string;
11
+ /** The data hash of the binary it ran, when the locator names a cell of the transaction. */
12
+ codeHash?: string;
13
+ /** A name for it: `scriptNames` by its data hash (or its cell's type hash). */
14
+ name?: string;
15
+ /** The unstripped build its frames were named with (`--vm-symbols`): `bins` or `vmSymbols` by its data or type hash. */
16
+ symbols?: string;
17
+ /** Why no symbols build was used although one was given (it is not the same code). */
18
+ symbolsWarning?: string;
19
+ /** The replay's exit code (-1 with `error` when the VM stopped it), and its own cycle count, which counts loading the binary. */
20
+ result: number;
21
+ error?: string;
22
+ cycles: bigint;
23
+ /** Its folded stacks: its own instructions only. */
24
+ folded: Folded;
25
+ /** Set when the replay could not be run or read: no result, no profile. */
26
+ failed?: string;
27
+ }
28
+ /** The most spawned scripts profiled one by one per group; each is three runs of the whole group. */
29
+ export declare const MAX_CHILD_PROFILES = 16;
30
+ /**
31
+ * Shown when a spawned script's own replay ends non-zero but its group
32
+ * verified: typically a ckb-script-ipc server (`run_server`) that waits for
33
+ * another request after the spawning script has finished, and fails on the
34
+ * closed pipe. Its cycles up to there are the ones the group paid for.
35
+ */
36
+ export declare const SERVER_EXIT_NOTE = "the group verified: a spawned script whose own replay ends non-zero is usually a server (ckb-script-ipc run_server) still waiting after the spawning script finished; its profile up to there is complete";
37
+ /** Whether a child gets SERVER_EXIT_NOTE: its own replay ended non-zero (or the VM stopped it) while its group verified. */
38
+ export declare const serverExit: (c: Pick<FlameChild, "result" | "error" | "failed">, groupOk: boolean) => boolean;
39
+ /** Why a debugger (its `--version` line) older than CHILD_PROFILE_DEBUGGER profiles no spawned script on its own. */
40
+ export declare const olderDebuggerNote: (version: string) => string;
41
+ /** What profileChildren needs to replay a group's spawned VMs. */
42
+ export interface ChildContext {
43
+ /** The debugger's `--version` line: below 1.1.2 nothing is replayed. */
44
+ version: string;
45
+ /** The group's arguments (`debuggerArgs`, plus `--bin` if the root ran a build): each replay adds `--vm-id N`. */
46
+ base: string[];
47
+ /** Where the replay of VM `vm` writes its folded stacks (`--flamegraph-output`); `readFile` reads them there. */
48
+ out: (vm: number) => string;
49
+ exec: (args: string[]) => Promise<DebuggerOutput>;
50
+ readFile: (path: string) => Promise<string>;
51
+ /** The mock transaction the group runs in, and the group's cells: to find the binary each VM ran. */
52
+ mock: MockTransaction;
53
+ group: {
54
+ inputs: number[];
55
+ outputs: number[];
56
+ };
57
+ /** Names by `codeHash:hashType` (lowercase): a child is named by its data hash, or its cell's type hash. */
58
+ names?: Record<string, string>;
59
+ /** An unstripped build for a data or type hash, to name a child's frames (`--vm-symbols`), with how to read it. */
60
+ symbolsOf?: (hash: string) => string | undefined;
61
+ readBinary?: (path: string) => Promise<Uint8Array>;
62
+ namesOf?: (bin: string) => Promise<Map<bigint, string>>;
63
+ }
64
+ /**
65
+ * One `--vm-id` flamegraph replay per spawned VM of a group (generation 0, at
66
+ * most MAX_CHILD_PROFILES), from the spawn tree in the root's flamegraph run.
67
+ * Nothing is thrown: a replay that fails is a child with `failed`, and a
68
+ * debugger that cannot replay a child at all is a note.
69
+ */
70
+ export declare function profileChildren(rootOutput: string, ctx: ChildContext): Promise<{
71
+ children?: FlameChild[];
72
+ childrenNote?: string;
73
+ }>;
74
+ /** A group split by script: what profileSpawns returns for a group whose script spawned others. */
75
+ export interface SpawnSplit {
76
+ /** How many scripts the group's script spawned (`spawnedScripts`). */
77
+ spawned: number;
78
+ /** The spawning script's own profiled frames (VM 0 alone): loading it and the spawned scripts are in none of them. */
79
+ own: Folded;
80
+ /** Each spawned script's own profile (profileChildren), and why some have none. */
81
+ children?: FlameChild[];
82
+ childrenNote?: string;
83
+ }
84
+ /**
85
+ * Splits one group by script, with any ckb-debugger 1.1.2 or later: a
86
+ * flamegraph run of the group (the spawning script's own frames, and the
87
+ * spawn tree), then, if its script spawned others, one `--vm-id` replay per
88
+ * spawned VM (profileChildren). Undefined when it spawned nothing. The
89
+ * group's cycles are not read here: they come from its plain run
90
+ * (replayGroups), the total these frames sit inside. Throws (so the caller
91
+ * can say the spawn check failed, rather than that nothing was spawned) when
92
+ * the flamegraph run cannot be run or did not finish: no result lines, a
93
+ * crash (the WebAssembly build trapped, exit -1), a kill, cut-off output, or,
94
+ * for a script that spawned, no "Actual run" of its own. Also throws, before
95
+ * running anything, for a debugger older than 1.1.2 (olderDebuggerNote):
96
+ * 0.200.1, for one, has no flamegraph mode at all, so a spawn could not even
97
+ * be seen. A script that rejects is not a failed check: its run still shows
98
+ * what it spawned.
99
+ */
100
+ export declare function profileSpawns(ctx: ChildContext): Promise<SpawnSplit | undefined>;
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Per-child profiles of a script that spawns others (CKB2023 `spawn`): one
3
+ * `--vm-id` flamegraph replay per spawned VM, with ckb-debugger 1.1.2 or
4
+ * later. Plain TypeScript with no Node built-ins, like replay.ts and flame.ts,
5
+ * so the CLI (native debugger, profile.ts) and the web page (WebAssembly
6
+ * build, wasm.ts) split a group the same way and say the same thing about it.
7
+ */
8
+ import { sameCode } from "./elf.js";
9
+ import { nameAddresses, parseFolded, profilesChildren, spawnedScripts, spawnTree, CHILD_PROFILE_DEBUGGER } from "./flame.js";
10
+ import { parseDebuggerRun } from "./replay.js";
11
+ import { spawnedBinary } from "./spawn.js";
12
+ /** The most spawned scripts profiled one by one per group; each is three runs of the whole group. */
13
+ export const MAX_CHILD_PROFILES = 16;
14
+ /**
15
+ * Shown when a spawned script's own replay ends non-zero but its group
16
+ * verified: typically a ckb-script-ipc server (`run_server`) that waits for
17
+ * another request after the spawning script has finished, and fails on the
18
+ * closed pipe. Its cycles up to there are the ones the group paid for.
19
+ */
20
+ export const SERVER_EXIT_NOTE = "the group verified: a spawned script whose own replay ends non-zero is usually a server (ckb-script-ipc run_server) still waiting after the spawning script finished; its profile up to there is complete";
21
+ /** Whether a child gets SERVER_EXIT_NOTE: its own replay ended non-zero (or the VM stopped it) while its group verified. */
22
+ export const serverExit = (c, groupOk) => groupOk && !c.failed && (c.error !== undefined || c.result !== 0);
23
+ /** Why a debugger (its `--version` line) older than CHILD_PROFILE_DEBUGGER profiles no spawned script on its own. */
24
+ export const olderDebuggerNote = (version) => `${version} cannot profile a spawned script on its own (ckb-debugger ${CHILD_PROFILE_DEBUGGER} or later can)`;
25
+ /**
26
+ * One `--vm-id` flamegraph replay per spawned VM of a group (generation 0, at
27
+ * most MAX_CHILD_PROFILES), from the spawn tree in the root's flamegraph run.
28
+ * Nothing is thrown: a replay that fails is a child with `failed`, and a
29
+ * debugger that cannot replay a child at all is a note.
30
+ */
31
+ export async function profileChildren(rootOutput, ctx) {
32
+ if (!profilesChildren(ctx.version))
33
+ return { childrenNote: `${olderDebuggerNote(ctx.version)}: only the spawning script is profiled` };
34
+ const tree = spawnTree(rootOutput);
35
+ const spawned = tree.filter((v) => v.vm !== 0 && v.generation === 0);
36
+ const notes = [];
37
+ const execd = new Set(tree.filter((v) => v.generation > 0).map((v) => v.vm)).size;
38
+ if (execd)
39
+ notes.push(`${execd} VM${execd === 1 ? "" : "s"} ran exec: ckb-debugger replays only a VM's first program, so code reached by exec is not profiled`);
40
+ if (spawned.length > MAX_CHILD_PROFILES)
41
+ notes.push(`${spawned.length} spawned scripts: the first ${MAX_CHILD_PROFILES} are profiled`);
42
+ if (!spawned.length && !notes.length)
43
+ notes.push("the spawn tree could not be read: spawned scripts not profiled");
44
+ const names = ctx.names ?? {};
45
+ const children = [];
46
+ for (const v of spawned.slice(0, MAX_CHILD_PROFILES)) {
47
+ const binary = spawnedBinary(ctx.mock, v, ctx.group);
48
+ const hashes = binary ? [binary.dataHash, ...(binary.typeHash ? [binary.typeHash] : [])] : [];
49
+ const name = binary &&
50
+ (["data2", "data1", "data"].map((t) => names[`${binary.dataHash}:${t}`]).find(Boolean) ??
51
+ (binary.typeHash ? names[`${binary.typeHash}:type`] : undefined));
52
+ let symbols = ctx.symbolsOf ? hashes.map(ctx.symbolsOf).find(Boolean) : undefined;
53
+ let symbolsWarning;
54
+ if (symbols && binary) {
55
+ const same = ctx.readBinary
56
+ ? await ctx.readBinary(symbols).then((b) => sameCode(b, binary.data), () => undefined)
57
+ : undefined;
58
+ if (same !== true) {
59
+ symbolsWarning =
60
+ same === false
61
+ ? `${symbols} is not the same code as the binary VM ${v.vm} ran: its frames are left unnamed`
62
+ : `${symbols} could not be read as an ELF: VM ${v.vm}'s frames are left unnamed`;
63
+ symbols = undefined;
64
+ }
65
+ }
66
+ const child = {
67
+ vm: v.vm,
68
+ parent: v.parent,
69
+ locator: v.locator,
70
+ ...(binary && { codeHash: binary.dataHash }),
71
+ ...(name && { name }),
72
+ ...(symbols && { symbols }),
73
+ ...(symbolsWarning && { symbolsWarning }),
74
+ result: -1,
75
+ cycles: 0n,
76
+ folded: new Map(),
77
+ };
78
+ try {
79
+ const out = ctx.out(v.vm);
80
+ const run = await ctx.exec([
81
+ ...ctx.base,
82
+ "--vm-id",
83
+ String(v.vm),
84
+ ...(symbols ? ["--vm-symbols", symbols] : []),
85
+ "--enable-flamegraph",
86
+ "--flamegraph-output",
87
+ out,
88
+ ]);
89
+ if (!/^Actual run: start\s*$/m.test(run.stdout))
90
+ throw new Error(`no replay of VM ${v.vm} in ckb-debugger's output`);
91
+ const parsed = parseDebuggerRun(run);
92
+ child.result = parsed.result;
93
+ if (parsed.error)
94
+ child.error = parsed.error;
95
+ child.cycles = parsed.cycles;
96
+ const fnNames = symbols && ctx.namesOf ? await ctx.namesOf(symbols) : new Map();
97
+ child.folded = nameAddresses(parseFolded(await ctx.readFile(out).catch(() => "")), fnNames);
98
+ }
99
+ catch (e) {
100
+ child.failed = `the replay of VM ${v.vm} failed: ${e instanceof Error ? e.message : String(e)}`;
101
+ }
102
+ children.push(child);
103
+ }
104
+ return { ...(children.length && { children }), ...(notes.length && { childrenNote: notes.join("; ") }) };
105
+ }
106
+ /**
107
+ * Splits one group by script, with any ckb-debugger 1.1.2 or later: a
108
+ * flamegraph run of the group (the spawning script's own frames, and the
109
+ * spawn tree), then, if its script spawned others, one `--vm-id` replay per
110
+ * spawned VM (profileChildren). Undefined when it spawned nothing. The
111
+ * group's cycles are not read here: they come from its plain run
112
+ * (replayGroups), the total these frames sit inside. Throws (so the caller
113
+ * can say the spawn check failed, rather than that nothing was spawned) when
114
+ * the flamegraph run cannot be run or did not finish: no result lines, a
115
+ * crash (the WebAssembly build trapped, exit -1), a kill, cut-off output, or,
116
+ * for a script that spawned, no "Actual run" of its own. Also throws, before
117
+ * running anything, for a debugger older than 1.1.2 (olderDebuggerNote):
118
+ * 0.200.1, for one, has no flamegraph mode at all, so a spawn could not even
119
+ * be seen. A script that rejects is not a failed check: its run still shows
120
+ * what it spawned.
121
+ */
122
+ export async function profileSpawns(ctx) {
123
+ if (!profilesChildren(ctx.version))
124
+ throw new Error(olderDebuggerNote(ctx.version));
125
+ const rootOut = ctx.out(0);
126
+ const run = await ctx.exec([...ctx.base, "--enable-flamegraph", "--flamegraph-output", rootOut]);
127
+ const failed = (why) => new Error(`the spawn check (a profiled run of the group) failed: ${why}`);
128
+ // parseDebuggerRun turns a crash, a kill or cut-off output into an error (and throws on no result lines).
129
+ let parsed;
130
+ try {
131
+ parsed = parseDebuggerRun(run);
132
+ }
133
+ catch (e) {
134
+ throw failed(e instanceof Error ? e.message : String(e));
135
+ }
136
+ if (run.failure || run.exitCode === null || run.exitCode === -1)
137
+ throw failed(parsed.error ?? "the run did not finish");
138
+ const spawned = spawnedScripts(run.stdout);
139
+ if (!spawned)
140
+ return undefined;
141
+ if (!/^Actual run: start\s*$/m.test(run.stdout))
142
+ throw failed("no profiled run of the spawning script in ckb-debugger's output");
143
+ const own = parseFolded(await ctx.readFile(rootOut).catch(() => ""));
144
+ const { children, childrenNote } = await profileChildren(run.stdout, ctx);
145
+ return { spawned, own, ...(children && { children }), ...(childrenNote && { childrenNote }) };
146
+ }
package/dist/cli.js CHANGED
@@ -6,7 +6,8 @@ import { diffFailed, diffReports, formatMarkdown, formatText, termLine, termLine
6
6
  import { buildMockTransaction, httpRpc, mockFromTransaction } from "./mock.js";
7
7
  import { flameTree, foldedTotal, formatFolded, functionCosts, renderFlameSvg } from "./flame.js";
8
8
  import { flameTransaction, parseNames, profileTransaction, readTransactionJson } from "./profile.js";
9
- import { PUBLIC_RPC } from "./replay.js";
9
+ import { PUBLIC_RPC, sumLines } from "./replay.js";
10
+ import { serverExit, SERVER_EXIT_NOTE } from "./children.js";
10
11
  import { formatReport, runReport } from "./report.js";
11
12
  import { formatScan, scanTestMocks } from "./scan.js";
12
13
  const USAGE = `usage: ckb-cycles <tx-hash> [--mainnet] [--rpc <url>] [--timeout <s>] [--debugger <path>]
@@ -14,8 +15,9 @@ const USAGE = `usage: ckb-cycles <tx-hash> [--mainnet] [--rpc <url>] [--timeout
14
15
  ckb-cycles --file <tx.json> [same options]
15
16
  ckb-cycles report <cycles.json> [--json <out.json>] [--binaries-from <dir>]
16
17
  [--root <dir> ...] [--rpc <url>] [--timeout <s>] [--debugger <path>]
18
+ [--toolchain <text>]
17
19
  ckb-cycles diff <base.json> <head.json> [--markdown] [--fail-above <percent>]
18
- [--fail-on-result-change] [--allow-removed] [--title <s>]
20
+ [--fail-on-result-change] [--allow-removed] [--allow-changed-inputs] [--title <s>]
19
21
  [--base-label <s>] [--head-label <s>]
20
22
  ckb-cycles mock <tx-hash> | --file <tx.json> [-o <mock.json>] [--mainnet] [--rpc <url>]
21
23
  [--timeout <s>]
@@ -38,7 +40,8 @@ the cycles each group cost.
38
40
  repeat for several. Without :<hash-type> it applies to all of them
39
41
 
40
42
  Exit codes: 0 every script accepts (and the sum matches the node's)
41
- 1 a script rejects, or the sum differs from the node's
43
+ 1 a script rejects, the sum differs from the node's, or the sum is
44
+ over the node's per-transaction limit (max_block_cycles)
42
45
  2 usage or environment error: bad arguments, node unreachable,
43
46
  transaction not found, ckb-debugger missing or unreadable output
44
47
 
@@ -55,11 +58,18 @@ Exit codes: 0 every script accepts (and the sum matches the node's)
55
58
  cell dep in any scenario (both also in the JSON's "errors"):
56
59
  a rejection is a result, not an error. A binary missing from
57
60
  --binaries-from is left as the mock has it: only the groups
58
- whose code it is are marked, and not compared
61
+ whose code it is are marked, and not compared.
62
+ The JSON's "manifest" records what the numbers were measured
63
+ with: tool version, ckb-debugger version and the sha256 of its
64
+ binary, the git commit, per scenario the CKB-VM version, a hash
65
+ of its inputs and the swapped-in binaries' hashes. --toolchain
66
+ adds free text to it (e.g. the output of rustc -V)
59
67
  diff compares two reports group by group: Δ cycles, Δ%, and any group
60
68
  whose result changed (ok → exit 12, or back). --fail-above exits 1
61
69
  when a group grows by more than that percentage (a new group
62
70
  has no base, so no percentage: it is never over the threshold),
71
+ or a group that accepted on the base rejects on the head
72
+ (whatever its cycles; also with --fail-on-result-change),
63
73
  --fail-on-result-change when a result changed, including a new
64
74
  group that does not accept, or a group the VM stopped (also
65
75
  over any --fail-above). A scenario the head could not run
@@ -67,7 +77,12 @@ Exit codes: 0 every script accepts (and the sum matches the node's)
67
77
  unchecked: a scenario the base could not run, an error in
68
78
  either report, a base group with nothing in the head to
69
79
  compare with (removed, renamed, moved, or its binary not built
70
- on the base); --allow-removed accepts the last
80
+ on the base); --allow-removed accepts the last. With both
81
+ reports' manifests: a scenario whose inputs (mock, transaction
82
+ file, cycles.json entry) changed is NOT COMPARED, and fails a
83
+ gated diff unless --allow-changed-inputs; another ckb-debugger
84
+ (version or binary) or CKB-VM version fails a gated diff, and
85
+ is a note otherwise. A report without a manifest is a note
71
86
  mock saves a transaction as a ckb-debugger mock transaction, every cell
72
87
  it touches included, so a scenario can replay it with no node
73
88
  scan writes a cycles.json from the mock transactions a test suite wrote
@@ -83,7 +98,10 @@ Exit codes: 0 every script accepts (and the sum matches the node's)
83
98
  one frame: --bin gives an unstripped build of the same code (its
84
99
  cycles are checked against the deployed binary's). The profile
85
100
  leaves out loading the binary, so it is a little under the group's
86
- cycles`;
101
+ cycles. A script that spawns others: with ckb-debugger 1.1.2 or
102
+ later, each spawned script is profiled on its own (--vm-id) into
103
+ <dir>/<selector>.vm<N>.svg and .folded; --bin with the spawned
104
+ binary's data hash names its functions (--vm-symbols)`;
87
105
  const fmt = (n) => n.toLocaleString("en-US");
88
106
  const short = (h, n = 6) => (h.length > 2 * n + 2 ? `${h.slice(0, n + 2)}…${h.slice(-n)}` : h);
89
107
  function table(p) {
@@ -101,7 +119,7 @@ function table(p) {
101
119
  const line = (cells) => cells.map((c, i) => (i >= 4 ? c.padStart(widths[i]) : c.padEnd(widths[i]))).join(" ");
102
120
  const title = p.nodeCycles === undefined && p.status === "not on chain" ? "tx not on chain" : `tx ${p.hash} (${p.status})`;
103
121
  const out = [title, "", line(head), ...rows.map(line), ""];
104
- out.push(`measured (sum of groups) ${fmt(p.measuredCycles)}`);
122
+ out.push(...sumLines(p, termLine));
105
123
  if (p.nodeCycles !== undefined) {
106
124
  const match = p.nodeCycles === p.measuredCycles;
107
125
  out.push(`reported by node ${fmt(p.nodeCycles)} ${match ? "✓ match" : "✗ MISMATCH"}`);
@@ -138,6 +156,7 @@ async function reportCommand(args) {
138
156
  debugger: { type: "string" },
139
157
  "binaries-from": { type: "string" },
140
158
  root: { type: "string", multiple: true },
159
+ toolchain: { type: "string" },
141
160
  },
142
161
  });
143
162
  if (positionals.length !== 1) {
@@ -150,6 +169,7 @@ async function reportCommand(args) {
150
169
  ...(values.debugger && { debuggerPath: values.debugger }),
151
170
  ...(values["binaries-from"] && { binariesRoot: values["binaries-from"] }),
152
171
  ...(values.root && { roots: values.root }),
172
+ ...(values.toolchain && { toolchain: values.toolchain }),
153
173
  onScenario: (s) => console.error(`${s.error ? "✗" : "·"} ${termLine(s.name)}`),
154
174
  });
155
175
  const { errors, notices, ...data } = report;
@@ -179,6 +199,7 @@ async function diffCommand(args) {
179
199
  "fail-above": { type: "string" },
180
200
  "fail-on-result-change": { type: "boolean" },
181
201
  "allow-removed": { type: "boolean" },
202
+ "allow-changed-inputs": { type: "boolean" },
182
203
  "base-label": { type: "string" },
183
204
  "head-label": { type: "string" },
184
205
  title: { type: "string" },
@@ -197,6 +218,7 @@ async function diffCommand(args) {
197
218
  ...(failAbove !== undefined && { failAbove }),
198
219
  failOnResultChange: values["fail-on-result-change"],
199
220
  allowRemoved: values["allow-removed"],
221
+ allowChangedInputs: values["allow-changed-inputs"],
200
222
  };
201
223
  const diff = diffReports(await readReport(positionals[0]), await readReport(positionals[1]), options);
202
224
  console.log(values.markdown
@@ -334,7 +356,9 @@ async function flameCommand(args) {
334
356
  out.push(` profiled ${fmt(profiled)} cycles (loading the binary is not profiled)`);
335
357
  if (g.spawned) {
336
358
  const n = g.spawned;
337
- out.push(` spawned ${n} other script${n === 1 ? "" : "s"}: ${n === 1 ? "its" : "their"} cycles are in the ${fmt(g.cycles)}, not in this profile`, " (ckb-debugger profiles only the spawning script; --bin replaces only its binary)");
359
+ out.push(` spawned ${n} other script${n === 1 ? "" : "s"}: ${n === 1 ? "its" : "their"} cycles are in the ${fmt(g.cycles)}, not in this profile`, g.children?.length
360
+ ? " (this profile is the spawning script's own; each spawned script's follows, from a --vm-id replay)"
361
+ : " (ckb-debugger profiles only the spawning script; --bin replaces only its binary)");
338
362
  }
339
363
  out.push(" SELF TOTAL FUNCTION");
340
364
  for (const c of costs.slice(0, top))
@@ -343,11 +367,41 @@ async function flameCommand(args) {
343
367
  await writeFile(`${base}.folded`, formatFolded(g.folded) + "\n");
344
368
  await writeFile(`${base}.svg`, renderFlameSvg(flameTree(g.folded, g.selector), { title: `${g.selector} · ${label}` }));
345
369
  out.push(` → ${base}.svg, ${base}.folded`, "");
370
+ for (const c of g.children ?? [])
371
+ out.push(...(await childOutput(g.selector, c, dir, top, g.result === 0 && !g.error)));
372
+ if (g.childrenNote)
373
+ out.push(` spawned scripts: ${termLine(g.childrenNote)}`, "");
346
374
  }
347
375
  out.push(`${termLine(version)} · CKB-VM v${plan.scriptVersion} for type scripts · ${plan.network} · ${plan.rpcUrl}`);
348
376
  console.log(out.join("\n"));
349
377
  return bad || groups.some((g) => g.result !== 0) ? 1 : 0;
350
378
  }
379
+ /** One spawned script's profile under its group in `flame`, and its own .folded and .svg (`<selector>.vm<N>`). */
380
+ async function childOutput(selector, c, dir, top, groupOk) {
381
+ const label = termLine(c.name ?? (c.codeHash ? short(c.codeHash) : c.locator));
382
+ const head = ` spawned vm ${c.vm} ${label} (${termLine(c.locator)}${c.parent ? `, spawned by vm ${c.parent}` : ""})`;
383
+ if (c.failed)
384
+ return [`${head} not profiled`, ` ${termLine(c.failed)}`, ""];
385
+ const out = [`${head} ${c.error ? "error" : c.result === 0 ? "ok" : `exit ${c.result}`}`];
386
+ if (serverExit(c, groupOk))
387
+ out.push(` ${SERVER_EXIT_NOTE}`);
388
+ if (c.symbolsWarning)
389
+ out.push(` ${termLine(c.symbolsWarning)}`);
390
+ if (!c.folded.size)
391
+ return [...out, " no profile written", ""];
392
+ const costs = functionCosts(c.folded);
393
+ if (!c.symbols)
394
+ out.push(` no symbols build for it: pass --bin ${c.codeHash ?? "<data-hash>"}=<unstripped build> to name its functions`);
395
+ out.push(` profiled ${fmt(foldedTotal(c.folded))} cycles in its own frames (inside the group's total; loading it and the spawn are not profiled)`);
396
+ out.push(" SELF TOTAL FUNCTION");
397
+ for (const f of costs.slice(0, top))
398
+ out.push(` ${fmt(f.self).padStart(10)} ${fmt(f.total).padStart(10)} ${termLine(f.name)}`);
399
+ const base = join(dir, `${selector}.vm${c.vm}`);
400
+ await writeFile(`${base}.folded`, formatFolded(c.folded) + "\n");
401
+ await writeFile(`${base}.svg`, renderFlameSvg(flameTree(c.folded, `vm ${c.vm}`), { title: `${selector} · vm ${c.vm} · ${label}` }));
402
+ out.push(` → ${base}.svg, ${base}.folded`, "");
403
+ return out;
404
+ }
351
405
  async function main() {
352
406
  const [command, ...rest] = process.argv.slice(2);
353
407
  if (command === "report")
@@ -399,7 +453,8 @@ async function main() {
399
453
  console.log(table(p));
400
454
  }
401
455
  const mismatch = p.nodeCycles !== undefined && p.nodeCycles !== p.measuredCycles;
402
- return mismatch || p.groups.some((g) => g.result !== 0) ? 1 : 0;
456
+ // Over the node's per-transaction limit: the node would reject it, so a verdict too.
457
+ return mismatch || p.groups.some((g) => g.result !== 0) || p.overTxLimit !== undefined ? 1 : 0;
403
458
  }
404
459
  // 1 is a verdict (a script rejects, a sum differs, a diff over its thresholds);
405
460
  // anything thrown is the tool not getting that far: 2, so a CI job asserting a