@gmitch215/folio 1.0.0 → 1.0.1
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/README.md +78 -19
- package/dist/bake.d.ts +8 -3
- package/dist/bake.js +5 -2
- package/dist/compiler.d.ts +37 -1
- package/dist/compiler.js +116 -1
- package/dist/context.d.ts +33 -0
- package/dist/context.js +93 -0
- package/dist/coordinator.d.ts +1 -1
- package/dist/coordinator.js +4 -2
- package/dist/extract.d.ts +95 -1
- package/dist/extract.js +883 -242
- package/dist/frames.d.ts +15 -1
- package/dist/frames.js +49 -1
- package/dist/inline.d.ts +68 -0
- package/dist/inline.js +503 -0
- package/dist/iteration-admission.d.ts +76 -0
- package/dist/iteration-admission.js +72 -0
- package/dist/iteration-layout.d.ts +75 -0
- package/dist/iteration-layout.js +118 -0
- package/dist/iteration-owner.d.ts +32 -0
- package/dist/iteration-owner.js +223 -0
- package/dist/iteration-worker.d.ts +1 -0
- package/dist/iteration-worker.js +150 -0
- package/dist/iteration.d.ts +163 -0
- package/dist/iteration.js +435 -0
- package/dist/managed.d.ts +6 -0
- package/dist/managed.js +33 -10
- package/dist/node.d.ts +19 -5
- package/dist/node.js +344 -22
- package/dist/tasks.js +16 -3
- package/package.json +6 -2
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
2
|
+
import { iterationArguments, iterationEntryBytes } from "./iteration-layout.js";
|
|
3
|
+
const { module, memory, arena, globals, iterate, locals, frameSize, port, signal, control } = workerData;
|
|
4
|
+
const L = arena;
|
|
5
|
+
const ENTRY = iterationEntryBytes;
|
|
6
|
+
const exportsOf = new WebAssembly.Instance(module, { env: { memory } }).exports;
|
|
7
|
+
const u8 = new Uint8Array(memory.buffer);
|
|
8
|
+
const u32 = new Uint32Array(memory.buffer);
|
|
9
|
+
const counter = new Int32Array(control);
|
|
10
|
+
const done = new Int32Array(signal);
|
|
11
|
+
const now = () => performance.now();
|
|
12
|
+
const stats = { run: 0, scan: 0 };
|
|
13
|
+
// reads a bitmap into [start, length] runs of granules and zeroes it as it goes
|
|
14
|
+
function scan(base, size) {
|
|
15
|
+
const words = new Uint32Array(memory.buffer, base, size / 4);
|
|
16
|
+
const runs = [];
|
|
17
|
+
let start = -1;
|
|
18
|
+
for (let k = 0; k < words.length; k++) {
|
|
19
|
+
const w = words[k];
|
|
20
|
+
if (w === 0) {
|
|
21
|
+
if (start >= 0) {
|
|
22
|
+
runs.push(start, k * 32 - start);
|
|
23
|
+
start = -1;
|
|
24
|
+
}
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
words[k] = 0;
|
|
28
|
+
if (w === 0xffffffff) {
|
|
29
|
+
if (start < 0)
|
|
30
|
+
start = k * 32;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
for (let b = 0; b < 32; b++) {
|
|
34
|
+
if ((w >>> b) & 1) {
|
|
35
|
+
if (start < 0)
|
|
36
|
+
start = k * 32 + b;
|
|
37
|
+
}
|
|
38
|
+
else if (start >= 0) {
|
|
39
|
+
runs.push(start, k * 32 + b - start);
|
|
40
|
+
start = -1;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (start >= 0)
|
|
45
|
+
runs.push(start, words.length * 32 - start);
|
|
46
|
+
return Int32Array.from(runs);
|
|
47
|
+
}
|
|
48
|
+
// status 0 ran, 1 trapped, 2 undo log exhausted
|
|
49
|
+
function runOne(job, s) {
|
|
50
|
+
const index = job.first + s;
|
|
51
|
+
const top = L.slots + (s + 1) * L.slotBytes;
|
|
52
|
+
const frame = top - frameSize;
|
|
53
|
+
if (frameSize)
|
|
54
|
+
u8.copyWithin(frame, job.frameBase, job.frameBase + frameSize);
|
|
55
|
+
const shadow = L.frameShadow + s * L.frameSlotBytes;
|
|
56
|
+
if (frameSize) {
|
|
57
|
+
u8.fill(0, shadow, shadow + L.frameSlotBytes);
|
|
58
|
+
exportsOf[globals.frameLow].value = frame;
|
|
59
|
+
exportsOf[globals.frameSize].value = frameSize;
|
|
60
|
+
exportsOf[globals.frameShadow].value = shadow;
|
|
61
|
+
}
|
|
62
|
+
const rbase = L.rmaps + s * L.rsize;
|
|
63
|
+
const wbase = L.wmaps + s * L.wsize;
|
|
64
|
+
const lbase = L.logs + s * L.logBytes;
|
|
65
|
+
const limit = lbase + L.logCapacity * ENTRY;
|
|
66
|
+
exportsOf[globals.readBitmap].value = rbase;
|
|
67
|
+
exportsOf[globals.writeBitmap].value = wbase;
|
|
68
|
+
exportsOf[globals.logPointer].value = lbase;
|
|
69
|
+
exportsOf[globals.logLimit].value = limit;
|
|
70
|
+
exportsOf[globals.stampAddress].value = L.stamp;
|
|
71
|
+
const stack = exportsOf[globals.stackPointer];
|
|
72
|
+
if (stack)
|
|
73
|
+
stack.value = frameSize ? frame : top;
|
|
74
|
+
const args = iterationArguments(locals, job.entry, index, frame);
|
|
75
|
+
const t1 = now();
|
|
76
|
+
let status = 0;
|
|
77
|
+
try {
|
|
78
|
+
exportsOf[iterate](...args);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
const left = limit - (exportsOf[globals.logPointer].value >>> 0);
|
|
82
|
+
status =
|
|
83
|
+
error instanceof WebAssembly.RuntimeError &&
|
|
84
|
+
/unreachable/.test(error.message) &&
|
|
85
|
+
left < ENTRY
|
|
86
|
+
? 2
|
|
87
|
+
: 1;
|
|
88
|
+
}
|
|
89
|
+
const t2 = now();
|
|
90
|
+
stats.run += t2 - t1;
|
|
91
|
+
const entries = ((exportsOf[globals.logPointer].value >>> 0) - lbase) / ENTRY;
|
|
92
|
+
const written = new Int32Array(entries);
|
|
93
|
+
for (let e = 0; e < entries; e++)
|
|
94
|
+
written[e] = u32[(lbase + e * ENTRY) / 4] >>> 5;
|
|
95
|
+
written.sort();
|
|
96
|
+
const reads = scan(rbase, L.rsize);
|
|
97
|
+
let frameWritten = new Uint32Array(0);
|
|
98
|
+
let frameExposed = new Uint32Array(0);
|
|
99
|
+
if (frameSize) {
|
|
100
|
+
const granules = Math.ceil(frameSize / 32);
|
|
101
|
+
const words = (at) => u32.slice(at / 4, at / 4 + granules);
|
|
102
|
+
const copy = (at) => {
|
|
103
|
+
const found = words(at);
|
|
104
|
+
return found.some(Boolean) ? found : new Uint32Array(0);
|
|
105
|
+
};
|
|
106
|
+
frameWritten = copy(shadow);
|
|
107
|
+
frameExposed = copy(shadow + 4 * granules + 4);
|
|
108
|
+
}
|
|
109
|
+
if (status)
|
|
110
|
+
u8.fill(0, wbase, wbase + L.wsize);
|
|
111
|
+
else
|
|
112
|
+
for (let e = 0; e < entries; e++)
|
|
113
|
+
u8[wbase + (written[e] >>> 3)] = 0;
|
|
114
|
+
stats.scan += now() - t2;
|
|
115
|
+
return {
|
|
116
|
+
index,
|
|
117
|
+
status,
|
|
118
|
+
entries,
|
|
119
|
+
written,
|
|
120
|
+
reads,
|
|
121
|
+
frameWritten,
|
|
122
|
+
frameExposed
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
port.on('message', (job) => {
|
|
126
|
+
stats.run = 0;
|
|
127
|
+
stats.scan = 0;
|
|
128
|
+
let reply;
|
|
129
|
+
try {
|
|
130
|
+
const results = [];
|
|
131
|
+
for (;;) {
|
|
132
|
+
const k = Atomics.add(counter, 0, 1);
|
|
133
|
+
if (k >= job.count)
|
|
134
|
+
break;
|
|
135
|
+
const outcome = runOne(job, job.reverse ? job.count - 1 - k : k);
|
|
136
|
+
results.push(outcome);
|
|
137
|
+
// every later iteration is rolled back and replayed anyway, so leave them unstarted
|
|
138
|
+
if (outcome.status && !job.reverse)
|
|
139
|
+
Atomics.store(counter, 0, 1 << 30);
|
|
140
|
+
}
|
|
141
|
+
reply = { done: results, runMs: stats.run, scanMs: stats.scan };
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
reply = { error: String(error?.stack ?? error) };
|
|
145
|
+
}
|
|
146
|
+
port.postMessage(reply);
|
|
147
|
+
Atomics.add(done, 0, 1);
|
|
148
|
+
Atomics.notify(done, 0);
|
|
149
|
+
});
|
|
150
|
+
parentPort.postMessage({ ready: true });
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { IterationRoute } from './extract.ts';
|
|
2
|
+
import type { IterationAdmission, IterationStop } from './iteration-admission.ts';
|
|
3
|
+
import { type IterationArena, type IterationArenaOptions } from './iteration-layout.ts';
|
|
4
|
+
export { iterationArena, iterationArenaBytes, iterationArguments, iterationEntryBytes, iterationFit, iterationFrameShadow, iterationGranules } from './iteration-layout.ts';
|
|
5
|
+
export type { IterationArena, IterationArenaOptions } from './iteration-layout.ts';
|
|
6
|
+
/** Pool construction options. */
|
|
7
|
+
export interface IterationPoolOptions extends IterationArenaOptions {
|
|
8
|
+
/** Start of the arena, 64-byte aligned, in memory the caller keeps free while the pool is open. */
|
|
9
|
+
arenaBase: number;
|
|
10
|
+
/** Workers; defaults to up to four. */
|
|
11
|
+
width?: number;
|
|
12
|
+
}
|
|
13
|
+
/** One owner call's loop. */
|
|
14
|
+
export interface IterationJob {
|
|
15
|
+
/** Iterations to run. An entry value of zero runs 2^32 times in the program; refuse that upstream. */
|
|
16
|
+
trip: number;
|
|
17
|
+
/** The owner's frame local after its prologue; the image at `[frameBase, frameBase + size)` is copied. */
|
|
18
|
+
frameBase: number;
|
|
19
|
+
/** Loop-entry value of every local in `route.locals` that has an `argument`, except the frame. */
|
|
20
|
+
entry: Readonly<Record<number, number | bigint>>;
|
|
21
|
+
/** Runs iteration `k` serially on the owner frame. A throw is the program's trap. */
|
|
22
|
+
replay(k: number): void;
|
|
23
|
+
/** Test hook: `reverse` makes one worker run a batch last to first. Default `forward`. */
|
|
24
|
+
schedule?: 'forward' | 'reverse';
|
|
25
|
+
/** Test hook, unsound: validation checks to leave out. */
|
|
26
|
+
skip?: readonly ('raw' | 'war' | 'waw' | 'frame')[];
|
|
27
|
+
/**
|
|
28
|
+
* Runs iteration 0 through `replay` on the owner thread first and times it. That is the serial
|
|
29
|
+
* program's first iteration, so its writes stay and the batches cover iterations 1 to `trip - 1`
|
|
30
|
+
* from the owner frame it leaves. Called once with the time of that iteration, before anything
|
|
31
|
+
* is dispatched. A refusal runs the other iterations through `replay` and dispatches nothing.
|
|
32
|
+
* Without `admit` every iteration is dispatched.
|
|
33
|
+
*/
|
|
34
|
+
admit?: (plainIterationMs: number) => IterationAdmission;
|
|
35
|
+
/**
|
|
36
|
+
* Called after each batch that leaves iterations behind, with its wall (dispatch, validation,
|
|
37
|
+
* rollback and replay) and the serial time of its iterations at the time of iteration 0. True
|
|
38
|
+
* ends the dispatch and runs the rest through `replay`. Only used with `admit`.
|
|
39
|
+
*/
|
|
40
|
+
loses?: (batchWallMs: number, batchSerialMs: number) => boolean;
|
|
41
|
+
/** Like `loses`, for a batch slow enough that a second batch is not needed to confirm it. */
|
|
42
|
+
collapses?: (batchWallMs: number, batchSerialMs: number) => boolean;
|
|
43
|
+
}
|
|
44
|
+
/** What a run did. `regions`, `replayed`, `fallbacks` and `workers` parallel `ExecutionReport`. */
|
|
45
|
+
export interface IterationReport {
|
|
46
|
+
/** Workers that took part. */
|
|
47
|
+
workers: number;
|
|
48
|
+
/** Iterations run on workers, the regions of this mode. */
|
|
49
|
+
regions: number;
|
|
50
|
+
/** Dispatched iterations that were rolled back and rerun serially at the owner. */
|
|
51
|
+
replayed: number;
|
|
52
|
+
/** Iterations whose writes were undone before the rerun. */
|
|
53
|
+
rolledBack: number;
|
|
54
|
+
/** Batches whose first failure was a trap or an exhausted undo log, not a dependency. */
|
|
55
|
+
fallbacks: number;
|
|
56
|
+
/** Iterations that trapped on a worker. */
|
|
57
|
+
traps: number;
|
|
58
|
+
/** Iterations that exhausted their undo log. */
|
|
59
|
+
exhausted: number;
|
|
60
|
+
batches: number;
|
|
61
|
+
failedBatches: number;
|
|
62
|
+
undoEntries: number;
|
|
63
|
+
/** Ordered iteration pairs by hazard. */
|
|
64
|
+
conflicts: {
|
|
65
|
+
raw: number;
|
|
66
|
+
war: number;
|
|
67
|
+
waw: number;
|
|
68
|
+
frame: number;
|
|
69
|
+
};
|
|
70
|
+
/** From posting a batch to the last worker reply. */
|
|
71
|
+
dispatchMs: number;
|
|
72
|
+
validationMs: number;
|
|
73
|
+
rollbackMs: number;
|
|
74
|
+
replayMs: number;
|
|
75
|
+
/** Wasm time inside workers, summed. */
|
|
76
|
+
workerRunMs: number;
|
|
77
|
+
/** Bitmap scan and clear time inside workers, summed. */
|
|
78
|
+
workerScanMs: number;
|
|
79
|
+
/**
|
|
80
|
+
* Iterations the owner ran as the serial program without dispatching them: iteration 0, the
|
|
81
|
+
* rest of a loop after a stop, or all of them when admission refused the loop.
|
|
82
|
+
*/
|
|
83
|
+
serial: number;
|
|
84
|
+
/** What ended the dispatch early, or why admission refused the loop. */
|
|
85
|
+
stopped?: IterationStop;
|
|
86
|
+
/** What the admission callback saw and chose; set when the job had one. */
|
|
87
|
+
admission?: IterationAdmission;
|
|
88
|
+
}
|
|
89
|
+
/** What a worker returns for one iteration. */
|
|
90
|
+
export interface IterationOutcome {
|
|
91
|
+
index: number;
|
|
92
|
+
/** 0 ran, 1 trapped, 2 undo log exhausted */
|
|
93
|
+
status: number;
|
|
94
|
+
entries: number;
|
|
95
|
+
/** sorted write granules (32 bytes) from the undo log */
|
|
96
|
+
written: Int32Array;
|
|
97
|
+
/** [start, length] runs of read granules (64 bytes) */
|
|
98
|
+
reads: Int32Array;
|
|
99
|
+
/** written bit per frame byte, one i32 per 32-byte frame granule; empty when nothing was written */
|
|
100
|
+
frameWritten: Uint32Array;
|
|
101
|
+
/** bit per frame byte the iteration read before writing it, same layout; empty when none */
|
|
102
|
+
frameExposed: Uint32Array;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Index of the first outcome that cannot keep its writes, or -1. Writes land in place, so every
|
|
106
|
+
* ordered pair is checked three ways: RAW marks the reader, WAR the earlier reader, WAW the earlier
|
|
107
|
+
* writer. A fourth check covers the frame, which each iteration owns as a private copy: an
|
|
108
|
+
* iteration fails when it read a frame byte before writing it that an earlier iteration wrote, and
|
|
109
|
+
* counts once. Works on the returned lists, never on dense bitmaps.
|
|
110
|
+
*/
|
|
111
|
+
export declare function iterationVerdict(outcomes: readonly (Pick<IterationOutcome, 'status' | 'written' | 'reads'> & Partial<Pick<IterationOutcome, 'frameWritten' | 'frameExposed'>>)[], skip?: readonly ('raw' | 'war' | 'waw' | 'frame')[]): {
|
|
112
|
+
first: number;
|
|
113
|
+
firstFailure: number;
|
|
114
|
+
frameFirst: number;
|
|
115
|
+
conflicts: {
|
|
116
|
+
raw: number;
|
|
117
|
+
war: number;
|
|
118
|
+
waw: number;
|
|
119
|
+
frame: number;
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Runs the loop of one owner call on worker threads. Each iteration gets a private stack slot and
|
|
124
|
+
* tracks its reads and writes inline. The owner validates a batch in order, rolls back the first
|
|
125
|
+
* failing iteration and every later one from the undo logs, and replays them through `replay`.
|
|
126
|
+
* The arena belongs to the pool until `close`.
|
|
127
|
+
*
|
|
128
|
+
* Each iteration runs on a private copy of the owner frame, taken from the owner frame when its
|
|
129
|
+
* batch is dispatched. The lowered module tracks the frame: a written bit per frame byte, and an
|
|
130
|
+
* exposed bit per 32-byte frame granule that the iteration read before it wrote the byte. An
|
|
131
|
+
* iteration fails validation, like a RAW pair, when it has an exposed granule that an earlier
|
|
132
|
+
* iteration of the same batch wrote (`conflicts.frame`); it and every later one in the batch roll
|
|
133
|
+
* back and replay. After a batch validates, each kept
|
|
134
|
+
* iteration's written frame bytes are copied into the owner frame in order, so the next batch
|
|
135
|
+
* starts from the serial frame and a replay runs on it. The guarantee: a read of an owner frame
|
|
136
|
+
* byte that the same iteration has not written yet, with a write by an earlier iteration of the
|
|
137
|
+
* batch to that byte, is detected, and an earlier batch's writes are seen through the copy.
|
|
138
|
+
* Excluded: a read of stack below the frame (callee frames, indeterminate before the callee writes
|
|
139
|
+
* them) is not tracked, and imported host functions that touch memory are not seen. The shadow
|
|
140
|
+
* is byte-exact for scalar accesses and ranges; a conservative read would only add reports.
|
|
141
|
+
*
|
|
142
|
+
* With `admit`, iteration 0 runs first on the owner and the batches cover the rest. Dispatch
|
|
143
|
+
* stops after a batch that exhausted an undo log, had a frame conflict, replayed more than half of
|
|
144
|
+
* its iterations, or (with `loses` and `collapses`) two batches in a row did not beat the serial
|
|
145
|
+
* time of their iterations or one took far longer than it; the rest of the loop runs through
|
|
146
|
+
* `replay` and `report.stopped` names the reason. Once an iteration fails, the workers start no
|
|
147
|
+
* further iteration of that batch, because it and every later one replay anyway.
|
|
148
|
+
*/
|
|
149
|
+
export declare class IterationPool {
|
|
150
|
+
#private;
|
|
151
|
+
readonly arena: IterationArena;
|
|
152
|
+
readonly width: number;
|
|
153
|
+
private constructor();
|
|
154
|
+
/** The report of the last run, also after a run that threw. */
|
|
155
|
+
get report(): IterationReport;
|
|
156
|
+
/** Lowers the route's worker module inline and starts `width` workers over `memory`. */
|
|
157
|
+
static open(route: IterationRoute, memory: WebAssembly.Memory, options: IterationPoolOptions): Promise<IterationPool>;
|
|
158
|
+
/** Runs the loop to completion with ordered validation. Blocks the calling thread. */
|
|
159
|
+
run(job: IterationJob): IterationReport;
|
|
160
|
+
/** Stops the workers. Safe to call twice. */
|
|
161
|
+
close(): Promise<void>;
|
|
162
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
163
|
+
}
|