lup-system
Version:
NodeJS library to retrieve system information and utilization.
103 lines (102 loc) • 3.82 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CPU_COMPUTE_UTILIZATION_INTERVAL = void 0;
exports.stopCpuUtilizationComputation = stopCpuUtilizationComputation;
exports.getCpuInfo = getCpuInfo;
exports.getCpuUtilization = getCpuUtilization;
exports.getCpuCoreCount = getCpuCoreCount;
const os_1 = __importDefault(require("os"));
const utils_1 = require("./utils");
/** Intervall in milliseconds at which CPU utilization is computed. */
exports.CPU_COMPUTE_UTILIZATION_INTERVAL = 1000;
const CPU_COMPUTE_UTILIZATION_INITIAL_DELAY = 50;
let PREV_CPU_CORES = [];
const CPU_UTILIZATION = {
overall: 0,
cores: [],
};
let CPU_COMPUTE_RUNNING = false;
let CPU_COMPUTE_TIMEOUT = null;
async function computeCpuUtilization() {
const cpuCores = os_1.default.cpus();
const min = Math.min(cpuCores.length, PREV_CPU_CORES.length);
if (CPU_UTILIZATION.cores.length < min)
for (let i = CPU_UTILIZATION.cores.length; i < min; i++)
CPU_UTILIZATION.cores.push(0);
else if (CPU_UTILIZATION.cores.length > min)
CPU_UTILIZATION.cores = CPU_UTILIZATION.cores.slice(0, min);
let totalUsage = 0;
let totalTotal = 0;
for (let i = 0; i < min; i++) {
const prev = PREV_CPU_CORES[i];
const next = cpuCores[i];
const nextUsage = (next.times.user + next.times.nice + next.times.sys + next.times.irq) * 1.0;
const prevUsage = prev.times.user + prev.times.nice + prev.times.sys + prev.times.irq;
const usage = nextUsage - prevUsage;
const nextTotal = nextUsage + next.times.idle;
const prevTotal = prevUsage + prev.times.idle;
const total = nextTotal - prevTotal;
CPU_UTILIZATION.cores[i] = usage / total;
totalUsage += usage;
totalTotal += total;
}
CPU_UTILIZATION.overall = totalTotal !== 0 ? totalUsage / totalTotal : 0;
PREV_CPU_CORES = cpuCores;
}
async function runCpuComputeInterval() {
CPU_COMPUTE_RUNNING = true;
await computeCpuUtilization();
if (CPU_COMPUTE_RUNNING)
CPU_COMPUTE_TIMEOUT = setTimeout(runCpuComputeInterval, Math.max(exports.CPU_COMPUTE_UTILIZATION_INTERVAL, 1));
}
/**
* Stops the computation of CPU utilization.
* As soon as one of the getCpu*Utilization functions is called again, the computation will be restarted.
*/
function stopCpuUtilizationComputation() {
if (CPU_COMPUTE_TIMEOUT)
clearTimeout(CPU_COMPUTE_TIMEOUT);
CPU_COMPUTE_TIMEOUT = null;
CPU_COMPUTE_RUNNING = false;
}
/**
* Returns information about the CPU.
*
* @returns CPU information.
*/
async function getCpuInfo() {
const cpuCores = os_1.default.cpus();
return {
architecture: os_1.default.arch(),
coreCount: cpuCores.length,
endian: os_1.default.endianness(),
name: cpuCores[0].model,
speed: cpuCores[0].speed,
utilization: await getCpuUtilization(),
};
}
/**
* Returns the current CPU utilization.
* If the computation is not running, it will start the computation and return the initial values.
*
* @returns CPU utilization data.
*/
async function getCpuUtilization() {
if (!CPU_COMPUTE_RUNNING) {
await runCpuComputeInterval(); // runs the first computation immediately
await (0, utils_1.sleep)(CPU_COMPUTE_UTILIZATION_INITIAL_DELAY); // wait a bit to get initial values
await computeCpuUtilization(); // run second computation immediately to get initial values
}
return CPU_UTILIZATION;
}
/**
* Returns the number of CPU cores available on the system.
*
* @returns Number of CPU cores.
*/
function getCpuCoreCount() {
return os_1.default.cpus().length;
}