tla-ts 0.0.2 → 0.0.3-dev.19c763f
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 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +60 -1
- package/dist/index.d.ts +60 -1
- 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 +21 -2
- package/dist/run-cli.js +21 -2
- package/dist/transfer.cjs +27 -15
- package/dist/transfer.js +27 -15
- package/package.json +3 -1
- package/src/ast.ts +134 -4
- package/src/check.ts +230 -62
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +9 -19
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +75 -16
- package/src/errors.ts +46 -3
- package/src/evaluate.ts +78 -136
- 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 +11 -2
- package/src/live-check.ts +710 -0
- package/src/liveness.ts +5 -523
- package/src/obligation-action.ts +384 -0
- package/src/obligation-anon.ts +575 -0
- package/src/obligation-deref.ts +637 -0
- package/src/obligation-expression.ts +1060 -0
- package/src/obligation-format.ts +406 -0
- package/src/obligation-generate.ts +1074 -0
- package/src/obligation-levels.ts +329 -0
- package/src/obligation-module.ts +1042 -0
- package/src/obligation-printer.ts +889 -0
- package/src/obligation-proof.ts +76 -0
- package/src/obligation-source.ts +1084 -0
- package/src/obligation-tuples.ts +184 -0
- package/src/obligations.ts +316 -0
- package/src/parallel-worker.ts +166 -210
- package/src/parallel.ts +76 -35
- package/src/parser.ts +297 -53
- package/src/resolve.ts +16 -15
- package/src/run-cli.ts +353 -40
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +18 -6
- package/src/standard-levels.ts +3 -3
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +17 -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/transfer.ts +22 -1
- package/src/values.ts +70 -23
package/src/parallel-worker.ts
CHANGED
|
@@ -1,65 +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[][];
|
|
36
|
+
fingerprintStates: Value[][];
|
|
18
37
|
}
|
|
19
38
|
|
|
20
|
-
interface PeerMessage {
|
|
21
|
-
phase: "query" | "answer" | "sizes" | "balance";
|
|
22
|
-
level: number;
|
|
23
|
-
from: number;
|
|
24
|
-
fingerprints?: Int32Array;
|
|
25
|
-
bits?: Uint8Array;
|
|
26
|
-
states?: EncodedStates;
|
|
27
|
-
unmarks?: Int32Array;
|
|
28
|
-
size?: number;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface Transfer {
|
|
32
|
-
from: number;
|
|
33
|
-
to: number;
|
|
34
|
-
count: number;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const getBalancePlan = (sizes: number[]) => {
|
|
38
|
-
const total = sizes.reduce((sum, size) => sum + size, 0);
|
|
39
|
-
const average = total / sizes.length;
|
|
40
|
-
if (Math.max(...sizes) - Math.min(...sizes) <= Math.max(64, average / 4)) return [];
|
|
41
|
-
const targets = sizes.map(
|
|
42
|
-
(_, index) => Math.floor(average) + (index < total % sizes.length ? 1 : 0),
|
|
43
|
-
);
|
|
44
|
-
const donors = sizes.flatMap((size, index) =>
|
|
45
|
-
size > targets[index] ? [{ index, amount: size - targets[index] }] : [],
|
|
46
|
-
);
|
|
47
|
-
const receivers = sizes.flatMap((size, index) =>
|
|
48
|
-
size < targets[index] ? [{ index, amount: targets[index] - size }] : [],
|
|
49
|
-
);
|
|
50
|
-
const transfers: Transfer[] = [];
|
|
51
|
-
for (const donor of donors) {
|
|
52
|
-
for (const receiver of receivers) {
|
|
53
|
-
const moved = Math.min(donor.amount, receiver.amount);
|
|
54
|
-
if (moved === 0) continue;
|
|
55
|
-
transfers.push({ from: donor.index, to: receiver.index, count: moved });
|
|
56
|
-
donor.amount -= moved;
|
|
57
|
-
receiver.amount -= moved;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return transfers;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
39
|
const getParentPort = async () => {
|
|
64
40
|
if (typeof (globalThis as { postMessage?: unknown }).postMessage === "function") {
|
|
65
41
|
return globalThis as unknown as Port;
|
|
@@ -70,33 +46,6 @@ const getParentPort = async () => {
|
|
|
70
46
|
return threads.parentPort as unknown as Port;
|
|
71
47
|
};
|
|
72
48
|
|
|
73
|
-
const createInbox = () => {
|
|
74
|
-
const messages = new Map<string, PeerMessage[]>();
|
|
75
|
-
const waiters = new Map<string, { count: number; resolve: (messages: PeerMessage[]) => void }>();
|
|
76
|
-
const getKey = (phase: PeerMessage["phase"], level: number) => `${phase}:${level}`;
|
|
77
|
-
const settle = (key: string) => {
|
|
78
|
-
const waiter = waiters.get(key);
|
|
79
|
-
const received = messages.get(key) ?? [];
|
|
80
|
-
if (!waiter || received.length < waiter.count) return;
|
|
81
|
-
waiters.delete(key);
|
|
82
|
-
messages.delete(key);
|
|
83
|
-
waiter.resolve(received);
|
|
84
|
-
};
|
|
85
|
-
return {
|
|
86
|
-
deliver: (message: PeerMessage) => {
|
|
87
|
-
const key = getKey(message.phase, message.level);
|
|
88
|
-
messages.set(key, [...(messages.get(key) ?? []), message]);
|
|
89
|
-
settle(key);
|
|
90
|
-
},
|
|
91
|
-
receive: (phase: PeerMessage["phase"], level: number, count: number) =>
|
|
92
|
-
new Promise<PeerMessage[]>((resolve) => {
|
|
93
|
-
const key = getKey(phase, level);
|
|
94
|
-
waiters.set(key, { count, resolve });
|
|
95
|
-
settle(key);
|
|
96
|
-
}),
|
|
97
|
-
};
|
|
98
|
-
};
|
|
99
|
-
|
|
100
49
|
const run = (start: StartMessage, parent: Port) => {
|
|
101
50
|
const modules = new Map(start.modules);
|
|
102
51
|
const loadModule = (name: string) => modules.get(name);
|
|
@@ -117,165 +66,167 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
117
66
|
const nextAction = search?.spec.next;
|
|
118
67
|
if (!search || nextAction === undefined)
|
|
119
68
|
return parent.postMessage({ type: "abort" } satisfies WorkerMessage);
|
|
120
|
-
const count = start.count;
|
|
121
69
|
const self = start.index;
|
|
122
70
|
const canonicalize = createCanonicalizer();
|
|
123
71
|
const decode = createStateDecoder(environment.variables.length);
|
|
124
|
-
const
|
|
125
|
-
const
|
|
72
|
+
const shared = { fingerprints: start.fingerprints };
|
|
73
|
+
const requests = start.requests;
|
|
74
|
+
const remaining = start.remaining;
|
|
75
|
+
// Under symmetry, a successor the worker generated before has the representative it had then,
|
|
76
|
+
// so the worker skips the search for one.
|
|
77
|
+
const generatedStates = search.isSymmetric ? createFingerprintTable() : undefined;
|
|
126
78
|
const fingerprint = new Int32Array(2);
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
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?.();
|
|
132
83
|
});
|
|
133
|
-
for (let index = 0; index < start.seedFingerprints.length; index += 2) {
|
|
134
|
-
table.insert(start.seedFingerprints[index], start.seedFingerprints[index + 1]);
|
|
135
|
-
}
|
|
136
84
|
const frontier = { states: decode(start.seed).map(canonicalize) };
|
|
137
|
-
const send = (index: number, message: PeerMessage, transfer: unknown[]) =>
|
|
138
|
-
start.ports[index]?.postMessage(message, transfer);
|
|
139
85
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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];
|
|
154
130
|
if (search.findViolatedActionProperty(state, successor.state)) return undefined;
|
|
155
|
-
if (!search.satisfiesActionConstraints(state, successor.state))
|
|
156
|
-
|
|
157
|
-
writeStateFingerprint(search.getFingerprintState(successor.state), fingerprint);
|
|
158
|
-
const owner = getFingerprintOwner(fingerprint[1], count);
|
|
159
|
-
if (owner === self) {
|
|
160
|
-
if (!table.insert(fingerprint[0], fingerprint[1])) continue;
|
|
131
|
+
if (!search.satisfiesActionConstraints(state, successor.state)) {
|
|
132
|
+
// TLC checks the invariants of every successor it has not seen, even outside the model.
|
|
161
133
|
if (search.findViolatedInvariant(successor.state, state)) return undefined;
|
|
162
|
-
if (!search.satisfiesConstraints(successor.state, state)) {
|
|
163
|
-
table.remove(fingerprint[0], fingerprint[1]);
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
next.push(successor.state);
|
|
167
|
-
newStates[origin]++;
|
|
168
134
|
continue;
|
|
169
135
|
}
|
|
170
|
-
if (
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
136
|
+
if (generatedStates) {
|
|
137
|
+
writeStateFingerprint(successor.state, fingerprint);
|
|
138
|
+
if (!generatedStates.insert(fingerprint[0], fingerprint[1])) continue;
|
|
139
|
+
}
|
|
140
|
+
canonicalize(successor.state);
|
|
141
|
+
const fingerprintState = search.getFingerprintState(successor.state);
|
|
142
|
+
writeStateFingerprint(fingerprintState, fingerprint);
|
|
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++;
|
|
174
149
|
}
|
|
150
|
+
expansion.outDegrees[Math.min(expansion.added, OUT_DEGREE_BUCKETS - 1)]++;
|
|
151
|
+
expansion.successors = undefined;
|
|
152
|
+
expansion.index = 0;
|
|
153
|
+
expansion.added = 0;
|
|
175
154
|
}
|
|
176
|
-
return
|
|
155
|
+
return "done";
|
|
177
156
|
};
|
|
178
157
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
if (
|
|
194
|
-
if (
|
|
195
|
-
|
|
196
|
-
|
|
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);
|
|
197
178
|
continue;
|
|
198
179
|
}
|
|
199
|
-
|
|
200
|
-
|
|
180
|
+
expansion.states = decode(loot).map(canonicalize);
|
|
181
|
+
expansion.origin = 0;
|
|
182
|
+
expansion.end = expansion.states.length;
|
|
183
|
+
unlock(expansion);
|
|
184
|
+
return true;
|
|
201
185
|
}
|
|
202
|
-
return Int32Array.from(unmarks);
|
|
203
186
|
};
|
|
204
187
|
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
table.remove(fingerprints[index], fingerprints[index + 1]);
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
const balance = async (level: number, next: Value[][], sizes: number[]) => {
|
|
212
|
-
const transfers = getBalancePlan(sizes);
|
|
213
|
-
if (transfers.length === 0) return true;
|
|
214
|
-
const outgoing = new Map(peers.map((peer) => [peer.index, createStateEncoder()]));
|
|
215
|
-
for (const transfer of transfers.filter((candidate) => candidate.from === self)) {
|
|
216
|
-
const encoder = outgoing.get(transfer.to);
|
|
217
|
-
const states = next.splice(next.length - transfer.count, transfer.count);
|
|
218
|
-
if (!encoder || !states.every(encoder.encode)) return false;
|
|
219
|
-
}
|
|
220
|
-
outgoing.forEach((encoder, index) => {
|
|
221
|
-
const states = encoder.finish();
|
|
222
|
-
send(index, { phase: "balance", level, from: self, states }, [states.words.buffer]);
|
|
223
|
-
});
|
|
224
|
-
const incoming = await inbox.receive("balance", level, peers.length);
|
|
225
|
-
incoming.forEach((message) => {
|
|
226
|
-
if (message.states) next.push(...decode(message.states).map(canonicalize));
|
|
227
|
-
});
|
|
228
|
-
return true;
|
|
229
|
-
};
|
|
188
|
+
const report = (message: WorkerMessage) =>
|
|
189
|
+
parent.postMessage(message, message.type === "level" ? [message.fingerprints.buffer] : []);
|
|
230
190
|
|
|
231
191
|
const runLevel = async (level: number): Promise<LevelMessage | undefined> => {
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
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;
|
|
254
216
|
}
|
|
255
|
-
const added = next.length;
|
|
256
|
-
const outDegrees = Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0);
|
|
257
|
-
expanded.newStates.forEach((degree) => outDegrees[Math.min(degree, OUT_DEGREE_BUCKETS - 1)]++);
|
|
258
|
-
const fingerprints = BigInt64Array.from(next, (state) =>
|
|
259
|
-
environment.runtime.getFingerprint(state),
|
|
260
|
-
);
|
|
261
|
-
const sizes = Array.from({ length: count }, () => 0);
|
|
262
|
-
sizes[self] = added;
|
|
263
|
-
peers.forEach((peer) =>
|
|
264
|
-
send(peer.index, { phase: "sizes", level, from: self, size: added }, []),
|
|
265
|
-
);
|
|
266
|
-
const received = await inbox.receive("sizes", level, peers.length * 2);
|
|
267
|
-
received.forEach((message) => {
|
|
268
|
-
if (message.unmarks) removeAll(message.unmarks);
|
|
269
|
-
else sizes[message.from] = message.size ?? 0;
|
|
270
|
-
});
|
|
271
217
|
if (getRankedStringCount() !== startupStrings) return undefined;
|
|
272
|
-
|
|
273
|
-
frontier.states
|
|
274
|
-
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
|
+
};
|
|
275
228
|
};
|
|
276
229
|
|
|
277
|
-
const report = (message: WorkerMessage) =>
|
|
278
|
-
parent.postMessage(message, message.type === "level" ? [message.fingerprints.buffer] : []);
|
|
279
230
|
const step = (level: number) => {
|
|
280
231
|
runLevel(level).then(
|
|
281
232
|
(message) => report(message ?? { type: "abort" }),
|
|
@@ -283,9 +234,14 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
283
234
|
);
|
|
284
235
|
};
|
|
285
236
|
parent.addEventListener("message", (event) => {
|
|
286
|
-
const message = event.data as
|
|
237
|
+
const message = event.data as CoordinatorMessage;
|
|
287
238
|
if (message.type === "level") step(message.level);
|
|
239
|
+
if (message.type === "resize") {
|
|
240
|
+
shared.fingerprints = message.fingerprints;
|
|
241
|
+
waiters.resume();
|
|
242
|
+
}
|
|
288
243
|
});
|
|
244
|
+
Atomics.store(remaining, self, frontier.states.length);
|
|
289
245
|
report({ type: "ready" });
|
|
290
246
|
};
|
|
291
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();
|