vue-go-tsc 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/vue-go-tsc +116 -12
- package/bin/vue-go-tsc.test.js +58 -0
- package/package.json +1 -1
package/bin/vue-go-tsc
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { spawn } = require('child_process');
|
|
4
4
|
const fs = require('fs');
|
|
5
|
-
const { join, resolve } = require('path');
|
|
5
|
+
const { join, posix, resolve } = require('path');
|
|
6
6
|
const os = require('os');
|
|
7
7
|
|
|
8
8
|
const GiB = 1024 ** 3;
|
|
@@ -35,6 +35,14 @@ const MIN_CPU_FRACTION = 0.125;
|
|
|
35
35
|
// How long the launcher samples CPU activity before starting tsgo.
|
|
36
36
|
const CPU_SAMPLE_MS = 200;
|
|
37
37
|
|
|
38
|
+
// Fraction of a hard (cgroup) memory limit used as the soft Go heap ceiling.
|
|
39
|
+
// Past the hard limit the process is killed, so the GC only needs to work
|
|
40
|
+
// harder right before it: a lower ceiling buys nothing when the live heap
|
|
41
|
+
// genuinely needs more, it only makes the GC run continuously (a large Nuxt
|
|
42
|
+
// app ran 200 s in back-to-back GCs under a 4.1 GiB ceiling before being
|
|
43
|
+
// killed anyway).
|
|
44
|
+
const HARD_LIMIT_FRACTION = 0.9;
|
|
45
|
+
|
|
38
46
|
// Never set a heap ceiling below this: a limit under a project's live heap
|
|
39
47
|
// makes the Go GC run continuously instead of making progress.
|
|
40
48
|
const MIN_MEM_LIMIT = 1 * GiB;
|
|
@@ -193,11 +201,25 @@ function computeCodegenWorkers(procs, availableMem) {
|
|
|
193
201
|
return workers;
|
|
194
202
|
}
|
|
195
203
|
|
|
204
|
+
// Soft Go heap ceiling under a hard memory limit (cgroup): most of it, shared
|
|
205
|
+
// between the concurrent runs (they are all inside the same limit).
|
|
206
|
+
function computeHardGoMemLimit(hardLimit, instances = 1) {
|
|
207
|
+
if (!Number.isFinite(hardLimit) || hardLimit <= 0) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
const n = Math.max(1, Math.floor(instances) || 1);
|
|
211
|
+
return Math.floor((hardLimit * HARD_LIMIT_FRACTION) / n);
|
|
212
|
+
}
|
|
213
|
+
|
|
196
214
|
// Return a copy of `env` with GOMAXPROCS and GOMEMLIMIT defaulted from the
|
|
197
215
|
// machine resources, what is currently free, and the number of concurrent
|
|
198
216
|
// runs. Explicit values are always respected (never overridden). With the
|
|
199
217
|
// Volar codegen, its Node processes get a share of the memory budget.
|
|
200
|
-
|
|
218
|
+
//
|
|
219
|
+
// `hardLimit` is the cgroup memory limit, when there is one: exceeding it gets
|
|
220
|
+
// the process killed, so the ceiling follows it rather than the RAM that
|
|
221
|
+
// happens to be free when the run starts.
|
|
222
|
+
function resolveEnv(env, { cpus, totalMem, instances = 1, idleCpus, availableMem, hardLimit, codegen = 'go', volar }) {
|
|
201
223
|
const out = { ...env };
|
|
202
224
|
if (!out.GOMAXPROCS) {
|
|
203
225
|
const procs = computeGoMaxProcs(cpus, instances, idleCpus);
|
|
@@ -219,7 +241,7 @@ function resolveEnv(env, { cpus, totalMem, instances = 1, idleCpus, availableMem
|
|
|
219
241
|
delete out.GOLAR_PLUGIN;
|
|
220
242
|
}
|
|
221
243
|
if (!out.GOMEMLIMIT) {
|
|
222
|
-
let limit = computeGoMemLimit(totalMem, instances, availableMem);
|
|
244
|
+
let limit = computeHardGoMemLimit(hardLimit, instances) ?? computeGoMemLimit(totalMem, instances, availableMem);
|
|
223
245
|
if (limit && workers > 0) {
|
|
224
246
|
limit = Math.floor(Math.max(Math.min(MIN_MEM_LIMIT, limit), limit - VOLAR_BASE_MEM - workers * VOLAR_WORKER_MEM));
|
|
225
247
|
}
|
|
@@ -296,18 +318,96 @@ function availableCpus() {
|
|
|
296
318
|
return typeof os.availableParallelism === 'function' ? os.availableParallelism() : os.cpus().length;
|
|
297
319
|
}
|
|
298
320
|
|
|
299
|
-
//
|
|
300
|
-
//
|
|
301
|
-
|
|
302
|
-
|
|
321
|
+
// cgroup limits at or above this are "unlimited" (cgroup v1 reports a huge
|
|
322
|
+
// page-aligned number instead of "max").
|
|
323
|
+
const CGROUP_UNLIMITED = 2 ** 60;
|
|
324
|
+
|
|
325
|
+
function parseCgroupBytes(text) {
|
|
326
|
+
const value = Number(String(text).trim());
|
|
327
|
+
return Number.isFinite(value) && value > 0 && value < CGROUP_UNLIMITED ? value : undefined;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// The tightest cgroup memory limit of this process and the usage counted
|
|
331
|
+
// against it: { limit, usage } (usage may be undefined), or undefined when
|
|
332
|
+
// there is none (or not on Linux).
|
|
333
|
+
//
|
|
334
|
+
// process.constrainedMemory() only reads the process's own cgroup, but a CI
|
|
335
|
+
// job or container runtime often sets the limit on a parent (the job's slice)
|
|
336
|
+
// and leaves the leaf unlimited, so this walks from the process's cgroup up to
|
|
337
|
+
// the root and keeps the smallest memory.max / memory.high (v2) or
|
|
338
|
+
// memory.limit_in_bytes (v1).
|
|
339
|
+
function cgroupMemoryLimit(readFile = (p) => fs.readFileSync(p, 'utf8'), root = '/sys/fs/cgroup') {
|
|
340
|
+
let membership;
|
|
341
|
+
try {
|
|
342
|
+
membership = readFile('/proc/self/cgroup');
|
|
343
|
+
} catch {
|
|
344
|
+
return undefined;
|
|
345
|
+
}
|
|
346
|
+
let best;
|
|
347
|
+
const read = (file) => {
|
|
348
|
+
try {
|
|
349
|
+
return readFile(file);
|
|
350
|
+
} catch {
|
|
351
|
+
return undefined;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
for (const line of membership.split('\n')) {
|
|
355
|
+
// <hierarchy-id>:<controllers>:<path>; the path itself may contain ':'.
|
|
356
|
+
const first = line.indexOf(':');
|
|
357
|
+
const second = line.indexOf(':', first + 1);
|
|
358
|
+
if (first < 0 || second < 0) {
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
const id = line.slice(0, first);
|
|
362
|
+
const controllers = line.slice(first + 1, second).split(',');
|
|
363
|
+
let dir = line.slice(second + 1);
|
|
364
|
+
let base;
|
|
365
|
+
let files;
|
|
366
|
+
if (id === '0' && controllers.length === 1 && controllers[0] === '') {
|
|
367
|
+
base = root;
|
|
368
|
+
files = { limits: ['memory.max', 'memory.high'], usage: 'memory.current' };
|
|
369
|
+
} else if (controllers.includes('memory')) {
|
|
370
|
+
base = join(root, 'memory');
|
|
371
|
+
files = { limits: ['memory.limit_in_bytes'], usage: 'memory.usage_in_bytes' };
|
|
372
|
+
} else {
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
for (;;) {
|
|
376
|
+
const cgroupDir = join(base, dir);
|
|
377
|
+
for (const name of files.limits) {
|
|
378
|
+
const limit = parseCgroupBytes(read(join(cgroupDir, name)));
|
|
379
|
+
if (limit !== undefined && (!best || limit < best.limit)) {
|
|
380
|
+
best = { limit, usage: parseCgroupBytes(read(join(cgroupDir, files.usage))) };
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (dir === '/' || dir === '') {
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
dir = posix.dirname(dir);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return best;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Usable RAM: honors a cgroup memory limit (containers, CI jobs) when it is
|
|
393
|
+
// tighter than physical RAM, including a limit set on a parent cgroup.
|
|
394
|
+
function totalMemory(cgroup = cgroupMemoryLimit()) {
|
|
395
|
+
let total = os.totalmem();
|
|
303
396
|
const constrained = typeof process.constrainedMemory === 'function' ? process.constrainedMemory() : 0;
|
|
304
|
-
|
|
397
|
+
if (constrained > 0 && constrained < total) {
|
|
398
|
+
total = constrained;
|
|
399
|
+
}
|
|
400
|
+
return cgroup && cgroup.limit < total ? cgroup.limit : total;
|
|
305
401
|
}
|
|
306
402
|
|
|
307
403
|
// RAM currently available to this process (MemAvailable on Linux, bounded by
|
|
308
404
|
// the cgroup limit when there is one).
|
|
309
|
-
function freeMemory() {
|
|
310
|
-
|
|
405
|
+
function freeMemory(cgroup = cgroupMemoryLimit()) {
|
|
406
|
+
const available = typeof process.availableMemory === 'function' ? process.availableMemory() : os.freemem();
|
|
407
|
+
if (cgroup && cgroup.usage !== undefined) {
|
|
408
|
+
return Math.min(available, Math.max(0, cgroup.limit - cgroup.usage));
|
|
409
|
+
}
|
|
410
|
+
return available;
|
|
311
411
|
}
|
|
312
412
|
|
|
313
413
|
// Idle cores right now, sampled over CPU_SAMPLE_MS.
|
|
@@ -340,12 +440,14 @@ async function main() {
|
|
|
340
440
|
// Sampling costs CPU_SAMPLE_MS of startup; skip it when it cannot matter.
|
|
341
441
|
const idleCpus = process.env.GOMAXPROCS ? undefined : await sampleIdleCpus(cpus);
|
|
342
442
|
|
|
443
|
+
const cgroup = cgroupMemoryLimit();
|
|
343
444
|
const env = resolveEnv(process.env, {
|
|
344
445
|
cpus,
|
|
345
|
-
totalMem: totalMemory(),
|
|
446
|
+
totalMem: totalMemory(cgroup),
|
|
346
447
|
instances,
|
|
347
448
|
idleCpus,
|
|
348
|
-
availableMem: freeMemory(),
|
|
449
|
+
availableMem: freeMemory(cgroup),
|
|
450
|
+
hardLimit: cgroup?.limit,
|
|
349
451
|
codegen,
|
|
350
452
|
volar: {
|
|
351
453
|
pluginEntry: VOLAR_PLUGIN,
|
|
@@ -403,9 +505,11 @@ module.exports = {
|
|
|
403
505
|
computeCodegenWorkers,
|
|
404
506
|
computeGoMaxProcs,
|
|
405
507
|
computeGoMemLimit,
|
|
508
|
+
computeHardGoMemLimit,
|
|
406
509
|
measureIdleCpus,
|
|
407
510
|
resolveEnv,
|
|
408
511
|
registerInstance,
|
|
512
|
+
cgroupMemoryLimit,
|
|
409
513
|
GiB,
|
|
410
514
|
DEFAULT_CPU_FRACTION,
|
|
411
515
|
DEFAULT_MEM_FRACTION,
|
package/bin/vue-go-tsc.test.js
CHANGED
|
@@ -11,9 +11,11 @@ const {
|
|
|
11
11
|
computeCodegenWorkers,
|
|
12
12
|
computeGoMaxProcs,
|
|
13
13
|
computeGoMemLimit,
|
|
14
|
+
computeHardGoMemLimit,
|
|
14
15
|
measureIdleCpus,
|
|
15
16
|
resolveEnv,
|
|
16
17
|
registerInstance,
|
|
18
|
+
cgroupMemoryLimit,
|
|
17
19
|
GiB,
|
|
18
20
|
DEFAULT_MEM_FRACTION,
|
|
19
21
|
MIN_MEM_LIMIT,
|
|
@@ -243,3 +245,59 @@ test('resolveEnv with the Go codegen never enables the plugin', () => {
|
|
|
243
245
|
const env = resolveEnv({ GOLAR_PLUGIN: 'vue' }, { ...machine, codegen: 'go' });
|
|
244
246
|
assert.strictEqual('GOLAR_PLUGIN' in env, false);
|
|
245
247
|
});
|
|
248
|
+
|
|
249
|
+
function fakeCgroupFs(files) {
|
|
250
|
+
return (p) => {
|
|
251
|
+
if (!(p in files)) {
|
|
252
|
+
throw Object.assign(new Error(`ENOENT: ${p}`), { code: 'ENOENT' });
|
|
253
|
+
}
|
|
254
|
+
return files[p];
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
test('cgroupMemoryLimit finds a v2 limit set on a parent cgroup', () => {
|
|
259
|
+
const read = fakeCgroupFs({
|
|
260
|
+
'/proc/self/cgroup': '0::/seve.slice/job-42/step\n',
|
|
261
|
+
'/sys/fs/cgroup/seve.slice/job-42/step/memory.max': 'max\n',
|
|
262
|
+
'/sys/fs/cgroup/seve.slice/job-42/step/memory.current': String(1 * GiB),
|
|
263
|
+
'/sys/fs/cgroup/seve.slice/job-42/memory.max': String(8 * GiB),
|
|
264
|
+
'/sys/fs/cgroup/seve.slice/job-42/memory.current': String(3 * GiB),
|
|
265
|
+
'/sys/fs/cgroup/seve.slice/memory.max': String(15 * GiB),
|
|
266
|
+
'/sys/fs/cgroup/memory.max': 'max',
|
|
267
|
+
});
|
|
268
|
+
assert.deepStrictEqual(cgroupMemoryLimit(read), { limit: 8 * GiB, usage: 3 * GiB });
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
test('cgroupMemoryLimit honors memory.high and cgroup v1', () => {
|
|
272
|
+
const v2 = fakeCgroupFs({
|
|
273
|
+
'/proc/self/cgroup': '0::/job\n',
|
|
274
|
+
'/sys/fs/cgroup/job/memory.max': String(10 * GiB),
|
|
275
|
+
'/sys/fs/cgroup/job/memory.high': String(8 * GiB),
|
|
276
|
+
});
|
|
277
|
+
assert.deepStrictEqual(cgroupMemoryLimit(v2), { limit: 8 * GiB, usage: undefined });
|
|
278
|
+
const v1 = fakeCgroupFs({
|
|
279
|
+
'/proc/self/cgroup': '12:cpu,cpuacct:/docker/abc\n4:memory:/docker/abc\n',
|
|
280
|
+
'/sys/fs/cgroup/memory/docker/abc/memory.limit_in_bytes': String(6 * GiB),
|
|
281
|
+
'/sys/fs/cgroup/memory/docker/abc/memory.usage_in_bytes': String(1 * GiB),
|
|
282
|
+
'/sys/fs/cgroup/memory/memory.limit_in_bytes': '9223372036854771712',
|
|
283
|
+
});
|
|
284
|
+
assert.deepStrictEqual(cgroupMemoryLimit(v1), { limit: 6 * GiB, usage: 1 * GiB });
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test('cgroupMemoryLimit is undefined without a limit or outside Linux', () => {
|
|
288
|
+
assert.strictEqual(cgroupMemoryLimit(fakeCgroupFs({})), undefined);
|
|
289
|
+
const unlimited = fakeCgroupFs({ '/proc/self/cgroup': '0::/\n', '/sys/fs/cgroup/memory.max': 'max\n' });
|
|
290
|
+
assert.strictEqual(cgroupMemoryLimit(unlimited), undefined);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
test('resolveEnv follows a hard cgroup limit instead of the free RAM', () => {
|
|
294
|
+
const env = resolveEnv({}, { ...machine, totalMem: 10 * GiB, availableMem: 5 * GiB, hardLimit: 8 * GiB });
|
|
295
|
+
assert.strictEqual(Number(env.GOMEMLIMIT), computeHardGoMemLimit(8 * GiB));
|
|
296
|
+
assert.strictEqual(computeHardGoMemLimit(8 * GiB), Math.floor(8 * GiB * 0.9));
|
|
297
|
+
assert.strictEqual(computeHardGoMemLimit(8 * GiB, 2), Math.floor(8 * GiB * 0.9 / 2));
|
|
298
|
+
const volar = resolveEnv({ GOLAR_PLUGIN_WORKERS: '1' }, { ...machine, hardLimit: 8 * GiB, codegen: 'volar' });
|
|
299
|
+
assert.strictEqual(Number(volar.GOMEMLIMIT), computeHardGoMemLimit(8 * GiB) - 200 * 1024 ** 2 * 2, 'Volar memory reserved');
|
|
300
|
+
const explicit = resolveEnv({ GOMEMLIMIT: '5GiB' }, { ...machine, hardLimit: 8 * GiB });
|
|
301
|
+
assert.strictEqual(explicit.GOMEMLIMIT, '5GiB');
|
|
302
|
+
assert.strictEqual(computeHardGoMemLimit(undefined), null);
|
|
303
|
+
});
|