@smoothbricks/nx-plugin 0.7.4 → 0.7.6
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 +59 -30
- package/dist/.build-inputs.json +15 -8
- package/dist/executors/bounded-exec/cpu-pool-thread.d.ts +3 -2
- package/dist/executors/bounded-exec/cpu-pool-thread.d.ts.map +1 -1
- package/dist/executors/bounded-exec/cpu-pool-thread.js +1 -1
- package/dist/executors/bounded-exec/cpu-pool.d.ts +24 -10
- package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
- package/dist/executors/bounded-exec/cpu-pool.js +96 -52
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +192 -82
- package/dist/executors/bounded-exec/host-memory.d.ts +47 -0
- package/dist/executors/bounded-exec/host-memory.d.ts.map +1 -0
- package/dist/executors/bounded-exec/host-memory.darwin.d.ts +3 -0
- package/dist/executors/bounded-exec/host-memory.darwin.d.ts.map +1 -0
- package/dist/executors/bounded-exec/host-memory.darwin.js +40 -0
- package/dist/executors/bounded-exec/host-memory.js +29 -0
- package/dist/executors/bounded-exec/host-memory.linux.d.ts +14 -0
- package/dist/executors/bounded-exec/host-memory.linux.d.ts.map +1 -0
- package/dist/executors/bounded-exec/host-memory.linux.js +104 -0
- package/dist/executors/bounded-exec/leaf-peak.d.ts +55 -0
- package/dist/executors/bounded-exec/leaf-peak.d.ts.map +1 -0
- package/dist/executors/bounded-exec/leaf-peak.js +146 -0
- package/dist/executors/bounded-exec/mbx-pool.d.ts +17 -9
- package/dist/executors/bounded-exec/mbx-pool.d.ts.map +1 -1
- package/dist/executors/bounded-exec/mbx-pool.js +29 -11
- package/dist/executors/bounded-exec/mbx-reservations.d.ts +41 -0
- package/dist/executors/bounded-exec/mbx-reservations.d.ts.map +1 -0
- package/dist/executors/bounded-exec/mbx-reservations.js +372 -0
- package/dist/executors/bounded-exec/peak-waiter.d.ts +2 -0
- package/dist/executors/bounded-exec/peak-waiter.d.ts.map +1 -0
- package/dist/executors/bounded-exec/peak-waiter.js +53 -0
- package/dist/executors/bounded-exec/pool-admission.d.ts +38 -0
- package/dist/executors/bounded-exec/pool-admission.d.ts.map +1 -0
- package/dist/executors/bounded-exec/pool-admission.js +31 -0
- package/dist/executors/bounded-exec/run-cpu-pool.d.ts +6 -4
- package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
- package/dist/executors/bounded-exec/run-cpu-pool.js +20 -7
- package/dist/executors/bounded-exec/runner-sizing.d.ts +5 -5
- package/dist/executors/bounded-exec/runner-sizing.d.ts.map +1 -1
- package/dist/executors/bounded-exec/runner-sizing.js +6 -6
- package/dist/executors/bounded-exec/verdict.d.ts +21 -2
- package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
- package/dist/executors/bounded-exec/verdict.js +27 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/package.json +1 -1
- package/src/executors/bounded-exec/cpu-pool-thread.ts +4 -3
- package/src/executors/bounded-exec/cpu-pool.ts +111 -49
- package/src/executors/bounded-exec/executor.ts +202 -77
- package/src/executors/bounded-exec/host-memory.darwin.ts +44 -0
- package/src/executors/bounded-exec/host-memory.linux.ts +115 -0
- package/src/executors/bounded-exec/host-memory.ts +70 -0
- package/src/executors/bounded-exec/leaf-peak.ts +151 -0
- package/src/executors/bounded-exec/mbx-pool.ts +33 -13
- package/src/executors/bounded-exec/mbx-reservations.ts +300 -0
- package/src/executors/bounded-exec/peak-waiter.ts +82 -0
- package/src/executors/bounded-exec/pool-admission.ts +64 -0
- package/src/executors/bounded-exec/run-cpu-pool.ts +20 -6
- package/src/executors/bounded-exec/runner-sizing.ts +6 -6
- package/src/executors/bounded-exec/verdict.ts +40 -6
- package/src/index.ts +4 -1
- package/test-timings.json +134 -131
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host-memory.linux.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/host-memory.linux.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAA6B,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAchG,yEAAyE;AACzE,MAAM,MAAM,QAAQ,GAChB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC5C;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AA2B5E,2DAA2D;AAC3D,wBAAsB,cAAc,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,gBAAgB,CAAC,CA8CzG;AAqBD,eAAO,MAAM,WAAW,EAAE,UAAyE,CAAC"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { posix } from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* Linux's memory mechanisms. The run's memory is the least of the host's `MemTotal` and every finite `memory.max` of
|
|
5
|
+
* the cgroup v2 the run's process is in and of its ancestors, up to the root of the mounted hierarchy: the kernel holds
|
|
6
|
+
* a cgroup to its ancestors' limits too, so a job in cowshed's `background` class, whose own `memory.max` is `max`, has
|
|
7
|
+
* the class's three quarters of the host. A host with no cgroup v2 hierarchy, or a hierarchy that sets no limit, gives
|
|
8
|
+
* the run `MemTotal`.
|
|
9
|
+
*/
|
|
10
|
+
const MEMINFO = '/proc/meminfo';
|
|
11
|
+
const CGROUP = '/proc/self/cgroup';
|
|
12
|
+
const MOUNTINFO = '/proc/self/mountinfo';
|
|
13
|
+
const failed = (code, subject, cause) => ({
|
|
14
|
+
ok: false,
|
|
15
|
+
error: { operation: 'read-run-memory', code, subject, cause },
|
|
16
|
+
});
|
|
17
|
+
const unreadable = (subject, read) => failed('unreadable', subject, `${read.code}: ${read.message}`);
|
|
18
|
+
/** A mount point or root as mountinfo writes it, its space, tab, newline and backslash escaped in octal. */
|
|
19
|
+
const decodeOctal = (field) => field.replace(/\\([0-7]{3})/g, (_, octal) => String.fromCharCode(Number.parseInt(octal, 8)));
|
|
20
|
+
/** The cgroup v2 mounts mountinfo lists: the cgroup each shows (`root`) and where (`point`). */
|
|
21
|
+
function cgroup2Mounts(mountinfo) {
|
|
22
|
+
const mounts = [];
|
|
23
|
+
for (const line of mountinfo.split('\n')) {
|
|
24
|
+
const fields = line.split(' ');
|
|
25
|
+
const separator = fields.indexOf('-');
|
|
26
|
+
const [root, point] = [fields[3], fields[4]];
|
|
27
|
+
if (separator !== -1 && fields[separator + 1] === 'cgroup2' && root !== undefined && point !== undefined) {
|
|
28
|
+
mounts.push({ root: decodeOctal(root), point: decodeOctal(point) });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return mounts;
|
|
32
|
+
}
|
|
33
|
+
/** The run's memory, reading every file through `read`. */
|
|
34
|
+
export async function linuxRunMemory(read) {
|
|
35
|
+
const meminfo = await read(MEMINFO);
|
|
36
|
+
if (!meminfo.ok)
|
|
37
|
+
return unreadable(MEMINFO, meminfo);
|
|
38
|
+
const total = /^MemTotal:\s+(\d+) kB$/m.exec(meminfo.text)?.[1];
|
|
39
|
+
if (total === undefined)
|
|
40
|
+
return failed('malformed', MEMINFO, 'it names no MemTotal in kB');
|
|
41
|
+
let least = { bytes: Number(total) * 1024, source: `${MEMINFO} MemTotal` };
|
|
42
|
+
const cgroups = await read(CGROUP);
|
|
43
|
+
if (!cgroups.ok)
|
|
44
|
+
return unreadable(CGROUP, cgroups);
|
|
45
|
+
const path = /^0::(\/.*)$/m.exec(cgroups.text)?.[1];
|
|
46
|
+
if (path === undefined) {
|
|
47
|
+
// No cgroup v2 hierarchy holds the run: the host's memory is its memory.
|
|
48
|
+
return { ok: true, memory: least };
|
|
49
|
+
}
|
|
50
|
+
const mounts = await read(MOUNTINFO);
|
|
51
|
+
if (!mounts.ok)
|
|
52
|
+
return unreadable(MOUNTINFO, mounts);
|
|
53
|
+
const mount = cgroup2Mounts(mounts.text).find(({ root }) => root === '/' || path === root || path.startsWith(`${root}/`));
|
|
54
|
+
if (mount === undefined) {
|
|
55
|
+
return failed('unmounted', path, `${MOUNTINFO} lists no cgroup2 mount that shows the run's cgroup`);
|
|
56
|
+
}
|
|
57
|
+
const top = posix.resolve(mount.point);
|
|
58
|
+
const own = posix.resolve(top, `.${mount.root === '/' ? path : path.slice(mount.root.length)}`);
|
|
59
|
+
if (own !== top && !own.startsWith(`${top}/`)) {
|
|
60
|
+
return failed('unmounted', path, `the run's cgroup lies outside the cgroup2 mount at ${top}`);
|
|
61
|
+
}
|
|
62
|
+
for (let directory = own;;) {
|
|
63
|
+
const file = posix.join(directory, 'memory.max');
|
|
64
|
+
const limit = await read(file);
|
|
65
|
+
if (limit.ok) {
|
|
66
|
+
const value = limit.text.trim();
|
|
67
|
+
if (value !== 'max') {
|
|
68
|
+
if (!/^\d+$/.test(value)) {
|
|
69
|
+
return failed('malformed', file, `it holds ${JSON.stringify(value)}, not max or bytes`);
|
|
70
|
+
}
|
|
71
|
+
// A limit above the host's memory (a near-2^63 one included) is no constraint, and is never made a number.
|
|
72
|
+
if (BigInt(value) < BigInt(least.bytes))
|
|
73
|
+
least = { bytes: Number(value), source: file };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (limit.code !== 'ENOENT') {
|
|
77
|
+
// ENOENT: the hierarchy's root, or a cgroup whose parent enables no memory controller, has no limit of its own.
|
|
78
|
+
return unreadable(file, limit);
|
|
79
|
+
}
|
|
80
|
+
if (directory === top)
|
|
81
|
+
break;
|
|
82
|
+
directory = posix.dirname(directory);
|
|
83
|
+
}
|
|
84
|
+
return { ok: true, memory: least };
|
|
85
|
+
}
|
|
86
|
+
async function readText(path) {
|
|
87
|
+
try {
|
|
88
|
+
return { ok: true, text: await readFile(path, 'utf8') };
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
const code = error instanceof Error && 'code' in error && typeof error.code === 'string' ? error.code : 'EIO';
|
|
92
|
+
return { ok: false, code, message: error instanceof Error ? error.message : String(error) };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** Bun before 1.4.0 reports Linux's `ru_maxrss` as the kernel does, in KiB; from 1.4.0 it reports bytes. */
|
|
96
|
+
function peakBytes(maxRss, bunVersion) {
|
|
97
|
+
const version = /^(\d+)\.(\d+)\./.exec(bunVersion);
|
|
98
|
+
if (version === null) {
|
|
99
|
+
throw new Error(`Bun names its version ${JSON.stringify(bunVersion)}, which is no semver`);
|
|
100
|
+
}
|
|
101
|
+
const [major, minor] = [Number(version[1]), Number(version[2])];
|
|
102
|
+
return major < 1 || (major === 1 && minor < 4) ? maxRss * 1024 : maxRss;
|
|
103
|
+
}
|
|
104
|
+
export const linuxMemory = { readRunMemory: () => linuxRunMemory(readText), peakBytes };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { IOType } from 'node:child_process';
|
|
2
|
+
import type { Readable } from 'node:stream';
|
|
3
|
+
/**
|
|
4
|
+
* A leaf's memory peak (specs/cowshed/05_gateway.md, "Without mbx, every Nx run budgets itself"; 13_telemetry.md, "Job
|
|
5
|
+
* resource peaks"): the exited command's rusage `ru_maxrss`, which its waiter (`peak-waiter.ts`) reads. The leaf runs
|
|
6
|
+
* no cgroup of its own, even under a cowshed supervisor, whose subtree of the command is an observed fold inside the
|
|
7
|
+
* job's cgroup (07_api.md, `JobSubtreeHandle`), so the peak is the rusage on Linux and macOS alike. The executor writes
|
|
8
|
+
* it into the task's record, and the task's next run is charged it in its run's pool.
|
|
9
|
+
*/
|
|
10
|
+
/** The descriptor the waiter reports on: past the lifecycle channel (3), the subtree admission (4) and mbx's grant (5). */
|
|
11
|
+
export declare const PEAK_FD = 6;
|
|
12
|
+
/** A leaf's peak in bytes, or why it went unmeasured. */
|
|
13
|
+
export type LeafPeak = {
|
|
14
|
+
readonly kind: 'measured';
|
|
15
|
+
readonly bytes: number;
|
|
16
|
+
} | {
|
|
17
|
+
readonly kind: 'unmeasured';
|
|
18
|
+
readonly reason: string;
|
|
19
|
+
};
|
|
20
|
+
/** The Bun a leaf's waiter runs on, or why its peak goes unmeasured. */
|
|
21
|
+
export type PeakWaiter = {
|
|
22
|
+
readonly kind: 'waiter';
|
|
23
|
+
readonly runtime: string;
|
|
24
|
+
} | {
|
|
25
|
+
readonly kind: 'unmeasured';
|
|
26
|
+
readonly reason: string;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The Bun that runs this executor, else the first `bun` on the executor's own PATH, never one a target's environment
|
|
30
|
+
* names: the waiter is the executor's, not the command's.
|
|
31
|
+
*/
|
|
32
|
+
export declare function peakWaiter(): PeakWaiter;
|
|
33
|
+
/**
|
|
34
|
+
* The spawn of `file args` under the waiter on `runtime`. `descriptors` are the command's own past stderr, from fd 3:
|
|
35
|
+
* the waiter passes each one that is not ignored at its own number, and takes `PEAK_FD` for its report. The runtime's
|
|
36
|
+
* config is empty, so no preload a workspace's `bunfig.toml` names runs in the waiter.
|
|
37
|
+
*/
|
|
38
|
+
export declare function underWaiter(runtime: string, file: string, args: readonly string[], descriptors: readonly IOType[]): {
|
|
39
|
+
file: string;
|
|
40
|
+
args: string[];
|
|
41
|
+
stdio: IOType[];
|
|
42
|
+
};
|
|
43
|
+
/** What a leaf's waiter reports on its descriptor. */
|
|
44
|
+
export interface WaiterReport {
|
|
45
|
+
/**
|
|
46
|
+
* The command's pid once the waiter started it: the leaf's runner, a member of the group the waiter leads. Null when
|
|
47
|
+
* the waiter ended without naming one.
|
|
48
|
+
*/
|
|
49
|
+
readonly started: Promise<number | null>;
|
|
50
|
+
/** Once the waiter ends: the exited command's peak, or why it reported none. */
|
|
51
|
+
readonly peak: Promise<LeafPeak>;
|
|
52
|
+
}
|
|
53
|
+
/** What the waiter reports on `stream`: the command's pid as it starts it, and the command's peak once it ends. */
|
|
54
|
+
export declare function readWaiterReport(stream: Readable | null | undefined): WaiterReport;
|
|
55
|
+
//# sourceMappingURL=leaf-peak.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leaf-peak.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/leaf-peak.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAM5C;;;;;;GAMG;AAEH,2HAA2H;AAC3H,eAAO,MAAM,OAAO,IAAI,CAAC;AAiBzB,yDAAyD;AACzD,MAAM,MAAM,QAAQ,GAChB;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACrD;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE7D,wEAAwE;AACxE,MAAM,MAAM,UAAU,GAClB;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACrD;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE7D;;;GAGG;AACH,wBAAgB,UAAU,IAAI,UAAU,CASvC;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,WAAW,EAAE,SAAS,MAAM,EAAE,GAC7B;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,EAAE,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAanD;AAED,sDAAsD;AACtD,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACzC,gFAAgF;IAChF,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;CAClC;AAED,mHAAmH;AACnH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,QAAQ,GAAG,IAAI,GAAG,SAAS,GAAG,YAAY,CAuDlF"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import * as _isTypeUint32_1 from "typia/lib/internal/_isTypeUint32";
|
|
2
|
+
import * as _isTypeUint64_1 from "typia/lib/internal/_isTypeUint64";
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import typia from 'typia';
|
|
5
|
+
import { HOST_MEMORY } from './host-memory.js';
|
|
6
|
+
import { resolveOnPath } from './runtime-pin.js';
|
|
7
|
+
/**
|
|
8
|
+
* A leaf's memory peak (specs/cowshed/05_gateway.md, "Without mbx, every Nx run budgets itself"; 13_telemetry.md, "Job
|
|
9
|
+
* resource peaks"): the exited command's rusage `ru_maxrss`, which its waiter (`peak-waiter.ts`) reads. The leaf runs
|
|
10
|
+
* no cgroup of its own, even under a cowshed supervisor, whose subtree of the command is an observed fold inside the
|
|
11
|
+
* job's cgroup (07_api.md, `JobSubtreeHandle`), so the peak is the rusage on Linux and macOS alike. The executor writes
|
|
12
|
+
* it into the task's record, and the task's next run is charged it in its run's pool.
|
|
13
|
+
*/
|
|
14
|
+
/** The descriptor the waiter reports on: past the lifecycle channel (3), the subtree admission (4) and mbx's grant (5). */
|
|
15
|
+
export const PEAK_FD = 6;
|
|
16
|
+
/** The waiter's module, in the form this one was loaded in: compiled JavaScript under Nx, TypeScript under Bun. */
|
|
17
|
+
const WAITER = fileURLToPath(new URL(import.meta.url.endsWith('.ts') ? './peak-waiter.ts' : './peak-waiter.js', import.meta.url));
|
|
18
|
+
const isWaiterLine = (() => {
|
|
19
|
+
const _io0 = (input, _exceptionable = true) => "number" === typeof input.started && _isTypeUint32_1._isTypeUint32(input.started) && (1 === Object.keys(input).length || Object.keys(input).every(key => {
|
|
20
|
+
if (["started"].some(prop => key === prop))
|
|
21
|
+
return true;
|
|
22
|
+
const value = input[key];
|
|
23
|
+
if (undefined === value)
|
|
24
|
+
return true;
|
|
25
|
+
return false;
|
|
26
|
+
}));
|
|
27
|
+
const _io1 = (input, _exceptionable = true) => "number" === typeof input.peakBytes && _isTypeUint64_1._isTypeUint64(input.peakBytes) && (1 === Object.keys(input).length || Object.keys(input).every(key => {
|
|
28
|
+
if (["peakBytes"].some(prop => key === prop))
|
|
29
|
+
return true;
|
|
30
|
+
const value = input[key];
|
|
31
|
+
if (undefined === value)
|
|
32
|
+
return true;
|
|
33
|
+
return false;
|
|
34
|
+
}));
|
|
35
|
+
const _io2 = (input, _exceptionable = true) => "string" === typeof input.unmeasured && (1 === Object.keys(input).length || Object.keys(input).every(key => {
|
|
36
|
+
if (["unmeasured"].some(prop => key === prop))
|
|
37
|
+
return true;
|
|
38
|
+
const value = input[key];
|
|
39
|
+
if (undefined === value)
|
|
40
|
+
return true;
|
|
41
|
+
return false;
|
|
42
|
+
}));
|
|
43
|
+
const _iu0 = (input, _exceptionable = true) => (() => {
|
|
44
|
+
if (undefined !== input.started)
|
|
45
|
+
return _io0(input, true && _exceptionable);
|
|
46
|
+
else if (undefined !== input.peakBytes)
|
|
47
|
+
return _io1(input, true && _exceptionable);
|
|
48
|
+
else if (undefined !== input.unmeasured)
|
|
49
|
+
return _io2(input, true && _exceptionable);
|
|
50
|
+
else
|
|
51
|
+
return false;
|
|
52
|
+
})();
|
|
53
|
+
return (input, _exceptionable = true) => "object" === typeof input && null !== input && _iu0(input, true);
|
|
54
|
+
})();
|
|
55
|
+
/**
|
|
56
|
+
* The Bun that runs this executor, else the first `bun` on the executor's own PATH, never one a target's environment
|
|
57
|
+
* names: the waiter is the executor's, not the command's.
|
|
58
|
+
*/
|
|
59
|
+
export function peakWaiter() {
|
|
60
|
+
if (HOST_MEMORY === null) {
|
|
61
|
+
return { kind: 'unmeasured', reason: `${process.platform} has no reader of a leaf's peak` };
|
|
62
|
+
}
|
|
63
|
+
const runtime = process.versions.bun === undefined ? resolveOnPath('bun', process.env.PATH, process.cwd()) : process.execPath;
|
|
64
|
+
return runtime === null
|
|
65
|
+
? { kind: 'unmeasured', reason: 'no bun is on PATH to wait on the command and read its rusage' }
|
|
66
|
+
: { kind: 'waiter', runtime };
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The spawn of `file args` under the waiter on `runtime`. `descriptors` are the command's own past stderr, from fd 3:
|
|
70
|
+
* the waiter passes each one that is not ignored at its own number, and takes `PEAK_FD` for its report. The runtime's
|
|
71
|
+
* config is empty, so no preload a workspace's `bunfig.toml` names runs in the waiter.
|
|
72
|
+
*/
|
|
73
|
+
export function underWaiter(runtime, file, args, descriptors) {
|
|
74
|
+
if (descriptors.length > PEAK_FD - 3) {
|
|
75
|
+
throw new RangeError(`a leaf's own descriptors end before ${PEAK_FD}, the waiter's report; it names ${descriptors.length + 3}`);
|
|
76
|
+
}
|
|
77
|
+
const passed = descriptors.flatMap((io, at) => (io === 'ignore' ? [] : [at + 3]));
|
|
78
|
+
const padded = Array.from({ length: PEAK_FD - 3 }, (_, at) => descriptors[at] ?? 'ignore');
|
|
79
|
+
return {
|
|
80
|
+
file: runtime,
|
|
81
|
+
args: ['--config=/dev/null', WAITER, String(PEAK_FD), passed.join(','), file, ...args],
|
|
82
|
+
stdio: ['pipe', 'pipe', 'pipe', ...padded, 'pipe'],
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** What the waiter reports on `stream`: the command's pid as it starts it, and the command's peak once it ends. */
|
|
86
|
+
export function readWaiterReport(stream) {
|
|
87
|
+
if (stream === null || stream === undefined) {
|
|
88
|
+
return {
|
|
89
|
+
started: Promise.resolve(null),
|
|
90
|
+
peak: Promise.resolve({ kind: 'unmeasured', reason: 'the waiter was started without its report descriptor' }),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
// `Promise.withResolvers` would read better but needs lib es2024; this package inherits lib es2022 from
|
|
94
|
+
// tsconfig.base.json. The report's close names null at the latest.
|
|
95
|
+
let name;
|
|
96
|
+
const started = new Promise((named) => {
|
|
97
|
+
name = named;
|
|
98
|
+
});
|
|
99
|
+
// The waiter's last word: the line after its start, or what was wrong with a line.
|
|
100
|
+
let end = null;
|
|
101
|
+
const line = (text) => {
|
|
102
|
+
let parsed;
|
|
103
|
+
try {
|
|
104
|
+
parsed = JSON.parse(text);
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
if (!(error instanceof SyntaxError))
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
if (!isWaiterLine(parsed)) {
|
|
111
|
+
end = { kind: 'unmeasured', reason: `the waiter reported ${JSON.stringify(text)}, which is no peak` };
|
|
112
|
+
}
|
|
113
|
+
else if ('started' in parsed) {
|
|
114
|
+
name(parsed.started);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
end =
|
|
118
|
+
'peakBytes' in parsed
|
|
119
|
+
? { kind: 'measured', bytes: parsed.peakBytes }
|
|
120
|
+
: { kind: 'unmeasured', reason: parsed.unmeasured };
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
let pending = '';
|
|
124
|
+
stream.setEncoding('utf8');
|
|
125
|
+
// eslint-disable-next-line deadline/no-unbounded-wait -- WHY unbounded: the report pipe is the waiter's alone (Bun closes every descriptor it does not pass), so it ends when the waiter does, and the executor's bounds end the waiter
|
|
126
|
+
const peak = new Promise((settle) => {
|
|
127
|
+
stream.on('data', (chunk) => {
|
|
128
|
+
pending += chunk;
|
|
129
|
+
for (let at = pending.indexOf('\n'); at !== -1; at = pending.indexOf('\n')) {
|
|
130
|
+
line(pending.slice(0, at));
|
|
131
|
+
pending = pending.slice(at + 1);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
stream.once('error', (error) => {
|
|
135
|
+
name(null);
|
|
136
|
+
settle({ kind: 'unmeasured', reason: `its report descriptor: ${error.message}` });
|
|
137
|
+
});
|
|
138
|
+
stream.once('close', () => {
|
|
139
|
+
if (pending.trim() !== '')
|
|
140
|
+
line(pending.trim());
|
|
141
|
+
name(null);
|
|
142
|
+
settle(end ?? { kind: 'unmeasured', reason: 'the waiter ended before it could report the command' });
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
return { started, peak };
|
|
146
|
+
}
|
|
@@ -10,17 +10,19 @@ export interface MbxPool {
|
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
12
|
* The pool the environment names, or `null` where the project has no mbx and cowshed's own pools apply unchanged, or
|
|
13
|
-
* where this run is already inside an mbx reservation:
|
|
14
|
-
* non-Cargo work shares the enclosing grant
|
|
15
|
-
* inside a job"). A second reservation would
|
|
16
|
-
* nested leaf queued behind the very work it
|
|
13
|
+
* where this run is already inside an mbx reservation: whatever holds one hands its work `MBX_SCHED_DISABLE=1` (a
|
|
14
|
+
* reserved leaf's shell, `mbx reserve`, a held cowshed job), and nested non-Cargo work shares the enclosing grant
|
|
15
|
+
* instead of reserving twice (specs/cowshed/05_gateway.md, "Parallelism inside a job"). A second reservation would
|
|
16
|
+
* wait in the pool while its parent holds a grant, so under a full pool the nested leaf queued behind the very work it
|
|
17
|
+
* belongs to.
|
|
17
18
|
*/
|
|
18
19
|
export declare function mbxPool(environment: Readonly<NodeJS.ProcessEnv>): MbxPool | null;
|
|
19
20
|
/**
|
|
20
21
|
* How a leaf enters the pool once. A Cargo leaf that builds runs through mbx, which admits its compilers and supported
|
|
21
|
-
* test work itself, so wrapping it would charge it twice. Any other leaf reserves for its whole run
|
|
22
|
-
*
|
|
23
|
-
* the runner runs on the count it is granted (`sizeRunnerToGrant`). That includes a
|
|
22
|
+
* test work itself, so wrapping it would charge it twice. Any other leaf reserves for its whole run, asking its Nx
|
|
23
|
+
* run's mbx reservation endpoint (mbx-reservations.ts) for at most its runner's count: mbx grants as many of those
|
|
24
|
+
* cores as are free once one is, and the runner runs on the count it is granted (`sizeRunnerToGrant`). That includes a
|
|
25
|
+
* nextest run of test binaries
|
|
24
26
|
* already built (the inferred extracted-archive run, or one from `--archive-file`): it compiles nothing, and beneath a
|
|
25
27
|
* reservation mbx schedules none of its test processes, so they run inside the one grant. Admitted one by one under
|
|
26
28
|
* `MBX_SCHEDULER_TESTS=1`, each queued for a ticket of its own: in a downstream gate on 2026-10-30, codec tests of at
|
|
@@ -55,8 +57,9 @@ export declare function mbxLeaf(pool: MbxPool, demand: Demand, command: string):
|
|
|
55
57
|
*/
|
|
56
58
|
export declare function mbxIdleGated(leaf: MbxLeaf | null, command: string, environment: Readonly<NodeJS.ProcessEnv>): boolean;
|
|
57
59
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
+
* The descriptor between a pooled leaf's shell and its executor. For a Cargo leaf mbx writes one byte on it per
|
|
61
|
+
* admission (g) and permit drop (r), every Cargo descendant inheriting it through MBX_GRANT_FD. A reserved leaf's shell
|
|
62
|
+
* writes the cores it asks for on it, one line, and reads back the count the pool granted (`reservation`).
|
|
60
63
|
*/
|
|
61
64
|
export declare const MBX_GRANT_FD = 5;
|
|
62
65
|
/**
|
|
@@ -71,4 +74,9 @@ export declare function withMbxLeaf(command: string, leaf: MbxLeaf | null): stri
|
|
|
71
74
|
* is reserved.
|
|
72
75
|
*/
|
|
73
76
|
export declare function withCountedReservation(fixture: string, command: string, leaf: ReservedLeaf): string;
|
|
77
|
+
/**
|
|
78
|
+
* The cores a reserved leaf's shell asked for on `MBX_GRANT_FD` (`reservation`): a whole count from one to `most`, the
|
|
79
|
+
* most its runner may ask for; anything else is `null`.
|
|
80
|
+
*/
|
|
81
|
+
export declare function askedCores(line: string, most: number): number | null;
|
|
74
82
|
//# sourceMappingURL=mbx-pool.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mbx-pool.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/mbx-pool.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,MAAM,
|
|
1
|
+
{"version":3,"file":"mbx-pool.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/mbx-pool.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,MAAM,EAIX,KAAK,cAAc,EAEpB,MAAM,oBAAoB,CAAC;AAG5B;;;;;GAKG;AACH,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,GAAG,KAAK,CAAC;CACrC;AAED;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,OAAO,GAAG,IAAI,CAKhF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,OAAO,GACf;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAC1B;IACE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,GAAG,KAAK,CAAC;IACpC,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC;CACjC,CAAC;AAEN,sCAAsC;AACtC,MAAM,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,EAAE;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,CAAC,CAAC;AAKjE,+FAA+F;AAC/F,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAE5D;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAoBtF;AAQD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,OAAO,CAkDrH;AAED;;;;GAIG;AACH,eAAO,MAAM,YAAY,IAAI,CAAC;AAE9B;;;GAGG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI,GAAG,MAAM,CAKzE;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,GAAG,MAAM,CAInG;AAkBD;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAIpE"}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { shellTokens } from '../../runtime-input-policy.js';
|
|
2
|
-
import { CPU_TOKENS_ENV, grantedSizing, invocationSpan, WANT_ENV, } from './runner-sizing.js';
|
|
2
|
+
import { CPU_TOKENS_ENV, GRANTED_CPUS_ENV, grantedSizing, invocationSpan, WANT_ENV, } from './runner-sizing.js';
|
|
3
3
|
import { shellQuote } from './verdict.js';
|
|
4
4
|
/**
|
|
5
5
|
* The pool the environment names, or `null` where the project has no mbx and cowshed's own pools apply unchanged, or
|
|
6
|
-
* where this run is already inside an mbx reservation:
|
|
7
|
-
* non-Cargo work shares the enclosing grant
|
|
8
|
-
* inside a job"). A second reservation would
|
|
9
|
-
* nested leaf queued behind the very work it
|
|
6
|
+
* where this run is already inside an mbx reservation: whatever holds one hands its work `MBX_SCHED_DISABLE=1` (a
|
|
7
|
+
* reserved leaf's shell, `mbx reserve`, a held cowshed job), and nested non-Cargo work shares the enclosing grant
|
|
8
|
+
* instead of reserving twice (specs/cowshed/05_gateway.md, "Parallelism inside a job"). A second reservation would
|
|
9
|
+
* wait in the pool while its parent holds a grant, so under a full pool the nested leaf queued behind the very work it
|
|
10
|
+
* belongs to.
|
|
10
11
|
*/
|
|
11
12
|
export function mbxPool(environment) {
|
|
12
13
|
if (environment.MBX_SCHEDULER !== '1' || !environment.MBX_CACHE_DIR || environment.MBX_SCHED_DISABLE === '1') {
|
|
@@ -118,8 +119,9 @@ export function mbxIdleGated(leaf, command, environment) {
|
|
|
118
119
|
}
|
|
119
120
|
}
|
|
120
121
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
122
|
+
* The descriptor between a pooled leaf's shell and its executor. For a Cargo leaf mbx writes one byte on it per
|
|
123
|
+
* admission (g) and permit drop (r), every Cargo descendant inheriting it through MBX_GRANT_FD. A reserved leaf's shell
|
|
124
|
+
* writes the cores it asks for on it, one line, and reads back the count the pool granted (`reservation`).
|
|
123
125
|
*/
|
|
124
126
|
export const MBX_GRANT_FD = 5;
|
|
125
127
|
/**
|
|
@@ -144,11 +146,27 @@ export function withCountedReservation(fixture, command, leaf) {
|
|
|
144
146
|
return `bounded_exec_units=$(${fixture}) && ${WANT_ENV}=$(printf '%s\\n' "$bounded_exec_units" | { ${count}; }) && ${reservation(command, leaf)}`;
|
|
145
147
|
}
|
|
146
148
|
/**
|
|
147
|
-
* The reservation of `WANT_ENV` cores, set by the caller, that runs `command` once mbx grants it
|
|
148
|
-
*
|
|
149
|
-
*
|
|
149
|
+
* The reservation of `WANT_ENV` cores, set by the caller, that runs `command` once mbx grants it. The shell asks its
|
|
150
|
+
* executor on `MBX_GRANT_FD`, one line naming the count; the executor asks its run's mbx reservation endpoint and
|
|
151
|
+
* answers the cores granted, one line, or ends the descriptor unanswered when mbx refused, so the read fails and
|
|
152
|
+
* nothing runs (executor.ts). The shell then closes the descriptor, which the command never inherits, gives the command
|
|
153
|
+
* what `mbx reserve` gives its own (`GRANTED_CPUS_ENV`, `MBX_SCHED_DISABLE=1` and an empty `MBX_SCHED_DIR`, so mbx work
|
|
154
|
+
* inside runs under the grant), sizes the runner to the grant (`grantedSizing`), says so on stderr and runs the command
|
|
155
|
+
* in the same shell: no process starts for the reservation.
|
|
150
156
|
*/
|
|
151
157
|
function reservation(command, leaf) {
|
|
152
158
|
const said = `printf 'cowshed: mbx reserve running cpus=%s asked=%s\\n' "$${CPU_TOKENS_ENV}" "$${WANT_ENV}" >&2; `;
|
|
153
|
-
|
|
159
|
+
const ask = `printf '%s\\n' "$${WANT_ENV}" >&${MBX_GRANT_FD} && IFS= read -r ${GRANTED_CPUS_ENV} <&${MBX_GRANT_FD} && exec ${MBX_GRANT_FD}>&-`;
|
|
160
|
+
const held = `export ${GRANTED_CPUS_ENV} MBX_SCHED_DISABLE=1 MBX_SCHED_DIR=`;
|
|
161
|
+
return `export ${WANT_ENV} && ${ask} && ${held} && eval ${shellQuote(`${grantedSizing(leaf.demand)}${said}${command}`)}`;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The cores a reserved leaf's shell asked for on `MBX_GRANT_FD` (`reservation`): a whole count from one to `most`, the
|
|
165
|
+
* most its runner may ask for; anything else is `null`.
|
|
166
|
+
*/
|
|
167
|
+
export function askedCores(line, most) {
|
|
168
|
+
if (!/^[1-9]\d{0,9}$/.test(line))
|
|
169
|
+
return null;
|
|
170
|
+
const cores = Number(line);
|
|
171
|
+
return cores <= most ? cores : null;
|
|
154
172
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { PostTasksExecutionContext, PreTasksExecutionContext } from 'nx/src/project-graph/plugins/public-api.js';
|
|
2
|
+
/**
|
|
3
|
+
* mbx's reservation endpoint of one Nx run (specs/cowshed/05_gateway.md, "Parallelism inside a job"). A reserved leaf
|
|
4
|
+
* once ran under its own `mbx reserve`, two mbx processes per task whose dynamic loading alone kept the host's vnode
|
|
5
|
+
* table recycling. With mbx's pool, the run's `preTasksExecution` starts `mbx reservations <socket>` once and publishes
|
|
6
|
+
* the socket in `RESERVATIONS_ENV`, which every task inherits; each task's executor asks it for its leaf's cores over
|
|
7
|
+
* one connection per process (`reserveCores`), and the run's `postTasksExecution` closes the endpoint's standard input,
|
|
8
|
+
* which ends it. Admission is `mbx reserve`'s: the same pool, priority and order. A grant lasts until its leaf ends, or
|
|
9
|
+
* until its connection closes, so an executor that dies gives back what it held.
|
|
10
|
+
*/
|
|
11
|
+
export declare const RESERVATIONS_ENV = "MBX_RESERVATIONS_SOCKET";
|
|
12
|
+
/**
|
|
13
|
+
* Serve `context`'s run an mbx reservation endpoint and publish it in `env`, which every task inherits, when the
|
|
14
|
+
* environment names mbx's pool (`mbxPool`). An endpoint that does not start is said, and each reserved leaf of the run
|
|
15
|
+
* then refuses to run, naming `RESERVATIONS_ENV`; a leaf in no pool, or one mbx admits itself, runs as before.
|
|
16
|
+
*/
|
|
17
|
+
export declare function serveRunReservations(context: Pick<PreTasksExecutionContext, 'id'>, env?: NodeJS.ProcessEnv): Promise<void>;
|
|
18
|
+
/** Close the endpoint `context`'s run was served, if it was served one: its standard input's end ends it. */
|
|
19
|
+
export declare function closeRunReservations(context: Pick<PostTasksExecutionContext, 'id'>): Promise<void>;
|
|
20
|
+
/** The pool's answer to one reservation: the cores it holds and a promise of why it was lost, or why there are none. */
|
|
21
|
+
export type Granted = {
|
|
22
|
+
readonly ok: true;
|
|
23
|
+
readonly cpus: number;
|
|
24
|
+
readonly lost: Promise<string>;
|
|
25
|
+
} | {
|
|
26
|
+
readonly ok: false;
|
|
27
|
+
readonly error: string;
|
|
28
|
+
};
|
|
29
|
+
/** One request for cores on a run's endpoint. */
|
|
30
|
+
export interface Reservation {
|
|
31
|
+
/** Settles once the pool grants or refuses it, or its connection ends first. */
|
|
32
|
+
readonly granted: Promise<Granted>;
|
|
33
|
+
/** Gives the grant back, or withdraws the request while it waits. Later calls do nothing. */
|
|
34
|
+
release(): void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Ask the endpoint at `socket` for at most `cpus` cores at `priority`, over this process's one connection to it,
|
|
38
|
+
* opened by its first request and kept while another one may come.
|
|
39
|
+
*/
|
|
40
|
+
export declare function reserveCores(socket: string, cpus: number, priority: 'normal' | 'low'): Reservation;
|
|
41
|
+
//# sourceMappingURL=mbx-reservations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mbx-reservations.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/mbx-reservations.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,yBAAyB,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;AAMtH;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,4BAA4B,CAAC;AAmB1D;;;;GAIG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,IAAI,CAAC,wBAAwB,EAAE,IAAI,CAAC,EAC7C,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,OAAO,CAAC,IAAI,CAAC,CAaf;AAED,6GAA6G;AAC7G,wBAAsB,oBAAoB,CAAC,OAAO,EAAE,IAAI,CAAC,yBAAyB,EAAE,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAYxG;AAsED,wHAAwH;AACxH,MAAM,MAAM,OAAO,GACf;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;CAAE,GAC5E;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnD,iDAAiD;AACjD,MAAM,WAAW,WAAW;IAC1B,gFAAgF;IAChF,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IACnC,6FAA6F;IAC7F,OAAO,IAAI,IAAI,CAAC;CACjB;AAgID;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,KAAK,GAAG,WAAW,CAUlG"}
|