@gmitch215/folio 1.0.1 → 1.1.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/README.md +398 -28
- package/dist/affine-state.d.ts +171 -0
- package/dist/affine-state.js +763 -0
- package/dist/affine.d.ts +160 -0
- package/dist/affine.js +571 -0
- package/dist/boundaries.d.ts +59 -0
- package/dist/boundaries.js +127 -0
- package/dist/certified.d.ts +66 -0
- package/dist/certified.js +192 -0
- package/dist/certify.d.ts +98 -0
- package/dist/certify.js +2628 -0
- package/dist/context.d.ts +16 -2
- package/dist/context.js +60 -14
- package/dist/coordinator.js +50 -3
- package/dist/extract.d.ts +19 -0
- package/dist/extract.js +133 -5
- package/dist/fold.d.ts +32 -0
- package/dist/fold.js +105 -0
- package/dist/frames.d.ts +5 -1
- package/dist/frames.js +156 -9
- package/dist/inline.d.ts +114 -0
- package/dist/inline.js +276 -13
- package/dist/iteration-admission.d.ts +116 -7
- package/dist/iteration-admission.js +137 -8
- package/dist/iteration-layout.d.ts +60 -0
- package/dist/iteration-layout.js +54 -3
- package/dist/iteration-owner.d.ts +14 -1
- package/dist/iteration-owner.js +2 -2
- package/dist/iteration-worker.js +124 -58
- package/dist/iteration.d.ts +179 -13
- package/dist/iteration.js +845 -70
- package/dist/lease.d.ts +45 -0
- package/dist/lease.js +274 -0
- package/dist/managed.d.ts +72 -0
- package/dist/managed.js +425 -27
- package/dist/memo.d.ts +99 -0
- package/dist/memo.js +250 -0
- package/dist/node.d.ts +43 -1
- package/dist/node.js +87 -11
- package/dist/ownership-redirect.d.ts +30 -0
- package/dist/ownership-redirect.js +179 -0
- package/dist/ownership-slices.d.ts +44 -0
- package/dist/ownership-slices.js +72 -0
- package/dist/ownership.d.ts +161 -0
- package/dist/ownership.js +6568 -0
- package/dist/ranges.d.ts +78 -0
- package/dist/ranges.js +263 -0
- package/dist/read-scan.d.ts +18 -0
- package/dist/read-scan.js +94 -0
- package/dist/read-windows.d.ts +103 -0
- package/dist/read-windows.js +244 -0
- package/dist/router-worker.d.ts +1 -0
- package/dist/router-worker.js +4 -0
- package/dist/router.d.ts +203 -0
- package/dist/router.js +667 -0
- package/dist/runs.d.ts +44 -0
- package/dist/runs.js +182 -0
- package/dist/slices-worker.d.ts +1 -0
- package/dist/slices-worker.js +38 -0
- package/dist/slices.d.ts +126 -0
- package/dist/slices.js +441 -0
- package/dist/tasks.d.ts +14 -4
- package/dist/tasks.js +12 -11
- package/dist/transaction.d.ts +10 -0
- package/package.json +40 -4
|
@@ -16,16 +16,26 @@ export const iterationAdmissionConstants = {
|
|
|
16
16
|
fixedMs: 2,
|
|
17
17
|
margin: 1.25
|
|
18
18
|
};
|
|
19
|
+
/**
|
|
20
|
+
* With the second chance on, a loop whose remaining serial time is under the floor but over this
|
|
21
|
+
* fraction of it is admitted as a probe: its first timed batch has to beat the serial time of its
|
|
22
|
+
* iterations by the margin, or the dispatch ends there. The floor was fitted on a build with the
|
|
23
|
+
* full read scan and no read windows, so it is the least certain number in the decision.
|
|
24
|
+
*/
|
|
25
|
+
export const probeFraction = 0.5;
|
|
19
26
|
/**
|
|
20
27
|
* Decides before the first dispatch from the measured serial time of iteration 0, which has
|
|
21
28
|
* already run. Admits when at least two iterations are left for at least two workers and their
|
|
22
29
|
* serial time exceeds `fixedMs * margin`. Whether dispatch pays is then checked per batch.
|
|
23
30
|
*/
|
|
24
|
-
export function admitIteration({ plainIterationMs: t0, trip, width, constants = iterationAdmissionConstants }) {
|
|
31
|
+
export function admitIteration({ plainIterationMs: t0, trip, width, constants = iterationAdmissionConstants, secondChance = false }) {
|
|
25
32
|
const remaining = trip - 1;
|
|
26
33
|
const remainingSerialMs = remaining * t0;
|
|
27
34
|
const floorMs = constants.fixedMs * constants.margin;
|
|
28
|
-
const
|
|
35
|
+
const able = Number.isFinite(t0) && remaining >= 2 && width >= 2;
|
|
36
|
+
const admitted = able && remainingSerialMs > floorMs;
|
|
37
|
+
if (secondChance && able && !admitted && remainingSerialMs > floorMs * probeFraction)
|
|
38
|
+
return { plainIterationMs: t0, remainingSerialMs, floorMs, admitted: true, probe: true };
|
|
29
39
|
return { plainIterationMs: t0, remainingSerialMs, floorMs, admitted };
|
|
30
40
|
}
|
|
31
41
|
/** True when a batch did not beat the serial time of its iterations by the margin. */
|
|
@@ -41,23 +51,94 @@ export function batchCollapses(batchWallMs, batchSerialMs) {
|
|
|
41
51
|
/**
|
|
42
52
|
* Test and sweep seam, module state and not an option. `scale` multiplies the measured time of
|
|
43
53
|
* iteration 0 before the first decision and `batchScale` the wall of each batch before the batch
|
|
44
|
-
* rule; `force` admits any loop with an iteration left after
|
|
45
|
-
* batch rule. The structural stops
|
|
46
|
-
* are not affected.
|
|
54
|
+
* rule, and divides the bound of a batch; `force` admits any loop with an iteration left after
|
|
55
|
+
* iteration 0 and never stops on the batch rule or the bound. The structural stops
|
|
56
|
+
* (`log-exhausted`, `frame-carried dependence`, `dependency`) are not affected. `trace` records
|
|
57
|
+
* one entry per batch in the admission report. `gate` false keeps read windows armed whatever the
|
|
58
|
+
* counted batch says, so a sweep can compare the two.
|
|
59
|
+
* @internal
|
|
60
|
+
*/
|
|
61
|
+
export const admissionSeam = {
|
|
62
|
+
scale: 1,
|
|
63
|
+
batchScale: 1,
|
|
64
|
+
force: false,
|
|
65
|
+
trace: false,
|
|
66
|
+
gate: true,
|
|
67
|
+
estimator: 'median'
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* The serial estimate the probe holds after a block: the lower of the last two block means, the
|
|
71
|
+
* median of every block mean so far, or the mean of every probed iteration.
|
|
47
72
|
* @internal
|
|
48
73
|
*/
|
|
49
|
-
export
|
|
74
|
+
export function probeEstimate(kind, last, per, means, mean) {
|
|
75
|
+
if (kind === 'mean')
|
|
76
|
+
return mean;
|
|
77
|
+
if (kind === 'min')
|
|
78
|
+
return Math.min(last, per);
|
|
79
|
+
const sorted = [...means].sort((x, y) => x - y);
|
|
80
|
+
if (kind === 'q75')
|
|
81
|
+
return sorted[Math.min(sorted.length - 1, Math.ceil(0.75 * sorted.length) - 1)];
|
|
82
|
+
const mid = sorted.length >> 1;
|
|
83
|
+
return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
|
|
84
|
+
}
|
|
50
85
|
/** `admitIteration` through the seam; what the host function calls. */
|
|
51
|
-
export function decideAdmission(plainIterationMs, trip, width) {
|
|
86
|
+
export function decideAdmission(plainIterationMs, trip, width, secondChance = false) {
|
|
52
87
|
const decision = admitIteration({
|
|
53
88
|
plainIterationMs: plainIterationMs * admissionSeam.scale,
|
|
54
89
|
trip,
|
|
55
|
-
width
|
|
90
|
+
width,
|
|
91
|
+
secondChance
|
|
56
92
|
});
|
|
57
93
|
if (admissionSeam.force && trip >= 2 && width >= 2)
|
|
58
94
|
decision.admitted = true;
|
|
59
95
|
return decision;
|
|
60
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Updates the estimate from the worker-side times of the iterations a batch completed, with no
|
|
99
|
+
* host call per iteration. The first batch fixes `kappa` as the serial estimate over the least
|
|
100
|
+
* tracked time, at most 1 because a tracked iteration is never faster than a plain one. The least
|
|
101
|
+
* time, not the median, because the iteration that the estimate came from may be the smallest of
|
|
102
|
+
* a batch that is mostly large, and the median would then read its size as the tracking cost. The
|
|
103
|
+
* estimate is then the mean tracked time times `kappa`: a batch of iterations dearer than the
|
|
104
|
+
* earlier estimate raises it, and a batch of cheaper ones lowers it. When the first iteration was
|
|
105
|
+
* larger than every iteration of the batch, `kappa` is capped and the estimate is the tracked
|
|
106
|
+
* mean, an upper bound on the serial time. Times of zero or less are ignored. A batch on read
|
|
107
|
+
* windows is cheaper to track than the first, so it has its own scale, fitted the same way to the
|
|
108
|
+
* same reference the first time one runs; the unwindowed scale would read its time as serial time.
|
|
109
|
+
*/
|
|
110
|
+
export function refineEstimate(before, trackedMs, windows = false) {
|
|
111
|
+
const ran = trackedMs.filter((ms) => ms > 0);
|
|
112
|
+
if (!ran.length)
|
|
113
|
+
return before;
|
|
114
|
+
const referenceMs = before.referenceMs ?? before.estimateMs;
|
|
115
|
+
const learned = windows ? before.kappaWindows : before.kappa;
|
|
116
|
+
const kappa = learned ?? Math.min(1, referenceMs / Math.min(...ran));
|
|
117
|
+
return {
|
|
118
|
+
kappa: windows ? before.kappa : kappa,
|
|
119
|
+
kappaWindows: windows ? kappa : before.kappaWindows,
|
|
120
|
+
referenceMs,
|
|
121
|
+
estimateMs: (kappa * ran.reduce((sum, ms) => sum + ms, 0)) / ran.length
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Whether `remaining` more iterations at `perIterationMs` each are worth dispatching, through the
|
|
126
|
+
* seam. The pool asks it with the online estimate before each batch after the first and after the
|
|
127
|
+
* timed serial blocks of a refused loop.
|
|
128
|
+
*
|
|
129
|
+
* With `last`, the wall and size of the last timed batch, a loop under the floor still goes on
|
|
130
|
+
* when the batches it has seen say so: the pooled cost of what is left is the batch wall times the
|
|
131
|
+
* batches that cover it, and that times the margin has to be under their serial time. The floor
|
|
132
|
+
* prices the wake-up and the learning batch, which a loop that is running has already paid.
|
|
133
|
+
*/
|
|
134
|
+
export function decideRemaining(perIterationMs, remaining, width, last) {
|
|
135
|
+
if (decideAdmission(perIterationMs, remaining + 1, width).admitted)
|
|
136
|
+
return true;
|
|
137
|
+
if (!last || remaining < 2 || width < 2 || !(last.count > 0))
|
|
138
|
+
return false;
|
|
139
|
+
const pooled = Math.ceil(remaining / last.count) * last.wallMs * admissionSeam.batchScale;
|
|
140
|
+
return remaining * perIterationMs > pooled * iterationAdmissionConstants.margin;
|
|
141
|
+
}
|
|
61
142
|
/** `batchLoses` through the seam; what the host function passes the pool. */
|
|
62
143
|
export function decideBatch(batchWallMs, batchSerialMs) {
|
|
63
144
|
if (admissionSeam.force)
|
|
@@ -70,3 +151,51 @@ export function decideCollapse(batchWallMs, batchSerialMs) {
|
|
|
70
151
|
return false;
|
|
71
152
|
return batchCollapses(batchWallMs * admissionSeam.batchScale, batchSerialMs);
|
|
72
153
|
}
|
|
154
|
+
/** Speedup below which a batch cannot pay even with free dispatch, validation and joins. */
|
|
155
|
+
export const boundFloor = 1;
|
|
156
|
+
/**
|
|
157
|
+
* The best speedup a batch could reach: the serial time of the iterations it completed over the
|
|
158
|
+
* time `width` workers need for them at the tracked time they showed. Wake, join and validation
|
|
159
|
+
* only add to the wall, so a bound under one means the loop cannot win at this tracking cost.
|
|
160
|
+
*/
|
|
161
|
+
export function batchBound(perIterationMs, trackedMs, width) {
|
|
162
|
+
const ran = trackedMs.filter((ms) => ms > 0);
|
|
163
|
+
if (!ran.length || !(perIterationMs > 0) || width < 1)
|
|
164
|
+
return Infinity;
|
|
165
|
+
const mean = ran.reduce((sum, ms) => sum + ms, 0) / ran.length;
|
|
166
|
+
return (ran.length * perIterationMs) / (Math.ceil(ran.length / width) * mean);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* A loop whose first batch bounds the speedup above this has tracking cost to spare: realized
|
|
170
|
+
* speedups ran at about half the bound (aes-ctr 1.2 of 2.3, lz4-compress 1.2 of 2.4,
|
|
171
|
+
* aes-cbc-decrypt 1.2 of 2.0 on the Mac sweep), so such a loop goes on to full batches on its
|
|
172
|
+
* read windows. A loop under it gets one short batch on them first.
|
|
173
|
+
*/
|
|
174
|
+
export const proofBound = 2;
|
|
175
|
+
/** True when the bound of a batch is under `boundFloor`, through the seam; what the host passes. */
|
|
176
|
+
export function decideBound(bound) {
|
|
177
|
+
if (admissionSeam.force)
|
|
178
|
+
return false;
|
|
179
|
+
return bound / admissionSeam.batchScale < boundFloor;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* The wall and the serial time of the next batch of `next` iterations, from a batch that ran. Its
|
|
183
|
+
* wall minus the time its workers needed for the iterations it completed is the cost that does
|
|
184
|
+
* not shrink with the batch: wake, join and validation. The next batch pays that plus the
|
|
185
|
+
* workers' share of its own iterations. Validation grows with the batch, so the wall comes out
|
|
186
|
+
* low and a projected loss is a loss.
|
|
187
|
+
*/
|
|
188
|
+
export function projectBatch(wallMs, trackedMs, perIterationMs, next, width) {
|
|
189
|
+
const ran = trackedMs.filter((ms) => ms > 0);
|
|
190
|
+
if (!ran.length || !(perIterationMs > 0) || next < 1 || width < 1)
|
|
191
|
+
return undefined;
|
|
192
|
+
const mean = ran.reduce((sum, ms) => sum + ms, 0) / ran.length;
|
|
193
|
+
const fixed = Math.max(0, wallMs - Math.ceil(ran.length / width) * mean);
|
|
194
|
+
return { wallMs: Math.ceil(next / width) * mean + fixed, serialMs: next * perIterationMs };
|
|
195
|
+
}
|
|
196
|
+
/** True when a bound is low enough that the first batch on read windows should be a short one. */
|
|
197
|
+
export function decideProof(bound) {
|
|
198
|
+
if (admissionSeam.force)
|
|
199
|
+
return false;
|
|
200
|
+
return bound / admissionSeam.batchScale < proofBound;
|
|
201
|
+
}
|
|
@@ -4,6 +4,11 @@ export declare const iterationGranules: {
|
|
|
4
4
|
readonly read: 64;
|
|
5
5
|
readonly write: 32;
|
|
6
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* Undo entries per iteration of the grown log, 64 times the default capacity: 4 MiB of tracked
|
|
9
|
+
* writes. A batch whose log is exhausted is retried once at this size when the memory can grow.
|
|
10
|
+
*/
|
|
11
|
+
export declare const iterationLogCeiling = 131072;
|
|
7
12
|
/** Bytes of one undo entry: granule address, stamp, then the prior bytes of the granule. */
|
|
8
13
|
export declare const iterationEntryBytes: number;
|
|
9
14
|
/** Sizes of the per-batch arena. Every field has a default. */
|
|
@@ -16,6 +21,16 @@ export interface IterationArenaOptions {
|
|
|
16
21
|
slotBytes?: number;
|
|
17
22
|
/** Size of the owner frame whose reads the executor tracks, for the per-slot shadow. Default 0. */
|
|
18
23
|
frameBytes?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Bytes the slot-private windows of one loop may hold, a multiple of 64, which the frame shadows
|
|
26
|
+
* also cover. Default 0, which leaves windows off.
|
|
27
|
+
*/
|
|
28
|
+
windowBytes?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Reserve one summary byte per 64 words of every read bitmap, so the owner and the workers scan
|
|
31
|
+
* only the blocks a read touched. Default false.
|
|
32
|
+
*/
|
|
33
|
+
readSummary?: boolean;
|
|
19
34
|
}
|
|
20
35
|
/** Absolute addresses of the arena parts. Slot 0 sits above one unused guard slot. */
|
|
21
36
|
export interface IterationArena {
|
|
@@ -39,7 +54,19 @@ export interface IterationArena {
|
|
|
39
54
|
frameSlotBytes: number;
|
|
40
55
|
/** The tracked frame size. */
|
|
41
56
|
frameBytes: number;
|
|
57
|
+
/** The window bytes the shadows reserve room for. */
|
|
58
|
+
windowBytes: number;
|
|
59
|
+
/** Start of the read summaries, one per slot, after the frame shadows; 0 without a summary. */
|
|
60
|
+
smaps: number;
|
|
61
|
+
/** Bytes of one slot's read summary, a power of two of at least 64; 0 without a summary. */
|
|
62
|
+
ssize: number;
|
|
42
63
|
}
|
|
64
|
+
/** Words of a read bitmap one summary byte covers. */
|
|
65
|
+
export declare const iterationSummaryWords = 64;
|
|
66
|
+
/** Log2 of the bytes of address space one summary byte covers: 64 words of 32 granules. */
|
|
67
|
+
export declare const iterationSummaryShift: number;
|
|
68
|
+
/** Bytes of slot-private windows a Folio reserves room for in its arena. */
|
|
69
|
+
export declare const iterationWindowBytes = 24576;
|
|
43
70
|
/**
|
|
44
71
|
* Shadow of one tracked frame: `4 * ceil(size / 32)` bytes of written bits, one per frame byte
|
|
45
72
|
* (the bits of frame granule `g`, 32 bytes, are the i32 at `4 * g`), 4 pad bytes, the exposed bits
|
|
@@ -73,3 +100,36 @@ export declare function iterationFit(startBytes: number, options?: IterationAren
|
|
|
73
100
|
* keeps its entry value, and the frame local takes `frame`.
|
|
74
101
|
*/
|
|
75
102
|
export declare function iterationArguments(locals: readonly Pick<IterationLocal, 'local' | 'kind' | 'argument' | 'stride' | 'strideLocal'>[], entry: Readonly<Record<number, number | bigint>>, k: number, frame: number): (number | bigint)[];
|
|
103
|
+
/**
|
|
104
|
+
* Where one slot keeps the private copies of certified windows: each below the one before it, the
|
|
105
|
+
* first below the frame, each at its window's offset modulo 64. `low` is the lowest address used,
|
|
106
|
+
* where the slot's stack may start.
|
|
107
|
+
*/
|
|
108
|
+
export declare function iterationShadowCopies(top: number, frameSize: number, windows: readonly {
|
|
109
|
+
low: number;
|
|
110
|
+
end: number;
|
|
111
|
+
}[]): {
|
|
112
|
+
copies: {
|
|
113
|
+
copy: number;
|
|
114
|
+
delta: number;
|
|
115
|
+
}[];
|
|
116
|
+
low: number;
|
|
117
|
+
};
|
|
118
|
+
/** An address window the iterations of a loop run on slot-private copies of. */
|
|
119
|
+
export interface IterationWindow {
|
|
120
|
+
/** First byte, a multiple of 64. */
|
|
121
|
+
low: number;
|
|
122
|
+
/** One past the last byte, a multiple of 64. */
|
|
123
|
+
end: number;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Where one slot keeps its copy of a window: below the frame, so that the copy and the frame form
|
|
127
|
+
* one shadowed region from `copy` to the slot's top. The copy sits at the same offset modulo 64 as
|
|
128
|
+
* the window, so accesses keep their alignment. `frameOffset` is the frame's offset in the region.
|
|
129
|
+
*/
|
|
130
|
+
export declare function iterationWindowCopy(top: number, frameSize: number, window: IterationWindow): {
|
|
131
|
+
copy: number;
|
|
132
|
+
delta: number;
|
|
133
|
+
region: number;
|
|
134
|
+
frameOffset: number;
|
|
135
|
+
};
|
package/dist/iteration-layout.js
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
/** Granule sizes of the inline lowering the iteration executor runs. */
|
|
2
2
|
export const iterationGranules = { read: 64, write: 32 };
|
|
3
|
+
/**
|
|
4
|
+
* Undo entries per iteration of the grown log, 64 times the default capacity: 4 MiB of tracked
|
|
5
|
+
* writes. A batch whose log is exhausted is retried once at this size when the memory can grow.
|
|
6
|
+
*/
|
|
7
|
+
export const iterationLogCeiling = 131072;
|
|
3
8
|
/** Bytes of one undo entry: granule address, stamp, then the prior bytes of the granule. */
|
|
4
9
|
export const iterationEntryBytes = 8 + iterationGranules.write;
|
|
10
|
+
/** Words of a read bitmap one summary byte covers. */
|
|
11
|
+
export const iterationSummaryWords = 64;
|
|
12
|
+
/** Log2 of the bytes of address space one summary byte covers: 64 words of 32 granules. */
|
|
13
|
+
export const iterationSummaryShift = Math.log2(iterationGranules.read) + 11;
|
|
14
|
+
/** Bytes of slot-private windows a Folio reserves room for in its arena. */
|
|
15
|
+
export const iterationWindowBytes = 24576;
|
|
5
16
|
const align = (value, to) => Math.ceil(value / to) * to;
|
|
6
17
|
/**
|
|
7
18
|
* Shadow of one tracked frame: `4 * ceil(size / 32)` bytes of written bits, one per frame byte
|
|
@@ -24,8 +35,11 @@ export function iterationArena(base, memoryBytes, options = {}) {
|
|
|
24
35
|
const logCapacity = options.logCapacity ?? 2048;
|
|
25
36
|
const slotBytes = options.slotBytes ?? 32768;
|
|
26
37
|
const frameBytes = options.frameBytes ?? 0;
|
|
38
|
+
const windowBytes = options.windowBytes ?? 0;
|
|
27
39
|
if (!Number.isSafeInteger(frameBytes) || frameBytes < 0)
|
|
28
40
|
throw new RangeError('invalid frame');
|
|
41
|
+
if (!Number.isSafeInteger(windowBytes) || windowBytes < 0 || windowBytes % 64)
|
|
42
|
+
throw new RangeError('invalid window size');
|
|
29
43
|
if (!Number.isSafeInteger(base) || base < 0 || base % 64)
|
|
30
44
|
throw new RangeError('invalid base');
|
|
31
45
|
if (!Number.isSafeInteger(memoryBytes) || memoryBytes <= 0)
|
|
@@ -45,8 +59,15 @@ export function iterationArena(base, memoryBytes, options = {}) {
|
|
|
45
59
|
const wmaps = rmaps + batch * rsize;
|
|
46
60
|
const wsize = align(Math.ceil(memoryBytes / (iterationGranules.write * 8)), 64);
|
|
47
61
|
const frameShadow = wmaps + batch * wsize;
|
|
48
|
-
|
|
49
|
-
const
|
|
62
|
+
// a window copy sits below the frame, up to 63 bytes of padding apart, and both are shadowed
|
|
63
|
+
const frameSlotBytes = iterationFrameShadow(frameBytes + (windowBytes ? windowBytes + 64 : 0)).bytes;
|
|
64
|
+
const frameEnd = frameShadow + batch * frameSlotBytes;
|
|
65
|
+
let ssize = 0;
|
|
66
|
+
if (options.readSummary) {
|
|
67
|
+
const blocks = Math.ceil(rsize / 4 / iterationSummaryWords);
|
|
68
|
+
ssize = Math.max(64, 2 ** Math.ceil(Math.log2(blocks)));
|
|
69
|
+
}
|
|
70
|
+
const end = frameEnd + batch * ssize;
|
|
50
71
|
return {
|
|
51
72
|
base,
|
|
52
73
|
bytes: end - base,
|
|
@@ -64,7 +85,10 @@ export function iterationArena(base, memoryBytes, options = {}) {
|
|
|
64
85
|
wsize,
|
|
65
86
|
frameShadow,
|
|
66
87
|
frameSlotBytes,
|
|
67
|
-
frameBytes
|
|
88
|
+
frameBytes,
|
|
89
|
+
windowBytes,
|
|
90
|
+
smaps: ssize ? frameEnd : 0,
|
|
91
|
+
ssize
|
|
68
92
|
};
|
|
69
93
|
}
|
|
70
94
|
/** Bytes the caller must keep free for a pool over a memory of `memoryBytes`. */
|
|
@@ -116,3 +140,30 @@ export function iterationArguments(locals, entry, k, frame) {
|
|
|
116
140
|
}
|
|
117
141
|
return values;
|
|
118
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Where one slot keeps the private copies of certified windows: each below the one before it, the
|
|
145
|
+
* first below the frame, each at its window's offset modulo 64. `low` is the lowest address used,
|
|
146
|
+
* where the slot's stack may start.
|
|
147
|
+
*/
|
|
148
|
+
export function iterationShadowCopies(top, frameSize, windows) {
|
|
149
|
+
let at = top - frameSize;
|
|
150
|
+
const copies = windows.map((w) => {
|
|
151
|
+
const base = at - (w.end - w.low);
|
|
152
|
+
const copy = base - ((((base - w.low) % 64) + 64) % 64);
|
|
153
|
+
at = copy;
|
|
154
|
+
return { copy, delta: copy - w.low };
|
|
155
|
+
});
|
|
156
|
+
return { copies, low: at };
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Where one slot keeps its copy of a window: below the frame, so that the copy and the frame form
|
|
160
|
+
* one shadowed region from `copy` to the slot's top. The copy sits at the same offset modulo 64 as
|
|
161
|
+
* the window, so accesses keep their alignment. `frameOffset` is the frame's offset in the region.
|
|
162
|
+
*/
|
|
163
|
+
export function iterationWindowCopy(top, frameSize, window) {
|
|
164
|
+
const bytes = window.end - window.low;
|
|
165
|
+
const frame = top - frameSize;
|
|
166
|
+
const base = frame - bytes;
|
|
167
|
+
const copy = base - ((((base - window.low) % 64) + 64) % 64);
|
|
168
|
+
return { copy, delta: copy - window.low, region: top - copy, frameOffset: frame - copy };
|
|
169
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IterationRoute } from './extract.ts';
|
|
2
2
|
/** Why the iteration route is not used, in `ExecutionReport.reason`. */
|
|
3
|
-
export type IterationReason = 'no-arena' | 'memory-size-observed' | 'import-order' | 'workers-unavailable' | 'trip-zero' | 'no-admission' | 'log-exhausted' | 'frame-carried dependence';
|
|
3
|
+
export type IterationReason = 'no-arena' | 'memory-size-observed' | 'import-order' | 'workers-unavailable' | 'trip-zero' | 'no-admission' | 'log-exhausted' | 'frame-carried dependence' | 'stalled';
|
|
4
4
|
/** The owner module with its loop handed to `iterate`, and what the host needs to bind it. */
|
|
5
5
|
export interface IterationOwner {
|
|
6
6
|
module: Uint8Array;
|
|
@@ -18,6 +18,19 @@ export interface IterationOwner {
|
|
|
18
18
|
maximum?: number;
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
+
/** Reads the one imported memory's limits from the import section. */
|
|
22
|
+
export declare function importedMemory(bytes: Uint8Array): {
|
|
23
|
+
module: string;
|
|
24
|
+
name: string;
|
|
25
|
+
flags: number;
|
|
26
|
+
initial: number;
|
|
27
|
+
maximum?: number;
|
|
28
|
+
limits: [number, number];
|
|
29
|
+
header: number;
|
|
30
|
+
body: [number, number];
|
|
31
|
+
}[];
|
|
32
|
+
/** Rewrites the memory import limits to shared with the largest maximum. */
|
|
33
|
+
export declare function shareMemory(bytes: Uint8Array, memory: ReturnType<typeof importedMemory>[number]): Uint8Array<ArrayBuffer>;
|
|
21
34
|
/**
|
|
22
35
|
* Builds the owner module of an iteration route: the memory import is shared, and the one loop
|
|
23
36
|
* of the owner function becomes `if (iterate(entry locals...)) { final values } else { the loop }`.
|
package/dist/iteration-owner.js
CHANGED
|
@@ -13,7 +13,7 @@ function uleb(value) {
|
|
|
13
13
|
return out;
|
|
14
14
|
}
|
|
15
15
|
/** Reads the one imported memory's limits from the import section. */
|
|
16
|
-
function importedMemory(bytes) {
|
|
16
|
+
export function importedMemory(bytes) {
|
|
17
17
|
let at = 8;
|
|
18
18
|
const uint = () => {
|
|
19
19
|
let value = 0, shift = 0, byte;
|
|
@@ -92,7 +92,7 @@ function importedMemory(bytes) {
|
|
|
92
92
|
return [];
|
|
93
93
|
}
|
|
94
94
|
/** Rewrites the memory import limits to shared with the largest maximum. */
|
|
95
|
-
function shareMemory(bytes, memory) {
|
|
95
|
+
export function shareMemory(bytes, memory) {
|
|
96
96
|
const { limits, header, body } = memory;
|
|
97
97
|
const content = [
|
|
98
98
|
...bytes.subarray(body[0], limits[0]),
|
package/dist/iteration-worker.js
CHANGED
|
@@ -1,103 +1,141 @@
|
|
|
1
1
|
import { parentPort, workerData } from 'node:worker_threads';
|
|
2
|
-
import { iterationArguments, iterationEntryBytes } from "./iteration-layout.js";
|
|
3
|
-
|
|
2
|
+
import { iterationArguments, iterationEntryBytes, iterationShadowCopies, iterationWindowCopy } from "./iteration-layout.js";
|
|
3
|
+
import { markReadSummary, scanReads } from "./read-scan.js";
|
|
4
|
+
import { markReadWindow, readWindowsAt } from "./read-windows.js";
|
|
5
|
+
const { module, memory, arena, globals, iterate, locals, frameSize, port, signal, control, ownership } = workerData;
|
|
4
6
|
const L = arena;
|
|
5
7
|
const ENTRY = iterationEntryBytes;
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
8
|
+
const plain = new WebAssembly.Instance(module, { env: { memory } }).exports;
|
|
9
|
+
// the other lowerings, instantiated when the pool sends them, by key
|
|
10
|
+
const variants = new Map();
|
|
11
|
+
let u8 = new Uint8Array(memory.buffer);
|
|
12
|
+
let u32 = new Uint32Array(memory.buffer);
|
|
9
13
|
const counter = new Int32Array(control);
|
|
10
14
|
const done = new Int32Array(signal);
|
|
11
15
|
const now = () => performance.now();
|
|
12
|
-
const stats = { run: 0, scan: 0 };
|
|
13
|
-
//
|
|
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
|
|
16
|
+
const stats = { run: 0, scan: 0, words: 0, set: 0, mark: 0 };
|
|
17
|
+
// status 0 ran, 1 trapped, 2 undo log exhausted, 3 stopped by the owner's stall word
|
|
49
18
|
function runOne(job, s) {
|
|
50
19
|
const index = job.first + s;
|
|
51
20
|
const top = L.slots + (s + 1) * L.slotBytes;
|
|
52
21
|
const frame = top - frameSize;
|
|
22
|
+
const win = job.window ? iterationWindowCopy(top, frameSize, job.window) : undefined;
|
|
23
|
+
const exportsOf = job.variant ? variants.get(job.variant) : plain;
|
|
24
|
+
// the tracked region is the frame, or the window copy and the frame below which it sits
|
|
25
|
+
const region = win ? win.region : frameSize;
|
|
26
|
+
const regionLow = win ? win.copy : frame;
|
|
53
27
|
if (frameSize)
|
|
54
28
|
u8.copyWithin(frame, job.frameBase, job.frameBase + frameSize);
|
|
29
|
+
if (win)
|
|
30
|
+
u8.copyWithin(win.copy, job.window.low, job.window.end);
|
|
31
|
+
// a certified window is private and untracked: the slot starts from the owner's bytes
|
|
32
|
+
const own = ownership ? iterationShadowCopies(top, frameSize, ownership.windows) : undefined;
|
|
33
|
+
if (own)
|
|
34
|
+
ownership.windows.forEach((w, i) => {
|
|
35
|
+
u8.copyWithin(own.copies[i].copy, w.low, w.end);
|
|
36
|
+
exportsOf[w.delta].value = own.copies[i].delta;
|
|
37
|
+
});
|
|
55
38
|
const shadow = L.frameShadow + s * L.frameSlotBytes;
|
|
56
|
-
if (
|
|
39
|
+
if (region) {
|
|
57
40
|
u8.fill(0, shadow, shadow + L.frameSlotBytes);
|
|
58
|
-
exportsOf[globals.frameLow].value =
|
|
59
|
-
exportsOf[globals.frameSize].value =
|
|
41
|
+
exportsOf[globals.frameLow].value = regionLow;
|
|
42
|
+
exportsOf[globals.frameSize].value = region;
|
|
60
43
|
exportsOf[globals.frameShadow].value = shadow;
|
|
61
44
|
}
|
|
45
|
+
if (win) {
|
|
46
|
+
exportsOf[globals.windowLow].value = job.window.low;
|
|
47
|
+
exportsOf[globals.windowEnd].value = job.window.end;
|
|
48
|
+
exportsOf[globals.windowDelta].value = win.delta;
|
|
49
|
+
}
|
|
62
50
|
const rbase = L.rmaps + s * L.rsize;
|
|
51
|
+
const sbase = L.ssize ? L.smaps + s * L.ssize : 0;
|
|
63
52
|
const wbase = L.wmaps + s * L.wsize;
|
|
64
|
-
const lbase =
|
|
65
|
-
const limit = lbase +
|
|
53
|
+
const lbase = job.logs.base + s * job.logs.bytes;
|
|
54
|
+
const limit = lbase + job.logs.capacity * ENTRY;
|
|
66
55
|
exportsOf[globals.readBitmap].value = rbase;
|
|
56
|
+
if (L.ssize)
|
|
57
|
+
exportsOf[globals.readSummary].value = sbase;
|
|
67
58
|
exportsOf[globals.writeBitmap].value = wbase;
|
|
68
59
|
exportsOf[globals.logPointer].value = lbase;
|
|
69
60
|
exportsOf[globals.logLimit].value = limit;
|
|
70
61
|
exportsOf[globals.stampAddress].value = L.stamp;
|
|
71
62
|
const stack = exportsOf[globals.stackPointer];
|
|
72
63
|
if (stack)
|
|
73
|
-
stack.value = frameSize ? frame : top;
|
|
64
|
+
stack.value = own ? own.low : win ? win.copy : frameSize ? frame : top;
|
|
65
|
+
const slotted = exportsOf[globals.slotLow] !== undefined;
|
|
66
|
+
if (slotted) {
|
|
67
|
+
exportsOf[globals.exemptLow].value = top - L.slotBytes;
|
|
68
|
+
exportsOf[globals.exemptEnd].value = win ? win.copy : frame;
|
|
69
|
+
exportsOf[globals.slotLow].value = top - L.slotBytes;
|
|
70
|
+
exportsOf[globals.slotEnd].value = top;
|
|
71
|
+
exportsOf[globals.arenaLow].value = L.slots;
|
|
72
|
+
exportsOf[globals.arenaEnd].value = L.stamp;
|
|
73
|
+
exportsOf[globals.violation].value = 0;
|
|
74
|
+
}
|
|
75
|
+
const tm = now();
|
|
76
|
+
if (exportsOf[globals.readWindow0Low]) {
|
|
77
|
+
// the guard of a window with no bytes is false, so a stale window never carries over
|
|
78
|
+
const found = job.readWindows ? readWindowsAt(job.readWindows, index, job.memoryBytes) : [];
|
|
79
|
+
for (const [i, names] of [
|
|
80
|
+
[globals.readWindow0Low, globals.readWindow0Span],
|
|
81
|
+
[globals.readWindow1Low, globals.readWindow1Span]
|
|
82
|
+
].entries()) {
|
|
83
|
+
const w = found[i];
|
|
84
|
+
if (exportsOf[names[0]] === undefined)
|
|
85
|
+
continue;
|
|
86
|
+
exportsOf[names[0]].value = w ? w.low : 0;
|
|
87
|
+
exportsOf[names[1]].value = w ? w.bytes - 7 : 0;
|
|
88
|
+
if (w) {
|
|
89
|
+
markReadWindow(u8, rbase, w.low, w.bytes);
|
|
90
|
+
if (L.ssize)
|
|
91
|
+
markReadSummary(u8, sbase, L.ssize - 1, w.low, w.bytes);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
74
95
|
const args = iterationArguments(locals, job.entry, index, frame);
|
|
96
|
+
const counted = exportsOf[globals.readHits] !== undefined;
|
|
97
|
+
if (counted) {
|
|
98
|
+
exportsOf[globals.readHits].value = 0;
|
|
99
|
+
exportsOf[globals.readMisses].value = 0;
|
|
100
|
+
exportsOf[globals.readExempt].value = 0;
|
|
101
|
+
}
|
|
75
102
|
const t1 = now();
|
|
103
|
+
stats.mark += t1 - tm;
|
|
76
104
|
let status = 0;
|
|
105
|
+
let touched = -1;
|
|
77
106
|
try {
|
|
78
107
|
exportsOf[iterate](...args);
|
|
79
108
|
}
|
|
80
109
|
catch (error) {
|
|
110
|
+
const reached = slotted ? exportsOf[globals.violation].value >>> 0 : 0;
|
|
111
|
+
if (reached) {
|
|
112
|
+
touched = Math.floor((Math.max(reached, L.slots) - L.slots) / L.slotBytes);
|
|
113
|
+
touched = Math.min(touched, L.batch);
|
|
114
|
+
}
|
|
81
115
|
const left = limit - (exportsOf[globals.logPointer].value >>> 0);
|
|
116
|
+
const unreachable = error instanceof WebAssembly.RuntimeError && /unreachable/.test(error.message);
|
|
82
117
|
status =
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
left < ENTRY
|
|
86
|
-
|
|
87
|
-
|
|
118
|
+
!reached && unreachable && Atomics.load(u32, (L.stamp + 4) / 4) !== 0
|
|
119
|
+
? 3
|
|
120
|
+
: !reached && unreachable && left < ENTRY
|
|
121
|
+
? 2
|
|
122
|
+
: 1;
|
|
88
123
|
}
|
|
89
124
|
const t2 = now();
|
|
90
125
|
stats.run += t2 - t1;
|
|
126
|
+
const runMs = t2 - t1;
|
|
91
127
|
const entries = ((exportsOf[globals.logPointer].value >>> 0) - lbase) / ENTRY;
|
|
92
128
|
const written = new Int32Array(entries);
|
|
93
129
|
for (let e = 0; e < entries; e++)
|
|
94
130
|
written[e] = u32[(lbase + e * ENTRY) / 4] >>> 5;
|
|
95
131
|
written.sort();
|
|
96
|
-
const
|
|
132
|
+
const rwords = new Uint32Array(memory.buffer, rbase, L.rsize / 4);
|
|
133
|
+
const summary = L.ssize ? new Uint8Array(memory.buffer, sbase, L.ssize) : undefined;
|
|
134
|
+
const reads = scanReads(rwords, summary, stats);
|
|
97
135
|
let frameWritten = new Uint32Array(0);
|
|
98
136
|
let frameExposed = new Uint32Array(0);
|
|
99
|
-
if (
|
|
100
|
-
const granules = Math.ceil(
|
|
137
|
+
if (region) {
|
|
138
|
+
const granules = Math.ceil(region / 32);
|
|
101
139
|
const words = (at) => u32.slice(at / 4, at / 4 + granules);
|
|
102
140
|
const copy = (at) => {
|
|
103
141
|
const found = words(at);
|
|
@@ -116,15 +154,36 @@ function runOne(job, s) {
|
|
|
116
154
|
index,
|
|
117
155
|
status,
|
|
118
156
|
entries,
|
|
157
|
+
runMs,
|
|
119
158
|
written,
|
|
120
159
|
reads,
|
|
121
160
|
frameWritten,
|
|
122
|
-
frameExposed
|
|
161
|
+
frameExposed,
|
|
162
|
+
touched,
|
|
163
|
+
counts: counted
|
|
164
|
+
? {
|
|
165
|
+
hits: exportsOf[globals.readHits].value >>> 0,
|
|
166
|
+
misses: exportsOf[globals.readMisses].value >>> 0,
|
|
167
|
+
exempt: exportsOf[globals.readExempt].value >>> 0
|
|
168
|
+
}
|
|
169
|
+
: undefined
|
|
123
170
|
};
|
|
124
171
|
}
|
|
125
172
|
port.on('message', (job) => {
|
|
173
|
+
if (job.install) {
|
|
174
|
+
variants.set(job.key, new WebAssembly.Instance(job.install, { env: { memory } }).exports);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
// the pool may have grown the memory for a larger log; the old views end at the old length
|
|
178
|
+
if (job.memoryBytes > u8.length) {
|
|
179
|
+
u8 = new Uint8Array(memory.buffer);
|
|
180
|
+
u32 = new Uint32Array(memory.buffer);
|
|
181
|
+
}
|
|
126
182
|
stats.run = 0;
|
|
127
183
|
stats.scan = 0;
|
|
184
|
+
stats.words = 0;
|
|
185
|
+
stats.set = 0;
|
|
186
|
+
stats.mark = 0;
|
|
128
187
|
let reply;
|
|
129
188
|
try {
|
|
130
189
|
const results = [];
|
|
@@ -138,7 +197,14 @@ port.on('message', (job) => {
|
|
|
138
197
|
if (outcome.status && !job.reverse)
|
|
139
198
|
Atomics.store(counter, 0, 1 << 30);
|
|
140
199
|
}
|
|
141
|
-
reply = {
|
|
200
|
+
reply = {
|
|
201
|
+
done: results,
|
|
202
|
+
runMs: stats.run,
|
|
203
|
+
scanMs: stats.scan,
|
|
204
|
+
scanWords: stats.words,
|
|
205
|
+
scanSet: stats.set,
|
|
206
|
+
markMs: stats.mark
|
|
207
|
+
};
|
|
142
208
|
}
|
|
143
209
|
catch (error) {
|
|
144
210
|
reply = { error: String(error?.stack ?? error) };
|