slotBudget

tcmts.cli.running.slotBudget · #szlnjha4 · term · runs on the server

uses node:os

How many slots the machine has, and why: TCM_SLOTS (or TCM_SUITE_SLOTS) if set, else what defaultSlots makes of its logical cores, its performance cores (on a chip with two kinds) and its memory, as machineSlots works it out.

import { defaultSlots } from "#codebase/tcmts/cli/budget";
import { spawnSync } from "node:child_process";
import os from "node:os";

export function slotBudget(): {
  size: number;
  env?: string;
  cpus: number;
  performance: number;
  memory: number;
  byCores: number;
  byMemory: number;
} {
  const cpus = os.availableParallelism();
  let performance = cpus;
  if (process.platform === "darwin") {
    const r = spawnSync("sysctl", ["-n", "hw.perflevel0.logicalcpu"], { encoding: "utf8" });
    const n = Number(r.stdout?.trim());
    if (r.status === 0 && n > 0) performance = n;
  }
  const memory = os.totalmem();
  const p = Math.min(Math.max(performance, 0), cpus) || cpus;
  const machine = {
    cpus,
    performance,
    memory,
    byCores: p + Math.floor((cpus - p) / 3),
    byMemory: Math.floor(memory / 2 ** 31),
  };
  for (const env of ["TCM_SLOTS", "TCM_SUITE_SLOTS"]) {
    const given = Number(process.env[env]);
    if (process.env[env] && Number.isInteger(given) && given > 0)
      return { size: given, env, ...machine };
  }
  return { size: defaultSlots(cpus, performance, memory), ...machine };
}

uses

used by