Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tools/live_heap_check.mjs

5.4 KiB, 1 run

created by r1870400018:66296, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1#!/usr/bin/env node
2// live_heap_check.mjs -- does the live view's open stay inside the PDF door's heap, as a bounded multiple.
3//
4// `compileProjectDelta` opened with `known: []` is the live view's first build, and wasm linear memory only
5// grows, so `heapMB()` after it is the high-water mark Daimond's `holdCheck` compares for the life of the
6// page. The open draws every page, yet the engine's own working set is the PDF door's, so the open must not
7// cost much more than a PDF of the same document. Before the delta drew each page as it was taken and held
8// every SVG in wasm, the open cost 9.4 times a PDF at 50 pages and 17 times at 300; the check holds it to
9// RATIO, and the heap after the edits to the same line. The open now holds the frame of each page it will
10// draw (about 0.1 MiB a page on the rich corpus) beyond what a PDF holds, and measures 1.3 to 1.5 times the
11// PDF door on the rich and synthetic corpora and 1.8 times on the plain-text one at 797 pages; the line of
12// 2.5 sits 1.4 times above the worst of those and 3.8 times below the fault it guards against.
13//
14// One fresh node process per measurement (`mem_open_probe.mjs`: `pdf`, then `edits` with the open and N
15// one-letter edits), on the rich synthetic corpus `heap_probe.mjs --dump` writes. Exit 0 when every line
16// holds, 1 when one fails, 2 when a measurement could not be made.
17//
18// node tools/live_heap_check.mjs --pkg PKG [--pages 50,300] [--ratio 2.5] [--edits 5] [--phone 768]
19//
20// Run it under a memory cap, as every heavy job on this host is, and a 300-page cell through the slot helper:
21// systemd-run --user --scope --quiet -p MemoryMax=3G --slice=claude-rc.slice node tools/live_heap_check.mjs
22//
23// Options:
24// --pkg DIR a wasm-pack `--target web` output directory for fe2o3_austenite (required)
25// --pages a,b page counts to generate (default 50)
26// --ratio R the most the open's heap may be, as a multiple of the PDF door's heap (default 2.5)
27// --phone MIB also hold the open to this absolute heap, Daimond's phone budget (default 768; 0 skips)
28// --edits N one-letter edits after the open (default 5)
29// --work DIR where the generated sources go (default ~/.cache/austenite/live_heap_check)
30// --json FILE write every measurement as JSON
31
32import { spawnSync } from 'node:child_process';
33import { mkdirSync, writeFileSync } from 'node:fs';
34import { dirname, join, resolve } from 'node:path';
35import { fileURLToPath } from 'node:url';
36
37const HERE = dirname(fileURLToPath(import.meta.url));
38const HOME = process.env.HOME || '';
39
40function opt(name, dflt) {
41 const i = process.argv.indexOf(name);
42 return i >= 0 && i + 1 < process.argv.length ? process.argv[i + 1] : dflt;
43}
44
45const pkg = opt('--pkg', null);
46if (!pkg) {
47 process.stderr.write('live_heap_check: --pkg is required\n');
48 process.exit(2);
49}
50const pages = opt('--pages', '50').split(',').map(Number);
51const ratio = +opt('--ratio', '2.5');
52const phone = +opt('--phone', '768');
53const edits = +opt('--edits', '5');
54const work = resolve(opt('--work', join(HOME, '.cache/austenite/live_heap_check')));
55const jsonOut = opt('--json', null);
56mkdirSync(work, { recursive: true });
57
58const node = (args, what) => {
59 const r = spawnSync(process.execPath, ['--expose-gc', ...args], { encoding: 'utf8', maxBuffer: 1 << 28 });
60 if (r.status !== 0) {
61 process.stderr.write(`live_heap_check: ${what} failed (exit ${r.status}): ${(r.stderr || '').slice(-400)}\n`);
62 process.exit(2);
63 }
64 return r;
65};
66
67const rows = [];
68let failed = false;
69for (const n of pages) {
70 const doc = join(work, `rich${n}.typ`);
71 writeFileSync(doc, node([join(HERE, 'heap_probe.mjs'), '--dump', String(n)], `dump ${n}`).stdout);
72 const probe = (exp, extra = []) => JSON.parse(node(
73 [join(HERE, 'mem_open_probe.mjs'), '--pkg', pkg, '--doc', doc, '--exp', exp, ...extra], `${exp} ${n}`,
74 ).stdout.trim().split('\n').pop());
75 const p = probe('pdf');
76 const o = probe('edits', ['--edits', String(edits)]);
77 const open = o.calls.find((c) => c.what === 'open');
78 const last = o.calls[o.calls.length - 1];
79 if (!open || open.error || !p.calls[0] || p.calls[0].error) {
80 process.stderr.write(`live_heap_check: ${n} pages: a compile failed: ${JSON.stringify([open, p.calls[0]])}\n`);
81 process.exit(2);
82 }
83 const pdf = p.heap_end_mb;
84 const row = {
85 pages: n, pagesOut: open.pages, pdf_mib: pdf, open_mib: open.heap_mb, after_edits_mib: o.heap_end_mb,
86 open_ms: open.ms, edit_ms: o.calls.filter((c) => c.what === 'edit').map((c) => c.ms),
87 open_over_pdf: open.heap_mb / pdf, edits_over_pdf: o.heap_end_mb / pdf, last: last.what,
88 };
89 const bad = [];
90 if (row.open_over_pdf > ratio) bad.push(`the open is ${row.open_over_pdf.toFixed(2)}x the PDF door's heap, over ${ratio}`);
91 if (row.edits_over_pdf > ratio) bad.push(`after the edits ${row.edits_over_pdf.toFixed(2)}x, over ${ratio}`);
92 if (phone > 0 && o.heap_end_mb > phone) bad.push(`${o.heap_end_mb.toFixed(1)} MiB over the phone budget ${phone}`);
93 row.verdict = bad.length ? 'FAIL' : 'ok';
94 row.bad = bad;
95 failed = failed || bad.length > 0;
96 rows.push(row);
97 process.stdout.write(
98 `${row.verdict} ${n} pages (${open.pages} out): PDF ${pdf.toFixed(1)} MiB, open ${open.heap_mb.toFixed(1)} ` +
99 `(${row.open_over_pdf.toFixed(2)}x, ${open.ms} ms), after ${edits} edits ${o.heap_end_mb.toFixed(1)} ` +
100 `(${row.edits_over_pdf.toFixed(2)}x)${bad.length ? ': ' + bad.join('; ') : ''}\n`,
101 );
102}
103if (jsonOut) writeFileSync(jsonOut, JSON.stringify({ ratio, phone, edits, rows }, null, 1));
104process.exit(failed ? 1 : 0);