tla-ts 0.0.3-dev.763cc71 → 0.0.3-dev.91b9cf4
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 +22 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +18 -17
- package/dist/run-cli.js +14 -13
- package/dist/transfer.cjs +27 -15
- package/dist/transfer.js +27 -15
- package/package.json +1 -1
- package/src/ast.ts +90 -0
- package/src/check.ts +191 -54
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +9 -19
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +75 -16
- package/src/errors.ts +9 -2
- package/src/evaluate.ts +84 -141
- package/src/exit-codes.ts +21 -43
- package/src/fingerprints.ts +46 -11
- package/src/instances.ts +15 -14
- package/src/levels.ts +8 -8
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +710 -0
- package/src/liveness.ts +5 -523
- package/src/parallel-worker.ts +158 -221
- package/src/parallel.ts +76 -35
- package/src/parser.ts +12 -1
- package/src/resolve.ts +16 -15
- package/src/run-cli.ts +58 -13
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +18 -6
- package/src/standard-modules.ts +2 -7
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +66 -9
- package/src/symmetry.ts +14 -2
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +9 -28
- package/src/tlc-order.ts +17 -3
- package/src/values.ts +40 -11
package/src/parallel-worker.ts
CHANGED
|
@@ -1,66 +1,41 @@
|
|
|
1
1
|
import { createCanonicalizer } from "./canonical";
|
|
2
2
|
import { createSearch } from "./check";
|
|
3
3
|
import { createEnvironment } from "./environment";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
import {
|
|
5
|
+
createFingerprintTable,
|
|
6
|
+
insertSharedFingerprint,
|
|
7
|
+
isSharedFingerprintsFull,
|
|
8
|
+
writeStateFingerprint,
|
|
9
|
+
} from "./fingerprints";
|
|
10
|
+
import type {
|
|
11
|
+
CoordinatorMessage,
|
|
12
|
+
LevelMessage,
|
|
13
|
+
Port,
|
|
14
|
+
StartMessage,
|
|
15
|
+
WorkerMessage,
|
|
16
|
+
} from "./parallel";
|
|
17
|
+
import { getSuccessors, type Successor } from "./successors";
|
|
7
18
|
import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
|
|
8
19
|
import { OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
9
20
|
import { createStateDecoder, createStateEncoder, type EncodedStates } from "./transfer";
|
|
10
21
|
import { getRankedStringCount, setStringOrder, type Value } from "./values";
|
|
11
22
|
|
|
12
23
|
const WORKER_THREADS_MODULE = "node:worker_threads";
|
|
24
|
+
const LOCKED = -1;
|
|
13
25
|
|
|
14
|
-
interface
|
|
26
|
+
interface Expansion {
|
|
15
27
|
states: Value[][];
|
|
16
|
-
|
|
17
|
-
|
|
28
|
+
origin: number;
|
|
29
|
+
end: number;
|
|
30
|
+
successors: Successor[] | undefined;
|
|
31
|
+
index: number;
|
|
32
|
+
added: number;
|
|
33
|
+
generated: number;
|
|
34
|
+
outDegrees: number[];
|
|
35
|
+
next: Value[][];
|
|
18
36
|
fingerprintStates: Value[][];
|
|
19
37
|
}
|
|
20
38
|
|
|
21
|
-
interface PeerMessage {
|
|
22
|
-
phase: "query" | "answer" | "sizes" | "balance";
|
|
23
|
-
level: number;
|
|
24
|
-
from: number;
|
|
25
|
-
fingerprints?: Int32Array;
|
|
26
|
-
bits?: Uint8Array;
|
|
27
|
-
states?: EncodedStates;
|
|
28
|
-
unmarks?: Int32Array;
|
|
29
|
-
size?: number;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
interface Transfer {
|
|
33
|
-
from: number;
|
|
34
|
-
to: number;
|
|
35
|
-
count: number;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const getBalancePlan = (sizes: number[]) => {
|
|
39
|
-
const total = sizes.reduce((sum, size) => sum + size, 0);
|
|
40
|
-
const average = total / sizes.length;
|
|
41
|
-
if (Math.max(...sizes) - Math.min(...sizes) <= Math.max(64, average / 4)) return [];
|
|
42
|
-
const targets = sizes.map(
|
|
43
|
-
(_, index) => Math.floor(average) + (index < total % sizes.length ? 1 : 0),
|
|
44
|
-
);
|
|
45
|
-
const donors = sizes.flatMap((size, index) =>
|
|
46
|
-
size > targets[index] ? [{ index, amount: size - targets[index] }] : [],
|
|
47
|
-
);
|
|
48
|
-
const receivers = sizes.flatMap((size, index) =>
|
|
49
|
-
size < targets[index] ? [{ index, amount: targets[index] - size }] : [],
|
|
50
|
-
);
|
|
51
|
-
const transfers: Transfer[] = [];
|
|
52
|
-
for (const donor of donors) {
|
|
53
|
-
for (const receiver of receivers) {
|
|
54
|
-
const moved = Math.min(donor.amount, receiver.amount);
|
|
55
|
-
if (moved === 0) continue;
|
|
56
|
-
transfers.push({ from: donor.index, to: receiver.index, count: moved });
|
|
57
|
-
donor.amount -= moved;
|
|
58
|
-
receiver.amount -= moved;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return transfers;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
39
|
const getParentPort = async () => {
|
|
65
40
|
if (typeof (globalThis as { postMessage?: unknown }).postMessage === "function") {
|
|
66
41
|
return globalThis as unknown as Port;
|
|
@@ -71,33 +46,6 @@ const getParentPort = async () => {
|
|
|
71
46
|
return threads.parentPort as unknown as Port;
|
|
72
47
|
};
|
|
73
48
|
|
|
74
|
-
const createInbox = () => {
|
|
75
|
-
const messages = new Map<string, PeerMessage[]>();
|
|
76
|
-
const waiters = new Map<string, { count: number; resolve: (messages: PeerMessage[]) => void }>();
|
|
77
|
-
const getKey = (phase: PeerMessage["phase"], level: number) => `${phase}:${level}`;
|
|
78
|
-
const settle = (key: string) => {
|
|
79
|
-
const waiter = waiters.get(key);
|
|
80
|
-
const received = messages.get(key) ?? [];
|
|
81
|
-
if (!waiter || received.length < waiter.count) return;
|
|
82
|
-
waiters.delete(key);
|
|
83
|
-
messages.delete(key);
|
|
84
|
-
waiter.resolve(received);
|
|
85
|
-
};
|
|
86
|
-
return {
|
|
87
|
-
deliver: (message: PeerMessage) => {
|
|
88
|
-
const key = getKey(message.phase, message.level);
|
|
89
|
-
messages.set(key, [...(messages.get(key) ?? []), message]);
|
|
90
|
-
settle(key);
|
|
91
|
-
},
|
|
92
|
-
receive: (phase: PeerMessage["phase"], level: number, count: number) =>
|
|
93
|
-
new Promise<PeerMessage[]>((resolve) => {
|
|
94
|
-
const key = getKey(phase, level);
|
|
95
|
-
waiters.set(key, { count, resolve });
|
|
96
|
-
settle(key);
|
|
97
|
-
}),
|
|
98
|
-
};
|
|
99
|
-
};
|
|
100
|
-
|
|
101
49
|
const run = (start: StartMessage, parent: Port) => {
|
|
102
50
|
const modules = new Map(start.modules);
|
|
103
51
|
const loadModule = (name: string) => modules.get(name);
|
|
@@ -118,46 +66,73 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
118
66
|
const nextAction = search?.spec.next;
|
|
119
67
|
if (!search || nextAction === undefined)
|
|
120
68
|
return parent.postMessage({ type: "abort" } satisfies WorkerMessage);
|
|
121
|
-
const count = start.count;
|
|
122
69
|
const self = start.index;
|
|
123
70
|
const canonicalize = createCanonicalizer();
|
|
124
71
|
const decode = createStateDecoder(environment.variables.length);
|
|
125
|
-
const
|
|
126
|
-
const
|
|
72
|
+
const shared = { fingerprints: start.fingerprints };
|
|
73
|
+
const requests = start.requests;
|
|
74
|
+
const remaining = start.remaining;
|
|
127
75
|
// Under symmetry, a successor the worker generated before has the representative it had then,
|
|
128
76
|
// so the worker skips the search for one.
|
|
129
77
|
const generatedStates = search.isSymmetric ? createFingerprintTable() : undefined;
|
|
130
78
|
const fingerprint = new Int32Array(2);
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
peer.port.start?.();
|
|
79
|
+
const waiters = { resume: () => {}, loot: (_states: EncodedStates) => {} };
|
|
80
|
+
start.ports.forEach((port) => {
|
|
81
|
+
port?.addEventListener("message", (event) => waiters.loot(event.data as EncodedStates));
|
|
82
|
+
port?.start?.();
|
|
136
83
|
});
|
|
137
|
-
for (let index = 0; index < start.seedFingerprints.length; index += 2) {
|
|
138
|
-
table.insert(start.seedFingerprints[index], start.seedFingerprints[index + 1]);
|
|
139
|
-
}
|
|
140
84
|
const frontier = { states: decode(start.seed).map(canonicalize) };
|
|
141
|
-
const send = (index: number, message: PeerMessage, transfer: unknown[]) =>
|
|
142
|
-
start.ports[index]?.postMessage(message, transfer);
|
|
143
85
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
86
|
+
// A worker that runs out of states asks a peer for some by writing its index + 1 to the peer's
|
|
87
|
+
// request word. The peer sends half of the states it hasn't started, keeps expanding, and wakes
|
|
88
|
+
// the workers waiting to ask it next.
|
|
89
|
+
const answer = (expansion: Expansion, request: number) => {
|
|
90
|
+
const thief = Atomics.load(requests, self) - 1;
|
|
91
|
+
const given = Math.floor((expansion.end - expansion.origin - 1) / 2);
|
|
92
|
+
const encoder = createStateEncoder();
|
|
93
|
+
const states = expansion.states.slice(expansion.end - given, expansion.end);
|
|
94
|
+
const isEncoded = given > 0 && states.every(encoder.encode);
|
|
95
|
+
if (isEncoded) expansion.end -= given;
|
|
96
|
+
const loot = isEncoded ? encoder.finish() : createStateEncoder().finish();
|
|
97
|
+
start.ports[thief]?.postMessage(loot, [loot.words.buffer]);
|
|
98
|
+
Atomics.store(requests, self, request);
|
|
99
|
+
Atomics.notify(requests, self);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// A worker answers any request before it pauses or runs out of states, and takes no more until
|
|
103
|
+
// it resumes.
|
|
104
|
+
const lock = (expansion: Expansion) => {
|
|
105
|
+
Atomics.store(remaining, self, 0);
|
|
106
|
+
if (Atomics.compareExchange(requests, self, 0, LOCKED) === 0) return;
|
|
107
|
+
answer(expansion, LOCKED);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const unlock = (expansion: Expansion) => {
|
|
111
|
+
Atomics.store(remaining, self, expansion.end - expansion.origin);
|
|
112
|
+
Atomics.store(requests, self, 0);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// The worker pauses before a successor when the shared table needs to grow, and resumes there.
|
|
116
|
+
const expand = (expansion: Expansion) => {
|
|
117
|
+
for (; expansion.origin < expansion.end; expansion.origin++) {
|
|
118
|
+
const state = expansion.states[expansion.origin];
|
|
119
|
+
if (!expansion.successors) {
|
|
120
|
+
if (Atomics.load(requests, self) > 0) answer(expansion, 0);
|
|
121
|
+
Atomics.store(remaining, self, expansion.end - expansion.origin - 1);
|
|
122
|
+
expansion.successors = getSuccessors(nextAction, state, environment, search.spec.scope);
|
|
123
|
+
expansion.generated += expansion.successors.length;
|
|
124
|
+
if (expansion.successors.length === 0 && start.config.checkDeadlock) return undefined;
|
|
125
|
+
}
|
|
126
|
+
const successors = expansion.successors;
|
|
127
|
+
for (; expansion.index < successors.length; expansion.index++) {
|
|
128
|
+
if (isSharedFingerprintsFull(shared.fingerprints)) return "paused";
|
|
129
|
+
const successor = successors[expansion.index];
|
|
159
130
|
if (search.findViolatedActionProperty(state, successor.state)) return undefined;
|
|
160
|
-
if (!search.satisfiesActionConstraints(state, successor.state))
|
|
131
|
+
if (!search.satisfiesActionConstraints(state, successor.state)) {
|
|
132
|
+
// TLC checks the invariants of every successor it has not seen, even outside the model.
|
|
133
|
+
if (search.findViolatedInvariant(successor.state, state)) return undefined;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
161
136
|
if (generatedStates) {
|
|
162
137
|
writeStateFingerprint(successor.state, fingerprint);
|
|
163
138
|
if (!generatedStates.insert(fingerprint[0], fingerprint[1])) continue;
|
|
@@ -165,136 +140,93 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
165
140
|
canonicalize(successor.state);
|
|
166
141
|
const fingerprintState = search.getFingerprintState(successor.state);
|
|
167
142
|
writeStateFingerprint(fingerprintState, fingerprint);
|
|
168
|
-
|
|
169
|
-
if (
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
continue;
|
|
175
|
-
}
|
|
176
|
-
next.push(successor.state);
|
|
177
|
-
nextFingerprintStates.push(fingerprintState);
|
|
178
|
-
newStates[origin]++;
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
if (!known.insert(fingerprint[0], fingerprint[1])) continue;
|
|
182
|
-
pending[owner].states.push(successor.state);
|
|
183
|
-
pending[owner].origins.push(origin);
|
|
184
|
-
pending[owner].fingerprints.push(fingerprint[0], fingerprint[1]);
|
|
185
|
-
pending[owner].fingerprintStates.push(fingerprintState);
|
|
143
|
+
if (!insertSharedFingerprint(shared.fingerprints, fingerprint[0], fingerprint[1])) continue;
|
|
144
|
+
if (search.findViolatedInvariant(successor.state, state)) return undefined;
|
|
145
|
+
if (!search.satisfiesConstraints(successor.state, state)) continue;
|
|
146
|
+
expansion.next.push(successor.state);
|
|
147
|
+
expansion.fingerprintStates.push(fingerprintState);
|
|
148
|
+
expansion.added++;
|
|
186
149
|
}
|
|
150
|
+
expansion.outDegrees[Math.min(expansion.added, OUT_DEGREE_BUCKETS - 1)]++;
|
|
151
|
+
expansion.successors = undefined;
|
|
152
|
+
expansion.index = 0;
|
|
153
|
+
expansion.added = 0;
|
|
187
154
|
}
|
|
188
|
-
return
|
|
155
|
+
return "done";
|
|
189
156
|
};
|
|
190
157
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
if (search.findViolatedInvariant(state, parentState)) return undefined;
|
|
212
|
-
if (search.satisfiesConstraints(state, parentState)) {
|
|
213
|
-
next.push(state);
|
|
214
|
-
nextFingerprintStates.push(pending.fingerprintStates[index]);
|
|
215
|
-
newStates[pending.origins[index]]++;
|
|
158
|
+
// Each attempt asks the peer with the most states left. While another worker's request to that
|
|
159
|
+
// peer is open, the worker waits for the peer to answer it. The worker stops once no peer that
|
|
160
|
+
// takes requests has two states left.
|
|
161
|
+
const steal = async (expansion: Expansion) => {
|
|
162
|
+
const skipped = new Set<number>();
|
|
163
|
+
for (;;) {
|
|
164
|
+
const victim = start.ports.reduce<number | undefined>((best, port, index) => {
|
|
165
|
+
if (!port || skipped.has(index) || Atomics.load(remaining, index) < 2) return best;
|
|
166
|
+
return best === undefined || Atomics.load(remaining, index) > Atomics.load(remaining, best)
|
|
167
|
+
? index
|
|
168
|
+
: best;
|
|
169
|
+
}, undefined);
|
|
170
|
+
if (victim === undefined) return false;
|
|
171
|
+
const request = Atomics.compareExchange(requests, victim, 0, self + 1);
|
|
172
|
+
if (request === LOCKED) skipped.add(victim);
|
|
173
|
+
if (request > 0) Atomics.wait(requests, victim, request);
|
|
174
|
+
if (request !== 0) continue;
|
|
175
|
+
const loot = await new Promise<EncodedStates>((resolve) => (waiters.loot = resolve));
|
|
176
|
+
if (loot.count === 0) {
|
|
177
|
+
skipped.add(victim);
|
|
216
178
|
continue;
|
|
217
179
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
const removeAll = (fingerprints: Int32Array) => {
|
|
225
|
-
for (let index = 0; index < fingerprints.length; index += 2) {
|
|
226
|
-
table.remove(fingerprints[index], fingerprints[index + 1]);
|
|
180
|
+
expansion.states = decode(loot).map(canonicalize);
|
|
181
|
+
expansion.origin = 0;
|
|
182
|
+
expansion.end = expansion.states.length;
|
|
183
|
+
unlock(expansion);
|
|
184
|
+
return true;
|
|
227
185
|
}
|
|
228
186
|
};
|
|
229
187
|
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
if (transfers.length === 0) return true;
|
|
233
|
-
const outgoing = new Map(peers.map((peer) => [peer.index, createStateEncoder()]));
|
|
234
|
-
for (const transfer of transfers.filter((candidate) => candidate.from === self)) {
|
|
235
|
-
const encoder = outgoing.get(transfer.to);
|
|
236
|
-
const states = next.splice(next.length - transfer.count, transfer.count);
|
|
237
|
-
if (!encoder || !states.every(encoder.encode)) return false;
|
|
238
|
-
}
|
|
239
|
-
outgoing.forEach((encoder, index) => {
|
|
240
|
-
const states = encoder.finish();
|
|
241
|
-
send(index, { phase: "balance", level, from: self, states }, [states.words.buffer]);
|
|
242
|
-
});
|
|
243
|
-
const incoming = await inbox.receive("balance", level, peers.length);
|
|
244
|
-
incoming.forEach((message) => {
|
|
245
|
-
if (message.states) next.push(...decode(message.states).map(canonicalize));
|
|
246
|
-
});
|
|
247
|
-
return true;
|
|
248
|
-
};
|
|
188
|
+
const report = (message: WorkerMessage) =>
|
|
189
|
+
parent.postMessage(message, message.type === "level" ? [message.fingerprints.buffer] : []);
|
|
249
190
|
|
|
250
191
|
const runLevel = async (level: number): Promise<LevelMessage | undefined> => {
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
192
|
+
const expansion: Expansion = {
|
|
193
|
+
states: frontier.states,
|
|
194
|
+
origin: 0,
|
|
195
|
+
end: frontier.states.length,
|
|
196
|
+
successors: undefined,
|
|
197
|
+
index: 0,
|
|
198
|
+
added: 0,
|
|
199
|
+
generated: 0,
|
|
200
|
+
outDegrees: Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0),
|
|
201
|
+
next: [],
|
|
202
|
+
fingerprintStates: [],
|
|
203
|
+
};
|
|
204
|
+
for (;;) {
|
|
205
|
+
const status = expand(expansion);
|
|
206
|
+
if (status === undefined) return undefined;
|
|
207
|
+
lock(expansion);
|
|
208
|
+
if (status === "paused") {
|
|
209
|
+
const resumed = new Promise<void>((resolve) => (waiters.resume = resolve));
|
|
210
|
+
report({ type: "paused" });
|
|
211
|
+
await resumed;
|
|
212
|
+
unlock(expansion);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (!(await steal(expansion))) break;
|
|
275
216
|
}
|
|
276
|
-
const added = next.length;
|
|
277
|
-
const outDegrees = Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0);
|
|
278
|
-
expanded.newStates.forEach((degree) => outDegrees[Math.min(degree, OUT_DEGREE_BUCKETS - 1)]++);
|
|
279
|
-
const fingerprints = search.getStoredFingerprints(next, nextFingerprintStates);
|
|
280
|
-
const sizes = Array.from({ length: count }, () => 0);
|
|
281
|
-
sizes[self] = added;
|
|
282
|
-
peers.forEach((peer) =>
|
|
283
|
-
send(peer.index, { phase: "sizes", level, from: self, size: added }, []),
|
|
284
|
-
);
|
|
285
|
-
const received = await inbox.receive("sizes", level, peers.length * 2);
|
|
286
|
-
received.forEach((message) => {
|
|
287
|
-
if (message.unmarks) removeAll(message.unmarks);
|
|
288
|
-
else sizes[message.from] = message.size ?? 0;
|
|
289
|
-
});
|
|
290
217
|
if (getRankedStringCount() !== startupStrings) return undefined;
|
|
291
|
-
|
|
292
|
-
frontier.states
|
|
293
|
-
return {
|
|
218
|
+
frontier.states = expansion.next;
|
|
219
|
+
Atomics.store(remaining, self, frontier.states.length);
|
|
220
|
+
return {
|
|
221
|
+
type: "level",
|
|
222
|
+
level,
|
|
223
|
+
generated: expansion.generated,
|
|
224
|
+
added: expansion.next.length,
|
|
225
|
+
outDegrees: expansion.outDegrees,
|
|
226
|
+
fingerprints: search.getStoredFingerprints(expansion.next, expansion.fingerprintStates),
|
|
227
|
+
};
|
|
294
228
|
};
|
|
295
229
|
|
|
296
|
-
const report = (message: WorkerMessage) =>
|
|
297
|
-
parent.postMessage(message, message.type === "level" ? [message.fingerprints.buffer] : []);
|
|
298
230
|
const step = (level: number) => {
|
|
299
231
|
runLevel(level).then(
|
|
300
232
|
(message) => report(message ?? { type: "abort" }),
|
|
@@ -302,9 +234,14 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
302
234
|
);
|
|
303
235
|
};
|
|
304
236
|
parent.addEventListener("message", (event) => {
|
|
305
|
-
const message = event.data as
|
|
237
|
+
const message = event.data as CoordinatorMessage;
|
|
306
238
|
if (message.type === "level") step(message.level);
|
|
239
|
+
if (message.type === "resize") {
|
|
240
|
+
shared.fingerprints = message.fingerprints;
|
|
241
|
+
waiters.resume();
|
|
242
|
+
}
|
|
307
243
|
});
|
|
244
|
+
Atomics.store(remaining, self, frontier.states.length);
|
|
308
245
|
report({ type: "ready" });
|
|
309
246
|
};
|
|
310
247
|
|
package/src/parallel.ts
CHANGED
|
@@ -11,7 +11,14 @@ import { parseConfig, type ModelConfig } from "./config";
|
|
|
11
11
|
import { createEnvironment } from "./environment";
|
|
12
12
|
import { TlaEvaluationError } from "./errors";
|
|
13
13
|
import { isImpure } from "./evaluate";
|
|
14
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
createSharedFingerprints,
|
|
16
|
+
growSharedFingerprints,
|
|
17
|
+
insertSharedFingerprint,
|
|
18
|
+
isSharedFingerprintsFull,
|
|
19
|
+
writeStateFingerprint,
|
|
20
|
+
type SharedFingerprints,
|
|
21
|
+
} from "./fingerprints";
|
|
15
22
|
import { getInitialStates } from "./successors";
|
|
16
23
|
import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
|
|
17
24
|
import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
@@ -34,10 +41,11 @@ export interface StartMessage {
|
|
|
34
41
|
modules: [string, string | undefined][];
|
|
35
42
|
classes: [string, Uint8Array | undefined][];
|
|
36
43
|
index: number;
|
|
37
|
-
count: number;
|
|
38
44
|
ports: (Port | undefined)[];
|
|
39
45
|
seed: EncodedStates;
|
|
40
|
-
|
|
46
|
+
fingerprints: SharedFingerprints;
|
|
47
|
+
requests: Int32Array;
|
|
48
|
+
remaining: Int32Array;
|
|
41
49
|
}
|
|
42
50
|
|
|
43
51
|
export interface LevelMessage {
|
|
@@ -49,7 +57,15 @@ export interface LevelMessage {
|
|
|
49
57
|
fingerprints: BigInt64Array;
|
|
50
58
|
}
|
|
51
59
|
|
|
52
|
-
export type WorkerMessage =
|
|
60
|
+
export type WorkerMessage =
|
|
61
|
+
| { type: "ready" }
|
|
62
|
+
| { type: "abort" }
|
|
63
|
+
| { type: "paused" }
|
|
64
|
+
| LevelMessage;
|
|
65
|
+
|
|
66
|
+
export type CoordinatorMessage =
|
|
67
|
+
| { type: "level"; level: number }
|
|
68
|
+
| { type: "resize"; fingerprints: SharedFingerprints };
|
|
53
69
|
|
|
54
70
|
interface WorkerHandle {
|
|
55
71
|
postMessage: (message: unknown, transfer?: unknown[]) => void;
|
|
@@ -86,10 +102,24 @@ const spawnWorker = async (onMessage: (message: WorkerMessage) => void): Promise
|
|
|
86
102
|
|
|
87
103
|
export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
88
104
|
const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
|
|
89
|
-
const
|
|
105
|
+
const print = options.onPrint ?? console.log;
|
|
106
|
+
const printed = { count: 0 };
|
|
107
|
+
// The search on the calling thread prints what this one printed while it started, so it skips that.
|
|
108
|
+
const fallBack = () => {
|
|
109
|
+
const skipped = { count: printed.count };
|
|
110
|
+
return checkSequentially({
|
|
111
|
+
...options,
|
|
112
|
+
config,
|
|
113
|
+
onPrint: (text) => (skipped.count-- > 0 ? undefined : print(text)),
|
|
114
|
+
});
|
|
115
|
+
};
|
|
90
116
|
const count = options.workers ?? 1;
|
|
117
|
+
// TLC's workers write the -dump state graph in whichever order they find it, so one worker
|
|
118
|
+
// writes the graph a single TLC worker does.
|
|
91
119
|
const isSequential =
|
|
92
120
|
count < 2 ||
|
|
121
|
+
typeof SharedArrayBuffer !== "function" ||
|
|
122
|
+
options.dump !== undefined ||
|
|
93
123
|
config.view !== undefined ||
|
|
94
124
|
config.postconditions.length > 0 ||
|
|
95
125
|
config.possible.length > 0;
|
|
@@ -112,7 +142,10 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
112
142
|
source: options.source,
|
|
113
143
|
loadModule,
|
|
114
144
|
loadClass,
|
|
115
|
-
print:
|
|
145
|
+
print: (text) => {
|
|
146
|
+
printed.count++;
|
|
147
|
+
print(text);
|
|
148
|
+
},
|
|
116
149
|
files: { readFile: options.readFile, writeFile: options.writeFile },
|
|
117
150
|
});
|
|
118
151
|
const environment = specification.environment;
|
|
@@ -131,12 +164,9 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
131
164
|
}
|
|
132
165
|
|
|
133
166
|
const canonicalize = createCanonicalizer();
|
|
134
|
-
const
|
|
167
|
+
const shared = { fingerprints: createSharedFingerprints() };
|
|
135
168
|
const fingerprint = new Int32Array(2);
|
|
136
|
-
const seeds = Array.from({ length: count },
|
|
137
|
-
encoder: createStateEncoder(),
|
|
138
|
-
fingerprints: [] as number[],
|
|
139
|
-
}));
|
|
169
|
+
const seeds = Array.from({ length: count }, createStateEncoder);
|
|
140
170
|
const statistics = {
|
|
141
171
|
generatedStates: 0,
|
|
142
172
|
distinctStates: 0,
|
|
@@ -169,17 +199,15 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
169
199
|
canonicalize(state);
|
|
170
200
|
statistics.generatedStates++;
|
|
171
201
|
writeStateFingerprint(search.getFingerprintState(state), fingerprint);
|
|
172
|
-
if (
|
|
202
|
+
if (isSharedFingerprintsFull(shared.fingerprints)) {
|
|
203
|
+
shared.fingerprints = growSharedFingerprints(shared.fingerprints);
|
|
204
|
+
}
|
|
205
|
+
if (!insertSharedFingerprint(shared.fingerprints, fingerprint[0], fingerprint[1])) continue;
|
|
173
206
|
if (search.findViolatedInvariant(state) || search.findFailedInitialPredicate(state)) {
|
|
174
207
|
return false;
|
|
175
208
|
}
|
|
176
|
-
if (!search.satisfiesConstraints(state))
|
|
177
|
-
|
|
178
|
-
continue;
|
|
179
|
-
}
|
|
180
|
-
const seed = seeds[getFingerprintOwner(fingerprint[1], count)];
|
|
181
|
-
if (!seed.encoder.encode(state)) return false;
|
|
182
|
-
seed.fingerprints.push(fingerprint[0], fingerprint[1]);
|
|
209
|
+
if (!search.satisfiesConstraints(state)) continue;
|
|
210
|
+
if (!seeds[seeded.length % count].encode(state)) return false;
|
|
183
211
|
seeded.push(state);
|
|
184
212
|
statistics.distinctStates++;
|
|
185
213
|
}
|
|
@@ -200,18 +228,21 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
200
228
|
if (statistics.distinctStates === 0) return createSuccess();
|
|
201
229
|
|
|
202
230
|
const reports: WorkerMessage[] = [];
|
|
203
|
-
const pending = { resolve: (_reports: WorkerMessage[]) => {} };
|
|
231
|
+
const pending = { count, resolve: (_reports: WorkerMessage[]) => {} };
|
|
204
232
|
const onMessage = (message: WorkerMessage) => {
|
|
205
233
|
reports.push(message);
|
|
206
|
-
if (message.type === "abort" || reports.length === count) {
|
|
234
|
+
if (message.type === "abort" || reports.length === pending.count) {
|
|
207
235
|
pending.resolve(reports.splice(0));
|
|
208
236
|
}
|
|
209
237
|
};
|
|
210
|
-
const waitForReports = () =>
|
|
238
|
+
const waitForReports = (expected: number) =>
|
|
211
239
|
new Promise<WorkerMessage[]>((resolve) => {
|
|
240
|
+
pending.count = expected;
|
|
212
241
|
pending.resolve = resolve;
|
|
213
242
|
});
|
|
214
|
-
const ready = waitForReports();
|
|
243
|
+
const ready = waitForReports(count);
|
|
244
|
+
const requests = new Int32Array(new SharedArrayBuffer(count * 4));
|
|
245
|
+
const remaining = new Int32Array(new SharedArrayBuffer(count * 4));
|
|
215
246
|
const workers = await Promise.all(Array.from({ length: count }, () => spawnWorker(onMessage)));
|
|
216
247
|
const terminate = () => workers.forEach((worker) => worker.terminate());
|
|
217
248
|
const channels = new Map<string, MessageChannel>();
|
|
@@ -225,8 +256,7 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
225
256
|
const ports = Array.from({ length: count }, (_, peer) =>
|
|
226
257
|
peer === index ? undefined : getPort(index, peer),
|
|
227
258
|
);
|
|
228
|
-
const seed = seeds[index].
|
|
229
|
-
const seedFingerprints = Int32Array.from(seeds[index].fingerprints);
|
|
259
|
+
const seed = seeds[index].finish();
|
|
230
260
|
const message: StartMessage = {
|
|
231
261
|
type: "start",
|
|
232
262
|
source: options.source,
|
|
@@ -234,27 +264,38 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
234
264
|
modules: [...modules],
|
|
235
265
|
classes: [...classes],
|
|
236
266
|
index,
|
|
237
|
-
count,
|
|
238
267
|
ports,
|
|
239
268
|
seed,
|
|
240
|
-
|
|
269
|
+
fingerprints: shared.fingerprints,
|
|
270
|
+
requests,
|
|
271
|
+
remaining,
|
|
241
272
|
};
|
|
242
|
-
worker.postMessage(message, [
|
|
243
|
-
...ports.filter((port) => port !== undefined),
|
|
244
|
-
seed.words.buffer,
|
|
245
|
-
seedFingerprints.buffer,
|
|
246
|
-
]);
|
|
273
|
+
worker.postMessage(message, [...ports.filter((port) => port !== undefined), seed.words.buffer]);
|
|
247
274
|
});
|
|
248
275
|
const isAborted = (messages: WorkerMessage[]) =>
|
|
249
276
|
messages.some((message) => message.type === "abort");
|
|
277
|
+
const send = (message: CoordinatorMessage) =>
|
|
278
|
+
workers.forEach((worker) => worker.postMessage(message));
|
|
279
|
+
// Workers pause when the shared table needs to grow, and resume once it has.
|
|
280
|
+
const collectLevels = async (messages: WorkerMessage[]): Promise<WorkerMessage[]> => {
|
|
281
|
+
const paused = messages.filter((message) => message.type === "paused").length;
|
|
282
|
+
if (paused === 0 || isAborted(messages)) return messages;
|
|
283
|
+
shared.fingerprints = growSharedFingerprints(shared.fingerprints);
|
|
284
|
+
const resumed = waitForReports(paused);
|
|
285
|
+
send({ type: "resize", fingerprints: shared.fingerprints });
|
|
286
|
+
const reported = messages.filter((message) => message.type !== "paused");
|
|
287
|
+
return [...reported, ...(await collectLevels(await resumed))];
|
|
288
|
+
};
|
|
250
289
|
if (isAborted(await ready)) {
|
|
251
290
|
terminate();
|
|
252
291
|
return fallBack();
|
|
253
292
|
}
|
|
254
293
|
for (let level = 1; ; level++) {
|
|
255
|
-
const reported = waitForReports();
|
|
256
|
-
|
|
257
|
-
|
|
294
|
+
const reported = waitForReports(count);
|
|
295
|
+
// Every worker is idle, so peers can ask any of them for states once the level starts.
|
|
296
|
+
requests.forEach((_, index) => Atomics.store(requests, index, 0));
|
|
297
|
+
send({ type: "level", level });
|
|
298
|
+
const messages = await collectLevels(await reported);
|
|
258
299
|
if (isAborted(messages)) {
|
|
259
300
|
terminate();
|
|
260
301
|
return fallBack();
|