ckb-cycle-tools 1.1.0 → 1.2.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/dist/cli.js CHANGED
@@ -331,7 +331,12 @@ async function flameCommand(args) {
331
331
  const costs = functionCosts(g.folded);
332
332
  if (costs.length === 1)
333
333
  out.push(" one frame: the binary has no symbols. Pass an unstripped build with --bin <code-hash>=<elf>");
334
- out.push(` profiled ${fmt(profiled)} cycles (loading the binary is not profiled)`, " SELF TOTAL FUNCTION");
334
+ out.push(` profiled ${fmt(profiled)} cycles (loading the binary is not profiled)`);
335
+ if (g.spawned) {
336
+ 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)");
338
+ }
339
+ out.push(" SELF TOTAL FUNCTION");
335
340
  for (const c of costs.slice(0, top))
336
341
  out.push(` ${fmt(c.self).padStart(10)} ${fmt(c.total).padStart(10)} ${termLine(c.name)}`);
337
342
  const base = join(dir, g.selector);
package/dist/diff.d.ts CHANGED
@@ -64,6 +64,14 @@ export interface GroupResult {
64
64
  functions?: FunctionResult[];
65
65
  /** Why `functions` were not recorded although asked for (the symbols build costs other cycles…). */
66
66
  functionsWarning?: string;
67
+ /**
68
+ * How many scripts this one spawned (CKB2023 `spawn`), seen by the run that
69
+ * recorded `functions`. Their cycles are in `cycles`, but `functions` cover
70
+ * only this script: ckb-debugger profiles the root VM alone.
71
+ */
72
+ spawned?: number;
73
+ /** With `spawned`: the cycles the function profile covers (the root script's own run). */
74
+ profiled?: number;
67
75
  }
68
76
  export interface FunctionResult {
69
77
  name: string;
@@ -113,6 +121,10 @@ export interface DiffRow {
113
121
  functions?: FunctionDelta[];
114
122
  /** The head's reason for not recording functions. */
115
123
  functionsWarning?: string;
124
+ /** The head's script spawned this many others, whose cycles no function accounts for. */
125
+ spawned?: number;
126
+ /** With `spawned`: the cycles the head's function profile covers. */
127
+ profiled?: number;
116
128
  /** Why the base side of this group was not compared (its binary was not in the base checkout). */
117
129
  notCompared?: string;
118
130
  }
@@ -200,6 +212,14 @@ export declare const termLine: (s: string) => string;
200
212
  export declare const termLines: (s: string) => string;
201
213
  /** How many changed functions a comment lists per group. */
202
214
  export declare const FUNCTIONS_SHOWN = 10;
215
+ /** What a function list leaves out when the script spawned others: their cycles. */
216
+ export declare function spawnNote(r: {
217
+ spawned?: number;
218
+ profiled?: number;
219
+ head?: {
220
+ cycles?: number;
221
+ };
222
+ }): string;
203
223
  /** The hidden marker that identifies the sticky comment, so it is updated instead of duplicated. */
204
224
  export declare const COMMENT_MARKER = "<!-- ckb-cycles-report -->";
205
225
  export interface MarkdownOptions {
package/dist/diff.js CHANGED
@@ -130,6 +130,11 @@ function groupRow(scenario, measured, h, threshold) {
130
130
  row.functions = functionDeltas(b?.functions, h.functions);
131
131
  if (h?.functionsWarning)
132
132
  row.functionsWarning = h.functionsWarning;
133
+ if (h?.spawned) {
134
+ row.spawned = h.spawned;
135
+ if (h.profiled !== undefined)
136
+ row.profiled = h.profiled;
137
+ }
133
138
  return row;
134
139
  }
135
140
  /**
@@ -299,16 +304,29 @@ function functionBlocks(rows) {
299
304
  out.push(`<details><summary>Functions in ${where}: not recorded</summary>`, "", md(r.functionsWarning), "", "</details>", "");
300
305
  continue;
301
306
  }
302
- if (!r.functions?.length)
307
+ const spawn = r.spawned ? spawnNote(r) : undefined;
308
+ if (!r.functions?.length) {
309
+ // No function of the script itself changed: with spawned scripts, that is where the change is.
310
+ if (spawn && r.functions && r.kind !== "same") {
311
+ out.push(`<details><summary>Functions in ${where}: none of its own changed</summary>`, "", md(spawn), "", "</details>", "");
312
+ }
303
313
  continue;
314
+ }
304
315
  const shown = r.functions.slice(0, FUNCTIONS_SHOWN);
305
316
  const more = r.functions.length - shown.length;
306
317
  const n = r.functions.length;
307
- out.push(`<details><summary>Functions in ${where}: ${n} function${n === 1 ? "" : "s"} changed</summary>`, "", "| Function | Base self | Head self | Δ |", "|---|---:|---:|---:|", ...shown.map((f) => `| ${md(clipName(f.name))} | ${fnCell(f.base)} | ${fnCell(f.head)} | ${f.delta === undefined ? "" : signed(f.delta)} |`), "", ...(more > 0 ? [`and ${more} more.`, ""] : []), `<sub>Self cycles, from ckb-debugger's profiler with the symbols build; — means not among the ${FUNCTION_LIMIT} costliest (or not recorded).</sub>`, "", "</details>", "");
318
+ out.push(`<details><summary>Functions in ${where}: ${n} function${n === 1 ? "" : "s"} changed</summary>`, "", "| Function | Base self | Head self | Δ |", "|---|---:|---:|---:|", ...shown.map((f) => `| ${md(clipName(f.name))} | ${fnCell(f.base)} | ${fnCell(f.head)} | ${f.delta === undefined ? "" : signed(f.delta)} |`), "", ...(more > 0 ? [`and ${more} more.`, ""] : []), ...(spawn ? [md(spawn), ""] : []), `<sub>Self cycles, from ckb-debugger's profiler with the symbols build; — means not among the ${FUNCTION_LIMIT} costliest (or not recorded).</sub>`, "", "</details>", "");
308
319
  }
309
320
  return out;
310
321
  }
311
322
  const fnCell = (n) => (n === undefined ? "—" : fmt(n));
323
+ /** What a function list leaves out when the script spawned others: their cycles. */
324
+ export function spawnNote(r) {
325
+ const n = r.spawned ?? 0;
326
+ const covers = r.profiled !== undefined && r.head?.cycles !== undefined ? ` (the profile covers ${fmt(r.profiled)} of its ${fmt(r.head.cycles)} cycles)` : "";
327
+ return (`This script spawned ${n} other script${n === 1 ? "" : "s"}: ${n === 1 ? "its" : "their"} cycles are in the group's total, ` +
328
+ `but not in these functions, which are the spawning script's own${covers}.`);
329
+ }
312
330
  /** The hidden marker that identifies the sticky comment, so it is updated instead of duplicated. */
313
331
  export const COMMENT_MARKER = "<!-- ckb-cycles-report -->";
314
332
  /**
@@ -424,7 +442,11 @@ export function formatText(diff) {
424
442
  .map((f) => `${termLine(f.name)} ${f.delta !== undefined ? signed(f.delta) : `${fnText(f.base)} → ${fnText(f.head)}`}`);
425
443
  const more = r.functions.length - top.length;
426
444
  functions.push(`functions in ${where}: ${top.join(", ")}${more > 0 ? `, and ${more} more` : ""}`);
445
+ if (r.spawned)
446
+ functions.push(` ${spawnNote(r)}`);
427
447
  }
448
+ else if (r.functions && r.spawned)
449
+ functions.push(`functions in ${where}: none of its own changed. ${spawnNote(r)}`);
428
450
  }
429
451
  const errors = (diff.reportErrors ?? []).map((e) => `error: ${termLine(e)}`);
430
452
  const warnings = (diff.warnings ?? []).map((w) => `${termLine(w.scenario)}: warning: ${termLine(w.text)}`);
package/dist/flame.d.ts CHANGED
@@ -61,5 +61,19 @@ export declare function renderFlameSvg(tree: FlameNode, opts?: {
61
61
  width?: number;
62
62
  minWidth?: number;
63
63
  }): string;
64
- /** The `All cycles: N` line ckb-debugger prints in flamegraph mode: what the profile covers. */
64
+ /**
65
+ * The `All cycles: N` line ckb-debugger prints in flamegraph mode: what the
66
+ * profile covers. The last one: when the script spawns other scripts, the
67
+ * debugger first runs the whole group (printing its full total) to record
68
+ * the syscalls, then profiles the root script alone ("Actual run").
69
+ */
65
70
  export declare function profiledCycles(output: string): bigint | undefined;
71
+ /**
72
+ * How many scripts the profiled script spawned (CKB2023 `spawn`), from the
73
+ * `Spawn tree:` lines ckb-debugger prints in flamegraph mode: one per VM,
74
+ * the root first (`Spawn tree: 0 …`, then `Spawn tree: └── 1 …`). 0 when
75
+ * there are none. The debugger profiles only the root VM, so the spawned
76
+ * scripts' cycles are in the group's total but in none of its frames. (A
77
+ * script could print such a line itself; this count only adds a note.)
78
+ */
79
+ export declare function spawnedScripts(output: string): number;
package/dist/flame.js CHANGED
@@ -192,8 +192,28 @@ function colour(name) {
192
192
  const light = 45 + ((h >>> 8) % 15);
193
193
  return `hsl(${hue} 75% ${light}%)`;
194
194
  }
195
- /** The `All cycles: N` line ckb-debugger prints in flamegraph mode: what the profile covers. */
195
+ /**
196
+ * The `All cycles: N` line ckb-debugger prints in flamegraph mode: what the
197
+ * profile covers. The last one: when the script spawns other scripts, the
198
+ * debugger first runs the whole group (printing its full total) to record
199
+ * the syscalls, then profiles the root script alone ("Actual run").
200
+ */
196
201
  export function profiledCycles(output) {
197
- const m = /^All cycles:\s*(\d+)/m.exec(output);
198
- return m ? BigInt(m[1]) : undefined;
202
+ const all = [...output.matchAll(/^All cycles:\s*(\d+)/gm)];
203
+ return all.length ? BigInt(all[all.length - 1][1]) : undefined;
204
+ }
205
+ /**
206
+ * How many scripts the profiled script spawned (CKB2023 `spawn`), from the
207
+ * `Spawn tree:` lines ckb-debugger prints in flamegraph mode: one per VM,
208
+ * the root first (`Spawn tree: 0 …`, then `Spawn tree: └── 1 …`). 0 when
209
+ * there are none. The debugger profiles only the root VM, so the spawned
210
+ * scripts' cycles are in the group's total but in none of its frames. (A
211
+ * script could print such a line itself; this count only adds a note.)
212
+ */
213
+ export function spawnedScripts(output) {
214
+ const ids = new Set();
215
+ for (const m of output.matchAll(/^Spawn tree: [\s─-╿]*(\d+)\s/gm))
216
+ ids.add(m[1]);
217
+ ids.delete("0");
218
+ return ids.size;
199
219
  }
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ export { checkMockTransaction, checkTypeId, debuggerArgs, groupsOf, nodeVerdict,
8
8
  export type { DebuggerOutput, GroupRun, Profile, ProfiledGroup, ReplayOptions, ReplayPlan, RunDebugger } from "./replay.js";
9
9
  export { flameTransaction, profileTransaction, runNativeDebugger } from "./profile.js";
10
10
  export type { FlameGroup, ProfileOptions } from "./profile.js";
11
- export { flameTree, foldedTotal, formatFolded, frameName, functionCosts, parseFolded, profiledCycles, renderFlameSvg, splitStack, } from "./flame.js";
11
+ export { flameTree, foldedTotal, formatFolded, frameName, functionCosts, parseFolded, profiledCycles, spawnedScripts, renderFlameSvg, splitStack, } from "./flame.js";
12
12
  export type { FlameNode, Folded, FunctionCost } from "./flame.js";
13
13
  export { runWasmDebugger, wasmDebuggerVersion, wasmOutput, wasmRunner, WASM_TX_FILE } from "./wasm.js";
14
14
  export type { WasmRun } from "./wasm.js";
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ export { analyzeTransaction, groupScripts, knownScriptNames, TransactionNotFound
3
3
  export { buildMockTransaction, httpRpc, isCellbase, mockFromTransaction, nodeConsensus, parseOutPointVec, scriptVersionAt, } from "./mock.js";
4
4
  export { checkMockTransaction, checkTypeId, debuggerArgs, groupsOf, nodeVerdict, parseDebuggerOutput, parseDebuggerRun, planReplay, readTransactionJson, replayGroups, selectorFor, } from "./replay.js";
5
5
  export { flameTransaction, profileTransaction, runNativeDebugger } from "./profile.js";
6
- export { flameTree, foldedTotal, formatFolded, frameName, functionCosts, parseFolded, profiledCycles, renderFlameSvg, splitStack, } from "./flame.js";
6
+ export { flameTree, foldedTotal, formatFolded, frameName, functionCosts, parseFolded, profiledCycles, spawnedScripts, renderFlameSvg, splitStack, } from "./flame.js";
7
7
  export { runWasmDebugger, wasmDebuggerVersion, wasmOutput, wasmRunner, WASM_TX_FILE } from "./wasm.js";
8
8
  export { substituteBinaries } from "./substitute.js";
9
9
  export { COMMENT_MARKER, diffFailed, diffReports, formatMarkdown, formatText, FUNCTION_LIMIT, functionDeltas, FUNCTIONS_SHOWN, resultLabel, summarize, } from "./diff.js";
package/dist/profile.d.ts CHANGED
@@ -49,6 +49,12 @@ export interface FlameGroup {
49
49
  /** With `bin`: the exit code of that run (with the deployed binary), and whether the VM stopped it. */
50
50
  deployedResult?: number;
51
51
  deployedError?: string;
52
+ /**
53
+ * How many scripts this group's script spawned (CKB2023 `spawn`). ckb-debugger
54
+ * profiles only the root script: the spawned ones' cycles are in `cycles`
55
+ * but in no frame of `folded`, and `--bin` replaces only the root's binary.
56
+ */
57
+ spawned?: number;
52
58
  }
53
59
  /**
54
60
  * Profiles each script group with ckb-debugger's flamegraph mode. `bins` maps a
package/dist/profile.js CHANGED
@@ -5,7 +5,7 @@ import { join } from "node:path";
5
5
  import { promisify } from "node:util";
6
6
  import { checkDebuggerPath, resolveDebugger } from "./debugger.js";
7
7
  import { elfFunctionNames } from "./elf.js";
8
- import { nameAddresses, parseFolded } from "./flame.js";
8
+ import { nameAddresses, parseFolded, spawnedScripts } from "./flame.js";
9
9
  import { debuggerArgs, parseDebuggerRun, planReplay, replayGroups, selectorFor, TYPE_ID_CODE_HASH, } from "./replay.js";
10
10
  // The pure parts live in replay.ts, shared with the browser; existing imports keep working.
11
11
  export { checkTypeId, parseDebuggerOutput, parseDebuggerRun, parseNames, readTransactionJson, selectorFor, TYPE_ID_CODE_HASH, TYPE_ID_CYCLES, } from "./replay.js";
@@ -112,6 +112,7 @@ export async function flameTransaction(target, options) {
112
112
  "--flamegraph-output",
113
113
  out,
114
114
  ]);
115
+ const spawned = spawnedScripts(runOut.stdout);
115
116
  const names = bin ? await namesOf(bin) : new Map();
116
117
  const folded = nameAddresses(parseFolded(await options.readFile(out).catch(() => "")), names);
117
118
  // Flamegraph mode leaves out loading the binary, so result and cycles come
@@ -127,6 +128,7 @@ export async function flameTransaction(target, options) {
127
128
  ...(parsed.error && { error: parsed.error }),
128
129
  cycles: parsed.cycles,
129
130
  folded,
131
+ ...(spawned && { spawned }),
130
132
  ...(bin &&
131
133
  deployed && {
132
134
  bin,
package/dist/report.js CHANGED
@@ -16,8 +16,8 @@
16
16
  */
17
17
  import { readFile, realpath, stat } from "node:fs/promises";
18
18
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
19
- import { FUNCTION_LIMIT, resultLabel, termLine, termLines, unmatchedError } from "./diff.js";
20
- import { functionCosts } from "./flame.js";
19
+ import { FUNCTION_LIMIT, resultLabel, spawnNote, termLine, termLines, unmatchedError } from "./diff.js";
20
+ import { foldedTotal, functionCosts } from "./flame.js";
21
21
  import { buildMockTransaction, httpRpc, mockFromTransaction, scriptVersionAt } from "./mock.js";
22
22
  import { flameTransaction, parseNames, profileTransaction, readTransactionJson } from "./profile.js";
23
23
  import { consensusOf, nodeVerdict, PUBLIC_RPC } from "./replay.js";
@@ -342,6 +342,10 @@ async function recordFunctions(groups, mock, symbols, binDir, inside, options) {
342
342
  }
343
343
  for (const g of run) {
344
344
  const f = flame.groups.find((f) => f.selector === g.selector);
345
+ if (f?.spawned) {
346
+ g.spawned = f.spawned;
347
+ g.profiled = Number(foldedTotal(f.folded));
348
+ }
345
349
  if (!f)
346
350
  g.functionsWarning = "the flamegraph run did not profile this group";
347
351
  else if (f.cycles !== BigInt(g.cycles) || resultLabel(f) !== resultLabel(g)) {
@@ -436,6 +440,8 @@ export function formatReport(report) {
436
440
  else if (g.functions?.length) {
437
441
  const top = g.functions.slice(0, 3).map((f) => `${t(f.name)} ${fmt(f.self)}`);
438
442
  out.push("", `${t(s.name)} ${t(g.selector)}: costliest functions (self cycles): ${top.join(", ")}`);
443
+ if (g.spawned)
444
+ out.push(` ${spawnNote({ ...g, head: g })}`);
439
445
  }
440
446
  }
441
447
  }
@@ -130,6 +130,17 @@ function substituteOne(mock, sub, skipUnmatched) {
130
130
  if (argsMentions) {
131
131
  warnings.push(`the old code hash still appears in the args of ${argsMentions} script(s); args are not rewritten`);
132
132
  }
133
+ // A script that spawns or execs this code by its data hash may read that hash
134
+ // from a cell's data (a match cell naming its game): it will not find the new build.
135
+ const needle = oldDataHash.slice(2);
136
+ const dataMentions = [...mock.mock_info.inputs, ...mock.mock_info.cell_deps]
137
+ .map((c) => c.data)
138
+ .concat(mock.tx.outputs_data)
139
+ .filter((d) => d.toLowerCase().includes(needle)).length;
140
+ if (dataMentions) {
141
+ warnings.push(`the old code hash still appears in the data of ${dataMentions} cell(s); cell data is not rewritten, ` +
142
+ `so a script that spawns or execs this code by the hash it reads there does not find the new build`);
143
+ }
133
144
  }
134
145
  return {
135
146
  ...(sub.name && { name: sub.name }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ckb-cycle-tools",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Cycle measurement for CKB on-chain scripts",
5
5
  "keywords": [
6
6
  "ckb",
@@ -39,6 +39,7 @@
39
39
  "cycles": "npm run build && node dist/cli.js",
40
40
  "bench:build": "npm run build && node benchmarks/session-lock/build-js.mjs",
41
41
  "bench:session-lock": "npm run bench:build && node benchmarks/session-lock/compare.mjs",
42
+ "bench:floor": "npm run build && node benchmarks/session-lock/floor.mjs",
42
43
  "prepack": "npm run build",
43
44
  "pack:check": "node scripts/pack-check.mjs",
44
45
  "atlas": "tsx scripts/atlas.ts",