@smoothbricks/nx-plugin 0.7.5 → 0.7.7
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 +20 -3
- package/dist/.build-inputs.json +12 -6
- 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 +115 -62
- package/dist/executors/bounded-exec/host-memory.d.ts +54 -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 +115 -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 +137 -0
- package/dist/executors/bounded-exec/leaf-peak.d.ts +56 -0
- package/dist/executors/bounded-exec/leaf-peak.d.ts.map +1 -0
- package/dist/executors/bounded-exec/leaf-peak.js +147 -0
- package/dist/executors/bounded-exec/mbx-reservations.d.ts +22 -0
- package/dist/executors/bounded-exec/mbx-reservations.d.ts.map +1 -1
- package/dist/executors/bounded-exec/mbx-reservations.js +22 -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 +79 -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/verdict.d.ts +18 -0
- package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
- package/dist/executors/bounded-exec/verdict.js +21 -1
- package/managed/templates/github/actions/setup-devenv/action.yml +31 -2
- package/package.json +2 -2
- 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 +121 -59
- package/src/executors/bounded-exec/host-memory.darwin.ts +147 -0
- package/src/executors/bounded-exec/host-memory.linux.ts +147 -0
- package/src/executors/bounded-exec/host-memory.ts +77 -0
- package/src/executors/bounded-exec/leaf-peak.ts +152 -0
- package/src/executors/bounded-exec/mbx-reservations.ts +30 -0
- package/src/executors/bounded-exec/peak-waiter.ts +115 -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/verdict.ts +30 -1
- package/test-timings.json +136 -132
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { posix } from 'node:path';
|
|
3
|
+
import { childPids } from '../../process-children.linux.js';
|
|
4
|
+
/**
|
|
5
|
+
* Linux's memory mechanisms. The run's memory is the least of the host's `MemTotal` and every finite `memory.max` of
|
|
6
|
+
* the cgroup v2 the run's process is in and of its ancestors, up to the root of the mounted hierarchy: the kernel holds
|
|
7
|
+
* a cgroup to its ancestors' limits too, so a job in cowshed's `background` class, whose own `memory.max` is `max`, has
|
|
8
|
+
* the class's three quarters of the host. A host with no cgroup v2 hierarchy, or a hierarchy that sets no limit, gives
|
|
9
|
+
* the run `MemTotal`.
|
|
10
|
+
*/
|
|
11
|
+
const MEMINFO = '/proc/meminfo';
|
|
12
|
+
const CGROUP = '/proc/self/cgroup';
|
|
13
|
+
const MOUNTINFO = '/proc/self/mountinfo';
|
|
14
|
+
const failed = (code, subject, cause) => ({
|
|
15
|
+
ok: false,
|
|
16
|
+
error: { operation: 'read-run-memory', code, subject, cause },
|
|
17
|
+
});
|
|
18
|
+
const unreadable = (subject, read) => failed('unreadable', subject, `${read.code}: ${read.message}`);
|
|
19
|
+
/** A mount point or root as mountinfo writes it, its space, tab, newline and backslash escaped in octal. */
|
|
20
|
+
const decodeOctal = (field) => field.replace(/\\([0-7]{3})/g, (_, octal) => String.fromCharCode(Number.parseInt(octal, 8)));
|
|
21
|
+
/** The cgroup v2 mounts mountinfo lists: the cgroup each shows (`root`) and where (`point`). */
|
|
22
|
+
function cgroup2Mounts(mountinfo) {
|
|
23
|
+
const mounts = [];
|
|
24
|
+
for (const line of mountinfo.split('\n')) {
|
|
25
|
+
const fields = line.split(' ');
|
|
26
|
+
const separator = fields.indexOf('-');
|
|
27
|
+
const [root, point] = [fields[3], fields[4]];
|
|
28
|
+
if (separator !== -1 && fields[separator + 1] === 'cgroup2' && root !== undefined && point !== undefined) {
|
|
29
|
+
mounts.push({ root: decodeOctal(root), point: decodeOctal(point) });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return mounts;
|
|
33
|
+
}
|
|
34
|
+
/** The run's memory, reading every file through `read`. */
|
|
35
|
+
export async function linuxRunMemory(read) {
|
|
36
|
+
const meminfo = await read(MEMINFO);
|
|
37
|
+
if (!meminfo.ok)
|
|
38
|
+
return unreadable(MEMINFO, meminfo);
|
|
39
|
+
const total = /^MemTotal:\s+(\d+) kB$/m.exec(meminfo.text)?.[1];
|
|
40
|
+
if (total === undefined)
|
|
41
|
+
return failed('malformed', MEMINFO, 'it names no MemTotal in kB');
|
|
42
|
+
let least = { bytes: Number(total) * 1024, source: `${MEMINFO} MemTotal` };
|
|
43
|
+
const cgroups = await read(CGROUP);
|
|
44
|
+
if (!cgroups.ok)
|
|
45
|
+
return unreadable(CGROUP, cgroups);
|
|
46
|
+
const path = /^0::(\/.*)$/m.exec(cgroups.text)?.[1];
|
|
47
|
+
if (path === undefined) {
|
|
48
|
+
// No cgroup v2 hierarchy holds the run: the host's memory is its memory.
|
|
49
|
+
return { ok: true, memory: least };
|
|
50
|
+
}
|
|
51
|
+
const mounts = await read(MOUNTINFO);
|
|
52
|
+
if (!mounts.ok)
|
|
53
|
+
return unreadable(MOUNTINFO, mounts);
|
|
54
|
+
const mount = cgroup2Mounts(mounts.text).find(({ root }) => root === '/' || path === root || path.startsWith(`${root}/`));
|
|
55
|
+
if (mount === undefined) {
|
|
56
|
+
return failed('unmounted', path, `${MOUNTINFO} lists no cgroup2 mount that shows the run's cgroup`);
|
|
57
|
+
}
|
|
58
|
+
const top = posix.resolve(mount.point);
|
|
59
|
+
const own = posix.resolve(top, `.${mount.root === '/' ? path : path.slice(mount.root.length)}`);
|
|
60
|
+
if (own !== top && !own.startsWith(`${top}/`)) {
|
|
61
|
+
return failed('unmounted', path, `the run's cgroup lies outside the cgroup2 mount at ${top}`);
|
|
62
|
+
}
|
|
63
|
+
for (let directory = own;;) {
|
|
64
|
+
const file = posix.join(directory, 'memory.max');
|
|
65
|
+
const limit = await read(file);
|
|
66
|
+
if (limit.ok) {
|
|
67
|
+
const value = limit.text.trim();
|
|
68
|
+
if (value !== 'max') {
|
|
69
|
+
if (!/^\d+$/.test(value)) {
|
|
70
|
+
return failed('malformed', file, `it holds ${JSON.stringify(value)}, not max or bytes`);
|
|
71
|
+
}
|
|
72
|
+
// A limit above the host's memory (a near-2^63 one included) is no constraint, and is never made a number.
|
|
73
|
+
if (BigInt(value) < BigInt(least.bytes))
|
|
74
|
+
least = { bytes: Number(value), source: file };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else if (limit.code !== 'ENOENT') {
|
|
78
|
+
// ENOENT: the hierarchy's root, or a cgroup whose parent enables no memory controller, has no limit of its own.
|
|
79
|
+
return unreadable(file, limit);
|
|
80
|
+
}
|
|
81
|
+
if (directory === top)
|
|
82
|
+
break;
|
|
83
|
+
directory = posix.dirname(directory);
|
|
84
|
+
}
|
|
85
|
+
return { ok: true, memory: least };
|
|
86
|
+
}
|
|
87
|
+
async function readText(path) {
|
|
88
|
+
try {
|
|
89
|
+
return { ok: true, text: await readFile(path, 'utf8') };
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
const code = error instanceof Error && 'code' in error && typeof error.code === 'string' ? error.code : 'EIO';
|
|
93
|
+
return { ok: false, code, message: error instanceof Error ? error.message : String(error) };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** Bun before 1.4.0 reports Linux's `ru_maxrss` as the kernel does, in KiB; from 1.4.0 it reports bytes. */
|
|
97
|
+
function peakBytes(maxRss, bunVersion) {
|
|
98
|
+
const version = /^(\d+)\.(\d+)\./.exec(bunVersion);
|
|
99
|
+
if (version === null) {
|
|
100
|
+
throw new Error(`Bun names its version ${JSON.stringify(bunVersion)}, which is no semver`);
|
|
101
|
+
}
|
|
102
|
+
const [major, minor] = [Number(version[1]), Number(version[2])];
|
|
103
|
+
return major < 1 || (major === 1 && minor < 4) ? maxRss * 1024 : maxRss;
|
|
104
|
+
}
|
|
105
|
+
/** What the tree rooted at `root` holds resident: each member's `VmRSS`, the tree walked by the kernel's child links. */
|
|
106
|
+
async function treeResidentBytes(root) {
|
|
107
|
+
const seen = new Set();
|
|
108
|
+
let level = [root];
|
|
109
|
+
let total = 0;
|
|
110
|
+
while (level.length > 0) {
|
|
111
|
+
const members = level.filter((pid) => !seen.has(pid));
|
|
112
|
+
for (const pid of members)
|
|
113
|
+
seen.add(pid);
|
|
114
|
+
const [resident, children] = await Promise.all([Promise.all(members.map(residentBytes)), childPids(members)]);
|
|
115
|
+
total += resident.reduce((sum, bytes) => sum + bytes, 0);
|
|
116
|
+
level = children;
|
|
117
|
+
}
|
|
118
|
+
return total;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* `VmRSS` from `/proc/<pid>/status`: what the process holds resident, anonymous, file-backed and shared memory together.
|
|
122
|
+
* A process that has exited (`ENOENT`, `ESRCH`) holds nothing, and so does a zombie, whose status names no `VmRSS`.
|
|
123
|
+
*/
|
|
124
|
+
async function residentBytes(pid) {
|
|
125
|
+
let status;
|
|
126
|
+
try {
|
|
127
|
+
status = await readFile(`/proc/${pid}/status`, 'utf8');
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
if (error instanceof Error && 'code' in error && (error.code === 'ENOENT' || error.code === 'ESRCH'))
|
|
131
|
+
return 0;
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
const kib = /^VmRSS:\s+(\d+) kB$/m.exec(status)?.[1];
|
|
135
|
+
return kib === undefined ? 0 : Number(kib) * 1024;
|
|
136
|
+
}
|
|
137
|
+
export const linuxMemory = { readRunMemory: () => linuxRunMemory(readText), peakBytes, treeResidentBytes };
|
|
@@ -0,0 +1,56 @@
|
|
|
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 largest sum of what the exited command's process tree held side by side, as its waiter
|
|
6
|
+
* (`peak-waiter.ts`) sampled it, never less than the command's rusage `ru_maxrss`, which the waiter also reads. The
|
|
7
|
+
* leaf runs no cgroup of its own, even under a cowshed supervisor, whose subtree of the command is an observed fold
|
|
8
|
+
* inside the job's cgroup (07_api.md, `JobSubtreeHandle`), so the peak is read from the leaf's own tree on Linux and
|
|
9
|
+
* macOS alike. The executor writes it into the task's record, and the task's next run is charged it in its run's pool.
|
|
10
|
+
*/
|
|
11
|
+
/** The descriptor the waiter reports on: past the lifecycle channel (3), the subtree admission (4) and mbx's grant (5). */
|
|
12
|
+
export declare const PEAK_FD = 6;
|
|
13
|
+
/** A leaf's peak in bytes, or why it went unmeasured. */
|
|
14
|
+
export type LeafPeak = {
|
|
15
|
+
readonly kind: 'measured';
|
|
16
|
+
readonly bytes: number;
|
|
17
|
+
} | {
|
|
18
|
+
readonly kind: 'unmeasured';
|
|
19
|
+
readonly reason: string;
|
|
20
|
+
};
|
|
21
|
+
/** The Bun a leaf's waiter runs on, or why its peak goes unmeasured. */
|
|
22
|
+
export type PeakWaiter = {
|
|
23
|
+
readonly kind: 'waiter';
|
|
24
|
+
readonly runtime: string;
|
|
25
|
+
} | {
|
|
26
|
+
readonly kind: 'unmeasured';
|
|
27
|
+
readonly reason: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* The Bun that runs this executor, else the first `bun` on the executor's own PATH, never one a target's environment
|
|
31
|
+
* names: the waiter is the executor's, not the command's.
|
|
32
|
+
*/
|
|
33
|
+
export declare function peakWaiter(): PeakWaiter;
|
|
34
|
+
/**
|
|
35
|
+
* The spawn of `file args` under the waiter on `runtime`. `descriptors` are the command's own past stderr, from fd 3:
|
|
36
|
+
* the waiter passes each one that is not ignored at its own number, and takes `PEAK_FD` for its report. The runtime's
|
|
37
|
+
* config is empty, so no preload a workspace's `bunfig.toml` names runs in the waiter.
|
|
38
|
+
*/
|
|
39
|
+
export declare function underWaiter(runtime: string, file: string, args: readonly string[], descriptors: readonly IOType[]): {
|
|
40
|
+
file: string;
|
|
41
|
+
args: string[];
|
|
42
|
+
stdio: IOType[];
|
|
43
|
+
};
|
|
44
|
+
/** What a leaf's waiter reports on its descriptor. */
|
|
45
|
+
export interface WaiterReport {
|
|
46
|
+
/**
|
|
47
|
+
* The command's pid once the waiter started it: the leaf's runner, a member of the group the waiter leads. Null when
|
|
48
|
+
* the waiter ended without naming one.
|
|
49
|
+
*/
|
|
50
|
+
readonly started: Promise<number | null>;
|
|
51
|
+
/** Once the waiter ends: the exited command's peak, or why it reported none. */
|
|
52
|
+
readonly peak: Promise<LeafPeak>;
|
|
53
|
+
}
|
|
54
|
+
/** What the waiter reports on `stream`: the command's pid as it starts it, and the command's peak once it ends. */
|
|
55
|
+
export declare function readWaiterReport(stream: Readable | null | undefined): WaiterReport;
|
|
56
|
+
//# 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;;;;;;;GAOG;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,147 @@
|
|
|
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 largest sum of what the exited command's process tree held side by side, as its waiter
|
|
10
|
+
* (`peak-waiter.ts`) sampled it, never less than the command's rusage `ru_maxrss`, which the waiter also reads. The
|
|
11
|
+
* leaf runs no cgroup of its own, even under a cowshed supervisor, whose subtree of the command is an observed fold
|
|
12
|
+
* inside the job's cgroup (07_api.md, `JobSubtreeHandle`), so the peak is read from the leaf's own tree on Linux and
|
|
13
|
+
* macOS alike. The executor writes it into the task's record, and the task's next run is charged it in its run's pool.
|
|
14
|
+
*/
|
|
15
|
+
/** The descriptor the waiter reports on: past the lifecycle channel (3), the subtree admission (4) and mbx's grant (5). */
|
|
16
|
+
export const PEAK_FD = 6;
|
|
17
|
+
/** The waiter's module, in the form this one was loaded in: compiled JavaScript under Nx, TypeScript under Bun. */
|
|
18
|
+
const WAITER = fileURLToPath(new URL(import.meta.url.endsWith('.ts') ? './peak-waiter.ts' : './peak-waiter.js', import.meta.url));
|
|
19
|
+
const isWaiterLine = (() => {
|
|
20
|
+
const _io0 = (input, _exceptionable = true) => "number" === typeof input.started && _isTypeUint32_1._isTypeUint32(input.started) && (1 === Object.keys(input).length || Object.keys(input).every(key => {
|
|
21
|
+
if (["started"].some(prop => key === prop))
|
|
22
|
+
return true;
|
|
23
|
+
const value = input[key];
|
|
24
|
+
if (undefined === value)
|
|
25
|
+
return true;
|
|
26
|
+
return false;
|
|
27
|
+
}));
|
|
28
|
+
const _io1 = (input, _exceptionable = true) => "number" === typeof input.peakBytes && _isTypeUint64_1._isTypeUint64(input.peakBytes) && (1 === Object.keys(input).length || Object.keys(input).every(key => {
|
|
29
|
+
if (["peakBytes"].some(prop => key === prop))
|
|
30
|
+
return true;
|
|
31
|
+
const value = input[key];
|
|
32
|
+
if (undefined === value)
|
|
33
|
+
return true;
|
|
34
|
+
return false;
|
|
35
|
+
}));
|
|
36
|
+
const _io2 = (input, _exceptionable = true) => "string" === typeof input.unmeasured && (1 === Object.keys(input).length || Object.keys(input).every(key => {
|
|
37
|
+
if (["unmeasured"].some(prop => key === prop))
|
|
38
|
+
return true;
|
|
39
|
+
const value = input[key];
|
|
40
|
+
if (undefined === value)
|
|
41
|
+
return true;
|
|
42
|
+
return false;
|
|
43
|
+
}));
|
|
44
|
+
const _iu0 = (input, _exceptionable = true) => (() => {
|
|
45
|
+
if (undefined !== input.started)
|
|
46
|
+
return _io0(input, true && _exceptionable);
|
|
47
|
+
else if (undefined !== input.peakBytes)
|
|
48
|
+
return _io1(input, true && _exceptionable);
|
|
49
|
+
else if (undefined !== input.unmeasured)
|
|
50
|
+
return _io2(input, true && _exceptionable);
|
|
51
|
+
else
|
|
52
|
+
return false;
|
|
53
|
+
})();
|
|
54
|
+
return (input, _exceptionable = true) => "object" === typeof input && null !== input && _iu0(input, true);
|
|
55
|
+
})();
|
|
56
|
+
/**
|
|
57
|
+
* The Bun that runs this executor, else the first `bun` on the executor's own PATH, never one a target's environment
|
|
58
|
+
* names: the waiter is the executor's, not the command's.
|
|
59
|
+
*/
|
|
60
|
+
export function peakWaiter() {
|
|
61
|
+
if (HOST_MEMORY === null) {
|
|
62
|
+
return { kind: 'unmeasured', reason: `${process.platform} has no reader of a leaf's peak` };
|
|
63
|
+
}
|
|
64
|
+
const runtime = process.versions.bun === undefined ? resolveOnPath('bun', process.env.PATH, process.cwd()) : process.execPath;
|
|
65
|
+
return runtime === null
|
|
66
|
+
? { kind: 'unmeasured', reason: 'no bun is on PATH to wait on the command and read its rusage' }
|
|
67
|
+
: { kind: 'waiter', runtime };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The spawn of `file args` under the waiter on `runtime`. `descriptors` are the command's own past stderr, from fd 3:
|
|
71
|
+
* the waiter passes each one that is not ignored at its own number, and takes `PEAK_FD` for its report. The runtime's
|
|
72
|
+
* config is empty, so no preload a workspace's `bunfig.toml` names runs in the waiter.
|
|
73
|
+
*/
|
|
74
|
+
export function underWaiter(runtime, file, args, descriptors) {
|
|
75
|
+
if (descriptors.length > PEAK_FD - 3) {
|
|
76
|
+
throw new RangeError(`a leaf's own descriptors end before ${PEAK_FD}, the waiter's report; it names ${descriptors.length + 3}`);
|
|
77
|
+
}
|
|
78
|
+
const passed = descriptors.flatMap((io, at) => (io === 'ignore' ? [] : [at + 3]));
|
|
79
|
+
const padded = Array.from({ length: PEAK_FD - 3 }, (_, at) => descriptors[at] ?? 'ignore');
|
|
80
|
+
return {
|
|
81
|
+
file: runtime,
|
|
82
|
+
args: ['--config=/dev/null', WAITER, String(PEAK_FD), passed.join(','), file, ...args],
|
|
83
|
+
stdio: ['pipe', 'pipe', 'pipe', ...padded, 'pipe'],
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** What the waiter reports on `stream`: the command's pid as it starts it, and the command's peak once it ends. */
|
|
87
|
+
export function readWaiterReport(stream) {
|
|
88
|
+
if (stream === null || stream === undefined) {
|
|
89
|
+
return {
|
|
90
|
+
started: Promise.resolve(null),
|
|
91
|
+
peak: Promise.resolve({ kind: 'unmeasured', reason: 'the waiter was started without its report descriptor' }),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
// `Promise.withResolvers` would read better but needs lib es2024; this package inherits lib es2022 from
|
|
95
|
+
// tsconfig.base.json. The report's close names null at the latest.
|
|
96
|
+
let name;
|
|
97
|
+
const started = new Promise((named) => {
|
|
98
|
+
name = named;
|
|
99
|
+
});
|
|
100
|
+
// The waiter's last word: the line after its start, or what was wrong with a line.
|
|
101
|
+
let end = null;
|
|
102
|
+
const line = (text) => {
|
|
103
|
+
let parsed;
|
|
104
|
+
try {
|
|
105
|
+
parsed = JSON.parse(text);
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
if (!(error instanceof SyntaxError))
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
if (!isWaiterLine(parsed)) {
|
|
112
|
+
end = { kind: 'unmeasured', reason: `the waiter reported ${JSON.stringify(text)}, which is no peak` };
|
|
113
|
+
}
|
|
114
|
+
else if ('started' in parsed) {
|
|
115
|
+
name(parsed.started);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
end =
|
|
119
|
+
'peakBytes' in parsed
|
|
120
|
+
? { kind: 'measured', bytes: parsed.peakBytes }
|
|
121
|
+
: { kind: 'unmeasured', reason: parsed.unmeasured };
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
let pending = '';
|
|
125
|
+
stream.setEncoding('utf8');
|
|
126
|
+
// 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
|
|
127
|
+
const peak = new Promise((settle) => {
|
|
128
|
+
stream.on('data', (chunk) => {
|
|
129
|
+
pending += chunk;
|
|
130
|
+
for (let at = pending.indexOf('\n'); at !== -1; at = pending.indexOf('\n')) {
|
|
131
|
+
line(pending.slice(0, at));
|
|
132
|
+
pending = pending.slice(at + 1);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
stream.once('error', (error) => {
|
|
136
|
+
name(null);
|
|
137
|
+
settle({ kind: 'unmeasured', reason: `its report descriptor: ${error.message}` });
|
|
138
|
+
});
|
|
139
|
+
stream.once('close', () => {
|
|
140
|
+
if (pending.trim() !== '')
|
|
141
|
+
line(pending.trim());
|
|
142
|
+
name(null);
|
|
143
|
+
settle(end ?? { kind: 'unmeasured', reason: 'the waiter ended before it could report the command' });
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
return { started, peak };
|
|
147
|
+
}
|
|
@@ -9,6 +9,28 @@ import type { PostTasksExecutionContext, PreTasksExecutionContext } from 'nx/src
|
|
|
9
9
|
* until its connection closes, so an executor that dies gives back what it held.
|
|
10
10
|
*/
|
|
11
11
|
export declare const RESERVATIONS_ENV = "MBX_RESERVATIONS_SOCKET";
|
|
12
|
+
/**
|
|
13
|
+
* Why the run serves its tasks no endpoint, published beside {@link RESERVATIONS_ENV} when its endpoint did not start.
|
|
14
|
+
* Every task inherits it, so each reserved leaf's refusal names the cause: a gate's failures-only output shows the
|
|
15
|
+
* leaves' refusals, never the run's own warning.
|
|
16
|
+
*/
|
|
17
|
+
export declare const RESERVATIONS_UNSERVED_ENV = "BOUNDED_EXEC_RESERVATIONS_UNSERVED";
|
|
18
|
+
/** What a task's environment says of its run's reservation endpoint. */
|
|
19
|
+
export type RunEndpoint = {
|
|
20
|
+
readonly kind: 'served';
|
|
21
|
+
readonly socket: string;
|
|
22
|
+
} | {
|
|
23
|
+
readonly kind: 'unserved';
|
|
24
|
+
readonly reason: string;
|
|
25
|
+
} | {
|
|
26
|
+
readonly kind: 'unpublished';
|
|
27
|
+
};
|
|
28
|
+
/** The endpoint `env` names, else why its run serves none, else that its run published neither. */
|
|
29
|
+
export declare function runEndpoint(env: Readonly<NodeJS.ProcessEnv>): RunEndpoint;
|
|
30
|
+
/** Why a task's run serves it no endpoint, as its reserved leaf's refusal says it. */
|
|
31
|
+
export declare function describeUnserved(endpoint: Exclude<RunEndpoint, {
|
|
32
|
+
kind: 'served';
|
|
33
|
+
}>): string;
|
|
12
34
|
/**
|
|
13
35
|
* Serve `context`'s run an mbx reservation endpoint and publish it in `env`, which every task inherits, when the
|
|
14
36
|
* environment names mbx's pool (`mbxPool`). An endpoint that does not start is said, and each reserved leaf of the run
|
|
@@ -1 +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;
|
|
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;AAE1D;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,uCAAuC,CAAC;AAE9E,wEAAwE;AACxE,MAAM,MAAM,WAAW,GACnB;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACpD;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACtD;IAAE,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAA;CAAE,CAAC;AAErC,mGAAmG;AACnG,wBAAgB,WAAW,CAAC,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,WAAW,CAKzE;AAED,sFAAsF;AACtF,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,OAAO,CAAC,WAAW,EAAE;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,CAAC,GAAG,MAAM,CAI3F;AAmBD;;;;GAIG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,IAAI,CAAC,wBAAwB,EAAE,IAAI,CAAC,EAC7C,GAAG,GAAE,MAAM,CAAC,UAAwB,GACnC,OAAO,CAAC,IAAI,CAAC,CAef;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"}
|
|
@@ -21,6 +21,26 @@ import { mbxPool } from './mbx-pool.js';
|
|
|
21
21
|
* until its connection closes, so an executor that dies gives back what it held.
|
|
22
22
|
*/
|
|
23
23
|
export const RESERVATIONS_ENV = 'MBX_RESERVATIONS_SOCKET';
|
|
24
|
+
/**
|
|
25
|
+
* Why the run serves its tasks no endpoint, published beside {@link RESERVATIONS_ENV} when its endpoint did not start.
|
|
26
|
+
* Every task inherits it, so each reserved leaf's refusal names the cause: a gate's failures-only output shows the
|
|
27
|
+
* leaves' refusals, never the run's own warning.
|
|
28
|
+
*/
|
|
29
|
+
export const RESERVATIONS_UNSERVED_ENV = 'BOUNDED_EXEC_RESERVATIONS_UNSERVED';
|
|
30
|
+
/** The endpoint `env` names, else why its run serves none, else that its run published neither. */
|
|
31
|
+
export function runEndpoint(env) {
|
|
32
|
+
const socket = env[RESERVATIONS_ENV] ?? '';
|
|
33
|
+
if (socket !== '')
|
|
34
|
+
return { kind: 'served', socket };
|
|
35
|
+
const reason = env[RESERVATIONS_UNSERVED_ENV] ?? '';
|
|
36
|
+
return reason === '' ? { kind: 'unpublished' } : { kind: 'unserved', reason };
|
|
37
|
+
}
|
|
38
|
+
/** Why a task's run serves it no endpoint, as its reserved leaf's refusal says it. */
|
|
39
|
+
export function describeUnserved(endpoint) {
|
|
40
|
+
return endpoint.kind === 'unserved'
|
|
41
|
+
? `the run's endpoint did not start (${endpoint.reason})`
|
|
42
|
+
: `the run published neither an endpoint nor why it has none (${RESERVATIONS_UNSERVED_ENV} is unset too), so its preTasksExecution never served this run or its environment never reached this task`;
|
|
43
|
+
}
|
|
24
44
|
const ENDPOINT_DIRECTORY = 'mbx-r-';
|
|
25
45
|
const ENDPOINT_SOCKET = 'r.sock';
|
|
26
46
|
/** What is kept of the endpoint's stderr to say why it ended: its last bytes. */
|
|
@@ -39,10 +59,12 @@ export async function serveRunReservations(context, env = process.env) {
|
|
|
39
59
|
const started = await startEndpoint(env);
|
|
40
60
|
if (!started.ok) {
|
|
41
61
|
logger.warn(`bounded-exec: this run's mbx reservation endpoint did not start (${started.error}); its reserved leaves refuse to run`);
|
|
62
|
+
env[RESERVATIONS_UNSERVED_ENV] = started.error;
|
|
42
63
|
return;
|
|
43
64
|
}
|
|
44
65
|
endpoints.set(context.id, started.endpoint);
|
|
45
66
|
env[RESERVATIONS_ENV] = started.socket;
|
|
67
|
+
delete env[RESERVATIONS_UNSERVED_ENV];
|
|
46
68
|
}
|
|
47
69
|
/** Close the endpoint `context`'s run was served, if it was served one: its standard input's end ends it. */
|
|
48
70
|
export async function closeRunReservations(context) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"peak-waiter.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/peak-waiter.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { writeSync } from 'node:fs';
|
|
2
|
+
import { HOST_MEMORY } from './host-memory.js';
|
|
3
|
+
function isBunSpawner(runtime) {
|
|
4
|
+
return (typeof runtime === 'object' &&
|
|
5
|
+
runtime !== null &&
|
|
6
|
+
'spawn' in runtime &&
|
|
7
|
+
typeof runtime.spawn === 'function' &&
|
|
8
|
+
'version' in runtime &&
|
|
9
|
+
typeof runtime.version === 'string');
|
|
10
|
+
}
|
|
11
|
+
/** How often, in ms, the waiter sums what its command's tree holds while the command runs. */
|
|
12
|
+
const SAMPLE_MS = 250;
|
|
13
|
+
const IGNORED = ['SIGINT', 'SIGTERM', 'SIGHUP'];
|
|
14
|
+
const bun = Reflect.get(globalThis, 'Bun');
|
|
15
|
+
const [reportFd, list, file, ...args] = process.argv.slice(2);
|
|
16
|
+
if (!isBunSpawner(bun) || HOST_MEMORY === null || reportFd === undefined || list === undefined || file === undefined) {
|
|
17
|
+
throw new Error('peak-waiter runs on Bun, on Linux or macOS, as `peak-waiter.js <report fd> <fds> <file> [args…]`');
|
|
18
|
+
}
|
|
19
|
+
const memory = HOST_MEMORY;
|
|
20
|
+
const report = (line) => {
|
|
21
|
+
try {
|
|
22
|
+
writeSync(Number(reportFd), `${JSON.stringify(line)}\n`);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// The executor that would read the report is gone.
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const passed = list === '' ? [] : list.split(',').map(Number);
|
|
29
|
+
const stdio = ['inherit', 'inherit', 'inherit'];
|
|
30
|
+
for (let fd = 3; fd <= Math.max(2, ...passed); fd += 1)
|
|
31
|
+
stdio.push(passed.includes(fd) ? fd : 'ignore');
|
|
32
|
+
const ignore = () => { };
|
|
33
|
+
for (const signal of IGNORED)
|
|
34
|
+
process.on(signal, ignore);
|
|
35
|
+
let child;
|
|
36
|
+
try {
|
|
37
|
+
child = bun.spawn([file, ...args], { stdio });
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
41
|
+
report({ unmeasured: `the command did not start: ${reason}` });
|
|
42
|
+
writeSync(2, `bounded-exec: could not start ${file}: ${reason}\n`);
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
report({ started: child.pid });
|
|
46
|
+
const root = child.pid;
|
|
47
|
+
let treePeak = 0;
|
|
48
|
+
let unreadTree = null;
|
|
49
|
+
let reading = null;
|
|
50
|
+
const sample = async () => {
|
|
51
|
+
try {
|
|
52
|
+
treePeak = Math.max(treePeak, await memory.treeResidentBytes(root));
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
unreadTree ??= error instanceof Error ? error.message : String(error);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
// A read still running when the next period starts takes that period too: reads of one tree never overlap.
|
|
59
|
+
const sampler = setInterval(() => {
|
|
60
|
+
reading ??= sample().finally(() => {
|
|
61
|
+
reading = null;
|
|
62
|
+
});
|
|
63
|
+
}, SAMPLE_MS);
|
|
64
|
+
await child.exited;
|
|
65
|
+
clearInterval(sampler);
|
|
66
|
+
await reading;
|
|
67
|
+
if (unreadTree !== null) {
|
|
68
|
+
writeSync(2, `bounded-exec: the command tree's resident sum could not be read (${unreadTree}); its peak is the larger of what was read and its largest process's ru_maxrss\n`);
|
|
69
|
+
}
|
|
70
|
+
const usage = child.resourceUsage();
|
|
71
|
+
report(usage === undefined
|
|
72
|
+
? { unmeasured: 'Bun read no resource usage for the exited command' }
|
|
73
|
+
: { peakBytes: Math.max(treePeak, memory.peakBytes(usage.maxRSS, bun.version)) });
|
|
74
|
+
if (child.signalCode !== null) {
|
|
75
|
+
for (const signal of IGNORED)
|
|
76
|
+
process.removeListener(signal, ignore);
|
|
77
|
+
process.kill(process.pid, child.signalCode);
|
|
78
|
+
}
|
|
79
|
+
process.exit(child.exitCode ?? 1);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The admission decision of a CPU pool (`cpu-pool.ts`; specs/cowshed/05_gateway.md, "Without mbx, every Nx run budgets
|
|
3
|
+
* itself"): which queued requests a pool grants, in FIFO order, from the tokens it has free and, for a run's own pool,
|
|
4
|
+
* the run's memory. Pure: the pool reads its jobserver fifo for the free count and keeps the holders' charges, and this
|
|
5
|
+
* decides.
|
|
6
|
+
*
|
|
7
|
+
* Every grant needs both. The head takes `min(want, free)` tokens once one is free, and only while the charges of the
|
|
8
|
+
* leaves holding grants plus its own fit the run's memory; a head that cannot be granted holds up every request behind
|
|
9
|
+
* it, and no request holds part of a grant while it waits for the rest. A leaf is charged its task's last recorded
|
|
10
|
+
* memory peak, and nothing while its task has none. A leaf whose own charge alone exceeds the run's memory could never
|
|
11
|
+
* fit, so it is granted once nothing else holds memory, and the grant says so.
|
|
12
|
+
*/
|
|
13
|
+
/** What a queued request asks: whole tokens, at least one, and its charge in bytes (0 for a task with no peak). */
|
|
14
|
+
export interface PoolAsk {
|
|
15
|
+
readonly want: number;
|
|
16
|
+
readonly memory: number;
|
|
17
|
+
}
|
|
18
|
+
/** The run's memory a pool charges against, and the bytes its holders are charged; null for a pool that charges none. */
|
|
19
|
+
export interface MemoryLedger {
|
|
20
|
+
readonly capacity: number;
|
|
21
|
+
readonly held: number;
|
|
22
|
+
}
|
|
23
|
+
/** One request's grant: the request, its tokens, and whether its charge alone exceeds the run's memory. */
|
|
24
|
+
export interface PoolGrant<Ask extends PoolAsk> {
|
|
25
|
+
readonly request: Ask;
|
|
26
|
+
readonly tokens: number;
|
|
27
|
+
readonly oversized: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The grants of the queue's first requests, in its order, and what the first request left waiting waits for: free
|
|
31
|
+
* tokens (`cpu`) or the run's memory (`memory`). `waiting` is null when every request was granted.
|
|
32
|
+
*/
|
|
33
|
+
export interface Admission<Ask extends PoolAsk> {
|
|
34
|
+
readonly grants: readonly PoolGrant<Ask>[];
|
|
35
|
+
readonly waiting: 'cpu' | 'memory' | null;
|
|
36
|
+
}
|
|
37
|
+
export declare function admit<Ask extends PoolAsk>(queue: readonly Ask[], available: number, memory: MemoryLedger | null): Admission<Ask>;
|
|
38
|
+
//# sourceMappingURL=pool-admission.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pool-admission.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/pool-admission.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,mHAAmH;AACnH,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,yHAAyH;AACzH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,2GAA2G;AAC3G,MAAM,WAAW,SAAS,CAAC,GAAG,SAAS,OAAO;IAC5C,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;CAC7B;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS,CAAC,GAAG,SAAS,OAAO;IAC5C,QAAQ,CAAC,MAAM,EAAE,SAAS,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;IAC3C,QAAQ,CAAC,OAAO,EAAE,KAAK,GAAG,QAAQ,GAAG,IAAI,CAAC;CAC3C;AAED,wBAAgB,KAAK,CAAC,GAAG,SAAS,OAAO,EACvC,KAAK,EAAE,SAAS,GAAG,EAAE,EACrB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,YAAY,GAAG,IAAI,GAC1B,SAAS,CAAC,GAAG,CAAC,CAkBhB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The admission decision of a CPU pool (`cpu-pool.ts`; specs/cowshed/05_gateway.md, "Without mbx, every Nx run budgets
|
|
3
|
+
* itself"): which queued requests a pool grants, in FIFO order, from the tokens it has free and, for a run's own pool,
|
|
4
|
+
* the run's memory. Pure: the pool reads its jobserver fifo for the free count and keeps the holders' charges, and this
|
|
5
|
+
* decides.
|
|
6
|
+
*
|
|
7
|
+
* Every grant needs both. The head takes `min(want, free)` tokens once one is free, and only while the charges of the
|
|
8
|
+
* leaves holding grants plus its own fit the run's memory; a head that cannot be granted holds up every request behind
|
|
9
|
+
* it, and no request holds part of a grant while it waits for the rest. A leaf is charged its task's last recorded
|
|
10
|
+
* memory peak, and nothing while its task has none. A leaf whose own charge alone exceeds the run's memory could never
|
|
11
|
+
* fit, so it is granted once nothing else holds memory, and the grant says so.
|
|
12
|
+
*/
|
|
13
|
+
export function admit(queue, available, memory) {
|
|
14
|
+
const grants = [];
|
|
15
|
+
let free = available;
|
|
16
|
+
let held = memory?.held ?? 0;
|
|
17
|
+
for (const request of queue) {
|
|
18
|
+
const oversized = memory !== null && request.memory > memory.capacity;
|
|
19
|
+
if (memory !== null && held + request.memory > memory.capacity && !(oversized && held === 0)) {
|
|
20
|
+
return { grants, waiting: 'memory' };
|
|
21
|
+
}
|
|
22
|
+
const tokens = Math.min(request.want, free);
|
|
23
|
+
if (tokens === 0) {
|
|
24
|
+
return { grants, waiting: 'cpu' };
|
|
25
|
+
}
|
|
26
|
+
free -= tokens;
|
|
27
|
+
held += request.memory;
|
|
28
|
+
grants.push({ request, tokens, oversized });
|
|
29
|
+
}
|
|
30
|
+
return { grants, waiting: null };
|
|
31
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PostTasksExecutionContext, PreTasksExecutionContext } from 'nx/src/project-graph/plugins/public-api.js';
|
|
2
|
+
import { type RunMemoryReading } from './host-memory.js';
|
|
2
3
|
/**
|
|
3
4
|
* The count of the pool a run is served, or null when it is served none. A run that inherits a pool (an enclosing
|
|
4
5
|
* executor's) takes from that one; with mbx, its pool is the host's one ledger, and a run pool beside it would count
|
|
@@ -6,11 +7,12 @@ import type { PostTasksExecutionContext, PreTasksExecutionContext } from 'nx/src
|
|
|
6
7
|
*/
|
|
7
8
|
export declare function runPoolTokens(env: Readonly<NodeJS.ProcessEnv>, cores: number): number | null;
|
|
8
9
|
/**
|
|
9
|
-
* Serve `context`'s run a pool of `runPoolTokens
|
|
10
|
-
*
|
|
11
|
-
*
|
|
10
|
+
* Serve `context`'s run a pool of `runPoolTokens`, charged against the run's memory `readMemory` reads, and publish it
|
|
11
|
+
* in `env`, which every task inherits, unless the run is served none. A run whose memory cannot be read throws
|
|
12
|
+
* `RunMemoryUnreadable`, which fails the run before any task starts: it is not shown to have room. A pool that cannot
|
|
13
|
+
* be made is said, and the run's tasks are then sized only by the ceilings they inherit.
|
|
12
14
|
*/
|
|
13
|
-
export declare function serveRunCpuPool(context: Pick<PreTasksExecutionContext, 'id'>, env?: NodeJS.ProcessEnv, cores?: number): Promise<void>;
|
|
15
|
+
export declare function serveRunCpuPool(context: Pick<PreTasksExecutionContext, 'id'>, env?: NodeJS.ProcessEnv, cores?: number, readMemory?: () => Promise<RunMemoryReading>): Promise<void>;
|
|
14
16
|
/** Close the pool `context`'s run was served, if it was served one. */
|
|
15
17
|
export declare function closeRunCpuPool(context: Pick<PostTasksExecutionContext, 'id'>): Promise<void>;
|
|
16
18
|
//# sourceMappingURL=run-cpu-pool.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run-cpu-pool.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/run-cpu-pool.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,yBAAyB,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;
|
|
1
|
+
{"version":3,"file":"run-cpu-pool.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/run-cpu-pool.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,yBAAyB,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;AAGtH,OAAO,EAAE,KAAK,gBAAgB,EAAsC,MAAM,kBAAkB,CAAC;AAqD7F;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAM5F;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,OAAO,EAAE,IAAI,CAAC,wBAAwB,EAAE,IAAI,CAAC,EAC7C,GAAG,GAAE,MAAM,CAAC,UAAwB,EACpC,KAAK,GAAE,MAA+B,EACtC,UAAU,GAAE,MAAM,OAAO,CAAC,gBAAgB,CAAiB,GAC1D,OAAO,CAAC,IAAI,CAAC,CAoBf;AAoED,uEAAuE;AACvE,wBAAsB,eAAe,CAAC,OAAO,EAAE,IAAI,CAAC,yBAAyB,EAAE,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAUnG"}
|