@isentinel/machine-budget 0.0.0-stage → 0.1.0
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/LICENSE.md +21 -0
- package/README.md +90 -2
- package/dist/index.d.mts +112 -0
- package/dist/index.mjs +1115 -0
- package/package.json +35 -3
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Christopher Buss
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,91 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @isentinel/machine-budget
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Leases memory and threads to Node dev tools from one budget shared by the whole
|
|
4
|
+
machine. Two lint runs in two worktrees, a mutation test and a type check each
|
|
5
|
+
ask the budget before they start workers, so together they do not run the
|
|
6
|
+
machine out of memory.
|
|
7
|
+
|
|
8
|
+
The budget is a SQLite ledger (`node:sqlite`) in the system temp directory.
|
|
9
|
+
Every process of one user on one machine opens the same ledger. A process that
|
|
10
|
+
dies loses its leases at the next lease attempt of any other process.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install --save-dev @isentinel/machine-budget
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Requires Node 24.12 or later, or Bun 1.4 or later.
|
|
19
|
+
|
|
20
|
+
## Use
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { openMachineBudget } from "@isentinel/machine-budget";
|
|
24
|
+
|
|
25
|
+
const budget = openMachineBudget({ log: console.error, workspaceRoot });
|
|
26
|
+
const lease = await budget.leaseAsync({
|
|
27
|
+
cost: { pass: "type-aware", project: "app" },
|
|
28
|
+
maxWorkers: 6,
|
|
29
|
+
memoryPerWorker: 2 * 2 ** 30,
|
|
30
|
+
minWorkers: 1,
|
|
31
|
+
});
|
|
32
|
+
// Start the child with lease.workers workers.
|
|
33
|
+
lease.release({ peakMemory });
|
|
34
|
+
budget.close();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`leaseAsync` waits until the minimum fits, then grants the most workers that
|
|
38
|
+
fit. A request with a `cost` uses the learned memory of one worker once three
|
|
39
|
+
peaks are known, and `memoryPerWorker` before that. Costs are kept per git
|
|
40
|
+
clone: all worktrees of one clone share them. A waiting request prints one line
|
|
41
|
+
through `log`. `isShared` is true when leases come from the shared ledger, and
|
|
42
|
+
false when the budget is off.
|
|
43
|
+
|
|
44
|
+
A run that resizes while it runs holds one lease for each worker. `tryLease`
|
|
45
|
+
grants the most workers that fit now, or returns `undefined`, and never waits.
|
|
46
|
+
`runCapWorkers` tells how many workers of a request the run cap lets the run
|
|
47
|
+
hold now, its own leases included; the run frees leases above it. A request with
|
|
48
|
+
`isMainLaneOnly` never takes the small-job lane, for leases held long.
|
|
49
|
+
|
|
50
|
+
## Measure a process tree
|
|
51
|
+
|
|
52
|
+
A child that runs its workers as separate processes reports two peaks:
|
|
53
|
+
`peakMemory` for the whole tree and `peakPerWorker` for its biggest worker. A
|
|
54
|
+
release with `peakPerWorker` learns that value as the memory of one worker, in
|
|
55
|
+
place of `peakMemory` divided by the workers.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { watchTree } from "@isentinel/machine-budget";
|
|
59
|
+
|
|
60
|
+
import assert from "node:assert";
|
|
61
|
+
|
|
62
|
+
const child = spawn(process.execPath, ["pool.js", String(lease.workers)], {
|
|
63
|
+
windowsHide: true,
|
|
64
|
+
});
|
|
65
|
+
assert(child.pid !== undefined);
|
|
66
|
+
const watch = watchTree(child.pid);
|
|
67
|
+
await once(child, "exit");
|
|
68
|
+
const { peakMemory, peakPerWorker } = watch.stop();
|
|
69
|
+
lease.release({ peakMemory: peakMemory ?? 0, peakPerWorker });
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Call `watchTree` right after the spawn. It samples the tree ten times a second,
|
|
73
|
+
so a process that starts and exits between two samples is missed.
|
|
74
|
+
|
|
75
|
+
- Windows: the child joins a Job Object. `peakMemory` is the peak commit of the
|
|
76
|
+
job, and `peakPerWorker` is the highest peak commit of one descendant.
|
|
77
|
+
- Linux: `peakPerWorker` is the highest peak resident set (`VmHWM`) of one
|
|
78
|
+
descendant, and `peakMemory` is the highest sum of resident sets over the
|
|
79
|
+
samples.
|
|
80
|
+
- Elsewhere both peaks are `undefined`. `watchTree` never throws.
|
|
81
|
+
|
|
82
|
+
## Environment
|
|
83
|
+
|
|
84
|
+
- `MACHINE_BUDGET=off` grants every request its maximum at once. `CI` set to any
|
|
85
|
+
value other than `false` does the same.
|
|
86
|
+
- `MACHINE_BUDGET_DIR` moves the ledger. The default is `machine-budget-<user>`
|
|
87
|
+
in the system temp directory.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
//#region src/admission.d.ts
|
|
2
|
+
/** The worker range and the memory of one worker. */
|
|
3
|
+
interface WorkerRequest {
|
|
4
|
+
/** Keeps the request out of the small-job lane, for leases held long. */
|
|
5
|
+
isMainLaneOnly?: boolean;
|
|
6
|
+
/** The most workers the client can use. */
|
|
7
|
+
maxWorkers: number;
|
|
8
|
+
/** The memory of one worker, in bytes. */
|
|
9
|
+
memoryPerWorker: number;
|
|
10
|
+
/** The fewest workers the client can run with. */
|
|
11
|
+
minWorkers: number;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/learned-cost.d.ts
|
|
15
|
+
/** What a cost is learned for, inside one clone. */
|
|
16
|
+
interface CostScope {
|
|
17
|
+
/** The pass of the tool, such as `type-aware`. */
|
|
18
|
+
pass: string;
|
|
19
|
+
/** The project the worker runs for. */
|
|
20
|
+
project: string;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/budget-types.d.ts
|
|
24
|
+
/** What one child process asks for. */
|
|
25
|
+
interface LeaseRequest extends WorkerRequest {
|
|
26
|
+
/** What to learn the cost for; without it, `memoryPerWorker` is used. */
|
|
27
|
+
cost?: CostScope;
|
|
28
|
+
}
|
|
29
|
+
/** What a client reports when its child exits. */
|
|
30
|
+
interface LeaseReport {
|
|
31
|
+
/** The peak memory of the child process, in bytes. */
|
|
32
|
+
peakMemory: number;
|
|
33
|
+
/**
|
|
34
|
+
* The highest peak of one worker process, in bytes; learned as is when
|
|
35
|
+
* given.
|
|
36
|
+
*/
|
|
37
|
+
peakPerWorker?: number;
|
|
38
|
+
}
|
|
39
|
+
/** Memory and threads that one child process holds. */
|
|
40
|
+
interface Lease {
|
|
41
|
+
/** The memory of one worker that the lease counts, in bytes. */
|
|
42
|
+
memoryPerWorker: number;
|
|
43
|
+
/** Gives the memory and threads back, and records the peak if reported. */
|
|
44
|
+
release: (report?: LeaseReport) => void;
|
|
45
|
+
/** The workers that the child may run. */
|
|
46
|
+
workers: number;
|
|
47
|
+
}
|
|
48
|
+
/** The machine budget, as one run sees it. */
|
|
49
|
+
interface MachineBudget {
|
|
50
|
+
/** Leaves the budget and gives back every lease of the run. */
|
|
51
|
+
close: () => void;
|
|
52
|
+
/** True when leases come from the ledger that other runs share. */
|
|
53
|
+
readonly isShared: boolean;
|
|
54
|
+
/** Waits until the request fits, then leases the most workers that fit. */
|
|
55
|
+
leaseAsync: (request: LeaseRequest) => Promise<Lease>;
|
|
56
|
+
/** The most workers of the request that the run cap lets this run hold. */
|
|
57
|
+
runCapWorkers: (request: LeaseRequest) => number;
|
|
58
|
+
/** Leases the most workers that fit now, or nothing, and never waits. */
|
|
59
|
+
tryLease: (request: LeaseRequest) => Lease | undefined;
|
|
60
|
+
}
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/tree-watch.d.ts
|
|
63
|
+
/**
|
|
64
|
+
* The peaks of a process tree in bytes, undefined where the platform cannot
|
|
65
|
+
* read them.
|
|
66
|
+
*/
|
|
67
|
+
interface TreePeaks {
|
|
68
|
+
/** The peak memory of the whole tree. */
|
|
69
|
+
peakMemory: number | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* The highest peak of one descendant, which a lease reports as
|
|
72
|
+
* `peakPerWorker`.
|
|
73
|
+
*/
|
|
74
|
+
peakPerWorker: number | undefined;
|
|
75
|
+
}
|
|
76
|
+
/** A watch on a running process and every process it starts. */
|
|
77
|
+
interface TreeWatch {
|
|
78
|
+
/** Stops the watch and returns the peaks since it began. */
|
|
79
|
+
stop: () => TreePeaks;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
//#region src/index.d.ts
|
|
83
|
+
/** How a run joins the machine budget. */
|
|
84
|
+
export interface MachineBudgetOptions {
|
|
85
|
+
/** Prints the one line of a waiting request, such as `console.error`. */
|
|
86
|
+
log: (line: string) => void;
|
|
87
|
+
/** The root of the clone that runs; its git common directory keys costs. */
|
|
88
|
+
workspaceRoot: string;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Joins the budget that every dev tool on this machine shares. Close it when
|
|
92
|
+
* the run ends; a run that dies is taken back by the next run that leases.
|
|
93
|
+
* `MACHINE_BUDGET=off` or CI turns it off, and `MACHINE_BUDGET_DIR` moves the
|
|
94
|
+
* ledger.
|
|
95
|
+
*
|
|
96
|
+
* @param options - The logger and the workspace root.
|
|
97
|
+
* @returns The budget of this run.
|
|
98
|
+
*/
|
|
99
|
+
export declare function openMachineBudget(options: MachineBudgetOptions): MachineBudget;
|
|
100
|
+
/**
|
|
101
|
+
* Watches a running process and every process it starts from now on, to
|
|
102
|
+
* report the peaks that a lease release takes. Call it right after the spawn;
|
|
103
|
+
* a descendant that starts and ends between two samples is missed. On Windows
|
|
104
|
+
* the root joins a Job Object; on Linux the peaks are resident sets read from
|
|
105
|
+
* `/proc`; elsewhere both peaks are undefined. It never throws.
|
|
106
|
+
*
|
|
107
|
+
* @param pid - The pid of the process at the top of the tree.
|
|
108
|
+
* @returns The watch; stop it once the process exits.
|
|
109
|
+
*/
|
|
110
|
+
export declare function watchTree(pid: number): TreeWatch;
|
|
111
|
+
//#endregion
|
|
112
|
+
export type { CostScope, Lease, LeaseReport, LeaseRequest, MachineBudget, TreePeaks, TreeWatch };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1115 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
import { execFileSync } from "node:child_process";
|
|
4
|
+
import { mkdirSync, readFileSync, readdirSync } from "node:fs";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { setTimeout } from "node:timers/promises";
|
|
8
|
+
import { type } from "arktype";
|
|
9
|
+
import { DatabaseSync } from "node:sqlite";
|
|
10
|
+
import { Buffer } from "node:buffer";
|
|
11
|
+
import { randomUUID } from "node:crypto";
|
|
12
|
+
import { doNothing } from "remeda";
|
|
13
|
+
//#region src/learned-cost.ts
|
|
14
|
+
const MINIMUM_SAMPLES = 3;
|
|
15
|
+
const PERCENTILE = .9;
|
|
16
|
+
/**
|
|
17
|
+
* Names a learned cost: the clone's git common directory, the project and the
|
|
18
|
+
* pass. All worktrees of one clone share the common directory, so they share
|
|
19
|
+
* costs.
|
|
20
|
+
*
|
|
21
|
+
* @param gitCommonDirectory - The git common directory of the clone.
|
|
22
|
+
* @param scope - The project and the pass.
|
|
23
|
+
* @returns The key of the cost in the ledger.
|
|
24
|
+
*/
|
|
25
|
+
function costKey(gitCommonDirectory, scope) {
|
|
26
|
+
return JSON.stringify([
|
|
27
|
+
gitCommonDirectory,
|
|
28
|
+
scope.project,
|
|
29
|
+
scope.pass
|
|
30
|
+
]);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Estimates the memory of one worker from the peaks of earlier runs.
|
|
34
|
+
*
|
|
35
|
+
* @param samples - Peak memory of one worker in earlier runs, in bytes.
|
|
36
|
+
* @returns About the 90th percentile, or undefined with too few samples.
|
|
37
|
+
*/
|
|
38
|
+
function learnedCost(samples) {
|
|
39
|
+
if (samples.length < MINIMUM_SAMPLES) return;
|
|
40
|
+
const sorted = samples.toSorted((left, right) => left - right);
|
|
41
|
+
return sorted[Math.ceil(sorted.length * PERCENTILE) - 1];
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/ledger.ts
|
|
45
|
+
const LEDGER_FORMAT = 1;
|
|
46
|
+
const BUSY_TIMEOUT_MS = 3e4;
|
|
47
|
+
const LAST_SEEN = "last_seen_at";
|
|
48
|
+
const SCHEMA = `
|
|
49
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
50
|
+
id TEXT PRIMARY KEY,
|
|
51
|
+
pid INTEGER NOT NULL,
|
|
52
|
+
started_at INTEGER NOT NULL,
|
|
53
|
+
heartbeat_at INTEGER NOT NULL
|
|
54
|
+
);
|
|
55
|
+
CREATE TABLE IF NOT EXISTS leases (
|
|
56
|
+
id TEXT PRIMARY KEY,
|
|
57
|
+
session TEXT NOT NULL,
|
|
58
|
+
lane TEXT NOT NULL,
|
|
59
|
+
memory INTEGER NOT NULL,
|
|
60
|
+
threads INTEGER NOT NULL
|
|
61
|
+
);
|
|
62
|
+
CREATE TABLE IF NOT EXISTS waiters (
|
|
63
|
+
id TEXT PRIMARY KEY,
|
|
64
|
+
session TEXT NOT NULL,
|
|
65
|
+
is_big INTEGER NOT NULL,
|
|
66
|
+
since INTEGER NOT NULL
|
|
67
|
+
);
|
|
68
|
+
CREATE TABLE IF NOT EXISTS costs (
|
|
69
|
+
key TEXT NOT NULL,
|
|
70
|
+
at INTEGER NOT NULL,
|
|
71
|
+
bytes INTEGER NOT NULL
|
|
72
|
+
);
|
|
73
|
+
CREATE INDEX IF NOT EXISTS costs_by_key ON costs (key, at);
|
|
74
|
+
CREATE TABLE IF NOT EXISTS meta (
|
|
75
|
+
key TEXT PRIMARY KEY,
|
|
76
|
+
value INTEGER NOT NULL
|
|
77
|
+
);
|
|
78
|
+
`;
|
|
79
|
+
const sessionRows = type({
|
|
80
|
+
id: "string",
|
|
81
|
+
heartbeatAt: "number",
|
|
82
|
+
pid: "number",
|
|
83
|
+
startedAt: "number"
|
|
84
|
+
}).array();
|
|
85
|
+
const leaseRows = type({
|
|
86
|
+
id: "string",
|
|
87
|
+
lane: "'main' | 'small'",
|
|
88
|
+
memory: "number",
|
|
89
|
+
session: "string",
|
|
90
|
+
threads: "number"
|
|
91
|
+
}).array();
|
|
92
|
+
const waiterRows = type({
|
|
93
|
+
id: "string",
|
|
94
|
+
isBig: type("0 | 1").pipe((bit) => bit === 1),
|
|
95
|
+
session: "string",
|
|
96
|
+
since: "number"
|
|
97
|
+
}).array();
|
|
98
|
+
const valueRows = type({ value: "number" }).array();
|
|
99
|
+
/**
|
|
100
|
+
* Names the ledger file in a directory. A new format takes a new file, so an
|
|
101
|
+
* older client never reads a format it does not know.
|
|
102
|
+
*
|
|
103
|
+
* @param directory - Where the machine keeps its ledger.
|
|
104
|
+
* @returns The path of the ledger file.
|
|
105
|
+
*/
|
|
106
|
+
function ledgerPath(directory) {
|
|
107
|
+
return path.join(directory, `ledger-v${LEDGER_FORMAT}.sqlite`);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Opens the ledger, and makes its tables when they do not exist. The
|
|
111
|
+
* directory of the file must exist.
|
|
112
|
+
*
|
|
113
|
+
* @param file - A path from `ledgerPath`, or `:memory:` for a private one.
|
|
114
|
+
* @returns A handle that runs transactions on the file.
|
|
115
|
+
*/
|
|
116
|
+
function openLedger(file) {
|
|
117
|
+
const database = openDatabase(file);
|
|
118
|
+
function query(sql) {
|
|
119
|
+
return database.prepare(sql);
|
|
120
|
+
}
|
|
121
|
+
const transaction = {
|
|
122
|
+
...metaTable(query),
|
|
123
|
+
...sessionTable(query),
|
|
124
|
+
...leaseTable(query),
|
|
125
|
+
...waiterTable(query),
|
|
126
|
+
...costTable(query)
|
|
127
|
+
};
|
|
128
|
+
return {
|
|
129
|
+
close: () => {
|
|
130
|
+
database.close();
|
|
131
|
+
},
|
|
132
|
+
transact: (work) => {
|
|
133
|
+
database.exec("BEGIN IMMEDIATE");
|
|
134
|
+
try {
|
|
135
|
+
const result = work(transaction);
|
|
136
|
+
database.exec("COMMIT");
|
|
137
|
+
return result;
|
|
138
|
+
} catch (err) {
|
|
139
|
+
database.exec("ROLLBACK");
|
|
140
|
+
throw err;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function openDatabase(file) {
|
|
146
|
+
const database = new DatabaseSync(file, { timeout: BUSY_TIMEOUT_MS });
|
|
147
|
+
database.exec("PRAGMA journal_mode = WAL");
|
|
148
|
+
database.exec(SCHEMA);
|
|
149
|
+
return database;
|
|
150
|
+
}
|
|
151
|
+
function parseRows(schema, rows) {
|
|
152
|
+
const parsed = schema(rows.map((row) => ({ ...row })));
|
|
153
|
+
if (parsed instanceof type.errors) throw new Error(`machine budget ledger holds an invalid row: ${parsed.summary}`);
|
|
154
|
+
return parsed;
|
|
155
|
+
}
|
|
156
|
+
function metaTable(query) {
|
|
157
|
+
return {
|
|
158
|
+
lastSeenAt: () => {
|
|
159
|
+
const rows = query("SELECT value FROM meta WHERE key = ?").all(LAST_SEEN);
|
|
160
|
+
return parseRows(valueRows, rows)[0]?.value;
|
|
161
|
+
},
|
|
162
|
+
markSeen: (at) => {
|
|
163
|
+
query("INSERT INTO meta (key, value) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value").run(LAST_SEEN, at);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function sessionTable(query) {
|
|
168
|
+
return {
|
|
169
|
+
refreshHeartbeats: (at) => {
|
|
170
|
+
query("UPDATE sessions SET heartbeat_at = ?").run(at);
|
|
171
|
+
},
|
|
172
|
+
removeSessions: (ids) => {
|
|
173
|
+
for (const id of ids) {
|
|
174
|
+
query("DELETE FROM leases WHERE session = ?").run(id);
|
|
175
|
+
query("DELETE FROM waiters WHERE session = ?").run(id);
|
|
176
|
+
query("DELETE FROM sessions WHERE id = ?").run(id);
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
sessions: () => {
|
|
180
|
+
const rows = query("SELECT id, heartbeat_at AS heartbeatAt, pid, started_at AS startedAt FROM sessions").all();
|
|
181
|
+
return parseRows(sessionRows, rows);
|
|
182
|
+
},
|
|
183
|
+
upsertSession: ({ id, heartbeatAt, pid, startedAt }) => {
|
|
184
|
+
query("INSERT INTO sessions (id, pid, started_at, heartbeat_at) VALUES (?, ?, ?, ?) ON CONFLICT (id) DO UPDATE SET heartbeat_at = excluded.heartbeat_at").run(id, pid, startedAt, heartbeatAt);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function leaseTable(query) {
|
|
189
|
+
return {
|
|
190
|
+
addLease: ({ id, lane, memory, session, threads }) => {
|
|
191
|
+
query("INSERT OR IGNORE INTO leases (id, session, lane, memory, threads) VALUES (?, ?, ?, ?, ?)").run(id, session, lane, memory, threads);
|
|
192
|
+
},
|
|
193
|
+
leases: () => {
|
|
194
|
+
const rows = query("SELECT id, lane, memory, session, threads FROM leases").all();
|
|
195
|
+
return parseRows(leaseRows, rows);
|
|
196
|
+
},
|
|
197
|
+
removeLease: (id) => {
|
|
198
|
+
query("DELETE FROM leases WHERE id = ?").run(id);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function waiterTable(query) {
|
|
203
|
+
return {
|
|
204
|
+
removeWaiter: (id) => {
|
|
205
|
+
query("DELETE FROM waiters WHERE id = ?").run(id);
|
|
206
|
+
},
|
|
207
|
+
upsertWaiter: ({ id, isBig, session, since }) => {
|
|
208
|
+
query("INSERT OR IGNORE INTO waiters (id, session, is_big, since) VALUES (?, ?, ?, ?)").run(id, session, isBig ? 1 : 0, since);
|
|
209
|
+
},
|
|
210
|
+
waiters: () => {
|
|
211
|
+
const rows = query("SELECT id, is_big AS isBig, session, since FROM waiters").all();
|
|
212
|
+
return parseRows(waiterRows, rows);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function costTable(query) {
|
|
217
|
+
return {
|
|
218
|
+
addSample: ({ key, at, bytes }) => {
|
|
219
|
+
query("INSERT INTO costs (key, at, bytes) VALUES (?, ?, ?)").run(key, at, bytes);
|
|
220
|
+
query("DELETE FROM costs WHERE key = ? AND rowid NOT IN (SELECT rowid FROM costs WHERE key = ? ORDER BY at DESC, rowid DESC LIMIT ?)").run(key, key, 20);
|
|
221
|
+
},
|
|
222
|
+
costSamples: (key) => {
|
|
223
|
+
const rows = query("SELECT bytes AS value FROM costs WHERE key = ? ORDER BY at DESC, rowid DESC").all(key);
|
|
224
|
+
return parseRows(valueRows, rows).map((row) => row.value);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
//#endregion
|
|
229
|
+
//#region src/memory-probe.ts
|
|
230
|
+
const PAGE_SIZE = /page size of (\d+) bytes/;
|
|
231
|
+
const AVAILABLE_PAGES = /* @__PURE__ */ new Set([
|
|
232
|
+
"Pages free",
|
|
233
|
+
"Pages inactive",
|
|
234
|
+
"Pages purgeable"
|
|
235
|
+
]);
|
|
236
|
+
const PRESSURE_LEVELS = /* @__PURE__ */ new Map([
|
|
237
|
+
["1", "normal"],
|
|
238
|
+
["2", "warn"],
|
|
239
|
+
["4", "critical"]
|
|
240
|
+
]);
|
|
241
|
+
/**
|
|
242
|
+
* Reads the memory that can be given now. MacOS counts free, inactive and
|
|
243
|
+
* purgeable pages, and reports its pressure level; Windows and Linux report
|
|
244
|
+
* free memory, which on Linux is already the kernel's available estimate.
|
|
245
|
+
*
|
|
246
|
+
* @param sources - The operating system and how to read it.
|
|
247
|
+
* @returns The available memory and the pressure level.
|
|
248
|
+
*/
|
|
249
|
+
function readMemory(sources) {
|
|
250
|
+
if (sources.platform !== "darwin") return {
|
|
251
|
+
availableMemory: sources.freeMemory(),
|
|
252
|
+
pressure: "normal"
|
|
253
|
+
};
|
|
254
|
+
const vmStat = sources.run("vm_stat", []);
|
|
255
|
+
const level = sources.run("sysctl", ["-n", "kern.memorystatus_vm_pressure_level"]);
|
|
256
|
+
return {
|
|
257
|
+
availableMemory: (vmStat === void 0 ? void 0 : parseVmStat(vmStat)) ?? sources.freeMemory(),
|
|
258
|
+
pressure: parsePressure(level)
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
function parsePressure(level) {
|
|
262
|
+
if (level === void 0) return "normal";
|
|
263
|
+
return PRESSURE_LEVELS.get(level.trim()) ?? "normal";
|
|
264
|
+
}
|
|
265
|
+
function parseVmStat(output) {
|
|
266
|
+
const pageSize = PAGE_SIZE.exec(output)?.[1];
|
|
267
|
+
if (pageSize === void 0) return;
|
|
268
|
+
let pages = 0;
|
|
269
|
+
for (const line of output.split("\n")) {
|
|
270
|
+
const separator = line.indexOf(":");
|
|
271
|
+
if (AVAILABLE_PAGES.has(line.slice(0, separator))) pages += Number.parseInt(line.slice(separator + 1), 10);
|
|
272
|
+
}
|
|
273
|
+
return pages * Number(pageSize);
|
|
274
|
+
}
|
|
275
|
+
//#endregion
|
|
276
|
+
//#region src/tree-linux.ts
|
|
277
|
+
const BYTES_PER_KB = 1024;
|
|
278
|
+
const PARENT_FIELD = 1;
|
|
279
|
+
const PEAK_RESIDENT = /^VmHWM:\s+(\d+) kB$/m;
|
|
280
|
+
const RESIDENT = /^VmRSS:\s+(\d+) kB$/m;
|
|
281
|
+
/**
|
|
282
|
+
* Reads a process tree from `/proc`. Linux keeps no peak commit, so the peak
|
|
283
|
+
* of a descendant is its peak resident set, and the peak of the tree is the
|
|
284
|
+
* highest sum of resident sets over the samples.
|
|
285
|
+
*
|
|
286
|
+
* @param root - The pid at the top of the tree.
|
|
287
|
+
* @param source - How to read `/proc`.
|
|
288
|
+
* @returns The probe, which keeps the peaks of descendants that have exited.
|
|
289
|
+
*/
|
|
290
|
+
function linuxTreeProbe(root, source) {
|
|
291
|
+
const peaks = /* @__PURE__ */ new Map();
|
|
292
|
+
let treePeak;
|
|
293
|
+
return {
|
|
294
|
+
descendants: () => peaks,
|
|
295
|
+
sample: () => {
|
|
296
|
+
let resident = 0;
|
|
297
|
+
let isSeen = false;
|
|
298
|
+
for (const pid of [root, ...descendantsOf(source, root)]) {
|
|
299
|
+
const status = source.read(`/proc/${pid}/status`);
|
|
300
|
+
if (status === void 0) continue;
|
|
301
|
+
isSeen = true;
|
|
302
|
+
resident += bytes(status, RESIDENT);
|
|
303
|
+
if (pid !== root) peaks.set(pid, Math.max(peaks.get(pid) ?? 0, bytes(status, PEAK_RESIDENT)));
|
|
304
|
+
}
|
|
305
|
+
if (isSeen) treePeak = Math.max(treePeak ?? 0, resident);
|
|
306
|
+
},
|
|
307
|
+
tree: () => treePeak
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
function parentOf(source, pid) {
|
|
311
|
+
const stat = source.read(`/proc/${pid}/stat`);
|
|
312
|
+
if (stat === void 0) return;
|
|
313
|
+
return Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ", 2)[PARENT_FIELD]);
|
|
314
|
+
}
|
|
315
|
+
function descendantsOf(source, root) {
|
|
316
|
+
const children = /* @__PURE__ */ new Map();
|
|
317
|
+
for (const pid of source.pids()) {
|
|
318
|
+
const parent = parentOf(source, pid);
|
|
319
|
+
if (parent !== void 0) children.set(parent, [...children.get(parent) ?? [], pid]);
|
|
320
|
+
}
|
|
321
|
+
const found = [];
|
|
322
|
+
const pending = [root];
|
|
323
|
+
for (let pid = pending.pop(); pid !== void 0; pid = pending.pop()) {
|
|
324
|
+
const next = children.get(pid) ?? [];
|
|
325
|
+
found.push(...next);
|
|
326
|
+
pending.push(...next);
|
|
327
|
+
}
|
|
328
|
+
return found;
|
|
329
|
+
}
|
|
330
|
+
function bytes(status, field) {
|
|
331
|
+
return Number(field.exec(status)?.[1] ?? 0) * BYTES_PER_KB;
|
|
332
|
+
}
|
|
333
|
+
//#endregion
|
|
334
|
+
//#region src/tree-windows.ts
|
|
335
|
+
const QUERY_LIMITED_ACCESS = 4096;
|
|
336
|
+
const BASIC_PROCESS_ID_LIST = 3;
|
|
337
|
+
const EXTENDED_LIMIT_INFORMATION = 9;
|
|
338
|
+
const EXTENDED_LIMIT_SIZE = 144;
|
|
339
|
+
const PEAK_JOB_MEMORY_OFFSET = 136;
|
|
340
|
+
const LISTED_COUNT_OFFSET = 4;
|
|
341
|
+
const PROCESS_ID_LIST_HEADER = 8;
|
|
342
|
+
const PROCESS_ID_SIZE = 8;
|
|
343
|
+
const MEMORY_COUNTERS_SIZE = 72;
|
|
344
|
+
const PEAK_PAGEFILE_OFFSET = 64;
|
|
345
|
+
/**
|
|
346
|
+
* Puts the root in a new Job Object. The tree peak is the job's peak commit.
|
|
347
|
+
* A descendant's peak is its peak pagefile usage, read through a handle that
|
|
348
|
+
* opens when a sample first lists it, so the figure outlives the process.
|
|
349
|
+
*
|
|
350
|
+
* @param root - The pid at the top of the tree.
|
|
351
|
+
* @returns The probe, or undefined when koffi does not load or the root cannot
|
|
352
|
+
* join a job.
|
|
353
|
+
*/
|
|
354
|
+
function windowsTreeProbe(root) {
|
|
355
|
+
let kernel32;
|
|
356
|
+
try {
|
|
357
|
+
kernel32 = loadKernel32();
|
|
358
|
+
} catch {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const job = attach(kernel32, root);
|
|
362
|
+
if (job === null) return;
|
|
363
|
+
const state = {
|
|
364
|
+
handles: /* @__PURE__ */ new Map(),
|
|
365
|
+
job,
|
|
366
|
+
kernel32,
|
|
367
|
+
root
|
|
368
|
+
};
|
|
369
|
+
return {
|
|
370
|
+
close: () => {
|
|
371
|
+
closeJob(state);
|
|
372
|
+
},
|
|
373
|
+
descendants: () => descendantPeaks(state),
|
|
374
|
+
sample: () => {
|
|
375
|
+
openNewDescendants(state);
|
|
376
|
+
},
|
|
377
|
+
tree: () => jobPeak(state)
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
function loadKernel32() {
|
|
381
|
+
const library = createRequire(import.meta.url)("koffi").load("kernel32.dll");
|
|
382
|
+
return {
|
|
383
|
+
assign: library.func("bool AssignProcessToJobObject(void *job, void *process)"),
|
|
384
|
+
close: library.func("bool CloseHandle(void *handle)"),
|
|
385
|
+
createJob: library.func("void *CreateJobObjectW(void *attributes, void *name)"),
|
|
386
|
+
open: library.func("void *OpenProcess(uint32_t access, bool inherit, uint32_t pid)"),
|
|
387
|
+
queryJob: library.func("bool QueryInformationJobObject(void *job, int infoClass, _Out_ uint8_t *info, uint32_t size, void *returned)"),
|
|
388
|
+
readCounters: library.func("bool K32GetProcessMemoryInfo(void *process, _Out_ uint8_t *counters, uint32_t size)")
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
function attach(kernel32, root) {
|
|
392
|
+
const process = kernel32.open(257, false, root);
|
|
393
|
+
if (process === null) return null;
|
|
394
|
+
const job = kernel32.createJob(null, null);
|
|
395
|
+
const isAssigned = job !== null && kernel32.assign(job, process) === true;
|
|
396
|
+
kernel32.close(process);
|
|
397
|
+
if (!isAssigned && job !== null) kernel32.close(job);
|
|
398
|
+
return isAssigned ? job : null;
|
|
399
|
+
}
|
|
400
|
+
function listedProcesses({ job, kernel32 }) {
|
|
401
|
+
const list = Buffer.alloc(8200);
|
|
402
|
+
if (kernel32.queryJob(job, BASIC_PROCESS_ID_LIST, list, list.length, null) !== true) return [];
|
|
403
|
+
return Array.from({ length: list.readUInt32LE(LISTED_COUNT_OFFSET) }, (_, index) => {
|
|
404
|
+
return Number(list.readBigUInt64LE(PROCESS_ID_LIST_HEADER + index * PROCESS_ID_SIZE));
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
function openNewDescendants(state) {
|
|
408
|
+
for (const pid of listedProcesses(state)) {
|
|
409
|
+
if (pid === state.root || state.handles.has(pid)) continue;
|
|
410
|
+
const handle = state.kernel32.open(QUERY_LIMITED_ACCESS, false, pid);
|
|
411
|
+
if (handle !== null) state.handles.set(pid, handle);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
function descendantPeaks({ handles, kernel32 }) {
|
|
415
|
+
const counters = Buffer.alloc(MEMORY_COUNTERS_SIZE);
|
|
416
|
+
const peaks = /* @__PURE__ */ new Map();
|
|
417
|
+
for (const [pid, handle] of handles) if (kernel32.readCounters(handle, counters, counters.length) === true) peaks.set(pid, Number(counters.readBigUInt64LE(PEAK_PAGEFILE_OFFSET)));
|
|
418
|
+
return peaks;
|
|
419
|
+
}
|
|
420
|
+
function jobPeak({ job, kernel32 }) {
|
|
421
|
+
const limits = Buffer.alloc(EXTENDED_LIMIT_SIZE);
|
|
422
|
+
if (kernel32.queryJob(job, EXTENDED_LIMIT_INFORMATION, limits, limits.length, null) !== true) return;
|
|
423
|
+
return Number(limits.readBigUInt64LE(PEAK_JOB_MEMORY_OFFSET));
|
|
424
|
+
}
|
|
425
|
+
function closeJob({ handles, job, kernel32 }) {
|
|
426
|
+
for (const handle of handles.values()) kernel32.close(handle);
|
|
427
|
+
handles.clear();
|
|
428
|
+
kernel32.close(job);
|
|
429
|
+
}
|
|
430
|
+
//#endregion
|
|
431
|
+
//#region src/adapters.ts
|
|
432
|
+
const COMMAND_TIMEOUT_MS = 5e3;
|
|
433
|
+
const LINUX_CLOCK_TICKS_PER_SECOND = 100;
|
|
434
|
+
const LINUX_START_TIME_FIELD = 19;
|
|
435
|
+
const BOOT_TIME = /^btime (\d+)$/m;
|
|
436
|
+
const PID_NAME = /^\d+$/;
|
|
437
|
+
/**
|
|
438
|
+
* Opens the ledger of this machine in the directory that `ledgerDirectory`
|
|
439
|
+
* names for this process, and makes the directory when it does not exist.
|
|
440
|
+
*
|
|
441
|
+
* @returns The ledger that every run of this user on the machine opens.
|
|
442
|
+
*/
|
|
443
|
+
function openSharedLedger() {
|
|
444
|
+
const directory = ledgerDirectory(process.env);
|
|
445
|
+
mkdirSync(directory, { recursive: true });
|
|
446
|
+
return openLedger(ledgerPath(directory));
|
|
447
|
+
}
|
|
448
|
+
function ledgerDirectory(environment) {
|
|
449
|
+
const configured = environment["MACHINE_BUDGET_DIR"];
|
|
450
|
+
if (configured !== void 0 && configured !== "") return configured;
|
|
451
|
+
return path.join(os.tmpdir(), `machine-budget-${os.userInfo().username}`);
|
|
452
|
+
}
|
|
453
|
+
function constrainedMemory() {
|
|
454
|
+
return process.constrainedMemory?.() ?? 0;
|
|
455
|
+
}
|
|
456
|
+
function run(command, args) {
|
|
457
|
+
try {
|
|
458
|
+
return execFileSync(command, args, {
|
|
459
|
+
encoding: "utf8",
|
|
460
|
+
stdio: [
|
|
461
|
+
"ignore",
|
|
462
|
+
"pipe",
|
|
463
|
+
"ignore"
|
|
464
|
+
],
|
|
465
|
+
timeout: COMMAND_TIMEOUT_MS,
|
|
466
|
+
windowsHide: true
|
|
467
|
+
});
|
|
468
|
+
} catch {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function isAlive(pid) {
|
|
473
|
+
try {
|
|
474
|
+
process.kill(pid, 0);
|
|
475
|
+
return true;
|
|
476
|
+
} catch (err) {
|
|
477
|
+
return !(err instanceof Error) || !("code" in err) || err.code !== "ESRCH";
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
function linuxStartTime(pid) {
|
|
481
|
+
try {
|
|
482
|
+
const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
483
|
+
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
|
|
484
|
+
const bootTime = BOOT_TIME.exec(readFileSync("/proc/stat", "utf8"))?.[1];
|
|
485
|
+
const ticks = fields[LINUX_START_TIME_FIELD];
|
|
486
|
+
if (bootTime === void 0 || ticks === void 0) return;
|
|
487
|
+
return (Number(bootTime) + Number(ticks) / LINUX_CLOCK_TICKS_PER_SECOND) * 1e3;
|
|
488
|
+
} catch {
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
function parseTime(text) {
|
|
493
|
+
const time = text === void 0 ? NaN : Date.parse(text.trim());
|
|
494
|
+
return Number.isNaN(time) ? void 0 : time;
|
|
495
|
+
}
|
|
496
|
+
function windowsStartTime(pid) {
|
|
497
|
+
return parseTime(run("powershell.exe", [
|
|
498
|
+
"-NoProfile",
|
|
499
|
+
"-NonInteractive",
|
|
500
|
+
"-Command",
|
|
501
|
+
`(Get-Process -Id ${pid}).StartTime.ToUniversalTime().ToString('o')`
|
|
502
|
+
]));
|
|
503
|
+
}
|
|
504
|
+
const START_TIME_READERS = {
|
|
505
|
+
darwin: (pid) => parseTime(run("ps", [
|
|
506
|
+
"-o",
|
|
507
|
+
"lstart=",
|
|
508
|
+
"-p",
|
|
509
|
+
String(pid)
|
|
510
|
+
])),
|
|
511
|
+
linux: linuxStartTime,
|
|
512
|
+
win32: windowsStartTime
|
|
513
|
+
};
|
|
514
|
+
function startTime(pid) {
|
|
515
|
+
return START_TIME_READERS[process.platform]?.(pid);
|
|
516
|
+
}
|
|
517
|
+
/** The real machine and process. */
|
|
518
|
+
const nodePlatform = {
|
|
519
|
+
every: (milliseconds, work) => {
|
|
520
|
+
const timer = setInterval(work, milliseconds);
|
|
521
|
+
timer.unref();
|
|
522
|
+
return () => {
|
|
523
|
+
clearInterval(timer);
|
|
524
|
+
};
|
|
525
|
+
},
|
|
526
|
+
gitCommonDirectory: (workspaceRoot) => {
|
|
527
|
+
const output = run("git", [
|
|
528
|
+
"-C",
|
|
529
|
+
workspaceRoot,
|
|
530
|
+
"rev-parse",
|
|
531
|
+
"--path-format=absolute",
|
|
532
|
+
"--git-common-dir"
|
|
533
|
+
]);
|
|
534
|
+
return output === void 0 ? workspaceRoot : output.trim();
|
|
535
|
+
},
|
|
536
|
+
now: Date.now,
|
|
537
|
+
pid: process.pid,
|
|
538
|
+
probe: {
|
|
539
|
+
isAlive,
|
|
540
|
+
startTime
|
|
541
|
+
},
|
|
542
|
+
readMemory: () => readMemory({
|
|
543
|
+
freeMemory: os.freemem,
|
|
544
|
+
platform: process.platform,
|
|
545
|
+
run
|
|
546
|
+
}),
|
|
547
|
+
sleepAsync: async (milliseconds) => {
|
|
548
|
+
await setTimeout(milliseconds);
|
|
549
|
+
},
|
|
550
|
+
startedAt: Date.now() - process.uptime() * 1e3,
|
|
551
|
+
threads: os.availableParallelism(),
|
|
552
|
+
totalMemory: Math.min(os.totalmem(), constrainedMemory() || Infinity)
|
|
553
|
+
};
|
|
554
|
+
/**
|
|
555
|
+
* Chooses how this platform reads a process tree: a Job Object on Windows,
|
|
556
|
+
* `/proc` on Linux, and nothing elsewhere.
|
|
557
|
+
*
|
|
558
|
+
* @param pid - The pid at the top of the tree.
|
|
559
|
+
* @returns The probe of this platform, or undefined where it cannot read one.
|
|
560
|
+
*/
|
|
561
|
+
function treeProbe(pid) {
|
|
562
|
+
if (process.platform === "win32") return windowsTreeProbe(pid);
|
|
563
|
+
return process.platform === "linux" ? linuxTreeProbe(pid, {
|
|
564
|
+
pids: procPids,
|
|
565
|
+
read: readProc
|
|
566
|
+
}) : void 0;
|
|
567
|
+
}
|
|
568
|
+
function readProc(file) {
|
|
569
|
+
try {
|
|
570
|
+
return readFileSync(file, "utf8");
|
|
571
|
+
} catch {
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
function procPids() {
|
|
576
|
+
return readdirSync("/proc").filter((name) => PID_NAME.test(name)).map(Number);
|
|
577
|
+
}
|
|
578
|
+
//#endregion
|
|
579
|
+
//#region src/admission.ts
|
|
580
|
+
const BYTES_PER_GB$1 = 2 ** 30;
|
|
581
|
+
const MAIN_MEMORY_SHARE = .5;
|
|
582
|
+
const RESERVE_SHARE = .15;
|
|
583
|
+
const LANE_MEMORY_SHARE = .15;
|
|
584
|
+
const LANE_THREADS = 2;
|
|
585
|
+
const SMALL_REQUEST_BYTES = 2 * BYTES_PER_GB$1;
|
|
586
|
+
const AGING_MS = 6e4;
|
|
587
|
+
/**
|
|
588
|
+
* Whether a request takes the small-job lane when the main lane is full.
|
|
589
|
+
*
|
|
590
|
+
* @param request - The worker range and the memory of one worker.
|
|
591
|
+
* @returns True when the minimum workers need 2 GiB or less.
|
|
592
|
+
*/
|
|
593
|
+
function isSmallRequest(request) {
|
|
594
|
+
return request.minWorkers * request.memoryPerWorker <= SMALL_REQUEST_BYTES;
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Decides one request: the largest worker count that fits, or why it waits.
|
|
598
|
+
*
|
|
599
|
+
* @param input - The machine, the leases and waiters, and the request.
|
|
600
|
+
* @returns A grant with its lane and workers, or the reason to wait.
|
|
601
|
+
*/
|
|
602
|
+
function admit(input) {
|
|
603
|
+
const { leases, machine, request } = input;
|
|
604
|
+
const isBig = !isSmallRequest(request);
|
|
605
|
+
const isBehindAged = hasOlderAgedWaiter(input);
|
|
606
|
+
const mainLeases = leases.filter((lease) => lease.lane === "main");
|
|
607
|
+
if (isBig && machine.pressure !== "normal" && mainLeases.length > 0) return {
|
|
608
|
+
kind: "wait",
|
|
609
|
+
reason: "memory pressure"
|
|
610
|
+
};
|
|
611
|
+
const headroom = machine.availableMemory - machine.totalMemory * RESERVE_SHARE;
|
|
612
|
+
const main = isBehindAged ? {
|
|
613
|
+
kind: "blocked",
|
|
614
|
+
reason: "older request"
|
|
615
|
+
} : fitWorkers(request, mainFit(input, headroom));
|
|
616
|
+
if (main.kind === "fit") return {
|
|
617
|
+
kind: "grant",
|
|
618
|
+
lane: "main",
|
|
619
|
+
workers: main.workers
|
|
620
|
+
};
|
|
621
|
+
if (!isBehindAged && mainLeases.length === 0) return {
|
|
622
|
+
kind: "grant",
|
|
623
|
+
lane: "main",
|
|
624
|
+
workers: request.minWorkers
|
|
625
|
+
};
|
|
626
|
+
const lane = isBig || request.isMainLaneOnly === true ? main : fitWorkers(request, laneFit(input, headroom));
|
|
627
|
+
return lane.kind === "fit" ? {
|
|
628
|
+
kind: "grant",
|
|
629
|
+
lane: "small",
|
|
630
|
+
workers: lane.workers
|
|
631
|
+
} : {
|
|
632
|
+
kind: "wait",
|
|
633
|
+
reason: main.reason
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* The most workers of a request that the run cap lets one run hold now, its
|
|
638
|
+
* own leases included.
|
|
639
|
+
*
|
|
640
|
+
* @param input - The machine, the leases and waiters, and the request.
|
|
641
|
+
* @returns The worker count, at most the maximum of the request.
|
|
642
|
+
*/
|
|
643
|
+
function runCapWorkers(input) {
|
|
644
|
+
const { machine, request } = input;
|
|
645
|
+
const limit = mainLimit(machine);
|
|
646
|
+
const runs = activeRuns(input);
|
|
647
|
+
const byMemory = Math.floor(limit.memory / runs / request.memoryPerWorker);
|
|
648
|
+
return Math.min(request.maxWorkers, byMemory, Math.floor(limit.threads / runs));
|
|
649
|
+
}
|
|
650
|
+
function hasOlderAgedWaiter({ now, waiterId, waiters }) {
|
|
651
|
+
const since = waiters.find((waiter) => waiter.id === waiterId)?.since ?? now;
|
|
652
|
+
return waiters.some((waiter) => {
|
|
653
|
+
return waiter.isBig && waiter.since < since && now - waiter.since > AGING_MS;
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
function activeRuns({ leases, session, waiters }) {
|
|
657
|
+
const sessions = /* @__PURE__ */ new Set([session]);
|
|
658
|
+
for (const lease of leases) if (lease.lane === "main") sessions.add(lease.session);
|
|
659
|
+
for (const waiter of waiters) sessions.add(waiter.session);
|
|
660
|
+
return sessions.size;
|
|
661
|
+
}
|
|
662
|
+
function sum(leases) {
|
|
663
|
+
return leases.reduce((total, lease) => {
|
|
664
|
+
return {
|
|
665
|
+
memory: total.memory + lease.memory,
|
|
666
|
+
threads: total.threads + lease.threads
|
|
667
|
+
};
|
|
668
|
+
}, {
|
|
669
|
+
memory: 0,
|
|
670
|
+
threads: 0
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
function mainUsage(input) {
|
|
674
|
+
const mainLeases = input.leases.filter((lease) => lease.lane === "main");
|
|
675
|
+
const runLeases = mainLeases.filter((lease) => lease.session === input.session);
|
|
676
|
+
return {
|
|
677
|
+
machine: sum(mainLeases),
|
|
678
|
+
run: sum(runLeases)
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
function mainLimit(machine) {
|
|
682
|
+
return {
|
|
683
|
+
memory: machine.totalMemory * MAIN_MEMORY_SHARE,
|
|
684
|
+
threads: Math.max(1, machine.threads - LANE_THREADS)
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
function mainFit(input, headroom) {
|
|
688
|
+
const limit = mainLimit(input.machine);
|
|
689
|
+
const usage = mainUsage(input);
|
|
690
|
+
const runs = activeRuns(input);
|
|
691
|
+
return {
|
|
692
|
+
free: {
|
|
693
|
+
memory: limit.memory - usage.machine.memory,
|
|
694
|
+
threads: limit.threads - usage.machine.threads
|
|
695
|
+
},
|
|
696
|
+
headroom,
|
|
697
|
+
runFree: {
|
|
698
|
+
memory: limit.memory / runs - usage.run.memory,
|
|
699
|
+
threads: Math.floor(limit.threads / runs) - usage.run.threads
|
|
700
|
+
}
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
function laneFit({ leases, machine }, headroom) {
|
|
704
|
+
const used = sum(leases.filter((lease) => lease.lane === "small"));
|
|
705
|
+
return {
|
|
706
|
+
free: {
|
|
707
|
+
memory: machine.totalMemory * LANE_MEMORY_SHARE - used.memory,
|
|
708
|
+
threads: LANE_THREADS - used.threads
|
|
709
|
+
},
|
|
710
|
+
headroom,
|
|
711
|
+
runFree: void 0
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
function fitsPool(pool, demand) {
|
|
715
|
+
return demand.memory <= pool.memory && demand.threads <= pool.threads;
|
|
716
|
+
}
|
|
717
|
+
function failure(fit, demand) {
|
|
718
|
+
if (demand.memory > fit.free.memory) return "memory limit";
|
|
719
|
+
if (demand.threads > fit.free.threads) return "thread limit";
|
|
720
|
+
if (fit.runFree !== void 0 && !fitsPool(fit.runFree, demand)) return "run cap";
|
|
721
|
+
return demand.memory > fit.headroom ? "available memory" : void 0;
|
|
722
|
+
}
|
|
723
|
+
function demandOf(request, workers) {
|
|
724
|
+
return {
|
|
725
|
+
memory: workers * request.memoryPerWorker,
|
|
726
|
+
threads: workers
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
function fitWorkers(request, fit) {
|
|
730
|
+
const reason = failure(fit, demandOf(request, request.minWorkers));
|
|
731
|
+
if (reason !== void 0) return {
|
|
732
|
+
kind: "blocked",
|
|
733
|
+
reason
|
|
734
|
+
};
|
|
735
|
+
let workers = request.maxWorkers;
|
|
736
|
+
while (failure(fit, demandOf(request, workers)) !== void 0) workers -= 1;
|
|
737
|
+
return {
|
|
738
|
+
kind: "fit",
|
|
739
|
+
workers
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
//#endregion
|
|
743
|
+
//#region src/reclaim.ts
|
|
744
|
+
/** How often a live session stamps its heartbeat. */
|
|
745
|
+
const HEARTBEAT_INTERVAL_MS = 5e3;
|
|
746
|
+
const SUSPECT_MS = 3 * HEARTBEAT_INTERVAL_MS;
|
|
747
|
+
const STALE_MS = 6e4;
|
|
748
|
+
const CLOCK_JUMP_MS = 3e4;
|
|
749
|
+
const START_TOLERANCE_MS = 1e4;
|
|
750
|
+
/**
|
|
751
|
+
* Finds the sessions whose runs are gone.
|
|
752
|
+
*
|
|
753
|
+
* @param input - The sessions, the clock and a process probe.
|
|
754
|
+
* @returns The dead session ids.
|
|
755
|
+
*/
|
|
756
|
+
function findDeadSessions(input) {
|
|
757
|
+
const { lastSeenAt, now } = input;
|
|
758
|
+
const isHeartbeatSkipped = lastSeenAt !== void 0 && (now - lastSeenAt > CLOCK_JUMP_MS || now < lastSeenAt);
|
|
759
|
+
return {
|
|
760
|
+
dead: input.sessions.filter((session) => {
|
|
761
|
+
return session.id !== input.self && isDead({
|
|
762
|
+
...input,
|
|
763
|
+
isHeartbeatSkipped,
|
|
764
|
+
session
|
|
765
|
+
});
|
|
766
|
+
}).map((session) => session.id),
|
|
767
|
+
isHeartbeatSkipped
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
function isDead({ isHeartbeatSkipped, now, probe, session }) {
|
|
771
|
+
if (!probe.isAlive(session.pid)) return true;
|
|
772
|
+
const age = now - session.heartbeatAt;
|
|
773
|
+
if (age < SUSPECT_MS && age >= 0) return false;
|
|
774
|
+
const startedAt = probe.startTime(session.pid);
|
|
775
|
+
if (startedAt !== void 0 && Math.abs(startedAt - session.startedAt) > START_TOLERANCE_MS) return true;
|
|
776
|
+
return !isHeartbeatSkipped && age >= STALE_MS;
|
|
777
|
+
}
|
|
778
|
+
//#endregion
|
|
779
|
+
//#region src/budget.ts
|
|
780
|
+
const FIRST_RETRY_MS = 250;
|
|
781
|
+
const LAST_RETRY_MS = 2e3;
|
|
782
|
+
const BYTES_PER_GB = 2 ** 30;
|
|
783
|
+
/**
|
|
784
|
+
* Joins the machine budget. Each lease is one row in the shared ledger until
|
|
785
|
+
* it is released, the run closes, or a later run finds the run dead.
|
|
786
|
+
*
|
|
787
|
+
* @param options - The ledger, the platform and the logger.
|
|
788
|
+
* @returns The budget of this run.
|
|
789
|
+
*/
|
|
790
|
+
function openBudget(options) {
|
|
791
|
+
if (isBudgetOff(options.environment)) return unlimitedBudget;
|
|
792
|
+
return sharedBudget(joinSession(options));
|
|
793
|
+
}
|
|
794
|
+
function sharedBudget(session) {
|
|
795
|
+
const stop = session.options.platform.every(HEARTBEAT_INTERVAL_MS, () => {
|
|
796
|
+
session.ledger.transact((transaction) => restore(session, transaction));
|
|
797
|
+
});
|
|
798
|
+
let isClosed = false;
|
|
799
|
+
return {
|
|
800
|
+
close: () => {
|
|
801
|
+
if (isClosed) return;
|
|
802
|
+
isClosed = true;
|
|
803
|
+
stop();
|
|
804
|
+
leaveSession(session);
|
|
805
|
+
},
|
|
806
|
+
isShared: true,
|
|
807
|
+
leaseAsync: async (request) => {
|
|
808
|
+
assertRequest(request);
|
|
809
|
+
return leaseAsync(session, request);
|
|
810
|
+
},
|
|
811
|
+
runCapWorkers: (request) => {
|
|
812
|
+
assertRequest(request);
|
|
813
|
+
return readRunCap(session, request);
|
|
814
|
+
},
|
|
815
|
+
tryLease: (request) => {
|
|
816
|
+
assertRequest(request);
|
|
817
|
+
const outcome = attempt({
|
|
818
|
+
isWaiting: false,
|
|
819
|
+
request,
|
|
820
|
+
session,
|
|
821
|
+
waiterId: randomUUID()
|
|
822
|
+
});
|
|
823
|
+
return outcome.kind === "grant" ? toLease({
|
|
824
|
+
outcome,
|
|
825
|
+
request,
|
|
826
|
+
session
|
|
827
|
+
}) : void 0;
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
function isBudgetOff(environment) {
|
|
832
|
+
const ci = environment["CI"];
|
|
833
|
+
return environment["MACHINE_BUDGET"] === "off" || ci !== void 0 && ci !== "false";
|
|
834
|
+
}
|
|
835
|
+
function unlimitedLease(request) {
|
|
836
|
+
assertRequest(request);
|
|
837
|
+
const { maxWorkers, memoryPerWorker } = request;
|
|
838
|
+
return {
|
|
839
|
+
memoryPerWorker,
|
|
840
|
+
release: doNothing(),
|
|
841
|
+
workers: maxWorkers
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
const unlimitedBudget = {
|
|
845
|
+
close: doNothing(),
|
|
846
|
+
isShared: false,
|
|
847
|
+
leaseAsync: async (request) => Promise.try(() => unlimitedLease(request)),
|
|
848
|
+
runCapWorkers: (request) => unlimitedLease(request).workers,
|
|
849
|
+
tryLease: unlimitedLease
|
|
850
|
+
};
|
|
851
|
+
function assertRequest({ maxWorkers, memoryPerWorker, minWorkers }) {
|
|
852
|
+
if (!Number.isSafeInteger(minWorkers) || minWorkers < 1 || maxWorkers < minWorkers) throw new RangeError(`a lease needs 1 <= minWorkers <= maxWorkers, got ${minWorkers} and ${maxWorkers}`);
|
|
853
|
+
if (!Number.isSafeInteger(maxWorkers) || !Number.isFinite(memoryPerWorker) || memoryPerWorker <= 0) throw new RangeError(`a lease needs a whole maxWorkers and memoryPerWorker above 0, got ${maxWorkers} and ${memoryPerWorker}`);
|
|
854
|
+
}
|
|
855
|
+
function prune(session, transaction) {
|
|
856
|
+
const { platform } = session.options;
|
|
857
|
+
const now = platform.now();
|
|
858
|
+
const { dead, isHeartbeatSkipped } = findDeadSessions({
|
|
859
|
+
lastSeenAt: transaction.lastSeenAt(),
|
|
860
|
+
now,
|
|
861
|
+
probe: platform.probe,
|
|
862
|
+
self: session.id,
|
|
863
|
+
sessions: transaction.sessions()
|
|
864
|
+
});
|
|
865
|
+
transaction.removeSessions(dead);
|
|
866
|
+
if (isHeartbeatSkipped) transaction.refreshHeartbeats(now);
|
|
867
|
+
transaction.markSeen(now);
|
|
868
|
+
return now;
|
|
869
|
+
}
|
|
870
|
+
function restore(session, transaction) {
|
|
871
|
+
const { platform } = session.options;
|
|
872
|
+
const now = prune(session, transaction);
|
|
873
|
+
transaction.upsertSession({
|
|
874
|
+
id: session.id,
|
|
875
|
+
heartbeatAt: now,
|
|
876
|
+
pid: platform.pid,
|
|
877
|
+
startedAt: platform.startedAt
|
|
878
|
+
});
|
|
879
|
+
for (const lease of session.held.values()) transaction.addLease(lease);
|
|
880
|
+
return now;
|
|
881
|
+
}
|
|
882
|
+
function joinSession(options) {
|
|
883
|
+
const session = {
|
|
884
|
+
id: randomUUID(),
|
|
885
|
+
held: /* @__PURE__ */ new Map(),
|
|
886
|
+
ledger: options.openLedger(),
|
|
887
|
+
options,
|
|
888
|
+
repository: options.platform.gitCommonDirectory(options.workspaceRoot)
|
|
889
|
+
};
|
|
890
|
+
session.ledger.transact((transaction) => restore(session, transaction));
|
|
891
|
+
return session;
|
|
892
|
+
}
|
|
893
|
+
function leaveSession(session) {
|
|
894
|
+
session.ledger.transact((transaction) => {
|
|
895
|
+
transaction.removeSessions([session.id]);
|
|
896
|
+
});
|
|
897
|
+
session.ledger.close();
|
|
898
|
+
}
|
|
899
|
+
function workerMemory({ request, session, transaction }) {
|
|
900
|
+
if (request.cost === void 0) return request.memoryPerWorker;
|
|
901
|
+
return learnedCost(transaction.costSamples(costKey(session.repository, request.cost))) ?? request.memoryPerWorker;
|
|
902
|
+
}
|
|
903
|
+
function formatGb(bytes) {
|
|
904
|
+
return `${(bytes / BYTES_PER_GB).toFixed(1)} GB`;
|
|
905
|
+
}
|
|
906
|
+
function waitLine({ leases, reason, request }) {
|
|
907
|
+
const leased = leases.reduce((total, lease) => total + lease.memory, 0);
|
|
908
|
+
const runs = new Set(leases.map((lease) => lease.session));
|
|
909
|
+
return `machine budget: waiting for ${formatGb(request.minWorkers * request.memoryPerWorker)} and ${request.minWorkers} threads (${reason}); ${formatGb(leased)} leased by ${runs.size} runs`;
|
|
910
|
+
}
|
|
911
|
+
function decide(context) {
|
|
912
|
+
const { isWaiting, reading, session, transaction, waiterId } = context;
|
|
913
|
+
const { platform } = session.options;
|
|
914
|
+
const now = restore(session, transaction);
|
|
915
|
+
const request = {
|
|
916
|
+
...context.request,
|
|
917
|
+
memoryPerWorker: workerMemory(context)
|
|
918
|
+
};
|
|
919
|
+
const leases = transaction.leases();
|
|
920
|
+
const decision = admit({
|
|
921
|
+
leases,
|
|
922
|
+
machine: {
|
|
923
|
+
...reading,
|
|
924
|
+
threads: platform.threads,
|
|
925
|
+
totalMemory: platform.totalMemory
|
|
926
|
+
},
|
|
927
|
+
now,
|
|
928
|
+
request,
|
|
929
|
+
session: session.id,
|
|
930
|
+
waiterId,
|
|
931
|
+
waiters: transaction.waiters()
|
|
932
|
+
});
|
|
933
|
+
if (decision.kind === "wait") {
|
|
934
|
+
if (isWaiting) {
|
|
935
|
+
const isBig = !isSmallRequest(request);
|
|
936
|
+
transaction.upsertWaiter({
|
|
937
|
+
id: waiterId,
|
|
938
|
+
isBig,
|
|
939
|
+
session: session.id,
|
|
940
|
+
since: now
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
return {
|
|
944
|
+
kind: "wait",
|
|
945
|
+
line: waitLine({
|
|
946
|
+
leases,
|
|
947
|
+
reason: decision.reason,
|
|
948
|
+
request
|
|
949
|
+
})
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
const { lane, workers } = decision;
|
|
953
|
+
const memory = workers * request.memoryPerWorker;
|
|
954
|
+
const lease = {
|
|
955
|
+
id: randomUUID(),
|
|
956
|
+
lane,
|
|
957
|
+
memory,
|
|
958
|
+
session: session.id,
|
|
959
|
+
threads: workers
|
|
960
|
+
};
|
|
961
|
+
transaction.addLease(lease);
|
|
962
|
+
transaction.removeWaiter(waiterId);
|
|
963
|
+
return {
|
|
964
|
+
kind: "grant",
|
|
965
|
+
lease,
|
|
966
|
+
memoryPerWorker: request.memoryPerWorker
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
function attempt(pending) {
|
|
970
|
+
const reading = pending.session.options.platform.readMemory();
|
|
971
|
+
return pending.session.ledger.transact((transaction) => {
|
|
972
|
+
return decide({
|
|
973
|
+
...pending,
|
|
974
|
+
reading,
|
|
975
|
+
transaction
|
|
976
|
+
});
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
function readRunCap(session, request) {
|
|
980
|
+
const { platform } = session.options;
|
|
981
|
+
return session.ledger.transact((transaction) => {
|
|
982
|
+
return runCapWorkers({
|
|
983
|
+
leases: transaction.leases(),
|
|
984
|
+
machine: {
|
|
985
|
+
threads: platform.threads,
|
|
986
|
+
totalMemory: platform.totalMemory
|
|
987
|
+
},
|
|
988
|
+
request: {
|
|
989
|
+
...request,
|
|
990
|
+
memoryPerWorker: workerMemory({
|
|
991
|
+
request,
|
|
992
|
+
session,
|
|
993
|
+
transaction
|
|
994
|
+
})
|
|
995
|
+
},
|
|
996
|
+
session: session.id,
|
|
997
|
+
waiters: transaction.waiters()
|
|
998
|
+
});
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
function releaseLease({ lease, report, request, session }) {
|
|
1002
|
+
if (!session.held.delete(lease.id)) return;
|
|
1003
|
+
session.ledger.transact((transaction) => {
|
|
1004
|
+
const now = prune(session, transaction);
|
|
1005
|
+
transaction.removeLease(lease.id);
|
|
1006
|
+
if (report !== void 0 && request.cost !== void 0) {
|
|
1007
|
+
const bytes = report.peakPerWorker ?? Math.round(report.peakMemory / lease.threads);
|
|
1008
|
+
transaction.addSample({
|
|
1009
|
+
key: costKey(session.repository, request.cost),
|
|
1010
|
+
at: now,
|
|
1011
|
+
bytes
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
function toLease({ outcome, request, session }) {
|
|
1017
|
+
const { lease } = outcome;
|
|
1018
|
+
session.held.set(lease.id, lease);
|
|
1019
|
+
return {
|
|
1020
|
+
memoryPerWorker: outcome.memoryPerWorker,
|
|
1021
|
+
release: (report) => {
|
|
1022
|
+
releaseLease({
|
|
1023
|
+
lease,
|
|
1024
|
+
report,
|
|
1025
|
+
request,
|
|
1026
|
+
session
|
|
1027
|
+
});
|
|
1028
|
+
},
|
|
1029
|
+
workers: lease.threads
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
async function leaseAsync(session, request) {
|
|
1033
|
+
const pending = {
|
|
1034
|
+
isWaiting: true,
|
|
1035
|
+
request,
|
|
1036
|
+
session,
|
|
1037
|
+
waiterId: randomUUID()
|
|
1038
|
+
};
|
|
1039
|
+
for (let delay = FIRST_RETRY_MS;; delay = Math.min(delay * 2, LAST_RETRY_MS)) {
|
|
1040
|
+
const outcome = attempt(pending);
|
|
1041
|
+
if (outcome.kind === "grant") return toLease({
|
|
1042
|
+
outcome,
|
|
1043
|
+
request,
|
|
1044
|
+
session
|
|
1045
|
+
});
|
|
1046
|
+
if (delay === FIRST_RETRY_MS) session.options.log(outcome.line);
|
|
1047
|
+
await session.options.platform.sleepAsync(delay);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
//#endregion
|
|
1051
|
+
//#region src/tree-watch.ts
|
|
1052
|
+
const TREE_SAMPLE_MS = 100;
|
|
1053
|
+
const BLIND_WATCH = { stop: () => ({
|
|
1054
|
+
peakMemory: void 0,
|
|
1055
|
+
peakPerWorker: void 0
|
|
1056
|
+
}) };
|
|
1057
|
+
/**
|
|
1058
|
+
* Samples a process tree until the watch stops.
|
|
1059
|
+
*
|
|
1060
|
+
* @param options - The timer and the probe of the tree.
|
|
1061
|
+
* @returns The watch, which samples until its stop reads the peaks.
|
|
1062
|
+
*/
|
|
1063
|
+
function watchWith({ every, probe }) {
|
|
1064
|
+
if (probe === void 0) return BLIND_WATCH;
|
|
1065
|
+
probe.sample();
|
|
1066
|
+
const stopSampling = every(TREE_SAMPLE_MS, probe.sample);
|
|
1067
|
+
return { stop: () => {
|
|
1068
|
+
stopSampling();
|
|
1069
|
+
probe.sample();
|
|
1070
|
+
const peaks = [...probe.descendants().values()];
|
|
1071
|
+
const result = {
|
|
1072
|
+
peakMemory: probe.tree(),
|
|
1073
|
+
peakPerWorker: peaks.length === 0 ? void 0 : Math.max(...peaks)
|
|
1074
|
+
};
|
|
1075
|
+
probe.close?.();
|
|
1076
|
+
return result;
|
|
1077
|
+
} };
|
|
1078
|
+
}
|
|
1079
|
+
//#endregion
|
|
1080
|
+
//#region src/index.ts
|
|
1081
|
+
/**
|
|
1082
|
+
* Joins the budget that every dev tool on this machine shares. Close it when
|
|
1083
|
+
* the run ends; a run that dies is taken back by the next run that leases.
|
|
1084
|
+
* `MACHINE_BUDGET=off` or CI turns it off, and `MACHINE_BUDGET_DIR` moves the
|
|
1085
|
+
* ledger.
|
|
1086
|
+
*
|
|
1087
|
+
* @param options - The logger and the workspace root.
|
|
1088
|
+
* @returns The budget of this run.
|
|
1089
|
+
*/
|
|
1090
|
+
function openMachineBudget(options) {
|
|
1091
|
+
return openBudget({
|
|
1092
|
+
...options,
|
|
1093
|
+
environment: process.env,
|
|
1094
|
+
openLedger: openSharedLedger,
|
|
1095
|
+
platform: nodePlatform
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
/**
|
|
1099
|
+
* Watches a running process and every process it starts from now on, to
|
|
1100
|
+
* report the peaks that a lease release takes. Call it right after the spawn;
|
|
1101
|
+
* a descendant that starts and ends between two samples is missed. On Windows
|
|
1102
|
+
* the root joins a Job Object; on Linux the peaks are resident sets read from
|
|
1103
|
+
* `/proc`; elsewhere both peaks are undefined. It never throws.
|
|
1104
|
+
*
|
|
1105
|
+
* @param pid - The pid of the process at the top of the tree.
|
|
1106
|
+
* @returns The watch; stop it once the process exits.
|
|
1107
|
+
*/
|
|
1108
|
+
function watchTree(pid) {
|
|
1109
|
+
return watchWith({
|
|
1110
|
+
every: nodePlatform.every,
|
|
1111
|
+
probe: treeProbe(pid)
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
//#endregion
|
|
1115
|
+
export { openMachineBudget, watchTree };
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isentinel/machine-budget",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Leases memory and threads to Node dev tools from one budget shared by the whole machine.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"memory",
|
|
7
|
+
"concurrency",
|
|
8
|
+
"workers",
|
|
9
|
+
"lease",
|
|
10
|
+
"budget"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Christopher Buss <christopher.buss@pm.me> (https://github.com/christopher-buss)",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"default": "./dist/index.mjs"
|
|
20
|
+
},
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"arktype": "2.2.3",
|
|
29
|
+
"koffi": "3.3.3",
|
|
30
|
+
"remeda": "2.52.0"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=24.12.0"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
}
|
|
6
38
|
}
|