From e3dc1bfec5db922d8205a9bcf4daf42e3c28357e Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Sun, 20 Aug 2023 20:35:52 +0200 Subject: [PATCH] cpus() added steal and guest time (linux) --- .github/ISSUE_TEMPLATE/bug_report.md | 6 +++ lib/cpu.js | 73 +++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 5950129..f4e1158 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -28,5 +28,11 @@ A clear and concise description of what you expected to happen. - OS: [e.g. macOS] - Hardware [e.g. MacBook Pro 13] +To get all needed environment information, please run the following command: + +``` + npx systeminformation info +``` + **Additional context** Add any other context about the problem here. diff --git a/lib/cpu.js b/lib/cpu.js index af4aa22..0c16d5c 100644 --- a/lib/cpu.js +++ b/lib/cpu.js @@ -36,6 +36,8 @@ let _current_cpu = { system: 0, idle: 0, irq: 0, + steal: 0, + guest: 0, load: 0, tick: 0, ms: 0, @@ -45,12 +47,16 @@ let _current_cpu = { currentLoadNice: 0, currentLoadIdle: 0, currentLoadIrq: 0, + currentLoadSteal: 0, + currentLoadGuest: 0, rawCurrentLoad: 0, rawCurrentLoadUser: 0, rawCurrentLoadSystem: 0, rawCurrentLoadNice: 0, rawCurrentLoadIdle: 0, - rawCurrentLoadIrq: 0 + rawCurrentLoadIrq: 0, + rawCurrentLoadSteal: 0, + rawCurrentLoadGuest: 0 }; let _cpus = []; let _corecount = 0; @@ -1559,15 +1565,44 @@ function getLoad() { let now = Date.now() - _current_cpu.ms; if (now >= 200) { _current_cpu.ms = Date.now(); - const cpus = os.cpus(); + const cpus = os.cpus().map(function (cpu) { + cpu.times.steal = 0; + cpu.times.guest = 0; + return cpu; + }); let totalUser = 0; let totalSystem = 0; let totalNice = 0; let totalIrq = 0; let totalIdle = 0; + let totalSteal = 0; + let totalGuest = 0; let cores = []; _corecount = (cpus && cpus.length) ? cpus.length : 0; + // linux: try to get other cpu stats + if (_linux) { + try { + const lines = execSync('cat /proc/stat 2>/dev/null | grep cpu').split('\n'); + if (lines.length > 1) { + lines.shift(); + if (lines.length === cpus.length) { + for (let i = 0; i < lines.length; i++) { + let parts = lines[i].split(' '); + if (parts.length >= 10) { + const steal = parseFloat(parts[8]) || 0; + const guest = parseFloat(parts[9]) || 0; + cpus[i].times.steal = steal; + cpus[i].times.guest = guest; + } + } + } + } + } catch (e) { + util.noop(); + } + } + for (let i = 0; i < _corecount; i++) { const cpu = cpus[i].times; totalUser += cpu.user; @@ -1575,6 +1610,8 @@ function getLoad() { totalNice += cpu.nice; totalIdle += cpu.idle; totalIrq += cpu.irq; + totalSteal += cpu.steal || 0; + totalGuest += cpu.guest || 0; let tmpTick = (_cpus && _cpus[i] && _cpus[i].totalTick ? _cpus[i].totalTick : 0); let tmpLoad = (_cpus && _cpus[i] && _cpus[i].totalLoad ? _cpus[i].totalLoad : 0); let tmpUser = (_cpus && _cpus[i] && _cpus[i].user ? _cpus[i].user : 0); @@ -1582,9 +1619,11 @@ function getLoad() { let tmpNice = (_cpus && _cpus[i] && _cpus[i].nice ? _cpus[i].nice : 0); let tmpIdle = (_cpus && _cpus[i] && _cpus[i].idle ? _cpus[i].idle : 0); let tmpIrq = (_cpus && _cpus[i] && _cpus[i].irq ? _cpus[i].irq : 0); + let tmpSteal = (_cpus && _cpus[i] && _cpus[i].steal ? _cpus[i].steal : 0); + let tmpGuest = (_cpus && _cpus[i] && _cpus[i].guest ? _cpus[i].guest : 0); _cpus[i] = cpu; - _cpus[i].totalTick = _cpus[i].user + _cpus[i].sys + _cpus[i].nice + _cpus[i].irq + _cpus[i].idle; - _cpus[i].totalLoad = _cpus[i].user + _cpus[i].sys + _cpus[i].nice + _cpus[i].irq; + _cpus[i].totalTick = _cpus[i].user + _cpus[i].sys + _cpus[i].nice + _cpus[i].irq + _cpus[i].steal + _cpus[i].guest + _cpus[i].idle; + _cpus[i].totalLoad = _cpus[i].user + _cpus[i].sys + _cpus[i].nice + _cpus[i].irq + _cpus[i].steal + _cpus[i].guest; _cpus[i].currentTick = _cpus[i].totalTick - tmpTick; _cpus[i].load = (_cpus[i].totalLoad - tmpLoad); _cpus[i].loadUser = (_cpus[i].user - tmpUser); @@ -1592,6 +1631,8 @@ function getLoad() { _cpus[i].loadNice = (_cpus[i].nice - tmpNice); _cpus[i].loadIdle = (_cpus[i].idle - tmpIdle); _cpus[i].loadIrq = (_cpus[i].irq - tmpIrq); + _cpus[i].loadSteal = (_cpus[i].steal - tmpSteal); + _cpus[i].loadGuest = (_cpus[i].guest - tmpGuest); cores[i] = {}; cores[i].load = _cpus[i].load / _cpus[i].currentTick * 100; cores[i].loadUser = _cpus[i].loadUser / _cpus[i].currentTick * 100; @@ -1599,15 +1640,19 @@ function getLoad() { cores[i].loadNice = _cpus[i].loadNice / _cpus[i].currentTick * 100; cores[i].loadIdle = _cpus[i].loadIdle / _cpus[i].currentTick * 100; cores[i].loadIrq = _cpus[i].loadIrq / _cpus[i].currentTick * 100; + cores[i].loadSteal = _cpus[i].loadSteal / _cpus[i].currentTick * 100; + cores[i].loadGuest = _cpus[i].loadGuest / _cpus[i].currentTick * 100; cores[i].rawLoad = _cpus[i].load; cores[i].rawLoadUser = _cpus[i].loadUser; cores[i].rawLoadSystem = _cpus[i].loadSystem; cores[i].rawLoadNice = _cpus[i].loadNice; cores[i].rawLoadIdle = _cpus[i].loadIdle; cores[i].rawLoadIrq = _cpus[i].loadIrq; + cores[i].rawLoadSteal = _cpus[i].loadSteal; + cores[i].rawLoadGuest = _cpus[i].loadGuest; } - let totalTick = totalUser + totalSystem + totalNice + totalIrq + totalIdle; - let totalLoad = totalUser + totalSystem + totalNice + totalIrq; + let totalTick = totalUser + totalSystem + totalNice + totalIrq + totalSteal + totalGuest + totalIdle; + let totalLoad = totalUser + totalSystem + totalNice + totalIrq + totalSteal + totalGuest; let currentTick = totalTick - _current_cpu.tick; result = { avgLoad: avgLoad, @@ -1617,12 +1662,16 @@ function getLoad() { currentLoadNice: (totalNice - _current_cpu.nice) / currentTick * 100, currentLoadIdle: (totalIdle - _current_cpu.idle) / currentTick * 100, currentLoadIrq: (totalIrq - _current_cpu.irq) / currentTick * 100, + currentLoadSteal: (totalSteal - _current_cpu.steal) / currentTick * 100, + currentLoadGuest: (totalGuest - _current_cpu.guest) / currentTick * 100, rawCurrentLoad: (totalLoad - _current_cpu.load), rawCurrentLoadUser: (totalUser - _current_cpu.user), rawCurrentLoadSystem: (totalSystem - _current_cpu.system), rawCurrentLoadNice: (totalNice - _current_cpu.nice), rawCurrentLoadIdle: (totalIdle - _current_cpu.idle), rawCurrentLoadIrq: (totalIrq - _current_cpu.irq), + rawCurrentLoadSteal: (totalSteal - _current_cpu.steal), + rawCurrentLoadGuest: (totalGuest - _current_cpu.guest), cpus: cores }; _current_cpu = { @@ -1631,6 +1680,8 @@ function getLoad() { system: totalSystem, idle: totalIdle, irq: totalIrq, + steal: totalSteal, + guest: totalGuest, tick: totalTick, load: totalLoad, ms: _current_cpu.ms, @@ -1640,12 +1691,16 @@ function getLoad() { currentLoadNice: result.currentLoadNice, currentLoadIdle: result.currentLoadIdle, currentLoadIrq: result.currentLoadIrq, + currentLoadSteal: result.currentLoadSteal, + currentLoadGuest: result.currentLoadGuest, rawCurrentLoad: result.rawCurrentLoad, rawCurrentLoadUser: result.rawCurrentLoadUser, rawCurrentLoadSystem: result.rawCurrentLoadSystem, rawCurrentLoadNice: result.rawCurrentLoadNice, rawCurrentLoadIdle: result.rawCurrentLoadIdle, rawCurrentLoadIrq: result.rawCurrentLoadIrq, + rawCurrentLoadSteal: result.rawCurrentLoadSteal, + rawCurrentLoadGuest: result.rawCurrentLoadGuest, }; } else { let cores = []; @@ -1663,6 +1718,8 @@ function getLoad() { cores[i].rawLoadNice = _cpus[i].loadNice; cores[i].rawLoadIdle = _cpus[i].loadIdle; cores[i].rawLoadIrq = _cpus[i].loadIrq; + cores[i].rawLoadSteal = _cpus[i].loadSteal; + cores[i].rawLoadGuest = _cpus[i].loadGuest; } result = { avgLoad: avgLoad, @@ -1672,12 +1729,16 @@ function getLoad() { currentLoadNice: _current_cpu.currentLoadNice, currentLoadIdle: _current_cpu.currentLoadIdle, currentLoadIrq: _current_cpu.currentLoadIrq, + currentLoadSteal: _current_cpu.currentLoadSteal, + currentLoadGuest: _current_cpu.currentLoadGuest, rawCurrentLoad: _current_cpu.rawCurrentLoad, rawCurrentLoadUser: _current_cpu.rawCurrentLoadUser, rawCurrentLoadSystem: _current_cpu.rawCurrentLoadSystem, rawCurrentLoadNice: _current_cpu.rawCurrentLoadNice, rawCurrentLoadIdle: _current_cpu.rawCurrentLoadIdle, rawCurrentLoadIrq: _current_cpu.rawCurrentLoadIrq, + rawCurrentLoadSteal: _current_cpu.rawCurrentLoadSteal, + rawCurrentLoadGuest: _current_cpu.rawCurrentLoadGuest, cpus: cores }; }