vue-go-tsc 0.3.1 → 0.3.3

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 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
- function resolveEnv(env, { cpus, totalMem, instances = 1, idleCpus, availableMem, codegen = 'go', volar }) {
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
  }
@@ -231,6 +253,39 @@ function resolveEnv(env, { cpus, totalMem, instances = 1, idleCpus, availableMem
231
253
  return out;
232
254
  }
233
255
 
256
+ // Whether to print the resource budget: with tsc's own performance flags
257
+ // (`--diagnostics`, `--extendedDiagnostics`) or VUE_GO_TSC_VERBOSE. The budget
258
+ // depends on the machine's load at startup, so the same project can run in
259
+ // 30 s on one CI runner and 2 min on another; this line tells which.
260
+ function wantsBudget(args, env = {}) {
261
+ if (env.VUE_GO_TSC_VERBOSE && env.VUE_GO_TSC_VERBOSE !== '0') {
262
+ return true;
263
+ }
264
+ return args.some((arg) => arg === '--diagnostics' || arg === '--extendedDiagnostics');
265
+ }
266
+
267
+ // One line describing the budget `resolveEnv` gave the run and what it was
268
+ // derived from. Values the user set explicitly are marked as such.
269
+ function formatBudget(env, resolved, { cpus, idleCpus, instances = 1, totalMem, availableMem, hardLimit, codegen }) {
270
+ const gib = (bytes) => (Number.isFinite(bytes) ? `${(bytes / GiB).toFixed(1)} GiB` : 'unknown');
271
+ const origin = (name) => (env[name] ? ' (set)' : '');
272
+ const parts = [
273
+ `GOMAXPROCS=${resolved.GOMAXPROCS ?? 'default'}${origin('GOMAXPROCS')}`,
274
+ // An explicit GOMEMLIMIT keeps its own syntax (e.g. `5GiB`).
275
+ `GOMEMLIMIT=${env.GOMEMLIMIT || (resolved.GOMEMLIMIT ? gib(Number(resolved.GOMEMLIMIT)) : 'default')}${origin('GOMEMLIMIT')}`,
276
+ `codegen=${codegen}`,
277
+ ];
278
+ if (codegen === 'volar') {
279
+ parts.push(`workers=${resolved.GOLAR_PLUGIN_WORKERS}${origin('GOLAR_PLUGIN_WORKERS')}`);
280
+ }
281
+ const idle = Number.isFinite(idleCpus) ? `, ${idleCpus.toFixed(1)} idle` : '';
282
+ parts.push(
283
+ `from ${cpus} cores${idle}, ${instances} run(s), RAM ${gib(totalMem)} (${gib(availableMem)} available)` +
284
+ (Number.isFinite(hardLimit) ? `, cgroup limit ${gib(hardLimit)}` : '')
285
+ );
286
+ return `vue-go-tsc: ${parts.join(', ')}`;
287
+ }
288
+
234
289
  function isProcessAlive(pid) {
235
290
  try {
236
291
  process.kill(pid, 0);
@@ -296,18 +351,165 @@ function availableCpus() {
296
351
  return typeof os.availableParallelism === 'function' ? os.availableParallelism() : os.cpus().length;
297
352
  }
298
353
 
299
- // Usable RAM: honors a cgroup memory limit (containers) when it is tighter
300
- // than physical RAM.
301
- function totalMemory() {
302
- const total = os.totalmem();
354
+ // cgroup limits at or above this are "unlimited" (cgroup v1 reports a huge
355
+ // page-aligned number instead of "max").
356
+ const CGROUP_UNLIMITED = 2 ** 60;
357
+
358
+ function parseCgroupBytes(text) {
359
+ const value = Number(String(text).trim());
360
+ return Number.isFinite(value) && value > 0 && value < CGROUP_UNLIMITED ? value : undefined;
361
+ }
362
+
363
+ // Call `visit(dir, version)` for every cgroup directory of this process that
364
+ // carries `controller` (cgroup v2 unified hierarchy, or the v1 hierarchy of
365
+ // that controller), from the process's own cgroup up to the root. Returns
366
+ // false when the process's cgroups cannot be read (not on Linux).
367
+ function walkCgroups(readFile, root, controller, visit) {
368
+ let membership;
369
+ try {
370
+ membership = readFile('/proc/self/cgroup');
371
+ } catch {
372
+ return false;
373
+ }
374
+ for (const line of membership.split('\n')) {
375
+ // <hierarchy-id>:<controllers>:<path>; the path itself may contain ':'.
376
+ const first = line.indexOf(':');
377
+ const second = line.indexOf(':', first + 1);
378
+ if (first < 0 || second < 0) {
379
+ continue;
380
+ }
381
+ const id = line.slice(0, first);
382
+ const controllers = line.slice(first + 1, second).split(',');
383
+ let dir = line.slice(second + 1);
384
+ let base;
385
+ let version;
386
+ if (id === '0' && controllers.length === 1 && controllers[0] === '') {
387
+ base = root;
388
+ version = 2;
389
+ } else if (controllers.includes(controller)) {
390
+ base = join(root, controllers.join(','));
391
+ version = 1;
392
+ } else {
393
+ continue;
394
+ }
395
+ for (;;) {
396
+ visit(join(base, dir), version);
397
+ if (dir === '/' || dir === '') {
398
+ break;
399
+ }
400
+ dir = posix.dirname(dir);
401
+ }
402
+ }
403
+ return true;
404
+ }
405
+
406
+ function readOptional(readFile, file) {
407
+ try {
408
+ return readFile(file);
409
+ } catch {
410
+ return undefined;
411
+ }
412
+ }
413
+
414
+ // The tightest cgroup memory limit of this process and the usage counted
415
+ // against it: { limit, usage } (usage may be undefined), or undefined when
416
+ // there is none (or not on Linux).
417
+ //
418
+ // process.constrainedMemory() only reads the process's own cgroup, but a CI
419
+ // job or container runtime often sets the limit on a parent (the job's slice)
420
+ // and leaves the leaf unlimited, so this walks from the process's cgroup up to
421
+ // the root and keeps the smallest memory.max / memory.high (v2) or
422
+ // memory.limit_in_bytes (v1).
423
+ function cgroupMemoryLimit(readFile = (p) => fs.readFileSync(p, 'utf8'), root = '/sys/fs/cgroup') {
424
+ let best;
425
+ walkCgroups(readFile, root, 'memory', (cgroupDir, version) => {
426
+ const files =
427
+ version === 2
428
+ ? { limits: ['memory.max', 'memory.high'], usage: 'memory.current' }
429
+ : { limits: ['memory.limit_in_bytes'], usage: 'memory.usage_in_bytes' };
430
+ for (const name of files.limits) {
431
+ const limit = parseCgroupBytes(readOptional(readFile, join(cgroupDir, name)));
432
+ if (limit !== undefined && (!best || limit < best.limit)) {
433
+ best = { limit, usage: parseCgroupBytes(readOptional(readFile, join(cgroupDir, files.usage))) };
434
+ }
435
+ }
436
+ });
437
+ return best;
438
+ }
439
+
440
+ // The tightest cgroup CPU quota of this process, in cores (may be fractional),
441
+ // or undefined when there is none: cpu.max "<quota> <period>" (v2) or
442
+ // cpu.cfs_quota_us / cpu.cfs_period_us (v1), on the process's cgroup or any
443
+ // parent. The Go runtime honors this quota for its default GOMAXPROCS, but the
444
+ // launcher sets GOMAXPROCS itself, so it must apply the quota too: more Go
445
+ // threads than the quota only get the job throttled.
446
+ function cgroupCpuLimit(readFile = (p) => fs.readFileSync(p, 'utf8'), root = '/sys/fs/cgroup') {
447
+ let best;
448
+ walkCgroups(readFile, root, 'cpu', (cgroupDir, version) => {
449
+ let quota;
450
+ let period;
451
+ if (version === 2) {
452
+ [quota, period] = String(readOptional(readFile, join(cgroupDir, 'cpu.max')) ?? '').trim().split(/\s+/).map(Number);
453
+ } else {
454
+ quota = Number(readOptional(readFile, join(cgroupDir, 'cpu.cfs_quota_us')));
455
+ period = Number(readOptional(readFile, join(cgroupDir, 'cpu.cfs_period_us')));
456
+ }
457
+ // "max" (v2) parses to NaN and -1 (v1) means no quota.
458
+ if (Number.isFinite(quota) && quota > 0 && Number.isFinite(period) && period > 0) {
459
+ const cores = quota / period;
460
+ if (best === undefined || cores < best) {
461
+ best = cores;
462
+ }
463
+ }
464
+ });
465
+ return best;
466
+ }
467
+
468
+ // Usable RAM: honors a cgroup memory limit (containers, CI jobs) when it is
469
+ // tighter than physical RAM, including a limit set on a parent cgroup.
470
+ function totalMemory(cgroup = cgroupMemoryLimit()) {
471
+ let total = os.totalmem();
303
472
  const constrained = typeof process.constrainedMemory === 'function' ? process.constrainedMemory() : 0;
304
- return constrained > 0 && constrained < total ? constrained : total;
473
+ if (constrained > 0 && constrained < total) {
474
+ total = constrained;
475
+ }
476
+ return cgroup && cgroup.limit < total ? cgroup.limit : total;
305
477
  }
306
478
 
307
479
  // RAM currently available to this process (MemAvailable on Linux, bounded by
308
480
  // the cgroup limit when there is one).
309
- function freeMemory() {
310
- return typeof process.availableMemory === 'function' ? process.availableMemory() : os.freemem();
481
+ function freeMemory(cgroup = cgroupMemoryLimit()) {
482
+ const available = typeof process.availableMemory === 'function' ? process.availableMemory() : os.freemem();
483
+ if (cgroup && cgroup.usage !== undefined) {
484
+ return Math.min(available, Math.max(0, cgroup.limit - cgroup.usage));
485
+ }
486
+ return available;
487
+ }
488
+
489
+ // Cores this run may use: the CPUs it may be scheduled on, bounded by the
490
+ // cgroup CPU quota (rounded up: a 2.5-core quota still runs 3 threads at once).
491
+ function usableCpus(available, cpuQuota) {
492
+ if (Number.isFinite(cpuQuota) && cpuQuota > 0) {
493
+ return Math.max(1, Math.min(available, Math.ceil(cpuQuota)));
494
+ }
495
+ return available;
496
+ }
497
+
498
+ // Whether GOMAXPROCS should follow the cores idle right now.
499
+ //
500
+ // On a developer machine, that keeps a typecheck from freezing the editor. In a
501
+ // CI job confined to a cgroup (memory limit or CPU quota), the idle sample
502
+ // reads the whole host (/proc/stat): on a runner busy with other jobs it falls
503
+ // to ~0 and GOMAXPROCS to its floor (1 on an 8-core runner), with a single
504
+ // Volar codegen worker. The run then does the same work on one core and holds
505
+ // its memory reservation several times longer (26 s on an idle runner, 135 s
506
+ // on a busy one for the same Nuxt app), while the kernel already shares the
507
+ // cores fairly between the jobs' cgroups.
508
+ function shouldSampleIdleCpus(env, { memoryLimit, cpuQuota } = {}) {
509
+ if (env.GOMAXPROCS) {
510
+ return false;
511
+ }
512
+ return !(Number.isFinite(memoryLimit) || Number.isFinite(cpuQuota));
311
513
  }
312
514
 
313
515
  // Idle cores right now, sampled over CPU_SAMPLE_MS.
@@ -336,23 +538,34 @@ async function main() {
336
538
  const { instances, unregister } = registerInstance();
337
539
  process.on('exit', unregister);
338
540
 
339
- const cpus = availableCpus();
541
+ const cgroup = cgroupMemoryLimit();
542
+ const cpuQuota = cgroupCpuLimit();
543
+ const cpus = usableCpus(availableCpus(), cpuQuota);
340
544
  // Sampling costs CPU_SAMPLE_MS of startup; skip it when it cannot matter.
341
- const idleCpus = process.env.GOMAXPROCS ? undefined : await sampleIdleCpus(cpus);
545
+ const idleCpus = shouldSampleIdleCpus(process.env, { memoryLimit: cgroup?.limit, cpuQuota })
546
+ ? await sampleIdleCpus(cpus)
547
+ : undefined;
342
548
 
343
- const env = resolveEnv(process.env, {
549
+ const resources = {
344
550
  cpus,
345
- totalMem: totalMemory(),
551
+ totalMem: totalMemory(cgroup),
346
552
  instances,
347
553
  idleCpus,
348
- availableMem: freeMemory(),
554
+ availableMem: freeMemory(cgroup),
555
+ hardLimit: cgroup?.limit,
349
556
  codegen,
557
+ };
558
+ const env = resolveEnv(process.env, {
559
+ ...resources,
350
560
  volar: {
351
561
  pluginEntry: VOLAR_PLUGIN,
352
562
  nodePath: process.execPath,
353
563
  tsconfig: resolveProjectConfig(args, process.cwd()),
354
564
  },
355
565
  });
566
+ if (wantsBudget(args, process.env)) {
567
+ console.error(formatBudget(process.env, env, resources));
568
+ }
356
569
 
357
570
  // Forward the remaining arguments to the binary
358
571
  const child = spawn(BIN_PATH, args, {
@@ -403,9 +616,16 @@ module.exports = {
403
616
  computeCodegenWorkers,
404
617
  computeGoMaxProcs,
405
618
  computeGoMemLimit,
619
+ computeHardGoMemLimit,
406
620
  measureIdleCpus,
407
621
  resolveEnv,
622
+ wantsBudget,
623
+ formatBudget,
408
624
  registerInstance,
625
+ cgroupMemoryLimit,
626
+ cgroupCpuLimit,
627
+ usableCpus,
628
+ shouldSampleIdleCpus,
409
629
  GiB,
410
630
  DEFAULT_CPU_FRACTION,
411
631
  DEFAULT_MEM_FRACTION,
@@ -11,9 +11,16 @@ const {
11
11
  computeCodegenWorkers,
12
12
  computeGoMaxProcs,
13
13
  computeGoMemLimit,
14
+ computeHardGoMemLimit,
14
15
  measureIdleCpus,
15
16
  resolveEnv,
17
+ wantsBudget,
18
+ formatBudget,
16
19
  registerInstance,
20
+ cgroupMemoryLimit,
21
+ cgroupCpuLimit,
22
+ usableCpus,
23
+ shouldSampleIdleCpus,
17
24
  GiB,
18
25
  DEFAULT_MEM_FRACTION,
19
26
  MIN_MEM_LIMIT,
@@ -243,3 +250,128 @@ test('resolveEnv with the Go codegen never enables the plugin', () => {
243
250
  const env = resolveEnv({ GOLAR_PLUGIN: 'vue' }, { ...machine, codegen: 'go' });
244
251
  assert.strictEqual('GOLAR_PLUGIN' in env, false);
245
252
  });
253
+
254
+ function fakeCgroupFs(files) {
255
+ return (p) => {
256
+ if (!(p in files)) {
257
+ throw Object.assign(new Error(`ENOENT: ${p}`), { code: 'ENOENT' });
258
+ }
259
+ return files[p];
260
+ };
261
+ }
262
+
263
+ test('cgroupMemoryLimit finds a v2 limit set on a parent cgroup', () => {
264
+ const read = fakeCgroupFs({
265
+ '/proc/self/cgroup': '0::/seve.slice/job-42/step\n',
266
+ '/sys/fs/cgroup/seve.slice/job-42/step/memory.max': 'max\n',
267
+ '/sys/fs/cgroup/seve.slice/job-42/step/memory.current': String(1 * GiB),
268
+ '/sys/fs/cgroup/seve.slice/job-42/memory.max': String(8 * GiB),
269
+ '/sys/fs/cgroup/seve.slice/job-42/memory.current': String(3 * GiB),
270
+ '/sys/fs/cgroup/seve.slice/memory.max': String(15 * GiB),
271
+ '/sys/fs/cgroup/memory.max': 'max',
272
+ });
273
+ assert.deepStrictEqual(cgroupMemoryLimit(read), { limit: 8 * GiB, usage: 3 * GiB });
274
+ });
275
+
276
+ test('cgroupMemoryLimit honors memory.high and cgroup v1', () => {
277
+ const v2 = fakeCgroupFs({
278
+ '/proc/self/cgroup': '0::/job\n',
279
+ '/sys/fs/cgroup/job/memory.max': String(10 * GiB),
280
+ '/sys/fs/cgroup/job/memory.high': String(8 * GiB),
281
+ });
282
+ assert.deepStrictEqual(cgroupMemoryLimit(v2), { limit: 8 * GiB, usage: undefined });
283
+ const v1 = fakeCgroupFs({
284
+ '/proc/self/cgroup': '12:cpu,cpuacct:/docker/abc\n4:memory:/docker/abc\n',
285
+ '/sys/fs/cgroup/memory/docker/abc/memory.limit_in_bytes': String(6 * GiB),
286
+ '/sys/fs/cgroup/memory/docker/abc/memory.usage_in_bytes': String(1 * GiB),
287
+ '/sys/fs/cgroup/memory/memory.limit_in_bytes': '9223372036854771712',
288
+ });
289
+ assert.deepStrictEqual(cgroupMemoryLimit(v1), { limit: 6 * GiB, usage: 1 * GiB });
290
+ });
291
+
292
+ test('cgroupMemoryLimit is undefined without a limit or outside Linux', () => {
293
+ assert.strictEqual(cgroupMemoryLimit(fakeCgroupFs({})), undefined);
294
+ const unlimited = fakeCgroupFs({ '/proc/self/cgroup': '0::/\n', '/sys/fs/cgroup/memory.max': 'max\n' });
295
+ assert.strictEqual(cgroupMemoryLimit(unlimited), undefined);
296
+ });
297
+
298
+ test('resolveEnv follows a hard cgroup limit instead of the free RAM', () => {
299
+ const env = resolveEnv({}, { ...machine, totalMem: 10 * GiB, availableMem: 5 * GiB, hardLimit: 8 * GiB });
300
+ assert.strictEqual(Number(env.GOMEMLIMIT), computeHardGoMemLimit(8 * GiB));
301
+ assert.strictEqual(computeHardGoMemLimit(8 * GiB), Math.floor(8 * GiB * 0.9));
302
+ assert.strictEqual(computeHardGoMemLimit(8 * GiB, 2), Math.floor(8 * GiB * 0.9 / 2));
303
+ const volar = resolveEnv({ GOLAR_PLUGIN_WORKERS: '1' }, { ...machine, hardLimit: 8 * GiB, codegen: 'volar' });
304
+ assert.strictEqual(Number(volar.GOMEMLIMIT), computeHardGoMemLimit(8 * GiB) - 200 * 1024 ** 2 * 2, 'Volar memory reserved');
305
+ const explicit = resolveEnv({ GOMEMLIMIT: '5GiB' }, { ...machine, hardLimit: 8 * GiB });
306
+ assert.strictEqual(explicit.GOMEMLIMIT, '5GiB');
307
+ assert.strictEqual(computeHardGoMemLimit(undefined), null);
308
+ });
309
+
310
+ test('wantsBudget follows the tsc performance flags and VUE_GO_TSC_VERBOSE', () => {
311
+ assert.strictEqual(wantsBudget(['--noEmit']), false);
312
+ assert.strictEqual(wantsBudget(['--noEmit', '--extendedDiagnostics']), true);
313
+ assert.strictEqual(wantsBudget(['--diagnostics']), true);
314
+ assert.strictEqual(wantsBudget([], { VUE_GO_TSC_VERBOSE: '1' }), true);
315
+ assert.strictEqual(wantsBudget([], { VUE_GO_TSC_VERBOSE: '0' }), false);
316
+ });
317
+
318
+ test('formatBudget reports the resolved budget and what it came from', () => {
319
+ const resources = { cpus: 8, idleCpus: 0.4, instances: 2, totalMem: 8 * GiB, availableMem: 6 * GiB, hardLimit: 8 * GiB, codegen: 'volar' };
320
+ const resolved = resolveEnv({}, resources);
321
+ const line = formatBudget({}, resolved, resources);
322
+ assert.strictEqual(
323
+ line,
324
+ `vue-go-tsc: GOMAXPROCS=1, GOMEMLIMIT=${(Number(resolved.GOMEMLIMIT) / GiB).toFixed(1)} GiB, codegen=volar, workers=1, ` +
325
+ 'from 8 cores, 0.4 idle, 2 run(s), RAM 8.0 GiB (6.0 GiB available), cgroup limit 8.0 GiB'
326
+ );
327
+ });
328
+
329
+ test('formatBudget marks explicit values and keeps their syntax', () => {
330
+ const env = { GOMAXPROCS: '4', GOMEMLIMIT: '5GiB' };
331
+ const resources = { cpus: 16, totalMem: 32 * GiB, codegen: 'go' };
332
+ const line = formatBudget(env, resolveEnv(env, resources), resources);
333
+ assert.ok(line.startsWith('vue-go-tsc: GOMAXPROCS=4 (set), GOMEMLIMIT=5GiB (set), codegen=go, from 16 cores, 1 run(s)'), line);
334
+ assert.ok(!line.includes('workers='), 'no workers without the Volar codegen');
335
+ assert.ok(!line.includes('cgroup'), 'no cgroup limit reported when there is none');
336
+ });
337
+
338
+ test('cgroupCpuLimit finds the tightest quota on the cgroup or a parent (v2)', () => {
339
+ const read = fakeCgroupFs({
340
+ '/proc/self/cgroup': '0::/seve.slice/job-42/step\n',
341
+ '/sys/fs/cgroup/seve.slice/job-42/step/cpu.max': 'max 100000\n',
342
+ '/sys/fs/cgroup/seve.slice/job-42/cpu.max': '250000 100000\n',
343
+ '/sys/fs/cgroup/seve.slice/cpu.max': '800000 100000\n',
344
+ });
345
+ assert.strictEqual(cgroupCpuLimit(read), 2.5);
346
+ });
347
+
348
+ test('cgroupCpuLimit reads cgroup v1 and ignores unlimited quotas', () => {
349
+ const v1 = fakeCgroupFs({
350
+ '/proc/self/cgroup': '12:cpu,cpuacct:/docker/abc\n4:memory:/docker/abc\n',
351
+ '/sys/fs/cgroup/cpu,cpuacct/docker/abc/cpu.cfs_quota_us': '200000',
352
+ '/sys/fs/cgroup/cpu,cpuacct/docker/abc/cpu.cfs_period_us': '100000',
353
+ '/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us': '-1',
354
+ '/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us': '100000',
355
+ });
356
+ assert.strictEqual(cgroupCpuLimit(v1), 2);
357
+ const unlimited = fakeCgroupFs({ '/proc/self/cgroup': '0::/\n', '/sys/fs/cgroup/cpu.max': 'max 100000\n' });
358
+ assert.strictEqual(cgroupCpuLimit(unlimited), undefined);
359
+ assert.strictEqual(cgroupCpuLimit(fakeCgroupFs({})), undefined);
360
+ });
361
+
362
+ test('usableCpus applies the CPU quota, rounded up', () => {
363
+ assert.strictEqual(usableCpus(8, undefined), 8);
364
+ assert.strictEqual(usableCpus(8, 2.5), 3);
365
+ assert.strictEqual(usableCpus(8, 0.5), 1);
366
+ assert.strictEqual(usableCpus(4, 16), 4);
367
+ });
368
+
369
+ test('shouldSampleIdleCpus samples only outside a CI job cgroup', () => {
370
+ assert.strictEqual(shouldSampleIdleCpus({}), true, 'developer machine');
371
+ assert.strictEqual(shouldSampleIdleCpus({}, { memoryLimit: 8 * GiB }), false, 'memory-limited job');
372
+ assert.strictEqual(shouldSampleIdleCpus({}, { cpuQuota: 4 }), false, 'CPU-limited job');
373
+ assert.strictEqual(shouldSampleIdleCpus({ GOMAXPROCS: '2' }), false, 'explicit GOMAXPROCS');
374
+ // Without the sample, a job on an 8-core runner keeps half of the cores
375
+ // instead of falling to one whenever another job is running.
376
+ assert.strictEqual(computeGoMaxProcs(8, 1, undefined), 4);
377
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-go-tsc",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "Blazingly fast Vue 3 type checker powered by typescript-go — a Go-native alternative to vue-tsc",
5
5
  "keywords": [
6
6
  "vue",