cpus() added steal and guest time (linux)
This commit is contained in:
parent
dd1928f596
commit
e3dc1bfec5
6
.github/ISSUE_TEMPLATE/bug_report.md
vendored
6
.github/ISSUE_TEMPLATE/bug_report.md
vendored
@ -28,5 +28,11 @@ A clear and concise description of what you expected to happen.
|
|||||||
- OS: [e.g. macOS]
|
- OS: [e.g. macOS]
|
||||||
- Hardware [e.g. MacBook Pro 13]
|
- Hardware [e.g. MacBook Pro 13]
|
||||||
|
|
||||||
|
To get all needed environment information, please run the following command:
|
||||||
|
|
||||||
|
```
|
||||||
|
npx systeminformation info
|
||||||
|
```
|
||||||
|
|
||||||
**Additional context**
|
**Additional context**
|
||||||
Add any other context about the problem here.
|
Add any other context about the problem here.
|
||||||
|
|||||||
73
lib/cpu.js
73
lib/cpu.js
@ -36,6 +36,8 @@ let _current_cpu = {
|
|||||||
system: 0,
|
system: 0,
|
||||||
idle: 0,
|
idle: 0,
|
||||||
irq: 0,
|
irq: 0,
|
||||||
|
steal: 0,
|
||||||
|
guest: 0,
|
||||||
load: 0,
|
load: 0,
|
||||||
tick: 0,
|
tick: 0,
|
||||||
ms: 0,
|
ms: 0,
|
||||||
@ -45,12 +47,16 @@ let _current_cpu = {
|
|||||||
currentLoadNice: 0,
|
currentLoadNice: 0,
|
||||||
currentLoadIdle: 0,
|
currentLoadIdle: 0,
|
||||||
currentLoadIrq: 0,
|
currentLoadIrq: 0,
|
||||||
|
currentLoadSteal: 0,
|
||||||
|
currentLoadGuest: 0,
|
||||||
rawCurrentLoad: 0,
|
rawCurrentLoad: 0,
|
||||||
rawCurrentLoadUser: 0,
|
rawCurrentLoadUser: 0,
|
||||||
rawCurrentLoadSystem: 0,
|
rawCurrentLoadSystem: 0,
|
||||||
rawCurrentLoadNice: 0,
|
rawCurrentLoadNice: 0,
|
||||||
rawCurrentLoadIdle: 0,
|
rawCurrentLoadIdle: 0,
|
||||||
rawCurrentLoadIrq: 0
|
rawCurrentLoadIrq: 0,
|
||||||
|
rawCurrentLoadSteal: 0,
|
||||||
|
rawCurrentLoadGuest: 0
|
||||||
};
|
};
|
||||||
let _cpus = [];
|
let _cpus = [];
|
||||||
let _corecount = 0;
|
let _corecount = 0;
|
||||||
@ -1559,15 +1565,44 @@ function getLoad() {
|
|||||||
let now = Date.now() - _current_cpu.ms;
|
let now = Date.now() - _current_cpu.ms;
|
||||||
if (now >= 200) {
|
if (now >= 200) {
|
||||||
_current_cpu.ms = Date.now();
|
_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 totalUser = 0;
|
||||||
let totalSystem = 0;
|
let totalSystem = 0;
|
||||||
let totalNice = 0;
|
let totalNice = 0;
|
||||||
let totalIrq = 0;
|
let totalIrq = 0;
|
||||||
let totalIdle = 0;
|
let totalIdle = 0;
|
||||||
|
let totalSteal = 0;
|
||||||
|
let totalGuest = 0;
|
||||||
let cores = [];
|
let cores = [];
|
||||||
_corecount = (cpus && cpus.length) ? cpus.length : 0;
|
_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++) {
|
for (let i = 0; i < _corecount; i++) {
|
||||||
const cpu = cpus[i].times;
|
const cpu = cpus[i].times;
|
||||||
totalUser += cpu.user;
|
totalUser += cpu.user;
|
||||||
@ -1575,6 +1610,8 @@ function getLoad() {
|
|||||||
totalNice += cpu.nice;
|
totalNice += cpu.nice;
|
||||||
totalIdle += cpu.idle;
|
totalIdle += cpu.idle;
|
||||||
totalIrq += cpu.irq;
|
totalIrq += cpu.irq;
|
||||||
|
totalSteal += cpu.steal || 0;
|
||||||
|
totalGuest += cpu.guest || 0;
|
||||||
let tmpTick = (_cpus && _cpus[i] && _cpus[i].totalTick ? _cpus[i].totalTick : 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 tmpLoad = (_cpus && _cpus[i] && _cpus[i].totalLoad ? _cpus[i].totalLoad : 0);
|
||||||
let tmpUser = (_cpus && _cpus[i] && _cpus[i].user ? _cpus[i].user : 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 tmpNice = (_cpus && _cpus[i] && _cpus[i].nice ? _cpus[i].nice : 0);
|
||||||
let tmpIdle = (_cpus && _cpus[i] && _cpus[i].idle ? _cpus[i].idle : 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 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] = cpu;
|
||||||
_cpus[i].totalTick = _cpus[i].user + _cpus[i].sys + _cpus[i].nice + _cpus[i].irq + _cpus[i].idle;
|
_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].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].currentTick = _cpus[i].totalTick - tmpTick;
|
||||||
_cpus[i].load = (_cpus[i].totalLoad - tmpLoad);
|
_cpus[i].load = (_cpus[i].totalLoad - tmpLoad);
|
||||||
_cpus[i].loadUser = (_cpus[i].user - tmpUser);
|
_cpus[i].loadUser = (_cpus[i].user - tmpUser);
|
||||||
@ -1592,6 +1631,8 @@ function getLoad() {
|
|||||||
_cpus[i].loadNice = (_cpus[i].nice - tmpNice);
|
_cpus[i].loadNice = (_cpus[i].nice - tmpNice);
|
||||||
_cpus[i].loadIdle = (_cpus[i].idle - tmpIdle);
|
_cpus[i].loadIdle = (_cpus[i].idle - tmpIdle);
|
||||||
_cpus[i].loadIrq = (_cpus[i].irq - tmpIrq);
|
_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] = {};
|
||||||
cores[i].load = _cpus[i].load / _cpus[i].currentTick * 100;
|
cores[i].load = _cpus[i].load / _cpus[i].currentTick * 100;
|
||||||
cores[i].loadUser = _cpus[i].loadUser / _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].loadNice = _cpus[i].loadNice / _cpus[i].currentTick * 100;
|
||||||
cores[i].loadIdle = _cpus[i].loadIdle / _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].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].rawLoad = _cpus[i].load;
|
||||||
cores[i].rawLoadUser = _cpus[i].loadUser;
|
cores[i].rawLoadUser = _cpus[i].loadUser;
|
||||||
cores[i].rawLoadSystem = _cpus[i].loadSystem;
|
cores[i].rawLoadSystem = _cpus[i].loadSystem;
|
||||||
cores[i].rawLoadNice = _cpus[i].loadNice;
|
cores[i].rawLoadNice = _cpus[i].loadNice;
|
||||||
cores[i].rawLoadIdle = _cpus[i].loadIdle;
|
cores[i].rawLoadIdle = _cpus[i].loadIdle;
|
||||||
cores[i].rawLoadIrq = _cpus[i].loadIrq;
|
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 totalTick = totalUser + totalSystem + totalNice + totalIrq + totalSteal + totalGuest + totalIdle;
|
||||||
let totalLoad = totalUser + totalSystem + totalNice + totalIrq;
|
let totalLoad = totalUser + totalSystem + totalNice + totalIrq + totalSteal + totalGuest;
|
||||||
let currentTick = totalTick - _current_cpu.tick;
|
let currentTick = totalTick - _current_cpu.tick;
|
||||||
result = {
|
result = {
|
||||||
avgLoad: avgLoad,
|
avgLoad: avgLoad,
|
||||||
@ -1617,12 +1662,16 @@ function getLoad() {
|
|||||||
currentLoadNice: (totalNice - _current_cpu.nice) / currentTick * 100,
|
currentLoadNice: (totalNice - _current_cpu.nice) / currentTick * 100,
|
||||||
currentLoadIdle: (totalIdle - _current_cpu.idle) / currentTick * 100,
|
currentLoadIdle: (totalIdle - _current_cpu.idle) / currentTick * 100,
|
||||||
currentLoadIrq: (totalIrq - _current_cpu.irq) / 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),
|
rawCurrentLoad: (totalLoad - _current_cpu.load),
|
||||||
rawCurrentLoadUser: (totalUser - _current_cpu.user),
|
rawCurrentLoadUser: (totalUser - _current_cpu.user),
|
||||||
rawCurrentLoadSystem: (totalSystem - _current_cpu.system),
|
rawCurrentLoadSystem: (totalSystem - _current_cpu.system),
|
||||||
rawCurrentLoadNice: (totalNice - _current_cpu.nice),
|
rawCurrentLoadNice: (totalNice - _current_cpu.nice),
|
||||||
rawCurrentLoadIdle: (totalIdle - _current_cpu.idle),
|
rawCurrentLoadIdle: (totalIdle - _current_cpu.idle),
|
||||||
rawCurrentLoadIrq: (totalIrq - _current_cpu.irq),
|
rawCurrentLoadIrq: (totalIrq - _current_cpu.irq),
|
||||||
|
rawCurrentLoadSteal: (totalSteal - _current_cpu.steal),
|
||||||
|
rawCurrentLoadGuest: (totalGuest - _current_cpu.guest),
|
||||||
cpus: cores
|
cpus: cores
|
||||||
};
|
};
|
||||||
_current_cpu = {
|
_current_cpu = {
|
||||||
@ -1631,6 +1680,8 @@ function getLoad() {
|
|||||||
system: totalSystem,
|
system: totalSystem,
|
||||||
idle: totalIdle,
|
idle: totalIdle,
|
||||||
irq: totalIrq,
|
irq: totalIrq,
|
||||||
|
steal: totalSteal,
|
||||||
|
guest: totalGuest,
|
||||||
tick: totalTick,
|
tick: totalTick,
|
||||||
load: totalLoad,
|
load: totalLoad,
|
||||||
ms: _current_cpu.ms,
|
ms: _current_cpu.ms,
|
||||||
@ -1640,12 +1691,16 @@ function getLoad() {
|
|||||||
currentLoadNice: result.currentLoadNice,
|
currentLoadNice: result.currentLoadNice,
|
||||||
currentLoadIdle: result.currentLoadIdle,
|
currentLoadIdle: result.currentLoadIdle,
|
||||||
currentLoadIrq: result.currentLoadIrq,
|
currentLoadIrq: result.currentLoadIrq,
|
||||||
|
currentLoadSteal: result.currentLoadSteal,
|
||||||
|
currentLoadGuest: result.currentLoadGuest,
|
||||||
rawCurrentLoad: result.rawCurrentLoad,
|
rawCurrentLoad: result.rawCurrentLoad,
|
||||||
rawCurrentLoadUser: result.rawCurrentLoadUser,
|
rawCurrentLoadUser: result.rawCurrentLoadUser,
|
||||||
rawCurrentLoadSystem: result.rawCurrentLoadSystem,
|
rawCurrentLoadSystem: result.rawCurrentLoadSystem,
|
||||||
rawCurrentLoadNice: result.rawCurrentLoadNice,
|
rawCurrentLoadNice: result.rawCurrentLoadNice,
|
||||||
rawCurrentLoadIdle: result.rawCurrentLoadIdle,
|
rawCurrentLoadIdle: result.rawCurrentLoadIdle,
|
||||||
rawCurrentLoadIrq: result.rawCurrentLoadIrq,
|
rawCurrentLoadIrq: result.rawCurrentLoadIrq,
|
||||||
|
rawCurrentLoadSteal: result.rawCurrentLoadSteal,
|
||||||
|
rawCurrentLoadGuest: result.rawCurrentLoadGuest,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
let cores = [];
|
let cores = [];
|
||||||
@ -1663,6 +1718,8 @@ function getLoad() {
|
|||||||
cores[i].rawLoadNice = _cpus[i].loadNice;
|
cores[i].rawLoadNice = _cpus[i].loadNice;
|
||||||
cores[i].rawLoadIdle = _cpus[i].loadIdle;
|
cores[i].rawLoadIdle = _cpus[i].loadIdle;
|
||||||
cores[i].rawLoadIrq = _cpus[i].loadIrq;
|
cores[i].rawLoadIrq = _cpus[i].loadIrq;
|
||||||
|
cores[i].rawLoadSteal = _cpus[i].loadSteal;
|
||||||
|
cores[i].rawLoadGuest = _cpus[i].loadGuest;
|
||||||
}
|
}
|
||||||
result = {
|
result = {
|
||||||
avgLoad: avgLoad,
|
avgLoad: avgLoad,
|
||||||
@ -1672,12 +1729,16 @@ function getLoad() {
|
|||||||
currentLoadNice: _current_cpu.currentLoadNice,
|
currentLoadNice: _current_cpu.currentLoadNice,
|
||||||
currentLoadIdle: _current_cpu.currentLoadIdle,
|
currentLoadIdle: _current_cpu.currentLoadIdle,
|
||||||
currentLoadIrq: _current_cpu.currentLoadIrq,
|
currentLoadIrq: _current_cpu.currentLoadIrq,
|
||||||
|
currentLoadSteal: _current_cpu.currentLoadSteal,
|
||||||
|
currentLoadGuest: _current_cpu.currentLoadGuest,
|
||||||
rawCurrentLoad: _current_cpu.rawCurrentLoad,
|
rawCurrentLoad: _current_cpu.rawCurrentLoad,
|
||||||
rawCurrentLoadUser: _current_cpu.rawCurrentLoadUser,
|
rawCurrentLoadUser: _current_cpu.rawCurrentLoadUser,
|
||||||
rawCurrentLoadSystem: _current_cpu.rawCurrentLoadSystem,
|
rawCurrentLoadSystem: _current_cpu.rawCurrentLoadSystem,
|
||||||
rawCurrentLoadNice: _current_cpu.rawCurrentLoadNice,
|
rawCurrentLoadNice: _current_cpu.rawCurrentLoadNice,
|
||||||
rawCurrentLoadIdle: _current_cpu.rawCurrentLoadIdle,
|
rawCurrentLoadIdle: _current_cpu.rawCurrentLoadIdle,
|
||||||
rawCurrentLoadIrq: _current_cpu.rawCurrentLoadIrq,
|
rawCurrentLoadIrq: _current_cpu.rawCurrentLoadIrq,
|
||||||
|
rawCurrentLoadSteal: _current_cpu.rawCurrentLoadSteal,
|
||||||
|
rawCurrentLoadGuest: _current_cpu.rawCurrentLoadGuest,
|
||||||
cpus: cores
|
cpus: cores
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user