UNPKG

@nodedaemon/core

Version:

Production-ready Node.js process manager with zero external dependencies

82 lines 2.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.calculateCpuPercentage = calculateCpuPercentage; exports.getProcessCpuUsage = getProcessCpuUsage; exports.clearCpuCache = clearCpuCache; const os_1 = require("os"); const cpuUsageCache = new Map(); function getCpuUsage() { const cpuList = (0, os_1.cpus)(); let user = 0; let system = 0; let idle = 0; let total = 0; cpuList.forEach(cpu => { user += cpu.times.user; system += cpu.times.sys; idle += cpu.times.idle; total += cpu.times.user + cpu.times.sys + cpu.times.idle + cpu.times.irq; }); return { user, system, idle, total }; } function calculateCpuPercentage(pid) { if (!pid) return 0; const now = Date.now(); const currentCpuUsage = getCpuUsage(); const cached = cpuUsageCache.get(pid); if (!cached) { cpuUsageCache.set(pid, { lastCpuUsage: currentCpuUsage, lastCheck: now }); return 0; } const timeDiff = now - cached.lastCheck; if (timeDiff < 1000) { return 0; // Need at least 1 second between measurements } const totalDiff = currentCpuUsage.total - cached.lastCpuUsage.total; const idleDiff = currentCpuUsage.idle - cached.lastCpuUsage.idle; if (totalDiff === 0) return 0; const usage = 100 - (100 * idleDiff / totalDiff); cpuUsageCache.set(pid, { lastCpuUsage: currentCpuUsage, lastCheck: now }); return Math.max(0, Math.min(100, usage)); } function getProcessCpuUsage(pid) { return new Promise((resolve) => { try { if (process.platform === 'win32') { // Windows: Use wmic command const { exec } = require('child_process'); exec(`wmic process where ProcessId=${pid} get PercentProcessorTime`, (error, stdout) => { if (error) { resolve(0); return; } const lines = stdout.trim().split('\n'); const cpu = parseFloat(lines[1]) || 0; resolve(cpu); }); } else { // Unix/Linux: Use ps command const { exec } = require('child_process'); exec(`ps -p ${pid} -o %cpu`, (error, stdout) => { if (error) { resolve(0); return; } const lines = stdout.trim().split('\n'); const cpu = parseFloat(lines[1]) || 0; resolve(cpu); }); } } catch { resolve(0); } }); } function clearCpuCache(pid) { cpuUsageCache.delete(pid); } //# sourceMappingURL=cpu.js.map