Skip to content

Commit

Permalink
cpus() added steal and guest time (linux)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhildebrandt committed Aug 20, 2023
1 parent dd1928f commit e3dc1bf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
73 changes: 67 additions & 6 deletions lib/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ let _current_cpu = {
system: 0,
idle: 0,
irq: 0,
steal: 0,
guest: 0,
load: 0,
tick: 0,
ms: 0,
Expand All @@ -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;
Expand Down Expand Up @@ -1559,55 +1565,94 @@ 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;
totalSystem += cpu.sys;
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);
let tmpSystem = (_cpus && _cpus[i] && _cpus[i].sys ? _cpus[i].sys : 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 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);
_cpus[i].loadSystem = (_cpus[i].sys - tmpSystem);
_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;
cores[i].loadSystem = _cpus[i].loadSystem / _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].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,
Expand All @@ -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 = {
Expand All @@ -1631,6 +1680,8 @@ function getLoad() {
system: totalSystem,
idle: totalIdle,
irq: totalIrq,
steal: totalSteal,
guest: totalGuest,
tick: totalTick,
load: totalLoad,
ms: _current_cpu.ms,
Expand All @@ -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 = [];
Expand All @@ -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,
Expand All @@ -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
};
}
Expand Down

0 comments on commit e3dc1bf

Please sign in to comment.