-
Notifications
You must be signed in to change notification settings - Fork 0
/
getCPUInfo.js
96 lines (81 loc) · 2.83 KB
/
getCPUInfo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* get CPU Info
*
* @version 1.0
*
* @author m13p4
* @copyright Meliantchenkov Pavel
*/
const OS = require("os");
module.exports = function(callback, timeOutMS)
{
if(!callback || !OS) return;
function _getCPUInfo()
{
var cpuInfo = OS.cpus(), i = 0,
res = {
sum: {
user: 0,
nice: 0,
sys: 0,
idle: 0,
irq: 0,
total: 0
},
length: cpuInfo.length
};
for(; i < cpuInfo.length; i++)
{
res[i] = Object.assign({model: cpuInfo[i].model, speed: cpuInfo[i].speed}, cpuInfo[i].times, {
total: cpuInfo[i].times.user +
cpuInfo[i].times.nice +
cpuInfo[i].times.sys +
cpuInfo[i].times.idle +
cpuInfo[i].times.irq
});
res.sum.user += cpuInfo[i].times.user;
res.sum.nice += cpuInfo[i].times.nice;
res.sum.sys += cpuInfo[i].times.sys;
res.sum.idle += cpuInfo[i].times.idle;
res.sum.irq += cpuInfo[i].times.irq;
res.sum.total += res[i].total;
}
return res;
}
var res1 = _getCPUInfo();
setTimeout(function()
{
var res2 = _getCPUInfo(), i = 0,
res = {
sum: {
user: res2.sum.user - res1.sum.user,
nice: res2.sum.nice - res1.sum.nice,
sys: res2.sum.sys - res1.sum.sys,
idle: res2.sum.idle - res1.sum.idle,
irq: res2.sum.irq - res1.sum.irq,
total: res2.sum.total - res1.sum.total,
free: 0, inUse:0
},
length: res1.length
};
res.sum.free = res.sum.idle / res.sum.total;
res.sum.inUse = 1 - res.sum.free;
for(; i < res1.length; i++)
{
res[i] = {
model: res1[i].model,
speed: res1[i].speed,
user: res2[i].user - res1[i].user,
nice: res2[i].nice - res1[i].nice,
sys: res2[i].sys - res1[i].sys,
idle: res2[i].idle - res1[i].idle,
irq: res2[i].irq - res1[i].irq,
total: res2[i].total - res1[i].total,
free: 0, inUse:0
};
res[i].free = res[i].idle / res[i].total;
res[i].inUse = 1 - res[i].free;
}
callback(res);
}, timeOutMS || 1000);
}