-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysload.js
187 lines (165 loc) · 6.71 KB
/
sysload.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*!
** SysLoad -- System Load Determination
** Copyright (c) 2017-2021 Dr. Ralf S. Engelschall <rse@engelschall.com>
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* internal requirements */
const os = require("os")
/* the exported API class */
module.exports = class SysLoad {
/* initialize internal state */
constructor (config = "std") {
if (typeof config === "string") {
if (config === "std") {
/* the standard time slot configuration */
this.config = {
"s1": 1,
"s10": 10 * 1,
"m1": 6 * 10 * 1,
"m10": 10 * 6 * 10 * 1,
"h1": 6 * 10 * 6 * 10 * 1,
"h10": 10 * 6 * 10 * 6 * 10 * 1
}
}
else if (config === "unix") {
/* the Unix-style time slot configuration */
this.config = {
"m5": 5 * 60,
"m10": 10 * 60,
"m15": 15 * 60
}
}
else
throw new Error("invalid time slot configuration (expected \"std\" or \"unix\")")
}
else if (typeof config === "object")
this.config = config
else
throw new Error("invalid time slot configuration (expected string or object)")
this.measuring = false
this.loads = null
}
/* measure once over a certain duration */
measure (duration = 100) {
return new Promise((resolve /*, reject */) => {
/* determine absolute CPU times once */
const cpuTimes = () => {
let idle = 0
let total = 0
const cpus = os.cpus()
for (let i = 0; i < cpus.length; i++) {
for (const type in cpus[i].times)
total += cpus[i].times[type]
idle += cpus[i].times.idle
}
return {
total: total / cpus.length,
idle: idle / cpus.length
}
}
/* determine CPU times at start */
const start = cpuTimes()
setTimeout(() => {
/* determine CPU times at end */
const end = cpuTimes()
/* calculate the difference */
const diff = { total: 0, idle: 0 }
if (end.total > start.total) {
/* regular case */
diff.total = end.total - start.total
diff.idle = end.idle - start.idle
}
else {
/* special case: integer overflow */
diff.total = Math.MAX_SAFE_INTEGER - start.total + end.total
diff.idle = Math.MAX_SAFE_INTEGER - start.idle + end.idle
}
/* calculate the system load */
let load = 100 - (100 * diff.idle / diff.total)
/* reduce resolution */
load = Math.trunc(load * 10) / 10
resolve(load)
}, duration)
})
}
/* start continuous measurement */
start () {
/* sanity check usage context */
if (this.measuring)
throw new Error("continuous measurement already in progress (stop first)")
/* indicate measurement */
this.measuring = true
/* the measurement intervals */
const measurementDuration = 1000
/* initialize load values and provide function for accounting subsequent values */
this.loads = {}
Object.keys(this.config).forEach((slot) => {
this.loads[slot] = []
})
const account = (slot, duration, load) => {
this.loads[slot].push(load)
if (this.loads[slot].length > Math.round(duration / measurementDuration))
this.loads[slot].shift()
}
/* continuously perform measurements */
const handler = async () => {
if (!this.measuring)
return
/* perform a single measurement over the measurement duration */
const load = await this.measure(measurementDuration)
/* account the measured load for different average slots */
Object.keys(this.config).forEach((slot) => {
account(slot, this.config[slot] * 1000, load)
})
this.timer = setTimeout(handler, 0)
}
this.timer = setTimeout(handler, 0)
}
/* stop continuous measurement */
stop () {
/* sanity check usage context */
if (!this.measuring)
throw new Error("continuous measurement not started (start first)")
/* indicate that the measuring should stop */
this.measuring = false
}
/* retrieve averaged system load values */
average () {
/* sanity check usage context */
if (!this.measuring)
throw new Error("continuous measurement not started (start first)")
/* calculate a single average load value */
const calcAverage = (slot) => {
if (this.loads[slot].length === 0)
return 0.0
let load = this.loads[slot].reduce((sum, val) =>
sum + val, 0) / this.loads[slot].length
load = Math.trunc(load * 10) / 10
return load
}
/* provide the averaged load values */
const result = {}
Object.keys(this.config).forEach((slot) => {
result[slot] = calcAverage(slot)
})
return result
}
}