@smoothbricks/nx-plugin 0.5.0 → 0.5.2
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 +107 -46
- package/dist/bin/smoo-nx-test-clocks.js +2 -2
- package/dist/bin/smoo-nx-test-walks.js +6 -9
- package/dist/cargo-closure-input.d.ts.map +1 -1
- package/dist/cargo-closure-input.js +25 -15
- package/dist/ensure-built-witness.d.ts +33 -5
- package/dist/ensure-built-witness.d.ts.map +1 -1
- package/dist/ensure-built-witness.js +84 -18
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +33 -14
- package/dist/executors/bounded-exec/cpu-pool.d.ts +14 -7
- package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
- package/dist/executors/bounded-exec/cpu-pool.js +297 -38
- package/dist/executors/bounded-exec/cpu-tokens.d.ts +19 -7
- package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -1
- package/dist/executors/bounded-exec/cpu-tokens.js +21 -7
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +12 -6
- package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
- package/dist/executors/bounded-exec/run-cpu-pool.js +5 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/owned-installation.d.ts +6 -5
- package/dist/owned-installation.d.ts.map +1 -1
- package/dist/owned-installation.js +324 -201
- package/dist/producer-receipts.d.ts +10 -0
- package/dist/producer-receipts.d.ts.map +1 -1
- package/dist/producer-receipts.js +30 -5
- package/dist/test-walk-policy.d.ts +8 -0
- package/dist/test-walk-policy.d.ts.map +1 -1
- package/dist/test-walk-policy.js +34 -0
- package/dist/workspace-manifest.d.ts +5 -5
- package/dist/workspace-manifest.d.ts.map +1 -1
- package/managed/raw/patches/nx@23.2.1.patch +14 -3
- package/managed/raw/tooling/direnv/developer-links.ts +37 -10
- package/managed/raw/tooling/direnv/devenv.smoo.nix +1 -3
- package/managed/raw/tooling/direnv/linked-snapshots.ts +49 -12
- package/managed/raw/tooling/direnv/repo-path +1 -1
- package/managed/raw/tooling/direnv/setup-environment.ts +2 -0
- package/managed/raw/tooling/git-hooks/pre-push.sh +34 -11
- package/package.json +3 -3
- package/runtime-input.sh +21 -9
- package/src/__tests__/cargo-closure-filesets.ts +94 -0
- package/src/__tests__/counted-cargo.ts +1 -1
- package/src/__tests__/owned-installation.ts +5 -6
- package/src/bin/smoo-nx-test-clocks.ts +2 -2
- package/src/bin/smoo-nx-test-walks.ts +6 -9
- package/src/cargo-closure-filesets.test.ts +63 -0
- package/src/cargo-closure-input.test.ts +2 -2
- package/src/cargo-closure-input.ts +23 -15
- package/src/cargo-closure-key.test.ts +1 -1
- package/src/cargo-resolve-cache.test.ts +1 -1
- package/src/cross-check-policy.test.ts +53 -12
- package/src/ensure-built-witness.test.ts +53 -2
- package/src/ensure-built-witness.ts +103 -18
- package/src/ensure-built.test.ts +79 -5
- package/src/ensure-built.ts +51 -15
- package/src/executors/bounded-exec/cpu-pool.test.ts +132 -4
- package/src/executors/bounded-exec/cpu-pool.ts +300 -46
- package/src/executors/bounded-exec/cpu-tokens.test.ts +60 -15
- package/src/executors/bounded-exec/cpu-tokens.ts +34 -11
- package/src/executors/bounded-exec/executor.test.ts +72 -4
- package/src/executors/bounded-exec/executor.ts +12 -6
- package/src/executors/bounded-exec/run-cpu-pool.test.ts +3 -0
- package/src/executors/bounded-exec/run-cpu-pool.ts +5 -4
- package/src/index.ts +4 -0
- package/src/linked-snapshots.nx.test.ts +1 -1
- package/src/nextest-partitions.test.ts +1 -1
- package/src/nx-daemon-validity.test.ts +51 -0
- package/src/owned-installation.test.ts +105 -0
- package/src/owned-installation.ts +335 -93
- package/src/producer-receipts.test.ts +37 -15
- package/src/producer-receipts.ts +29 -5
- package/src/runtime-input.test.ts +22 -1
- package/src/task-results.test.ts +1 -1
- package/src/test-walk-policy.test.ts +35 -0
- package/src/test-walk-policy.ts +32 -0
- package/src/workspace-manifest.ts +5 -5
- package/test-timings.json +94 -92
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { closeSync, constants, openSync, readSync, writeSync } from 'node:fs';
|
|
1
3
|
import { chmod, rm } from 'node:fs/promises';
|
|
2
|
-
import { createServer, type Server,
|
|
4
|
+
import { createServer, type Server, Socket } from 'node:net';
|
|
3
5
|
import { join } from 'node:path';
|
|
6
|
+
import { promisify } from 'node:util';
|
|
4
7
|
import typia from 'typia';
|
|
5
8
|
import { ownedSocketDirectory } from '../../testing.js';
|
|
6
9
|
import { CPU_TOKENS_ENV, parseInheritedTokens, type TokenGrant } from './cpu-tokens.js';
|
|
@@ -17,6 +20,13 @@ import { holdLease } from './gateway-lease.js';
|
|
|
17
20
|
* its FIFO queue. A grant lasts while its connection does, so a child that dies returns its tokens
|
|
18
21
|
* with its socket. A pool grants only from its own count, and a pool whose upstream grant has gone
|
|
19
22
|
* grants nothing.
|
|
23
|
+
*
|
|
24
|
+
* The same count is a GNU make jobserver fifo beside the socket, mode 0600, which Cargo, its build
|
|
25
|
+
* scripts and every nested `cargo` read through `CARGO_MAKEFLAGS`/`MAKEFLAGS` (specs/cowshed/05_gateway.md,
|
|
26
|
+
* "The jobserver fifo"). A pool of `g` holds `g − 1` token bytes there; the holder's own command holds the
|
|
27
|
+
* implicit first token. A socket grant takes fifo bytes first and that implicit slot last, which the holder
|
|
28
|
+
* is not using while it waits on the child it lends it to; release writes the bytes back and frees the slot.
|
|
29
|
+
* A Cargo reading the fifo and a nested runner holding a socket grant so share one count.
|
|
20
30
|
*/
|
|
21
31
|
|
|
22
32
|
/** The environment name a child reads its parent's pool endpoint from; its count is `CPU_TOKENS_ENV`. */
|
|
@@ -31,12 +41,12 @@ export interface InheritedCpuPool {
|
|
|
31
41
|
|
|
32
42
|
/** A running pool: the environment a child inherits to reach it, and how to close it. */
|
|
33
43
|
export interface CpuPool {
|
|
34
|
-
/** The endpoint
|
|
44
|
+
/** The endpoint, the count the pool actually holds, and its make jobserver fifo. */
|
|
35
45
|
readonly environment: Record<string, string>;
|
|
36
46
|
/**
|
|
37
|
-
* Refuses new grants, ends every child connection and removes the pool's
|
|
38
|
-
* cached: every call, and an upstream-loss close, returns the first
|
|
39
|
-
* cleanup is an `ok: false` value, never a rejection.
|
|
47
|
+
* Refuses new grants, ends every child connection, drains and closes the jobserver fifo, and removes the pool's
|
|
48
|
+
* directory with the fifo in it. The outcome is cached: every call, and an upstream-loss close, returns the first
|
|
49
|
+
* close's outcome. A failed cleanup is an `ok: false` value, never a rejection.
|
|
40
50
|
*/
|
|
41
51
|
close(): Promise<CloseOutcome>;
|
|
42
52
|
}
|
|
@@ -57,13 +67,8 @@ export interface HostWaits {
|
|
|
57
67
|
interface CpuPoolRequest {
|
|
58
68
|
op: 'cpu-pool';
|
|
59
69
|
want: number & typia.tags.Type<'uint32'> & typia.tags.Minimum<1>;
|
|
60
|
-
/** Ask the owning pool to serve this allocation as its own child pool
|
|
70
|
+
/** Ask the owning pool to serve this allocation as its own child pool, with a jobserver fifo of its own. */
|
|
61
71
|
children?: boolean;
|
|
62
|
-
/**
|
|
63
|
-
* Cargo's launcher asks a compile-only build to grow. This pool lends a fixed count, so it accepts the field and never
|
|
64
|
-
* grows a child; the gateway's pools do (specs/cowshed/05_gateway.md, "A short build grows").
|
|
65
|
-
*/
|
|
66
|
-
grow?: boolean;
|
|
67
72
|
}
|
|
68
73
|
|
|
69
74
|
/**
|
|
@@ -81,7 +86,8 @@ type CpuPoolReply =
|
|
|
81
86
|
| { ok: true; wait: 'held' }
|
|
82
87
|
| { ok: false; code: 'invalid-request'; error: string };
|
|
83
88
|
|
|
84
|
-
|
|
89
|
+
/** Exactly a request's fields, as the gateway's own pools read them: any other field is an invalid request. */
|
|
90
|
+
const isRequest = typia.createEquals<CpuPoolRequest | HostWaitRequest>();
|
|
85
91
|
const stringifyReply = typia.json.createStringify<CpuPoolReply>();
|
|
86
92
|
|
|
87
93
|
/** A request over this many bytes is refused as `invalid-request` (specs/cowshed/05_gateway.md, "Wire"). */
|
|
@@ -92,14 +98,22 @@ const TOO_LONG = `a request is one line of at most ${MAX_REQUEST_BYTES} bytes`;
|
|
|
92
98
|
const EMPTY: Buffer = Buffer.alloc(0);
|
|
93
99
|
const POOL_DIRECTORY = 'cpu-pool-';
|
|
94
100
|
const POOL_SOCKET = 'p.sock';
|
|
101
|
+
/** The jobserver fifo's name beside the socket; the directory's random name is what makes the path unguessable. */
|
|
102
|
+
const POOL_FIFO = 'j';
|
|
103
|
+
/** The byte make's protocol writes for a token. Any byte read is one token; the pool writes this one back. */
|
|
104
|
+
const TOKEN = 0x2b;
|
|
95
105
|
const CLOSED_WHILE_MADE = 'the grant that lends this CPU pool closed while the pool was being made';
|
|
106
|
+
const run = promisify(execFile);
|
|
96
107
|
|
|
97
|
-
/**
|
|
98
|
-
*
|
|
108
|
+
/**
|
|
109
|
+
* A child's phase. Tokens are owed back only while `holding`: the bytes it took from the jobserver fifo, and whether it
|
|
110
|
+
* holds the pool's implicit slot. A queued child holds nothing; a `waiting` child holds a host wait on the run's idle
|
|
111
|
+
* clock.
|
|
112
|
+
*/
|
|
99
113
|
type Phase =
|
|
100
114
|
| { readonly kind: 'reading' }
|
|
101
115
|
| { readonly kind: 'queued' }
|
|
102
|
-
| { readonly kind: 'holding'; readonly
|
|
116
|
+
| { readonly kind: 'holding'; readonly bytes: number; readonly slot: boolean }
|
|
103
117
|
| { readonly kind: 'waiting' }
|
|
104
118
|
| { readonly kind: 'done' };
|
|
105
119
|
|
|
@@ -175,11 +189,159 @@ export async function takeParentCpuTokens(parent: InheritedCpuPool, want: number
|
|
|
175
189
|
return { granted: true, tokens, release: lease.release, closed: lease.closed, inherited: true };
|
|
176
190
|
}
|
|
177
191
|
|
|
192
|
+
/** A thrown value's message. */
|
|
193
|
+
const describe = (error: unknown): string => (error instanceof Error ? error.message : String(error));
|
|
194
|
+
|
|
195
|
+
/** Write `count` token bytes to the fifo's descriptor; a short write throws. The fifo holds a pipe buffer of them. */
|
|
196
|
+
function writeTokens(fd: number, count: number): void {
|
|
197
|
+
if (count === 0) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const written = writeSync(fd, Buffer.alloc(count, TOKEN));
|
|
201
|
+
if (written !== count) {
|
|
202
|
+
throw new Error(`it took ${written} of ${count} token bytes`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Up to `most` token bytes read from the fifo's non-blocking descriptor: 0 when it is empty. */
|
|
207
|
+
function readTokens(fd: number, into: Buffer, most: number): number {
|
|
208
|
+
try {
|
|
209
|
+
return readSync(fd, into, 0, Math.min(most, into.length), null);
|
|
210
|
+
} catch (error) {
|
|
211
|
+
const code = error instanceof Error && 'code' in error ? error.code : undefined;
|
|
212
|
+
if (code === 'EAGAIN' || code === 'EWOULDBLOCK') {
|
|
213
|
+
return 0;
|
|
214
|
+
}
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Make the jobserver fifo at `path`, mode 0600, holding `bytes` tokens. Node has no mkfifo, so the system's `mkfifo`
|
|
221
|
+
* makes it. The pool keeps it open read-write and non-blocking: the fifo always has a writer, so its bytes outlive
|
|
222
|
+
* every client that opens and closes it, and the pool's own reads and writes never block.
|
|
223
|
+
*/
|
|
224
|
+
async function makeJobserverFifo(
|
|
225
|
+
path: string,
|
|
226
|
+
bytes: number,
|
|
227
|
+
): Promise<{ ok: true; fd: number } | { ok: false; error: string }> {
|
|
228
|
+
try {
|
|
229
|
+
await run('mkfifo', ['-m', '600', path]);
|
|
230
|
+
} catch (error) {
|
|
231
|
+
return { ok: false, error: `the CPU pool's jobserver fifo could not be made: ${describe(error)}` };
|
|
232
|
+
}
|
|
233
|
+
let fd: number;
|
|
234
|
+
try {
|
|
235
|
+
fd = openSync(path, constants.O_RDWR | constants.O_NONBLOCK);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
return { ok: false, error: `the CPU pool's jobserver fifo could not be opened: ${describe(error)}` };
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
writeTokens(fd, bytes);
|
|
241
|
+
} catch (error) {
|
|
242
|
+
const failure = `the CPU pool's jobserver fifo did not take its ${bytes} tokens: ${describe(error)}`;
|
|
243
|
+
try {
|
|
244
|
+
closeSync(fd);
|
|
245
|
+
} catch (closing) {
|
|
246
|
+
return { ok: false, error: `${failure}; its descriptor did not close: ${describe(closing)}` };
|
|
247
|
+
}
|
|
248
|
+
return { ok: false, error: failure };
|
|
249
|
+
}
|
|
250
|
+
return { ok: true, fd };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** The one Bun API the readability wait uses: a descriptor read as Bun's own poll-driven byte stream. */
|
|
254
|
+
interface BunFileStreams {
|
|
255
|
+
file(fd: number): { stream(): ReadableStream<Uint8Array> };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function isBunFileStreams(runtime: unknown): runtime is BunFileStreams {
|
|
259
|
+
return typeof runtime === 'object' && runtime !== null && 'file' in runtime && typeof runtime.file === 'function';
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const bunRuntime: unknown = Reflect.get(globalThis, 'Bun');
|
|
263
|
+
/** Bun's file streams when this process runs on Bun, else null (Node). */
|
|
264
|
+
const bunFiles: BunFileStreams | null = isBunFileStreams(bunRuntime) ? bunRuntime : null;
|
|
265
|
+
|
|
266
|
+
/** A wait for the fifo to become readable, until `stop`. */
|
|
267
|
+
interface FifoWatch {
|
|
268
|
+
stop(): void;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Wait, event-driven, for token bytes in the fifo at `path`: no polling loop and no blocking read on the threadpool.
|
|
273
|
+
* A fresh non-blocking read descriptor is watched by the runtime's event loop. Every byte it reads is taken from the
|
|
274
|
+
* fifo and reaches `took` as tokens in hand, also after `stop`; `stop` leaves unread bytes in the fifo.
|
|
275
|
+
*
|
|
276
|
+
* Node watches the descriptor as a libuv pipe stream (`net.Socket({ fd })`). Bun's `node:net` never reads a fifo, and
|
|
277
|
+
* its `tty` and `fs` read streams leave a blocking read behind that takes the next token after they are destroyed
|
|
278
|
+
* (measured on Bun 1.4.3), so on Bun the wait reads through `Bun.file(fd).stream()`, which polls the descriptor and
|
|
279
|
+
* leaves the fifo untouched when cancelled.
|
|
280
|
+
*/
|
|
281
|
+
function watchFifo(
|
|
282
|
+
path: string,
|
|
283
|
+
took: (bytes: number) => void,
|
|
284
|
+
failed: (error: string) => void,
|
|
285
|
+
): { ok: true; watch: FifoWatch } | { ok: false; error: string } {
|
|
286
|
+
let fd: number;
|
|
287
|
+
try {
|
|
288
|
+
fd = openSync(path, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
289
|
+
} catch (error) {
|
|
290
|
+
return { ok: false, error: describe(error) };
|
|
291
|
+
}
|
|
292
|
+
if (bunFiles !== null) {
|
|
293
|
+
const reader = bunFiles.file(fd).stream().getReader();
|
|
294
|
+
let stopped = false;
|
|
295
|
+
const next = (): void => {
|
|
296
|
+
void reader.read().then(
|
|
297
|
+
(chunk) => {
|
|
298
|
+
if (chunk.done) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
took(chunk.value.byteLength);
|
|
302
|
+
if (!stopped) next();
|
|
303
|
+
},
|
|
304
|
+
(error: unknown) => {
|
|
305
|
+
if (!stopped) failed(describe(error));
|
|
306
|
+
},
|
|
307
|
+
);
|
|
308
|
+
};
|
|
309
|
+
next();
|
|
310
|
+
const release = (): void => {
|
|
311
|
+
try {
|
|
312
|
+
closeSync(fd);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
failed(`its watch descriptor did not close: ${describe(error)}`);
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
return {
|
|
318
|
+
ok: true,
|
|
319
|
+
watch: {
|
|
320
|
+
stop: () => {
|
|
321
|
+
stopped = true;
|
|
322
|
+
void reader.cancel().then(release, release);
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
let reader: Socket;
|
|
328
|
+
try {
|
|
329
|
+
reader = new Socket({ fd, readable: true, writable: false });
|
|
330
|
+
} catch (error) {
|
|
331
|
+
closeSync(fd);
|
|
332
|
+
return { ok: false, error: describe(error) };
|
|
333
|
+
}
|
|
334
|
+
reader.on('data', (chunk: Buffer) => took(chunk.length));
|
|
335
|
+
reader.on('error', (error) => failed(error.message));
|
|
336
|
+
// Destroying the stream closes its descriptor and stops its reads at once.
|
|
337
|
+
return { ok: true, watch: { stop: () => reader.destroy() } };
|
|
338
|
+
}
|
|
339
|
+
|
|
178
340
|
/**
|
|
179
341
|
* Make a pool of `tokens` that lives while `closed` does: when `closed` resolves the pool refuses
|
|
180
|
-
* grants, ends its child connections and removes its directory. A child's
|
|
181
|
-
* to `waits` for as long as its connection holds it; a sub-pool reports to the
|
|
182
|
-
* Setup failures are values; a non-positive or unsafe count is a programmer bug and throws.
|
|
342
|
+
* grants, ends its child connections, drains its jobserver fifo and removes its directory. A child's
|
|
343
|
+
* `host-wait` is reported to `waits` for as long as its connection holds it; a sub-pool reports to the
|
|
344
|
+
* same `waits`. Setup failures are values; a non-positive or unsafe count is a programmer bug and throws.
|
|
183
345
|
*/
|
|
184
346
|
export async function createCpuPool(
|
|
185
347
|
tokens: number,
|
|
@@ -193,13 +355,26 @@ export async function createCpuPool(
|
|
|
193
355
|
try {
|
|
194
356
|
directory = await ownedSocketDirectory(POOL_DIRECTORY, POOL_SOCKET);
|
|
195
357
|
} catch (error) {
|
|
196
|
-
return {
|
|
197
|
-
ok: false,
|
|
198
|
-
error: `the CPU pool's directory could not be made: ${error instanceof Error ? error.message : String(error)}`,
|
|
199
|
-
};
|
|
358
|
+
return { ok: false, error: `the CPU pool's directory could not be made: ${describe(error)}` };
|
|
200
359
|
}
|
|
201
360
|
const socketPath = join(directory, POOL_SOCKET);
|
|
202
|
-
|
|
361
|
+
const fifoPath = join(directory, POOL_FIFO);
|
|
362
|
+
const fifo = await makeJobserverFifo(fifoPath, tokens - 1);
|
|
363
|
+
if (!fifo.ok) {
|
|
364
|
+
try {
|
|
365
|
+
await rm(directory, { recursive: true, force: true });
|
|
366
|
+
} catch (error) {
|
|
367
|
+
return { ok: false, error: `${fifo.error}; its directory was not removed: ${describe(error)}` };
|
|
368
|
+
}
|
|
369
|
+
return { ok: false, error: fifo.error };
|
|
370
|
+
}
|
|
371
|
+
const jobserver = fifo.fd;
|
|
372
|
+
// The pool reads at most what it holds at once: the fifo carries no more of its tokens than that.
|
|
373
|
+
const scratch = Buffer.allocUnsafe(tokens);
|
|
374
|
+
// The holder's implicit first token, while a socket child holds it.
|
|
375
|
+
let slotLent = false;
|
|
376
|
+
// The readability wait while the queue's head finds no token, else undefined.
|
|
377
|
+
let watch: FifoWatch | undefined;
|
|
203
378
|
let closing = false;
|
|
204
379
|
let listening = false;
|
|
205
380
|
let shut: Promise<CloseOutcome> | undefined;
|
|
@@ -215,21 +390,77 @@ export async function createCpuPool(
|
|
|
215
390
|
resolveStarted = done;
|
|
216
391
|
});
|
|
217
392
|
|
|
218
|
-
//
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
393
|
+
// A fault in the pool's own fifo, said and recorded; the pool then closes, as on a listener failure.
|
|
394
|
+
const fail = (failure: string): void => {
|
|
395
|
+
failures.push(failure);
|
|
396
|
+
process.stderr.write(`bounded-exec: the CPU pool at ${socketPath} failed: ${failure}\n`);
|
|
397
|
+
closeInBackground();
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const returnTokens = (count: number): void => {
|
|
401
|
+
try {
|
|
402
|
+
writeTokens(jobserver, count);
|
|
403
|
+
} catch (error) {
|
|
404
|
+
fail(`its jobserver fifo did not take back ${count} tokens: ${describe(error)}`);
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
const stopWatching = (): void => {
|
|
409
|
+
watch?.stop();
|
|
410
|
+
watch = undefined;
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
// The head found no token: wake when a holder outside the pool, such as Cargo, writes one back.
|
|
414
|
+
const watchForTokens = (): void => {
|
|
415
|
+
if (watch !== undefined) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
const watching = watchFifo(fifoPath, drain, (error) =>
|
|
419
|
+
fail(`its jobserver fifo failed while a child waited for a token: ${error}`),
|
|
420
|
+
);
|
|
421
|
+
if (watching.ok) {
|
|
422
|
+
watch = watching.watch;
|
|
423
|
+
} else {
|
|
424
|
+
fail(`its jobserver fifo could not be watched for a token: ${watching.error}`);
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
// FIFO: the head takes `min(want, available)` as one grant once any token is available, where available is the
|
|
429
|
+
// fifo bytes it can read without blocking plus the implicit slot while no child holds it. `arrived` is bytes the
|
|
430
|
+
// readability wait already took. A waiting child holds nothing, so no child waits while it holds tokens another
|
|
431
|
+
// child needs. Whatever bytes are in hand when no head can use them go straight back to the fifo.
|
|
432
|
+
function drain(arrived: number): void {
|
|
433
|
+
let inHand = arrived;
|
|
434
|
+
while (!closing) {
|
|
435
|
+
const waiter = queue[0];
|
|
223
436
|
if (waiter === undefined) {
|
|
224
|
-
|
|
437
|
+
break;
|
|
225
438
|
}
|
|
226
439
|
if (!waiter.child.socket.writable) {
|
|
227
440
|
// A child that half-closed or failed while queued. Its `close` finds it gone from the queue.
|
|
441
|
+
queue.shift();
|
|
228
442
|
continue;
|
|
229
443
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
444
|
+
if (inHand < waiter.want) {
|
|
445
|
+
try {
|
|
446
|
+
inHand += readTokens(jobserver, scratch, waiter.want - inHand);
|
|
447
|
+
} catch (error) {
|
|
448
|
+
fail(`its jobserver fifo could not be read: ${describe(error)}`);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
const granted = Math.min(waiter.want, inHand + (slotLent ? 0 : 1));
|
|
453
|
+
if (granted === 0) {
|
|
454
|
+
watchForTokens();
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
queue.shift();
|
|
458
|
+
// Fifo bytes first, the implicit slot last.
|
|
459
|
+
const bytes = Math.min(granted, inHand);
|
|
460
|
+
inHand -= bytes;
|
|
461
|
+
const slot = granted > bytes;
|
|
462
|
+
if (slot) slotLent = true;
|
|
463
|
+
waiter.child.phase = { kind: 'holding', bytes, slot };
|
|
233
464
|
if (!waiter.children) {
|
|
234
465
|
waiter.child.socket.write(frame({ ok: true, lease: 'granted', tokens: granted }));
|
|
235
466
|
continue;
|
|
@@ -259,7 +490,9 @@ export async function createCpuPool(
|
|
|
259
490
|
creating ??= [];
|
|
260
491
|
creating.push(creation);
|
|
261
492
|
}
|
|
262
|
-
|
|
493
|
+
stopWatching();
|
|
494
|
+
if (!closing) returnTokens(inHand);
|
|
495
|
+
}
|
|
263
496
|
|
|
264
497
|
const refuse = (child: Child, error: string): void => {
|
|
265
498
|
child.phase = { kind: 'done' };
|
|
@@ -284,12 +517,14 @@ export async function createCpuPool(
|
|
|
284
517
|
queue.splice(at, 1);
|
|
285
518
|
}
|
|
286
519
|
} else if (child.phase.kind === 'holding') {
|
|
287
|
-
|
|
520
|
+
// A closing pool drains its fifo whole; until then the bytes go back for the next holder.
|
|
521
|
+
if (child.phase.slot) slotLent = false;
|
|
522
|
+
if (!closing) returnTokens(child.phase.bytes);
|
|
288
523
|
} else if (child.phase.kind === 'waiting') {
|
|
289
524
|
waits.end();
|
|
290
525
|
}
|
|
291
526
|
child.phase = { kind: 'done' };
|
|
292
|
-
drain();
|
|
527
|
+
drain(0);
|
|
293
528
|
});
|
|
294
529
|
let buffered: Buffer = EMPTY;
|
|
295
530
|
// Refusing releases what the connection buffered: a refused request holds no storage.
|
|
@@ -330,21 +565,21 @@ export async function createCpuPool(
|
|
|
330
565
|
}
|
|
331
566
|
const line = buffered.subarray(0, newline).toString('utf8');
|
|
332
567
|
buffered = EMPTY;
|
|
333
|
-
let
|
|
568
|
+
let parsed: unknown;
|
|
334
569
|
try {
|
|
335
|
-
|
|
570
|
+
parsed = JSON.parse(line);
|
|
336
571
|
} catch (error) {
|
|
337
|
-
// isParse returns null for a wrong shape, but JSON syntax errors still throw.
|
|
338
572
|
if (!(error instanceof SyntaxError)) throw error;
|
|
339
|
-
|
|
573
|
+
parsed = undefined;
|
|
340
574
|
}
|
|
341
|
-
if (
|
|
575
|
+
if (!isRequest(parsed)) {
|
|
342
576
|
refuse(
|
|
343
577
|
child,
|
|
344
578
|
'a request is {"op":"cpu-pool","want":<a whole number of tokens, at least 1>} or {"op":"host-wait"}',
|
|
345
579
|
);
|
|
346
580
|
return;
|
|
347
581
|
}
|
|
582
|
+
const request = parsed;
|
|
348
583
|
if (request.op === 'host-wait') {
|
|
349
584
|
// Counted from here until the connection closes, which `close` reports however it ends.
|
|
350
585
|
child.phase = { kind: 'waiting' };
|
|
@@ -355,7 +590,7 @@ export async function createCpuPool(
|
|
|
355
590
|
child.phase = { kind: 'queued' };
|
|
356
591
|
queue.push({ child, want: request.want, children: request.children === true });
|
|
357
592
|
socket.write(frame({ ok: true, lease: 'queued' }));
|
|
358
|
-
drain();
|
|
593
|
+
drain(0);
|
|
359
594
|
});
|
|
360
595
|
};
|
|
361
596
|
|
|
@@ -366,6 +601,7 @@ export async function createCpuPool(
|
|
|
366
601
|
const shutdown = (): Promise<CloseOutcome> => {
|
|
367
602
|
shut ??= (async (): Promise<CloseOutcome> => {
|
|
368
603
|
closing = true;
|
|
604
|
+
stopWatching();
|
|
369
605
|
await started;
|
|
370
606
|
for (const socket of children) {
|
|
371
607
|
socket.destroy();
|
|
@@ -387,10 +623,23 @@ export async function createCpuPool(
|
|
|
387
623
|
if (!outcome.ok) failures.push(outcome.error);
|
|
388
624
|
}
|
|
389
625
|
}
|
|
626
|
+
// The fifo's bytes are drained before it goes: a Cargo that outlives the pool keeps its descriptor and the tokens
|
|
627
|
+
// it holds, and takes no more.
|
|
628
|
+
try {
|
|
629
|
+
let drained = readTokens(jobserver, scratch, scratch.length);
|
|
630
|
+
while (drained > 0) drained = readTokens(jobserver, scratch, scratch.length);
|
|
631
|
+
} catch (error) {
|
|
632
|
+
failures.push(`its jobserver fifo was not drained: ${describe(error)}`);
|
|
633
|
+
}
|
|
634
|
+
try {
|
|
635
|
+
closeSync(jobserver);
|
|
636
|
+
} catch (error) {
|
|
637
|
+
failures.push(`its jobserver fifo did not close: ${describe(error)}`);
|
|
638
|
+
}
|
|
390
639
|
try {
|
|
391
640
|
await rm(directory, { recursive: true, force: true });
|
|
392
641
|
} catch (error) {
|
|
393
|
-
failures.push(`its directory was not removed: ${
|
|
642
|
+
failures.push(`its directory was not removed: ${describe(error)}`);
|
|
394
643
|
}
|
|
395
644
|
return failures.length === 0
|
|
396
645
|
? { ok: true }
|
|
@@ -444,17 +693,22 @@ export async function createCpuPool(
|
|
|
444
693
|
// denies every other user the path.
|
|
445
694
|
await chmod(socketPath, 0o600);
|
|
446
695
|
} catch (error) {
|
|
447
|
-
return abort(
|
|
448
|
-
`the CPU pool's socket could not be made private: ${error instanceof Error ? error.message : String(error)}`,
|
|
449
|
-
);
|
|
696
|
+
return abort(`the CPU pool's socket could not be made private: ${describe(error)}`);
|
|
450
697
|
}
|
|
451
698
|
if (closing) {
|
|
452
699
|
return abort(CLOSED_WHILE_MADE);
|
|
453
700
|
}
|
|
701
|
+
// Cargo and make read the fifo from here; `-j` with a jobserver names no count of its own.
|
|
702
|
+
const jobserverFlags = `-j --jobserver-auth=fifo:${fifoPath}`;
|
|
454
703
|
return {
|
|
455
704
|
ok: true,
|
|
456
705
|
pool: {
|
|
457
|
-
environment: {
|
|
706
|
+
environment: {
|
|
707
|
+
[CPU_POOL_ENV]: socketPath,
|
|
708
|
+
[CPU_TOKENS_ENV]: String(tokens),
|
|
709
|
+
CARGO_MAKEFLAGS: jobserverFlags,
|
|
710
|
+
MAKEFLAGS: jobserverFlags,
|
|
711
|
+
},
|
|
458
712
|
close: shutdown,
|
|
459
713
|
},
|
|
460
714
|
};
|
|
@@ -46,6 +46,9 @@ const NEXTEST_PIECE =
|
|
|
46
46
|
`extracted="$(node node_modules/@smoothbricks/nx-plugin/dist/bin/smoo-nx-nextest-extract.js .cache/nextest/archive.tar.zst)" && ` +
|
|
47
47
|
`cargo --frozen nextest run --binaries-metadata "$extracted/m.json" --workspace-remap . -E 'package(core)' --partition hash:1/5 --no-tests=pass`;
|
|
48
48
|
|
|
49
|
+
/** The test-thread ceilings of a run whose environment names none. */
|
|
50
|
+
const NO_CEILINGS = { rust: null, nextest: null };
|
|
51
|
+
|
|
49
52
|
describe('cowshed CPU budget client', () => {
|
|
50
53
|
it('asks for what each runner would run at once', () => {
|
|
51
54
|
expect(runnerDemand(NEXTEST_PIECE, undefined, 18, {})).toEqual({ kind: 'nextest', want: 18 });
|
|
@@ -70,7 +73,7 @@ describe('cowshed CPU budget client', () => {
|
|
|
70
73
|
expect(runnerDemand('cargo test --workspace -- --nocapture', undefined, 18, {})).toEqual({
|
|
71
74
|
kind: 'cargo',
|
|
72
75
|
want: 18,
|
|
73
|
-
testThreads:
|
|
76
|
+
testThreads: NO_CEILINGS,
|
|
74
77
|
});
|
|
75
78
|
expect(runnerDemand('bun test --timeout=0 --parallel=3', undefined, 18, {})).toEqual({
|
|
76
79
|
kind: 'bun-parallel',
|
|
@@ -89,20 +92,29 @@ describe('cowshed CPU budget client', () => {
|
|
|
89
92
|
expect(runnerDemand('bun test --timeout=0 native.test.ts', undefined, 18, {})).toEqual({
|
|
90
93
|
kind: 'process',
|
|
91
94
|
want: 1,
|
|
95
|
+
testThreads: NO_CEILINGS,
|
|
92
96
|
});
|
|
93
97
|
expect(runnerDemand('cargo --frozen test --workspace', undefined, 18, {})).toEqual({
|
|
94
98
|
kind: 'cargo',
|
|
95
99
|
want: 18,
|
|
96
|
-
testThreads:
|
|
100
|
+
testThreads: NO_CEILINGS,
|
|
97
101
|
});
|
|
98
102
|
expect(runnerDemand('napi build --platform --release', undefined, 8, {})).toEqual({
|
|
99
103
|
kind: 'cargo',
|
|
100
104
|
want: 8,
|
|
101
|
-
testThreads:
|
|
105
|
+
testThreads: NO_CEILINGS,
|
|
106
|
+
});
|
|
107
|
+
expect(runnerDemand('bun scripts/test-shard.ts 3', undefined, 18, {})).toEqual({
|
|
108
|
+
kind: 'process',
|
|
109
|
+
want: 1,
|
|
110
|
+
testThreads: NO_CEILINGS,
|
|
102
111
|
});
|
|
103
|
-
expect(runnerDemand('bun scripts/test-shard.ts 3', undefined, 18, {})).toEqual({ kind: 'process', want: 1 });
|
|
104
112
|
// A declared parallelism outranks what the command names, within inherited ceilings.
|
|
105
|
-
expect(runnerDemand('bun scripts/test-shard.ts 3', 2, 18, {})).toEqual({
|
|
113
|
+
expect(runnerDemand('bun scripts/test-shard.ts 3', 2, 18, {})).toEqual({
|
|
114
|
+
kind: 'process',
|
|
115
|
+
want: 2,
|
|
116
|
+
testThreads: NO_CEILINGS,
|
|
117
|
+
});
|
|
106
118
|
expect(runnerDemand('cargo nextest run --test-threads=4', 6, 18, {})).toEqual({ kind: 'nextest', want: 6 });
|
|
107
119
|
});
|
|
108
120
|
|
|
@@ -132,7 +144,11 @@ describe('cowshed CPU budget client', () => {
|
|
|
132
144
|
// A declared parallelism outranks the flag.
|
|
133
145
|
expect(runnerDemand('bun stryker run --concurrency 8', 2, 18, {})).toEqual({ kind: 'stryker', want: 2 });
|
|
134
146
|
// A test file named for Stryker is one bun process.
|
|
135
|
-
expect(runnerDemand('bun test stryker.test.ts', undefined, 18, {})).toEqual({
|
|
147
|
+
expect(runnerDemand('bun test stryker.test.ts', undefined, 18, {})).toEqual({
|
|
148
|
+
kind: 'process',
|
|
149
|
+
want: 1,
|
|
150
|
+
testThreads: NO_CEILINGS,
|
|
151
|
+
});
|
|
136
152
|
});
|
|
137
153
|
|
|
138
154
|
it("asks for Vitest's --maxWorkers and go test's -p, else each runner's own per-core default", () => {
|
|
@@ -226,7 +242,7 @@ describe('cowshed CPU budget client', () => {
|
|
|
226
242
|
expect(runnerDemand('cargo test -p mutants', undefined, 18, {})).toEqual({
|
|
227
243
|
kind: 'cargo',
|
|
228
244
|
want: 18,
|
|
229
|
-
testThreads:
|
|
245
|
+
testThreads: NO_CEILINGS,
|
|
230
246
|
});
|
|
231
247
|
});
|
|
232
248
|
|
|
@@ -308,14 +324,41 @@ describe('cowshed CPU budget client', () => {
|
|
|
308
324
|
expect(sizeRunner({ kind: 'bun-parallel', want: 18 }, 'bun test --parallel', 7).command).toBe(
|
|
309
325
|
'bun test --parallel=7',
|
|
310
326
|
);
|
|
311
|
-
|
|
327
|
+
// Cargo's rustc count is its pool's jobserver fifo, never a `CARGO_BUILD_JOBS` from the grant; its test threads
|
|
328
|
+
// take no token, so they are sized here, libtest's and nextest's alike.
|
|
329
|
+
expect(sizeRunner({ kind: 'cargo', want: 18, testThreads: NO_CEILINGS }, 'cargo test', 4).env).toEqual({
|
|
312
330
|
[CPU_TOKENS_ENV]: '4',
|
|
313
|
-
CARGO_BUILD_JOBS: '4',
|
|
314
331
|
RUST_TEST_THREADS: '4',
|
|
332
|
+
NEXTEST_TEST_THREADS: '4',
|
|
333
|
+
});
|
|
334
|
+
expect(sizeRunner({ kind: 'process', want: 1, testThreads: NO_CEILINGS }, 'bun scripts/x.ts', 1)).toEqual({
|
|
335
|
+
command: 'bun scripts/x.ts',
|
|
336
|
+
env: { [CPU_TOKENS_ENV]: '1', RUST_TEST_THREADS: '1', NEXTEST_TEST_THREADS: '1' },
|
|
315
337
|
});
|
|
316
|
-
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it("sizes a process's test threads to its held grant, under inherited and configured ceilings", () => {
|
|
341
|
+
// A script's own `cargo test` takes rustc tokens from the executor's jobserver fifo, but its test threads take
|
|
342
|
+
// none, so the executor sets them to the whole grant the process holds.
|
|
343
|
+
const unbounded = runnerDemand('bun scripts/x.ts', 5, 18, {});
|
|
344
|
+
expect(unbounded).toEqual({ kind: 'process', want: 5, testThreads: NO_CEILINGS });
|
|
345
|
+
expect(sizeRunner(unbounded, 'bun scripts/x.ts', 5)).toEqual({
|
|
317
346
|
command: 'bun scripts/x.ts',
|
|
318
|
-
env: { [CPU_TOKENS_ENV]: '
|
|
347
|
+
env: { [CPU_TOKENS_ENV]: '5', RUST_TEST_THREADS: '5', NEXTEST_TEST_THREADS: '5' },
|
|
348
|
+
});
|
|
349
|
+
const capped = runnerDemand(
|
|
350
|
+
'bun scripts/x.ts',
|
|
351
|
+
5,
|
|
352
|
+
18,
|
|
353
|
+
{ RUST_TEST_THREADS: '4', NEXTEST_TEST_THREADS: '3' },
|
|
354
|
+
{ RUST_TEST_THREADS: '2', NEXTEST_TEST_THREADS: '64' },
|
|
355
|
+
);
|
|
356
|
+
// A configured count lowers an inherited ceiling and never raises it.
|
|
357
|
+
expect(capped).toEqual({ kind: 'process', want: 5, testThreads: { rust: 2, nextest: 3 } });
|
|
358
|
+
expect(sizeRunner(capped, 'bun scripts/x.ts', 5).env).toEqual({
|
|
359
|
+
[CPU_TOKENS_ENV]: '5',
|
|
360
|
+
RUST_TEST_THREADS: '2',
|
|
361
|
+
NEXTEST_TEST_THREADS: '3',
|
|
319
362
|
});
|
|
320
363
|
});
|
|
321
364
|
|
|
@@ -325,7 +368,7 @@ describe('cowshed CPU budget client', () => {
|
|
|
325
368
|
const nextest = runnerDemand('cargo nextest run --test-threads=18', 18, 18, inherited, configured);
|
|
326
369
|
const cargo = runnerDemand('cargo test', 18, 18, inherited, configured);
|
|
327
370
|
expect(nextest).toEqual({ kind: 'nextest', want: 6 });
|
|
328
|
-
expect(cargo).toEqual({ kind: 'cargo', want: 4, testThreads: 2 });
|
|
371
|
+
expect(cargo).toEqual({ kind: 'cargo', want: 4, testThreads: { rust: 2, nextest: 6 } });
|
|
329
372
|
for (const tokens of [15, 18]) {
|
|
330
373
|
expect(sizeRunner(nextest, 'cargo nextest run --test-threads=18', tokens)).toEqual({
|
|
331
374
|
command: 'cargo nextest run --test-threads=6',
|
|
@@ -333,15 +376,15 @@ describe('cowshed CPU budget client', () => {
|
|
|
333
376
|
});
|
|
334
377
|
expect(sizeRunner(cargo, 'cargo test', tokens).env).toEqual({
|
|
335
378
|
[CPU_TOKENS_ENV]: String(tokens),
|
|
336
|
-
CARGO_BUILD_JOBS: '4',
|
|
337
379
|
RUST_TEST_THREADS: '2',
|
|
380
|
+
NEXTEST_TEST_THREADS: '4',
|
|
338
381
|
});
|
|
339
382
|
}
|
|
340
383
|
expect(sizeRunner(nextest, NEXTEST_PIECE, 3).env.NEXTEST_TEST_THREADS).toBe('3');
|
|
341
384
|
expect(sizeRunner(cargo, 'cargo test', 3).env).toEqual({
|
|
342
385
|
[CPU_TOKENS_ENV]: '3',
|
|
343
|
-
CARGO_BUILD_JOBS: '3',
|
|
344
386
|
RUST_TEST_THREADS: '2',
|
|
387
|
+
NEXTEST_TEST_THREADS: '3',
|
|
345
388
|
});
|
|
346
389
|
expect(runnerDemand(NEXTEST_PIECE, 18, 18, inherited, { NEXTEST_TEST_THREADS: '2' }).want).toBe(2);
|
|
347
390
|
expect(runnerDemand('cargo build', 18, 18, {}, { CARGO_BUILD_JOBS: '4' }).want).toBe(4);
|
|
@@ -360,7 +403,9 @@ describe('cowshed CPU budget client', () => {
|
|
|
360
403
|
|
|
361
404
|
it('reads back the count a runner was sized to', () => {
|
|
362
405
|
expect(
|
|
363
|
-
parseInheritedTokens(
|
|
406
|
+
parseInheritedTokens(
|
|
407
|
+
sizeRunner({ kind: 'process', want: 5, testThreads: NO_CEILINGS }, 'bun scripts/x.ts', 5).env[CPU_TOKENS_ENV],
|
|
408
|
+
),
|
|
364
409
|
).toBe(5);
|
|
365
410
|
});
|
|
366
411
|
|