@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
|
@@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
3
3
|
import { Worker } from 'node:worker_threads';
|
|
4
4
|
import { logger } from 'nx/src/devkit-exports.js';
|
|
5
5
|
import { readInheritedCpuPool } from './cpu-pool.js';
|
|
6
|
+
import { RunMemoryUnreadable, readRunMemory } from './host-memory.js';
|
|
6
7
|
import { mbxPool } from './mbx-pool.js';
|
|
7
8
|
import { CPU_TOKENS_ENV, parseInheritedTokens } from './runner-sizing.js';
|
|
8
9
|
/**
|
|
@@ -20,6 +21,10 @@ import { CPU_TOKENS_ENV, parseInheritedTokens } from './runner-sizing.js';
|
|
|
20
21
|
* one token per core, or the inherited positive `BOUNDED_EXEC_CPU_TOKENS` when that is fewer: inside a cowshed job
|
|
21
22
|
* that count is the job's own, so the job's `--want` bounds the run.
|
|
22
23
|
*
|
|
24
|
+
* The pool also charges memory: each leaf is charged its task's last recorded peak, and granted only while the charges
|
|
25
|
+
* of the leaves holding grants and its own fit the run's memory (`host-memory.ts`: the cgroup's `memory.max` on Linux,
|
|
26
|
+
* else the host's). A run whose memory cannot be read does not start.
|
|
27
|
+
*
|
|
23
28
|
* The pool runs on a thread of its own (`cpu-pool-thread.ts`). The process that serves it, the Nx daemon when installed
|
|
24
29
|
* plugins load in it or the Nx CLI without one, hashes the run's tasks and builds its project graph in synchronous calls
|
|
25
30
|
* on its main thread. In a gate on 2026-10-28 one `hashPlans` ran for 14.2 s, and sixteen runners queued on a pool on
|
|
@@ -51,16 +56,21 @@ export function runPoolTokens(env, cores) {
|
|
|
51
56
|
return inherited === null ? cores : Math.min(inherited, cores);
|
|
52
57
|
}
|
|
53
58
|
/**
|
|
54
|
-
* Serve `context`'s run a pool of `runPoolTokens
|
|
55
|
-
*
|
|
56
|
-
*
|
|
59
|
+
* Serve `context`'s run a pool of `runPoolTokens`, charged against the run's memory `readMemory` reads, and publish it
|
|
60
|
+
* in `env`, which every task inherits, unless the run is served none. A run whose memory cannot be read throws
|
|
61
|
+
* `RunMemoryUnreadable`, which fails the run before any task starts: it is not shown to have room. A pool that cannot
|
|
62
|
+
* be made is said, and the run's tasks are then sized only by the ceilings they inherit.
|
|
57
63
|
*/
|
|
58
|
-
export async function serveRunCpuPool(context, env = process.env, cores = availableParallelism()) {
|
|
64
|
+
export async function serveRunCpuPool(context, env = process.env, cores = availableParallelism(), readMemory = readRunMemory) {
|
|
59
65
|
const tokens = runPoolTokens(env, cores);
|
|
60
66
|
if (tokens === null) {
|
|
61
67
|
return;
|
|
62
68
|
}
|
|
63
|
-
const
|
|
69
|
+
const memory = await readMemory();
|
|
70
|
+
if (!memory.ok) {
|
|
71
|
+
throw new RunMemoryUnreadable(memory.error);
|
|
72
|
+
}
|
|
73
|
+
const served = await servePoolThread(tokens, memory.memory.bytes);
|
|
64
74
|
if (!served.ok) {
|
|
65
75
|
logger.warn(`bounded-exec: this run's CPU pool failed (${served.error}); its tasks are sized only by the ceilings they inherit`);
|
|
66
76
|
return;
|
|
@@ -76,8 +86,11 @@ export async function serveRunCpuPool(context, env = process.env, cores = availa
|
|
|
76
86
|
* has said how its pool closed fails whichever wait is open, a later close returns that failure, and a thread lost while
|
|
77
87
|
* its pool serves is said at once.
|
|
78
88
|
*/
|
|
79
|
-
function servePoolThread(tokens) {
|
|
80
|
-
const thread = new Worker(THREAD_ENTRY, {
|
|
89
|
+
function servePoolThread(tokens, memory) {
|
|
90
|
+
const thread = new Worker(THREAD_ENTRY, {
|
|
91
|
+
workerData: { tokens, memory },
|
|
92
|
+
...THREAD_PRELOAD,
|
|
93
|
+
});
|
|
81
94
|
return new Promise((served) => {
|
|
82
95
|
let state = { kind: 'starting', served };
|
|
83
96
|
let closing;
|
|
@@ -104,6 +104,13 @@ export interface BoundedExecRecord {
|
|
|
104
104
|
* expired; written for the reader of a refused run, never read back.
|
|
105
105
|
*/
|
|
106
106
|
readonly cpuWait?: CpuWaitRow;
|
|
107
|
+
/**
|
|
108
|
+
* The command's memory peak in bytes when it ended (specs/cowshed/13_telemetry.md, "Job resource peaks"): its rusage
|
|
109
|
+
* `ru_maxrss`, the largest resident set any one process of its tree reached, as its waiter read it (`leaf-peak.ts`).
|
|
110
|
+
* Null when it was not measured: the command never ran, the waiter ended before it, or no Bun waited on it; never
|
|
111
|
+
* zero for that. The task's next run is charged it in its run's pool (`readLastPeak`).
|
|
112
|
+
*/
|
|
113
|
+
readonly peakBytes: number | null;
|
|
107
114
|
}
|
|
108
115
|
/** The Nx task a run executes as. Nx gives an executor no task id, only its parts. */
|
|
109
116
|
export interface BoundedExecTask {
|
|
@@ -223,6 +230,17 @@ export declare function clearTaskDirectory(directory: string): Promise<void>;
|
|
|
223
230
|
export declare function writeRecord(directory: string, record: BoundedExecRecord): Promise<void>;
|
|
224
231
|
/** The record in `directory`, or `null` when there is none. A malformed record throws. */
|
|
225
232
|
export declare function readRecord(directory: string): Promise<BoundedExecRecord | null>;
|
|
233
|
+
/**
|
|
234
|
+
* The peak the task's last record in `directory` names: its charge in its run's pool. Null when the task has no record
|
|
235
|
+
* or its record no peak; a record that cannot be read is an error naming it, never a charge of nothing.
|
|
236
|
+
*/
|
|
237
|
+
export declare function readLastPeak(directory: string): Promise<{
|
|
238
|
+
ok: true;
|
|
239
|
+
peakBytes: number | null;
|
|
240
|
+
} | {
|
|
241
|
+
ok: false;
|
|
242
|
+
error: string;
|
|
243
|
+
}>;
|
|
226
244
|
/** The JUnit report a command wrote into `directory`, or `null` when it wrote none. */
|
|
227
245
|
export declare function readJunitReport(directory: string, bunClosed?: (results: readonly NativeTestResult[]) => void): Promise<JunitFailures | null>;
|
|
228
246
|
/** A failed task of an Nx run, and whether its bounded-exec record shows only a bound. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verdict.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/verdict.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAmB,KAAK,QAAQ,EAAe,MAAM,gBAAgB,CAAC;AAC7E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAGhE;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,MAAM,kBAAkB,GAC1B;IAAE,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAA;CAAE,GAC9B,CAAC;IAAE,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAAG,kBAAkB,CAAC,GAChF;IACE,QAAQ,CAAC,OAAO,EAAE,OAAO,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;GAIG;AACH,wBAAsB,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CA6BrG;AAED,kFAAkF;AAClF,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,kBAAkB,GAAG,MAAM,CAQvE;AACD,wBAAgB,eAAe,CAAC,OAAO,EAAE,kBAAkB,GAAG,MAAM,CA2BnE;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,eAAe,GAAG,MAAM,CAgBjE"}
|
|
@@ -321,6 +321,21 @@ export async function readRecord(directory) {
|
|
|
321
321
|
}
|
|
322
322
|
return parseRecord(JSON.parse(text));
|
|
323
323
|
}
|
|
324
|
+
/**
|
|
325
|
+
* The peak the task's last record in `directory` names: its charge in its run's pool. Null when the task has no record
|
|
326
|
+
* or its record no peak; a record that cannot be read is an error naming it, never a charge of nothing.
|
|
327
|
+
*/
|
|
328
|
+
export async function readLastPeak(directory) {
|
|
329
|
+
try {
|
|
330
|
+
return { ok: true, peakBytes: (await readRecord(directory))?.peakBytes ?? null };
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
return {
|
|
334
|
+
ok: false,
|
|
335
|
+
error: `the task's last record ${join(directory, VERDICT_FILE)} cannot be read: ${error instanceof Error ? error.message : String(error)}`,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
}
|
|
324
339
|
/** The JUnit report a command wrote into `directory`, or `null` when it wrote none. */
|
|
325
340
|
export async function readJunitReport(directory, bunClosed) {
|
|
326
341
|
try {
|
|
@@ -341,7 +356,12 @@ function parseRecord(value) {
|
|
|
341
356
|
if (!isObject(value) || typeof value.task !== 'string' || !(typeof value.hash === 'string' || value.hash === null)) {
|
|
342
357
|
throw new Error(`not a bounded-exec record: ${JSON.stringify(value)}`);
|
|
343
358
|
}
|
|
344
|
-
|
|
359
|
+
// A record written before peaks were recorded carries none.
|
|
360
|
+
const peakBytes = value.peakBytes ?? null;
|
|
361
|
+
if (!(peakBytes === null || (typeof peakBytes === 'number' && Number.isSafeInteger(peakBytes) && peakBytes >= 0))) {
|
|
362
|
+
throw new Error(`not a bounded-exec record: ${JSON.stringify(value)}`);
|
|
363
|
+
}
|
|
364
|
+
return { task: value.task, hash: value.hash, verdict: parseVerdict(value.verdict), peakBytes };
|
|
345
365
|
}
|
|
346
366
|
function parseVerdict(value) {
|
|
347
367
|
if (isObject(value)) {
|
|
@@ -83,17 +83,46 @@ runs:
|
|
|
83
83
|
# Hash the exact identity (not a lossy path slug); keep cache artifacts
|
|
84
84
|
# and their metadata DB together, warm across runs of the same pull
|
|
85
85
|
# request or ref and job.
|
|
86
|
+
#
|
|
87
|
+
# A pull request's first run starts from its base branch's task records.
|
|
88
|
+
# Each bounded-exec leaf is charged its task's last recorded memory peak
|
|
89
|
+
# (specs/cowshed/05_gateway.md, "Without mbx, every Nx run budgets
|
|
90
|
+
# itself"), and a scope that holds no records charges every leaf nothing,
|
|
91
|
+
# so a pull request's first run would admit every leaf on CPU tokens
|
|
92
|
+
# alone. Its base branch's pushes keep their records in the base's scope:
|
|
93
|
+
# a pull request's scope that holds none takes a copy of each task's last
|
|
94
|
+
# record (its verdict.json, nothing beside it) from there, and its own
|
|
95
|
+
# runs replace them. The copy is the record itself, never a second store.
|
|
86
96
|
- name: 📂 Locate the Nx cache on the shared bind
|
|
87
97
|
if: steps.runner-kind.outputs.host == 'true'
|
|
88
98
|
shell: bash
|
|
89
99
|
env:
|
|
100
|
+
NX_CACHE_BIND: /var/cache/ci/nx
|
|
90
101
|
NX_CACHE_REPO: ${{ github.repository }}
|
|
91
102
|
NX_CACHE_LANE: ${{ github.workflow }}
|
|
92
103
|
SCOPE_REF: ${{ github.event.pull_request.number || github.ref }}
|
|
104
|
+
# A pull request's base branch, whose pushes run under refs/heads/<it>;
|
|
105
|
+
# empty for a push.
|
|
106
|
+
BASE_REF: ${{ github.base_ref }}
|
|
93
107
|
run: |
|
|
94
108
|
lane=$(printf '%s' "$NX_CACHE_LANE" | tr '[:upper:]' '[:lower:]' | tr -cs 'a-z0-9._-' '-')
|
|
95
|
-
|
|
96
|
-
|
|
109
|
+
# The cache root of the run identity SCOPE_REF names.
|
|
110
|
+
scope_root() {
|
|
111
|
+
scope=$(printf '%s\0%s\0%s\0%s\0%s' "$GITHUB_WORKFLOW" "$SCOPE_REF" "$GITHUB_JOB" "$RUNNER_OS" "$RUNNER_ARCH" | git hash-object --stdin)
|
|
112
|
+
printf '%s' "$NX_CACHE_BIND/$NX_CACHE_REPO/${lane:-default}/$scope"
|
|
113
|
+
}
|
|
114
|
+
NX_CACHE_ROOT=$(scope_root)
|
|
115
|
+
records="$NX_CACHE_ROOT/workspace-data/bounded-exec"
|
|
116
|
+
if [ -n "$BASE_REF" ] && [ ! -d "$records" ]; then
|
|
117
|
+
base="$(SCOPE_REF="refs/heads/$BASE_REF" scope_root)/workspace-data/bounded-exec"
|
|
118
|
+
for verdict in "$base"/*/verdict.json; do
|
|
119
|
+
[ -e "$verdict" ] || continue
|
|
120
|
+
task=$(basename "$(dirname "$verdict")")
|
|
121
|
+
mkdir -p "$records/$task"
|
|
122
|
+
cp "$verdict" "$records/$task/verdict.json"
|
|
123
|
+
done
|
|
124
|
+
if [ -d "$records" ]; then echo "task records: from the $BASE_REF scope"; fi
|
|
125
|
+
fi
|
|
97
126
|
mkdir -p "$NX_CACHE_ROOT/cache" "$NX_CACHE_ROOT/workspace-data"
|
|
98
127
|
{
|
|
99
128
|
echo "NX_CACHE_DIRECTORY=$NX_CACHE_ROOT/cache"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smoothbricks/nx-plugin",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./loader.js",
|
|
@@ -435,7 +435,7 @@
|
|
|
435
435
|
"{workspaceRoot}/packages/cowshed/crates/cowshed-core/src/port_zero.cjs"
|
|
436
436
|
],
|
|
437
437
|
"options": {
|
|
438
|
-
"command": "bun test --timeout=0 ./src/executors/bounded-exec/__tests__/cpu-pool.silence.ts",
|
|
438
|
+
"command": "bun test --config=/dev/null --timeout=0 ./src/executors/bounded-exec/__tests__/cpu-pool.silence.ts",
|
|
439
439
|
"cwd": "{projectRoot}",
|
|
440
440
|
"idleTimeoutMs": 180000,
|
|
441
441
|
"killAfterMs": 10000
|
|
@@ -9,13 +9,14 @@ import { type CloseOutcome, createCpuPool } from './cpu-pool.js';
|
|
|
9
9
|
* jobserver fifo and heartbeat share an event loop that runs nothing else, so the heartbeat proves that the actor which
|
|
10
10
|
* admits and grants is turning.
|
|
11
11
|
*
|
|
12
|
-
* The thread is started with the pool's count. It says once that the pool serves, with the environment a task inherits,
|
|
12
|
+
* The thread is started with the pool's count and the run's memory. It says once that the pool serves, with the environment a task inherits,
|
|
13
13
|
* or why it cannot; the host's one message is its close, after which the thread says how the pool closed.
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
-
/** What the thread is started with. */
|
|
16
|
+
/** What the thread is started with: the run's count, and the run's memory in bytes its grants are charged against. */
|
|
17
17
|
export interface CpuPoolThreadData {
|
|
18
18
|
readonly tokens: number;
|
|
19
|
+
readonly memory: number;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
/** What the thread says, in order: the pool serves or failed; after the host's close, how the pool closed. */
|
|
@@ -31,7 +32,7 @@ if (host === null) {
|
|
|
31
32
|
const report = (message: CpuPoolThreadReport): void => host.postMessage(message);
|
|
32
33
|
const data: CpuPoolThreadData = workerData;
|
|
33
34
|
// The pool lives until its host closes it, or with the thread.
|
|
34
|
-
const made = await createCpuPool(data.tokens, new Promise<void>(() => {}));
|
|
35
|
+
const made = await createCpuPool(data.tokens, new Promise<void>(() => {}), data.memory);
|
|
35
36
|
if (made.ok) {
|
|
36
37
|
const { pool } = made;
|
|
37
38
|
host.once('message', () => {
|
|
@@ -8,6 +8,7 @@ import typia from 'typia';
|
|
|
8
8
|
import { ownedSocketDirectory } from '../../testing.js';
|
|
9
9
|
import { holdLease, type Unleased } from './gateway-lease.js';
|
|
10
10
|
import { type CpuLeaseProgress, type CpuLeaseQueued, CpuLeaseWait, HEARTBEAT_MS } from './lease-wait.js';
|
|
11
|
+
import { admit } from './pool-admission.js';
|
|
11
12
|
import { CPU_TOKENS_ENV, parseInheritedTokens } from './runner-sizing.js';
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -23,6 +24,11 @@ import { CPU_TOKENS_ENV, parseInheritedTokens } from './runner-sizing.js';
|
|
|
23
24
|
* lasts while its connection does, so a child that dies returns its tokens with its socket. A pool
|
|
24
25
|
* grants only from its own count, and a pool whose upstream grant has gone grants nothing.
|
|
25
26
|
*
|
|
27
|
+
* A run's own pool also charges memory (`pool-admission.ts`; specs/cowshed/05_gateway.md, "Without mbx, every Nx run
|
|
28
|
+
* budgets itself"). A request names its task's last recorded peak as `memory`, in bytes, and is granted only while
|
|
29
|
+
* the charges of the leaves holding grants and its own fit the run's memory; a request that names none is charged
|
|
30
|
+
* nothing, as mbx's omitted `--memory` is. A sub-pool charges no memory: its holder's charge covers its tree.
|
|
31
|
+
*
|
|
26
32
|
* The same count is a GNU make jobserver fifo beside the socket, mode 0600, which Cargo, its build
|
|
27
33
|
* scripts and every nested `cargo` read through `CARGO_MAKEFLAGS`/`MAKEFLAGS` (specs/cowshed/05_gateway.md,
|
|
28
34
|
* "Cargo reads its holder's jobserver"). A pool of `g` holds `g − 1` token bytes there; the holder's own
|
|
@@ -55,17 +61,26 @@ export interface CpuPool {
|
|
|
55
61
|
|
|
56
62
|
export type CloseOutcome = { ok: true } | { ok: false; error: string };
|
|
57
63
|
|
|
58
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* The one line a child sends: a count of whole tokens, at least one, and the bytes it is charged, its task's last
|
|
66
|
+
* recorded memory peak. A child whose task has no recorded peak names none: it is charged nothing.
|
|
67
|
+
*/
|
|
59
68
|
interface CpuPoolRequest {
|
|
60
69
|
op: 'cpu-pool';
|
|
61
70
|
want: number & typia.tags.Type<'uint32'> & typia.tags.Minimum<1>;
|
|
71
|
+
memory?: number & typia.tags.Type<'uint64'>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A grant whose charge alone exceeds the run's memory, made while nothing else held memory: the run's memory. */
|
|
75
|
+
export interface Oversized {
|
|
76
|
+
readonly memory: number & typia.tags.Type<'uint64'>;
|
|
62
77
|
}
|
|
63
78
|
|
|
64
79
|
/** The pool's answers, in `holdLease`'s framing. */
|
|
65
80
|
type CpuPoolReply =
|
|
66
81
|
| CpuLeaseProgress
|
|
67
82
|
| CpuLeaseQueued
|
|
68
|
-
| { ok: true; lease: 'granted'; tokens: number }
|
|
83
|
+
| { ok: true; lease: 'granted'; tokens: number; oversized?: Oversized }
|
|
69
84
|
| { ok: false; code: 'invalid-request'; error: string };
|
|
70
85
|
|
|
71
86
|
/** Exactly a request's fields: any other field is an invalid request. */
|
|
@@ -94,7 +109,7 @@ const run = promisify(execFile);
|
|
|
94
109
|
type Phase =
|
|
95
110
|
| { readonly kind: 'reading' }
|
|
96
111
|
| { readonly kind: 'queued' }
|
|
97
|
-
| { readonly kind: 'holding'; readonly bytes: number; readonly slot: boolean }
|
|
112
|
+
| { readonly kind: 'holding'; readonly bytes: number; readonly slot: boolean; readonly memory: number }
|
|
98
113
|
| { readonly kind: 'done' };
|
|
99
114
|
|
|
100
115
|
/** A child connection and what the pool owes it. Its grant, or its place in line, ends with the connection. */
|
|
@@ -103,10 +118,11 @@ interface Child {
|
|
|
103
118
|
phase: Phase;
|
|
104
119
|
}
|
|
105
120
|
|
|
106
|
-
/** A queued child
|
|
121
|
+
/** A queued child, the count it asked for and the bytes it is charged. */
|
|
107
122
|
interface Waiter {
|
|
108
123
|
readonly child: Child;
|
|
109
124
|
readonly want: number;
|
|
125
|
+
readonly memory: number;
|
|
110
126
|
}
|
|
111
127
|
|
|
112
128
|
/** One framed reply line. The newline is the framing; every reply goes through here. */
|
|
@@ -127,21 +143,34 @@ export function readInheritedCpuPool(environment: Readonly<NodeJS.ProcessEnv>):
|
|
|
127
143
|
}
|
|
128
144
|
|
|
129
145
|
/**
|
|
130
|
-
* Tokens a parent pool lent, held until released, or why there are none. `
|
|
131
|
-
*
|
|
146
|
+
* Tokens a parent pool lent, held until released, or why there are none. `oversized` names the run's memory when the
|
|
147
|
+
* child's charge alone exceeds it and it was granted because nothing else held memory. `closed` resolves when the
|
|
148
|
+
* grant's connection closes, however it closes, so a sub-pool built on the grant ends with it.
|
|
132
149
|
*/
|
|
133
|
-
export type TokenGrant =
|
|
150
|
+
export type TokenGrant =
|
|
151
|
+
| { granted: true; tokens: number; oversized: Oversized | null; release: () => void; closed: Promise<void> }
|
|
152
|
+
| Unleased;
|
|
153
|
+
|
|
154
|
+
const isOversized = typia.createEquals<Oversized>();
|
|
134
155
|
|
|
135
156
|
/**
|
|
136
|
-
* Ask the immediate parent pool for `want` tokens
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
157
|
+
* Ask the immediate parent pool for `want` tokens, charged `memory` bytes: the task's last recorded peak, or null when
|
|
158
|
+
* it has none. The inherited count does not bound the ask: a pool grants at most what it holds. A granted lease is the
|
|
159
|
+
* child's local allocation: its connection is the grant, and closing it returns only that allocation. An absent
|
|
160
|
+
* endpoint grants nothing and refuses nothing; a connected endpoint's refusal or protocol failure must fail admission
|
|
161
|
+
* rather than run the child beside its parent's count.
|
|
140
162
|
*/
|
|
141
|
-
export async function takeParentCpuTokens(
|
|
163
|
+
export async function takeParentCpuTokens(
|
|
164
|
+
parent: InheritedCpuPool,
|
|
165
|
+
want: number,
|
|
166
|
+
memory: number | null,
|
|
167
|
+
): Promise<TokenGrant> {
|
|
142
168
|
if (!Number.isSafeInteger(want) || want < 1) {
|
|
143
169
|
throw new RangeError(`a child asks its parent pool for a positive count, not ${want} (pool ${parent.tokens})`);
|
|
144
170
|
}
|
|
171
|
+
if (memory !== null && (!Number.isSafeInteger(memory) || memory < 0)) {
|
|
172
|
+
throw new RangeError(`a child is charged a whole number of bytes, not ${memory}`);
|
|
173
|
+
}
|
|
145
174
|
if (parent.socket.length === 0 || parent.socket.includes('\0')) {
|
|
146
175
|
return {
|
|
147
176
|
granted: false,
|
|
@@ -149,11 +178,12 @@ export async function takeParentCpuTokens(parent: InheritedCpuPool, want: number
|
|
|
149
178
|
reason: `the parent CPU pool's endpoint ${JSON.stringify(parent.socket)} is not a socket path`,
|
|
150
179
|
};
|
|
151
180
|
}
|
|
152
|
-
const
|
|
181
|
+
const request = memory === null ? { op: 'cpu-pool', want } : { op: 'cpu-pool', want, memory };
|
|
182
|
+
const lease = await holdLease(parent.socket, request, 'the parent CPU pool', new CpuLeaseWait());
|
|
153
183
|
if (!lease.granted) {
|
|
154
184
|
return lease;
|
|
155
185
|
}
|
|
156
|
-
const tokens = lease.answer
|
|
186
|
+
const { tokens, oversized } = lease.answer;
|
|
157
187
|
if (typeof tokens !== 'number' || !Number.isInteger(tokens) || tokens < 1 || tokens > want) {
|
|
158
188
|
lease.release();
|
|
159
189
|
return {
|
|
@@ -162,7 +192,15 @@ export async function takeParentCpuTokens(parent: InheritedCpuPool, want: number
|
|
|
162
192
|
reason: `the parent CPU pool granted ${JSON.stringify(tokens)} tokens for ${want}`,
|
|
163
193
|
};
|
|
164
194
|
}
|
|
165
|
-
|
|
195
|
+
if (oversized !== undefined && !isOversized(oversized)) {
|
|
196
|
+
lease.release();
|
|
197
|
+
return {
|
|
198
|
+
granted: false,
|
|
199
|
+
cause: 'other',
|
|
200
|
+
reason: `the parent CPU pool named its memory ${JSON.stringify(oversized)} in a grant, which is no byte count`,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
return { granted: true, tokens, oversized: oversized ?? null, release: lease.release, closed: lease.closed };
|
|
166
204
|
}
|
|
167
205
|
|
|
168
206
|
/** A thrown value's message. */
|
|
@@ -315,12 +353,14 @@ function watchFifo(
|
|
|
315
353
|
|
|
316
354
|
/**
|
|
317
355
|
* Make a pool of `tokens` that lives while `closed` does: when `closed` resolves the pool refuses
|
|
318
|
-
* grants, ends its child connections, drains its jobserver fifo and removes its directory.
|
|
319
|
-
*
|
|
356
|
+
* grants, ends its child connections, drains its jobserver fifo and removes its directory. A run's own
|
|
357
|
+
* pool charges its grants against `memory`, the run's memory in bytes; a sub-pool charges none (null).
|
|
358
|
+
* Setup failures are values; a non-positive or unsafe count is a programmer bug and throws.
|
|
320
359
|
*/
|
|
321
360
|
export async function createCpuPool(
|
|
322
361
|
tokens: number,
|
|
323
362
|
closed: Promise<void>,
|
|
363
|
+
memory: number | null,
|
|
324
364
|
): Promise<{ ok: true; pool: CpuPool } | { ok: false; error: string }> {
|
|
325
365
|
if (!Number.isSafeInteger(tokens) || tokens < 1) {
|
|
326
366
|
throw new RangeError(`a CPU pool holds a positive safe integer of tokens, not ${tokens}`);
|
|
@@ -347,6 +387,8 @@ export async function createCpuPool(
|
|
|
347
387
|
const scratch = Buffer.allocUnsafe(tokens);
|
|
348
388
|
// The holder's implicit first token, while a socket child holds it.
|
|
349
389
|
let slotLent = false;
|
|
390
|
+
// The bytes the children holding grants are charged, against `memory`.
|
|
391
|
+
let heldMemory = 0;
|
|
350
392
|
// The readability wait while the queue's head finds no token, else undefined.
|
|
351
393
|
let watch: FifoWatch | undefined;
|
|
352
394
|
let closing = false;
|
|
@@ -404,46 +446,62 @@ export async function createCpuPool(
|
|
|
404
446
|
}
|
|
405
447
|
};
|
|
406
448
|
|
|
407
|
-
// FIFO: the head takes `min(want, available)` as one grant once any token is available,
|
|
408
|
-
//
|
|
409
|
-
//
|
|
410
|
-
// child needs. Whatever bytes are in hand when no head can
|
|
449
|
+
// FIFO (`admit`): the head takes `min(want, available)` as one grant once any token is available and, in a run's
|
|
450
|
+
// pool, its charge fits beside the holders'; available is the fifo bytes it can read without blocking plus the
|
|
451
|
+
// implicit slot while no child holds it. `arrived` is bytes the readability wait already took. A waiting child holds
|
|
452
|
+
// nothing, so no child waits while it holds tokens another child needs. Whatever bytes are in hand when no head can
|
|
453
|
+
// use them go straight back to the fifo. A head that waits for memory watches the fifo for nothing: only a holder's
|
|
454
|
+
// close frees memory, and that close drains again.
|
|
411
455
|
function drain(arrived: number): void {
|
|
456
|
+
if (closing) {
|
|
457
|
+
stopWatching();
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
// A child that half-closed or failed while queued leaves the line. Its `close` finds it gone from the queue.
|
|
461
|
+
for (let at = queue.length - 1; at >= 0; at -= 1) {
|
|
462
|
+
if (queue[at]?.child.socket.writable === false) queue.splice(at, 1);
|
|
463
|
+
}
|
|
412
464
|
let inHand = arrived;
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
}
|
|
423
|
-
if (inHand < waiter.want) {
|
|
424
|
-
try {
|
|
425
|
-
inHand += readTokens(jobserver, scratch, waiter.want - inHand);
|
|
426
|
-
} catch (error) {
|
|
427
|
-
fail(`its jobserver fifo could not be read: ${describe(error)}`);
|
|
428
|
-
return;
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
const granted = Math.min(waiter.want, inHand + (slotLent ? 0 : 1));
|
|
432
|
-
if (granted === 0) {
|
|
433
|
-
watchForTokens();
|
|
465
|
+
const wanted = Math.min(
|
|
466
|
+
queue.reduce((sum, waiter) => sum + waiter.want, 0),
|
|
467
|
+
scratch.length,
|
|
468
|
+
);
|
|
469
|
+
if (inHand < wanted) {
|
|
470
|
+
try {
|
|
471
|
+
inHand += readTokens(jobserver, scratch, wanted - inHand);
|
|
472
|
+
} catch (error) {
|
|
473
|
+
fail(`its jobserver fifo could not be read: ${describe(error)}`);
|
|
434
474
|
return;
|
|
435
475
|
}
|
|
436
|
-
|
|
476
|
+
}
|
|
477
|
+
const admission = admit(
|
|
478
|
+
queue,
|
|
479
|
+
inHand + (slotLent ? 0 : 1),
|
|
480
|
+
memory === null ? null : { capacity: memory, held: heldMemory },
|
|
481
|
+
);
|
|
482
|
+
queue.splice(0, admission.grants.length);
|
|
483
|
+
for (const { request, tokens: granted, oversized } of admission.grants) {
|
|
437
484
|
// Fifo bytes first, the implicit slot last.
|
|
438
485
|
const bytes = Math.min(granted, inHand);
|
|
439
486
|
inHand -= bytes;
|
|
440
487
|
const slot = granted > bytes;
|
|
441
488
|
if (slot) slotLent = true;
|
|
442
|
-
|
|
443
|
-
|
|
489
|
+
heldMemory += request.memory;
|
|
490
|
+
request.child.phase = { kind: 'holding', bytes, slot, memory: request.memory };
|
|
491
|
+
request.child.socket.write(
|
|
492
|
+
frame(
|
|
493
|
+
oversized && memory !== null
|
|
494
|
+
? { ok: true, lease: 'granted', tokens: granted, oversized: { memory } }
|
|
495
|
+
: { ok: true, lease: 'granted', tokens: granted },
|
|
496
|
+
),
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
if (admission.waiting === 'cpu') {
|
|
500
|
+
watchForTokens();
|
|
501
|
+
} else {
|
|
502
|
+
stopWatching();
|
|
444
503
|
}
|
|
445
|
-
|
|
446
|
-
if (!closing) returnTokens(inHand);
|
|
504
|
+
returnTokens(inHand);
|
|
447
505
|
}
|
|
448
506
|
|
|
449
507
|
const refuse = (child: Child, error: string): void => {
|
|
@@ -471,6 +529,7 @@ export async function createCpuPool(
|
|
|
471
529
|
} else if (child.phase.kind === 'holding') {
|
|
472
530
|
// A closing pool drains its fifo whole; until then the bytes go back for the next holder.
|
|
473
531
|
if (child.phase.slot) slotLent = false;
|
|
532
|
+
heldMemory -= child.phase.memory;
|
|
474
533
|
if (!closing) returnTokens(child.phase.bytes);
|
|
475
534
|
}
|
|
476
535
|
child.phase = { kind: 'done' };
|
|
@@ -523,12 +582,15 @@ export async function createCpuPool(
|
|
|
523
582
|
parsed = undefined;
|
|
524
583
|
}
|
|
525
584
|
if (!isRequest(parsed)) {
|
|
526
|
-
refuse(
|
|
585
|
+
refuse(
|
|
586
|
+
child,
|
|
587
|
+
'a request is {"op":"cpu-pool","want":<a whole number of tokens, at least 1>,"memory"?:<bytes it is charged>}',
|
|
588
|
+
);
|
|
527
589
|
return;
|
|
528
590
|
}
|
|
529
591
|
const request = parsed;
|
|
530
592
|
child.phase = { kind: 'queued' };
|
|
531
|
-
queue.push({ child, want: request.want });
|
|
593
|
+
queue.push({ child, want: request.want, memory: request.memory ?? 0 });
|
|
532
594
|
socket.write(frame({ ok: true, lease: 'queued', server: process.pid }));
|
|
533
595
|
drain(0);
|
|
534
596
|
});
|