ckb-cycle-tools 1.0.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/CHANGELOG.md +169 -0
- package/README.md +659 -394
- package/dist/cli.js +79 -23
- package/dist/diff.d.ts +94 -8
- package/dist/diff.js +167 -29
- package/dist/elf.d.ts +18 -0
- package/dist/elf.js +209 -0
- package/dist/flame.d.ts +21 -1
- package/dist/flame.js +43 -3
- package/dist/index.d.ts +4 -2
- package/dist/index.js +3 -2
- package/dist/profile.d.ts +11 -1
- package/dist/profile.js +20 -4
- package/dist/replay.d.ts +14 -1
- package/dist/replay.js +44 -2
- package/dist/report.d.ts +6 -1
- package/dist/report.js +86 -31
- package/dist/scan.d.ts +46 -0
- package/dist/scan.js +122 -0
- package/dist/substitute.d.ts +7 -0
- package/dist/substitute.js +39 -7
- package/dist/wasm.d.ts +1 -1
- package/dist/wasm.js +7 -0
- package/package.json +12 -4
package/dist/diff.js
CHANGED
|
@@ -6,11 +6,25 @@
|
|
|
6
6
|
*/
|
|
7
7
|
/** How many functions a report keeps per group. */
|
|
8
8
|
export const FUNCTION_LIMIT = 15;
|
|
9
|
+
/** The message for a binary no scenario's cell deps match, in a report's `errors` and the diff. */
|
|
10
|
+
export const unmatchedError = (name) => `binary "${name}" matched no cell dep in any scenario: check its outPoint / codeHash`;
|
|
9
11
|
/** The base side of a new group that does not accept: compared with "did not run". */
|
|
10
12
|
const NOT_RUN = "not run";
|
|
11
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* `ok`, `exit 12`, or `error: <kind>` when the VM stopped the script: the
|
|
15
|
+
* debugger's message without its `key=value` details (a pc, an instruction),
|
|
16
|
+
* so "invalid instruction" and "max cycles exceeded" are different results.
|
|
17
|
+
*/
|
|
12
18
|
export function resultLabel(g) {
|
|
13
|
-
return g.error ?
|
|
19
|
+
return g.error ? `error: ${errorKind(g.error)}` : g.result === 0 ? "ok" : `exit ${g.result}`;
|
|
20
|
+
}
|
|
21
|
+
function errorKind(error) {
|
|
22
|
+
const kind = error
|
|
23
|
+
.replace(/\s*\b\w+=\S*/g, "")
|
|
24
|
+
.replace(/\b0x[0-9a-fA-F]+\b/g, "")
|
|
25
|
+
.replace(/\s+/g, " ")
|
|
26
|
+
.trim();
|
|
27
|
+
return (kind || "vm").slice(0, 80);
|
|
14
28
|
}
|
|
15
29
|
export function diffReports(base, head, options = {}) {
|
|
16
30
|
const threshold = options.failAbove;
|
|
@@ -24,8 +38,9 @@ export function diffReports(base, head, options = {}) {
|
|
|
24
38
|
return;
|
|
25
39
|
}
|
|
26
40
|
// A base that could not run the scenario has nothing to compare against, so
|
|
27
|
-
// every group is new.
|
|
28
|
-
//
|
|
41
|
+
// every group is new. That is the base being broken, and the gate must not
|
|
42
|
+
// pass over it in silence. (Only a report before 1.1.0 records a missing
|
|
43
|
+
// binary as a scenario error with binaryMissing; its groups count as new.)
|
|
29
44
|
if (h && b?.error && !b.binaryMissing) {
|
|
30
45
|
rows.push({ scenario: name, group: "", script: "", kind: "base-error", overThreshold: false, note: `base: ${b.error}` });
|
|
31
46
|
}
|
|
@@ -42,16 +57,41 @@ export function diffReports(base, head, options = {}) {
|
|
|
42
57
|
for (const b of base.scenarios)
|
|
43
58
|
if (!headNames.has(b.name))
|
|
44
59
|
scenarioRows(undefined, b);
|
|
60
|
+
const groups = rows.filter((r) => r.kind !== "error" && r.kind !== "base-error");
|
|
45
61
|
return {
|
|
46
62
|
rows,
|
|
47
63
|
resultChanges: rows.filter((r) => r.kind === "result").length,
|
|
48
64
|
overThreshold: rows.filter((r) => r.overThreshold).length,
|
|
49
65
|
baseErrors: rows.filter((r) => r.kind === "base-error").length,
|
|
66
|
+
removed: groups.filter((r) => r.kind === "removed" || r.notCompared).length,
|
|
67
|
+
newGroups: groups.filter((r) => r.kind !== "removed" && (!r.base || r.base.result === NOT_RUN)).length,
|
|
68
|
+
reportErrors: [...reportErrors("base", base), ...reportErrors("head", head)],
|
|
69
|
+
warnings: head.scenarios.flatMap((s) => (s.warnings ?? []).map((text) => ({ scenario: s.name, text }))),
|
|
70
|
+
...(base.debugger && head.debugger && base.debugger !== head.debugger && { debuggers: { base: base.debugger, head: head.debugger } }),
|
|
50
71
|
...(threshold !== undefined && { threshold }),
|
|
51
72
|
};
|
|
52
73
|
}
|
|
53
|
-
|
|
74
|
+
/**
|
|
75
|
+
* A report's errors other than its scenarios' own (those are rows already),
|
|
76
|
+
* and, from its scenarios, any binary offered but swapped in nowhere: a report
|
|
77
|
+
* written without its `errors` still says that nothing measured that binary.
|
|
78
|
+
*/
|
|
79
|
+
function reportErrors(side, r) {
|
|
80
|
+
const ofScenarios = new Set(r.scenarios.filter((s) => s.error).map((s) => `scenario "${s.name}": ${s.error}`));
|
|
81
|
+
const out = new Set((Array.isArray(r.errors) ? r.errors : []).map(String).filter((e) => !ofScenarios.has(e)));
|
|
82
|
+
// As the report does: only when every scenario ran (one that failed may be the one that uses it).
|
|
83
|
+
if (!ofScenarios.size) {
|
|
84
|
+
const swapped = new Set(r.scenarios.flatMap((s) => s.substituted ?? []));
|
|
85
|
+
for (const name of new Set(r.scenarios.flatMap((s) => s.unmatched ?? [])))
|
|
86
|
+
if (!swapped.has(name))
|
|
87
|
+
out.add(unmatchedError(name));
|
|
88
|
+
}
|
|
89
|
+
return [...out].map((e) => `${side}: ${e}`);
|
|
90
|
+
}
|
|
91
|
+
function groupRow(scenario, measured, h, threshold) {
|
|
54
92
|
const side = (g) => ({ result: resultLabel(g), cycles: g.cycles });
|
|
93
|
+
// Measured without its binary (not in the base checkout): the mock's code, not the base's build.
|
|
94
|
+
const b = h && measured?.missingBinary ? undefined : measured;
|
|
55
95
|
const row = {
|
|
56
96
|
scenario,
|
|
57
97
|
group: (h ?? b).selector,
|
|
@@ -61,23 +101,40 @@ function groupRow(scenario, b, h, threshold) {
|
|
|
61
101
|
kind: !b ? "new" : !h ? "removed" : "same",
|
|
62
102
|
overThreshold: false,
|
|
63
103
|
};
|
|
104
|
+
if (measured?.missingBinary)
|
|
105
|
+
row.notCompared = `binary "${measured.missingBinary}" is not in the base checkout`;
|
|
64
106
|
if (b && h) {
|
|
65
107
|
row.delta = h.cycles - b.cycles;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
108
|
+
const changed = resultLabel(b) !== resultLabel(h);
|
|
109
|
+
row.kind = changed ? "result" : row.delta !== 0 ? "changed" : "same";
|
|
110
|
+
// A run the VM stopped has no cost to compare: no percentage, no growth.
|
|
111
|
+
if (!b.error && !h.error) {
|
|
112
|
+
if (b.cycles > 0)
|
|
113
|
+
row.percent = (row.delta / b.cycles) * 100;
|
|
114
|
+
row.overThreshold =
|
|
115
|
+
threshold !== undefined && row.delta > 0 && (row.percent === undefined || row.percent > threshold);
|
|
116
|
+
}
|
|
117
|
+
// The VM stopped the head (past the cycle limit, a bad instruction): its 0
|
|
118
|
+
// cycles are not a saving. Under --fail-above that is over any threshold.
|
|
119
|
+
if (h.error && changed && threshold !== undefined)
|
|
120
|
+
row.overThreshold = true;
|
|
71
121
|
}
|
|
72
122
|
else if (h && resultLabel(h) !== "ok") {
|
|
73
123
|
// A new group that does not accept: against "did not run", its result changed.
|
|
74
124
|
row.kind = "result";
|
|
75
125
|
row.base = { result: NOT_RUN };
|
|
126
|
+
if (h.error && threshold !== undefined)
|
|
127
|
+
row.overThreshold = true;
|
|
76
128
|
}
|
|
77
129
|
if (h?.functions)
|
|
78
130
|
row.functions = functionDeltas(b?.functions, h.functions);
|
|
79
131
|
if (h?.functionsWarning)
|
|
80
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
|
+
}
|
|
81
138
|
return row;
|
|
82
139
|
}
|
|
83
140
|
/**
|
|
@@ -105,14 +162,20 @@ export function functionDeltas(base, head) {
|
|
|
105
162
|
}
|
|
106
163
|
/**
|
|
107
164
|
* Whether the diff should fail a check, per the options it was made with. A
|
|
108
|
-
* scenario the head could not run always fails
|
|
109
|
-
*
|
|
165
|
+
* scenario the head could not run always fails. With a gate set (either
|
|
166
|
+
* option), so does anything that was not checked: a scenario the base could not
|
|
167
|
+
* run, an error in either report, and a base group with nothing to compare
|
|
168
|
+
* against (removed, renamed, moved, or measured without its binary) unless
|
|
169
|
+
* `allowRemoved`. A head group the VM stopped is a result change, and over any
|
|
170
|
+
* `failAbove` threshold.
|
|
110
171
|
*/
|
|
111
172
|
export function diffFailed(diff, options = {}) {
|
|
112
173
|
const gated = options.failAbove !== undefined || options.failOnResultChange === true;
|
|
113
174
|
return ((options.failAbove !== undefined && diff.overThreshold > 0) ||
|
|
114
175
|
(options.failOnResultChange === true && diff.resultChanges > 0) ||
|
|
115
176
|
(gated && diff.baseErrors > 0) ||
|
|
177
|
+
(gated && (diff.reportErrors?.length ?? 0) > 0) ||
|
|
178
|
+
(gated && !options.allowRemoved && (diff.removed ?? 0) > 0) ||
|
|
116
179
|
diff.rows.some((r) => r.kind === "error"));
|
|
117
180
|
}
|
|
118
181
|
const fmt = (n) => n.toLocaleString("en-US");
|
|
@@ -120,15 +183,16 @@ const signed = (n) => (n > 0 ? `+${fmt(n)}` : n < 0 ? `-${fmt(-n)}` : "0");
|
|
|
120
183
|
const pct = (p) => p === undefined ? "" : p === 0 ? "0%" : `${p > 0 ? "+" : "-"}${Math.abs(p) < 0.01 ? "<0.01" : Math.abs(p).toFixed(2)}%`;
|
|
121
184
|
const cell = (s) => !s || s.result === NOT_RUN ? "—" : s.result === "ok" ? fmt(s.cycles ?? 0) : `${fmt(s.cycles ?? 0)} (${s.result})`;
|
|
122
185
|
function note(r, threshold) {
|
|
186
|
+
const unpaired = r.notCompared ? ` (NOT COMPARED: ${r.notCompared})` : "";
|
|
123
187
|
switch (r.kind) {
|
|
124
188
|
case "result":
|
|
125
189
|
return r.base.result === NOT_RUN
|
|
126
|
-
? `NEW, DOES NOT ACCEPT: ${r.head.result}`
|
|
190
|
+
? `NEW, DOES NOT ACCEPT: ${r.head.result}${unpaired}`
|
|
127
191
|
: `RESULT CHANGED: ${r.base.result} → ${r.head.result}`;
|
|
128
192
|
case "new":
|
|
129
|
-
return "new";
|
|
193
|
+
return r.notCompared ? `NOT COMPARED: ${r.notCompared}` : "new";
|
|
130
194
|
case "removed":
|
|
131
|
-
return "
|
|
195
|
+
return "REMOVED: no head group to compare with";
|
|
132
196
|
case "error":
|
|
133
197
|
return r.note ?? "error";
|
|
134
198
|
case "base-error":
|
|
@@ -137,8 +201,12 @@ function note(r, threshold) {
|
|
|
137
201
|
return r.overThreshold ? `over ${threshold}%` : "";
|
|
138
202
|
}
|
|
139
203
|
}
|
|
140
|
-
/**
|
|
141
|
-
|
|
204
|
+
/**
|
|
205
|
+
* One line saying what matters, for the top of a comment or terminal output.
|
|
206
|
+
* Text from the reports in it (debugger versions) goes through `clean`:
|
|
207
|
+
* termLine by default, the markdown escape in a comment.
|
|
208
|
+
*/
|
|
209
|
+
export function summarize(diff, clean = termLine) {
|
|
142
210
|
const groups = diff.rows.filter((r) => r.kind !== "error" && r.kind !== "base-error");
|
|
143
211
|
const changed = groups.filter((r) => r.kind !== "same").length;
|
|
144
212
|
const errors = diff.rows.filter((r) => r.kind === "error").length;
|
|
@@ -153,6 +221,15 @@ export function summarize(diff) {
|
|
|
153
221
|
`${changed} of ${groups.length} script group${groups.length === 1 ? "" : "s"} changed`,
|
|
154
222
|
...(unchecked
|
|
155
223
|
? [`${unchecked} new group${s(unchecked)} not checked against the ${diff.threshold}% threshold (no base to compare with)`]
|
|
224
|
+
: diff.newGroups
|
|
225
|
+
? [`${diff.newGroups} new group${s(diff.newGroups)}`]
|
|
226
|
+
: []),
|
|
227
|
+
...(diff.removed
|
|
228
|
+
? [`${diff.removed} base group${s(diff.removed)} with nothing in the head to compare with (removed, renamed, moved or not built on the base)`]
|
|
229
|
+
: []),
|
|
230
|
+
...(diff.reportErrors?.length ? [`${diff.reportErrors.length} report error${s(diff.reportErrors.length)}`] : []),
|
|
231
|
+
...(diff.debuggers
|
|
232
|
+
? [`the reports come from different debuggers (base ${clean(diff.debuggers.base)}, head ${clean(diff.debuggers.head)}): cycles can differ for that alone`]
|
|
156
233
|
: []),
|
|
157
234
|
];
|
|
158
235
|
return parts.join(", ");
|
|
@@ -181,6 +258,15 @@ const oneLine = (s) => {
|
|
|
181
258
|
const t = String(s).replace(/[\r\n]+/g, " ").replace(INVISIBLE, "");
|
|
182
259
|
return t.length > TEXT_MAX ? `${t.slice(0, TEXT_MAX - 1)}\u2026` : t;
|
|
183
260
|
};
|
|
261
|
+
/**
|
|
262
|
+
* Text from a config, a build or a node (scenario, script and function names,
|
|
263
|
+
* warnings) for a terminal or a CI log: oneLine, and never `::` at the start of
|
|
264
|
+
* a line, which GitHub Actions would run as a workflow command (`::error::`,
|
|
265
|
+
* `::add-mask::`, `::stop-commands::`); a zero-width space breaks it.
|
|
266
|
+
*/
|
|
267
|
+
export const termLine = (s) => oneLine(s).replace(/^(\s*):(?=:)/, "$1:\u200b");
|
|
268
|
+
/** termLine for text that keeps its line breaks (a debugger's output in an error): each line cleaned the same way. */
|
|
269
|
+
export const termLines = (s) => String(s).split(/\r?\n|\r/).map(termLine).join("\n");
|
|
184
270
|
/**
|
|
185
271
|
* Text from the PR's own files (scenario and script names, the title, error
|
|
186
272
|
* messages) as inert markdown: one clipped line, no table breaks, no
|
|
@@ -218,16 +304,29 @@ function functionBlocks(rows) {
|
|
|
218
304
|
out.push(`<details><summary>Functions in ${where}: not recorded</summary>`, "", md(r.functionsWarning), "", "</details>", "");
|
|
219
305
|
continue;
|
|
220
306
|
}
|
|
221
|
-
|
|
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
|
+
}
|
|
222
313
|
continue;
|
|
314
|
+
}
|
|
223
315
|
const shown = r.functions.slice(0, FUNCTIONS_SHOWN);
|
|
224
316
|
const more = r.functions.length - shown.length;
|
|
225
317
|
const n = r.functions.length;
|
|
226
|
-
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>", "");
|
|
227
319
|
}
|
|
228
320
|
return out;
|
|
229
321
|
}
|
|
230
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
|
+
}
|
|
231
330
|
/** The hidden marker that identifies the sticky comment, so it is updated instead of duplicated. */
|
|
232
331
|
export const COMMENT_MARKER = "<!-- ckb-cycles-report -->";
|
|
233
332
|
/**
|
|
@@ -244,15 +343,21 @@ export function formatMarkdown(diff, options = {}) {
|
|
|
244
343
|
const head = "| Scenario | Group | Script | Base | Head | Δ | Δ% | |\n|---|---|---|---:|---:|---:|---:|---|";
|
|
245
344
|
const line = (r) => {
|
|
246
345
|
const flag = note(r, diff.threshold);
|
|
247
|
-
const strong = r.kind === "result" || r.kind === "error" || r.kind === "base-error" || r.overThreshold;
|
|
248
|
-
return (`| ${md(r.scenario)} | ${r.group ? code(r.group) : ""} | ${md(r.script)} | ${cell(r.base)} | ${cell(r.head)}` +
|
|
346
|
+
const strong = r.kind === "result" || r.kind === "error" || r.kind === "base-error" || r.kind === "removed" || r.overThreshold || !!r.notCompared;
|
|
347
|
+
return (`| ${md(r.scenario)} | ${r.group ? code(r.group) : ""} | ${md(r.script)} | ${md(cell(r.base))} | ${md(cell(r.head))}` +
|
|
249
348
|
` | ${r.delta === undefined ? "" : signed(r.delta)} | ${pct(r.percent)} | ${flag ? (strong ? `**${md(flag)}**` : md(flag)) : ""} |`);
|
|
250
349
|
};
|
|
251
350
|
const vs = options.baseLabel && options.headLabel ? ` (${code(options.baseLabel)} → ${code(options.headLabel)})` : "";
|
|
252
|
-
const top = [COMMENT_MARKER, `### ${md(options.title ?? "Cycle report")}`, "", `${summarize(diff)}${vs}.`, ""];
|
|
351
|
+
const top = [COMMENT_MARKER, `### ${md(options.title ?? "Cycle report")}`, "", `${summarize(diff, md)}${vs}.`, ""];
|
|
352
|
+
// A report's own errors fail a gated check: they go right under the summary.
|
|
353
|
+
const errors = diff.reportErrors?.length
|
|
354
|
+
? [`**Report errors** (nothing measured what they name):`, "", ...diff.reportErrors.slice(0, 20).map((e) => `- ${md(e)}`), ...(diff.reportErrors.length > 20 ? [`- and ${diff.reportErrors.length - 20} more`] : []), ""]
|
|
355
|
+
: [];
|
|
356
|
+
top.push(...errors);
|
|
253
357
|
const footer = ["<sub>Generated by ckb-cycles (ckb-cycle-tools): each scenario replayed in ckb-debugger, one run per script group.</sub>"];
|
|
254
358
|
let table = shown.length ? [head, ...shown.map(line), ""] : ["No script group changed its cycles or its result.", ""];
|
|
255
359
|
let blocks = functionBlocks(shown);
|
|
360
|
+
let warnings = diff.warnings?.length ? warningBlock(diff.warnings) : [];
|
|
256
361
|
let unchanged = same.length
|
|
257
362
|
? [
|
|
258
363
|
`<details><summary>${same.length} unchanged group${same.length === 1 ? "" : "s"}</summary>`,
|
|
@@ -268,8 +373,11 @@ export function formatMarkdown(diff, options = {}) {
|
|
|
268
373
|
// Over GitHub's limit the comment is refused, and the previous push's comment
|
|
269
374
|
// would stay up with old numbers. Least useful first: the function details,
|
|
270
375
|
// the unchanged groups, then the last (least important) changed rows.
|
|
271
|
-
const size = () => [...top, ...table, ...blocks, ...unchanged, ...footer].join("\n").length;
|
|
376
|
+
const size = () => [...top, ...table, ...blocks, ...warnings, ...unchanged, ...footer].join("\n").length;
|
|
272
377
|
const s = (n) => (n === 1 ? "" : "s");
|
|
378
|
+
if (size() >= COMMENT_MAX && warnings.length) {
|
|
379
|
+
warnings = [`<sub>${diff.warnings.length} scenario warning${s(diff.warnings.length)} left out: the comment would pass GitHub's size limit.</sub>`, ""];
|
|
380
|
+
}
|
|
273
381
|
if (size() >= COMMENT_MAX && blocks.length) {
|
|
274
382
|
blocks = ["<sub>Function details left out: the comment would pass GitHub's size limit.</sub>", ""];
|
|
275
383
|
}
|
|
@@ -285,7 +393,23 @@ export function formatMarkdown(diff, options = {}) {
|
|
|
285
393
|
const cut = rows.length - kept;
|
|
286
394
|
table = [...table.slice(0, 2), ...rows.slice(0, kept), "", `<sub>${cut} more changed row${s(cut)} left out: the comment would pass GitHub's size limit.</sub>`, ""];
|
|
287
395
|
}
|
|
288
|
-
return [...top, ...table, ...blocks, ...unchanged, ...footer].join("\n");
|
|
396
|
+
return [...top, ...table, ...blocks, ...warnings, ...unchanged, ...footer].join("\n");
|
|
397
|
+
}
|
|
398
|
+
/** How many scenario warnings a comment lists. */
|
|
399
|
+
const WARNINGS_SHOWN = 50;
|
|
400
|
+
/** The head's scenario warnings, folded: why a number may not be the build's (a binary no cell dep matched). */
|
|
401
|
+
function warningBlock(warnings) {
|
|
402
|
+
const n = warnings.length;
|
|
403
|
+
const shown = warnings.slice(0, WARNINGS_SHOWN);
|
|
404
|
+
return [
|
|
405
|
+
`<details><summary>${n} scenario warning${n === 1 ? "" : "s"}</summary>`,
|
|
406
|
+
"",
|
|
407
|
+
...shown.map((w) => `- ${md(w.scenario)}: ${md(w.text)}`),
|
|
408
|
+
...(n > shown.length ? [`- and ${n - shown.length} more`] : []),
|
|
409
|
+
"",
|
|
410
|
+
"</details>",
|
|
411
|
+
"",
|
|
412
|
+
];
|
|
289
413
|
}
|
|
290
414
|
/** The same table for a terminal. */
|
|
291
415
|
export function formatText(diff) {
|
|
@@ -299,7 +423,7 @@ export function formatText(diff) {
|
|
|
299
423
|
r.delta === undefined ? "" : signed(r.delta),
|
|
300
424
|
pct(r.percent),
|
|
301
425
|
note(r, diff.threshold),
|
|
302
|
-
]);
|
|
426
|
+
].map(termLine));
|
|
303
427
|
const widths = header.map((h, i) => Math.max(h.length, ...rows.map((r) => r[i].length)));
|
|
304
428
|
const right = new Set([3, 4, 5, 6]);
|
|
305
429
|
const line = (cells) => cells
|
|
@@ -309,17 +433,31 @@ export function formatText(diff) {
|
|
|
309
433
|
// Under the table, one line per group that recorded functions: the largest changes.
|
|
310
434
|
const functions = [];
|
|
311
435
|
for (const r of diff.rows.filter((r) => r.kind !== "same")) {
|
|
312
|
-
const where = `${r.scenario} ${r.group}`;
|
|
436
|
+
const where = `${termLine(r.scenario)} ${termLine(r.group)}`;
|
|
313
437
|
if (r.functionsWarning)
|
|
314
|
-
functions.push(`functions in ${where} not recorded: ${r.functionsWarning}`);
|
|
438
|
+
functions.push(`functions in ${where} not recorded: ${termLine(r.functionsWarning)}`);
|
|
315
439
|
else if (r.functions?.length) {
|
|
316
440
|
const top = r.functions
|
|
317
441
|
.slice(0, 5)
|
|
318
|
-
.map((f) => (f.delta !== undefined ?
|
|
442
|
+
.map((f) => `${termLine(f.name)} ${f.delta !== undefined ? signed(f.delta) : `${fnText(f.base)} → ${fnText(f.head)}`}`);
|
|
319
443
|
const more = r.functions.length - top.length;
|
|
320
444
|
functions.push(`functions in ${where}: ${top.join(", ")}${more > 0 ? `, and ${more} more` : ""}`);
|
|
445
|
+
if (r.spawned)
|
|
446
|
+
functions.push(` ${spawnNote(r)}`);
|
|
321
447
|
}
|
|
448
|
+
else if (r.functions && r.spawned)
|
|
449
|
+
functions.push(`functions in ${where}: none of its own changed. ${spawnNote(r)}`);
|
|
322
450
|
}
|
|
323
|
-
|
|
451
|
+
const errors = (diff.reportErrors ?? []).map((e) => `error: ${termLine(e)}`);
|
|
452
|
+
const warnings = (diff.warnings ?? []).map((w) => `${termLine(w.scenario)}: warning: ${termLine(w.text)}`);
|
|
453
|
+
return [
|
|
454
|
+
summarize(diff),
|
|
455
|
+
...(errors.length ? ["", ...errors] : []),
|
|
456
|
+
"",
|
|
457
|
+
line(header),
|
|
458
|
+
...rows.map(line),
|
|
459
|
+
...(functions.length ? ["", ...functions] : []),
|
|
460
|
+
...(warnings.length ? ["", ...warnings] : []),
|
|
461
|
+
].join("\n");
|
|
324
462
|
}
|
|
325
463
|
const fnText = (n) => (n === undefined ? "?" : fmt(n));
|
package/dist/elf.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function names from an ELF symbol table, for the frames ckb-debugger's
|
|
3
|
+
* profiler cannot name. The profiler names a frame from the DWARF line table;
|
|
4
|
+
* code without line info (the `_start` that ckb-std writes in assembly, the
|
|
5
|
+
* allocator shims rustc generates) comes out as `func_0x<address>`. The symbol
|
|
6
|
+
* table of the same unstripped build still has those names. Without them, a
|
|
7
|
+
* function that moved between two builds shows in a diff as one address gone
|
|
8
|
+
* and another new. Plain TypeScript with no Node built-ins, like flame.ts.
|
|
9
|
+
*/
|
|
10
|
+
/** Start address → name (demangled when it can be), of every function symbol in `.symtab`. */
|
|
11
|
+
export declare function elfFunctionNames(elf: Uint8Array): Map<bigint, string>;
|
|
12
|
+
/**
|
|
13
|
+
* A Rust symbol name as `crate::module::function`, for the plain paths a
|
|
14
|
+
* symbol table mostly holds: legacy (`_ZN…E`, hash dropped) and v0 (`_R…`).
|
|
15
|
+
* Anything it does not follow (generics, trait impls) comes back unchanged:
|
|
16
|
+
* still a stable name, only a less readable one.
|
|
17
|
+
*/
|
|
18
|
+
export declare function demangle(name: string): string;
|
package/dist/elf.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function names from an ELF symbol table, for the frames ckb-debugger's
|
|
3
|
+
* profiler cannot name. The profiler names a frame from the DWARF line table;
|
|
4
|
+
* code without line info (the `_start` that ckb-std writes in assembly, the
|
|
5
|
+
* allocator shims rustc generates) comes out as `func_0x<address>`. The symbol
|
|
6
|
+
* table of the same unstripped build still has those names. Without them, a
|
|
7
|
+
* function that moved between two builds shows in a diff as one address gone
|
|
8
|
+
* and another new. Plain TypeScript with no Node built-ins, like flame.ts.
|
|
9
|
+
*/
|
|
10
|
+
/** `__text_start`, `__init_array_end`, `_etext`, `_stext`, `end` and the like: labels, not functions. */
|
|
11
|
+
const LINKER_MARKER = /^(__\w*_(start|end)|_?_?(etext|edata|end|stext|ftext)|__executable_start|__ehdr_start|__global_pointer\$)$/;
|
|
12
|
+
/** Start address → name (demangled when it can be), of every function symbol in `.symtab`. */
|
|
13
|
+
export function elfFunctionNames(elf) {
|
|
14
|
+
const names = new Map();
|
|
15
|
+
// ELF64 little-endian only: what a CKB script is (RISC-V 64).
|
|
16
|
+
if (elf.length < 64 || elf[0] !== 0x7f || elf[1] !== 0x45 || elf[2] !== 0x4c || elf[3] !== 0x46)
|
|
17
|
+
return names;
|
|
18
|
+
if (elf[4] !== 2 || elf[5] !== 1)
|
|
19
|
+
return names;
|
|
20
|
+
const view = new DataView(elf.buffer, elf.byteOffset, elf.byteLength);
|
|
21
|
+
const u16 = (o) => view.getUint16(o, true);
|
|
22
|
+
const u32 = (o) => view.getUint32(o, true);
|
|
23
|
+
const u64 = (o) => view.getBigUint64(o, true);
|
|
24
|
+
const inside = (o, n) => o >= 0 && n >= 0 && o + n <= elf.length;
|
|
25
|
+
const shoff = Number(u64(0x28));
|
|
26
|
+
const shentsize = u16(0x3a);
|
|
27
|
+
const shnum = u16(0x3c);
|
|
28
|
+
if (shentsize < 64 || !inside(shoff, shentsize * shnum))
|
|
29
|
+
return names;
|
|
30
|
+
const section = (i) => {
|
|
31
|
+
const o = shoff + i * shentsize;
|
|
32
|
+
return { type: u32(o + 4), flags: u64(o + 8), offset: Number(u64(o + 0x18)), size: Number(u64(o + 0x20)), link: u32(o + 0x28), entsize: Number(u64(o + 0x38)) };
|
|
33
|
+
};
|
|
34
|
+
const SHT_SYMTAB = 2;
|
|
35
|
+
const SHF_EXECINSTR = 0x4n;
|
|
36
|
+
// SHN_LORESERVE and up: SHN_ABS, SHN_COMMON, SHN_XINDEX. Not an address in code.
|
|
37
|
+
const SHN_LORESERVE = 0xff00;
|
|
38
|
+
const inCode = (shndx) => shndx < shnum && (section(shndx).flags & SHF_EXECINSTR) !== 0n;
|
|
39
|
+
for (let i = 0; i < shnum; i++) {
|
|
40
|
+
const symtab = section(i);
|
|
41
|
+
if (symtab.type !== SHT_SYMTAB || symtab.entsize < 24 || !inside(symtab.offset, symtab.size))
|
|
42
|
+
continue;
|
|
43
|
+
if (symtab.link >= shnum)
|
|
44
|
+
continue;
|
|
45
|
+
const strtab = section(symtab.link);
|
|
46
|
+
if (!inside(strtab.offset, strtab.size))
|
|
47
|
+
continue;
|
|
48
|
+
const decoder = new TextDecoder();
|
|
49
|
+
for (let o = symtab.offset; o + symtab.entsize <= symtab.offset + symtab.size; o += symtab.entsize) {
|
|
50
|
+
const nameOff = u32(o);
|
|
51
|
+
const info = elf[o + 4];
|
|
52
|
+
const shndx = u16(o + 6);
|
|
53
|
+
const value = u64(o + 8);
|
|
54
|
+
const type = info & 0xf;
|
|
55
|
+
// STT_FUNC, or STT_NOTYPE for a global label in assembly (`_start`) in an
|
|
56
|
+
// executable section, defined in a real section (not SHN_UNDEF or SHN_ABS).
|
|
57
|
+
if (!(type === 2 || (type === 0 && info >> 4 !== 0)) || shndx === 0 || shndx >= SHN_LORESERVE || nameOff === 0 || nameOff >= strtab.size)
|
|
58
|
+
continue;
|
|
59
|
+
if (type === 0 && !inCode(shndx))
|
|
60
|
+
continue;
|
|
61
|
+
let end = strtab.offset + nameOff;
|
|
62
|
+
while (end < strtab.offset + strtab.size && elf[end] !== 0)
|
|
63
|
+
end++;
|
|
64
|
+
const raw = decoder.decode(elf.subarray(strtab.offset + nameOff, end));
|
|
65
|
+
if (!raw || raw.startsWith("$") || raw.startsWith(".L"))
|
|
66
|
+
continue;
|
|
67
|
+
// Section bounds the linker or a linker script defines; one can sit at `_start`'s address.
|
|
68
|
+
if (type === 0 && LINKER_MARKER.test(raw))
|
|
69
|
+
continue;
|
|
70
|
+
// A function and a label at the same address: the function's name wins.
|
|
71
|
+
if (type === 2 || !names.has(value))
|
|
72
|
+
names.set(value, demangle(raw));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return names;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* A Rust symbol name as `crate::module::function`, for the plain paths a
|
|
79
|
+
* symbol table mostly holds: legacy (`_ZN…E`, hash dropped) and v0 (`_R…`).
|
|
80
|
+
* Anything it does not follow (generics, trait impls) comes back unchanged:
|
|
81
|
+
* still a stable name, only a less readable one.
|
|
82
|
+
*/
|
|
83
|
+
export function demangle(name) {
|
|
84
|
+
if (name.startsWith("_ZN"))
|
|
85
|
+
return demangleLegacy(name) ?? name;
|
|
86
|
+
if (name.startsWith("_R"))
|
|
87
|
+
return demangleV0(name) ?? name;
|
|
88
|
+
return name;
|
|
89
|
+
}
|
|
90
|
+
function demangleLegacy(name) {
|
|
91
|
+
const parts = [];
|
|
92
|
+
let i = 3;
|
|
93
|
+
while (i < name.length && name[i] !== "E") {
|
|
94
|
+
const m = /^\d+/.exec(name.slice(i));
|
|
95
|
+
if (!m)
|
|
96
|
+
return undefined;
|
|
97
|
+
const len = Number(m[0]);
|
|
98
|
+
i += m[0].length;
|
|
99
|
+
if (i + len > name.length)
|
|
100
|
+
return undefined;
|
|
101
|
+
parts.push(name.slice(i, i + len));
|
|
102
|
+
i += len;
|
|
103
|
+
}
|
|
104
|
+
if (name[i] !== "E" || !parts.length)
|
|
105
|
+
return undefined;
|
|
106
|
+
if (parts.length > 1 && /^h[0-9a-f]{16}$/.test(parts[parts.length - 1]))
|
|
107
|
+
parts.pop();
|
|
108
|
+
const out = [];
|
|
109
|
+
for (const p of parts) {
|
|
110
|
+
const s = unescapeLegacy(p);
|
|
111
|
+
if (s === undefined)
|
|
112
|
+
return undefined;
|
|
113
|
+
out.push(s);
|
|
114
|
+
}
|
|
115
|
+
return out.join("::");
|
|
116
|
+
}
|
|
117
|
+
const LEGACY_ESCAPES = { SP: "@", BP: "*", RF: "&", LT: "<", GT: ">", LP: "(", RP: ")", C: "," };
|
|
118
|
+
/** One legacy path element: `$RF$`, `$u7b$` and the rest decoded, `..` as `::`. Undefined on an escape it does not know. */
|
|
119
|
+
function unescapeLegacy(part) {
|
|
120
|
+
// An element that would start with `$` is written `_$`.
|
|
121
|
+
let s = part.startsWith("_$") ? part.slice(1) : part;
|
|
122
|
+
let out = "";
|
|
123
|
+
while (s) {
|
|
124
|
+
if (s.startsWith("..")) {
|
|
125
|
+
out += "::";
|
|
126
|
+
s = s.slice(2);
|
|
127
|
+
}
|
|
128
|
+
else if (s[0] === "$") {
|
|
129
|
+
const end = s.indexOf("$", 1);
|
|
130
|
+
if (end < 0)
|
|
131
|
+
return undefined;
|
|
132
|
+
const esc = s.slice(1, end);
|
|
133
|
+
const code = /^u[0-9a-f]{1,6}$/.test(esc) ? parseInt(esc.slice(1), 16) : -1;
|
|
134
|
+
const ch = LEGACY_ESCAPES[esc] ?? (code >= 0 && code <= 0x10ffff ? String.fromCodePoint(code) : undefined);
|
|
135
|
+
if (ch === undefined)
|
|
136
|
+
return undefined;
|
|
137
|
+
out += ch;
|
|
138
|
+
s = s.slice(end + 1);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
const m = /^[^$.]+|^\./.exec(s);
|
|
142
|
+
out += m[0];
|
|
143
|
+
s = s.slice(m[0].length);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
function demangleV0(name) {
|
|
149
|
+
let i = 2;
|
|
150
|
+
const peek = () => name[i];
|
|
151
|
+
// `s<base62>_`: a disambiguator, not part of the name.
|
|
152
|
+
const skipDisambiguator = () => {
|
|
153
|
+
if (peek() !== "s")
|
|
154
|
+
return true;
|
|
155
|
+
const end = name.indexOf("_", i);
|
|
156
|
+
if (end < 0)
|
|
157
|
+
return false;
|
|
158
|
+
i = end + 1;
|
|
159
|
+
return true;
|
|
160
|
+
};
|
|
161
|
+
const ident = () => {
|
|
162
|
+
if (!skipDisambiguator())
|
|
163
|
+
return undefined;
|
|
164
|
+
if (peek() === "u")
|
|
165
|
+
return undefined; // punycode: leave it to a real demangler
|
|
166
|
+
const m = /^\d+/.exec(name.slice(i));
|
|
167
|
+
if (!m)
|
|
168
|
+
return undefined;
|
|
169
|
+
const len = Number(m[0]);
|
|
170
|
+
i += m[0].length;
|
|
171
|
+
if (peek() === "_")
|
|
172
|
+
i++;
|
|
173
|
+
if (i + len > name.length)
|
|
174
|
+
return undefined;
|
|
175
|
+
const s = name.slice(i, i + len);
|
|
176
|
+
i += len;
|
|
177
|
+
return s;
|
|
178
|
+
};
|
|
179
|
+
const path = (depth) => {
|
|
180
|
+
if (depth > 64)
|
|
181
|
+
return undefined;
|
|
182
|
+
const tag = name[i++];
|
|
183
|
+
if (tag === "C")
|
|
184
|
+
return ident();
|
|
185
|
+
if (tag === "N") {
|
|
186
|
+
const ns = name[i++];
|
|
187
|
+
const parent = path(depth + 1);
|
|
188
|
+
if (parent === undefined)
|
|
189
|
+
return undefined;
|
|
190
|
+
const id = ident();
|
|
191
|
+
if (id === undefined)
|
|
192
|
+
return undefined;
|
|
193
|
+
if (ns === "C")
|
|
194
|
+
return `${parent}::{closure}`;
|
|
195
|
+
if (ns >= "A" && ns <= "Z")
|
|
196
|
+
return `${parent}::{${id || "shim"}}`;
|
|
197
|
+
return `${parent}::${id}`;
|
|
198
|
+
}
|
|
199
|
+
return undefined;
|
|
200
|
+
};
|
|
201
|
+
if (/^\d/.test(name.slice(i)))
|
|
202
|
+
return undefined; // an encoding version this does not know
|
|
203
|
+
const p = path(0);
|
|
204
|
+
if (p === undefined)
|
|
205
|
+
return undefined;
|
|
206
|
+
// What may follow the path: an instantiating crate, or a suffix LLVM added.
|
|
207
|
+
const rest = name.slice(i);
|
|
208
|
+
return rest === "" || rest.startsWith("C") || rest.startsWith(".") ? p : undefined;
|
|
209
|
+
}
|
package/dist/flame.d.ts
CHANGED
|
@@ -25,6 +25,12 @@ export declare function splitStack(stack: string): string[];
|
|
|
25
25
|
export declare const MAX_DEPTH = 256;
|
|
26
26
|
/** Parses ckb-debugger's folded output: identical stacks merged, frames named by function only. */
|
|
27
27
|
export declare function parseFolded(text: string): Folded;
|
|
28
|
+
/**
|
|
29
|
+
* Renames each `func_0x<address>` frame (code the profiler found no line info
|
|
30
|
+
* for) to the function starting at that address in `names` (elf.ts), merging
|
|
31
|
+
* stacks that become the same. Frames it has no name for keep their address.
|
|
32
|
+
*/
|
|
33
|
+
export declare function nameAddresses(f: Folded, names: Map<bigint, string>): Folded;
|
|
28
34
|
export declare const foldedTotal: (f: Folded) => bigint;
|
|
29
35
|
/** Back to text, one merged stack per line, heaviest first: the format speedscope and inferno read. */
|
|
30
36
|
export declare function formatFolded(f: Folded): string;
|
|
@@ -55,5 +61,19 @@ export declare function renderFlameSvg(tree: FlameNode, opts?: {
|
|
|
55
61
|
width?: number;
|
|
56
62
|
minWidth?: number;
|
|
57
63
|
}): string;
|
|
58
|
-
/**
|
|
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
|
+
*/
|
|
59
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;
|