'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
const cgroup = require('./cgroup');
const perfNow = require('./perfNow');
const sleep = require('./sleep');
const memoize = require('./memoize');
const each = require('./each');
const isEmpty = require('./isEmpty');
const sum = require('./sum');
const isNaN = require('./isNaN');
 
const os = require('os');
 
const cpuNum = memoize(function() {
    return cgroup.cpuset.cpus().effective.length;
});
 
const DEFAULT_PERIOD = 50;
 
let lastUsage = 0;
let lastNow = 0;
 
function cpuUsage(period = 0) {
    if (!period && !lastNow) {
        period = DEFAULT_PERIOD;
    }
    let now = lastNow;
    let usage = lastUsage;
    if (period) {
        now = perfNow() * 1000;
        usage = cgroup.cpu.stat().usage;
    }
    return new Promise(resolve => {
        sleep(period).then(() => {
            lastUsage = cgroup.cpu.stat().usage;
            const delta = lastUsage - usage;
            lastNow = perfNow() * 1000;
            const totalTime = lastNow - now;
            resolve(delta / totalTime);
        });
    });
}
 
let lastCpuLoad = 0;
 
function cpuLoad(period = 0) {
    const cpus = cgroup.cpuset.cpus().effective;
 
    return Promise.all([cpuUsage(period), realCpuLoads(cpus, period)]).then(
        res => {
            const realCpuLoads = res[1];
            let cpuLoad = sum.apply(null, realCpuLoads) / cpus.length;
 
            const max = cgroup.cpu.max();
            if (max !== Infinity) {
                const cpuUsage = res[0];
                const quotaLoad = cpuUsage / max;
                if (quotaLoad > cpuLoad) {
                    cpuLoad = quotaLoad;
                }
            }
 
            if (isNaN(cpuLoad)) {
                return lastCpuLoad;
            }
            lastCpuLoad = cpuLoad;
            return cpuLoad;
        }
    );
}
 
let lastAllCpus = [];
 
function realCpuLoads(cpus, period = 0) {
    const cpuLoads = [];
    if (!period && isEmpty(lastAllCpus)) {
        period = DEFAULT_PERIOD;
    }
    let allCpus = lastAllCpus;
    if (period) {
        allCpus = os.cpus();
    }
 
    return new Promise(function(resolve) {
        sleep(period).then(() => {
            lastAllCpus = os.cpus();
            each(cpus, (cpu, idx) => {
                cpuLoads[idx] = calculateCpuLoad(
                    allCpus[cpu],
                    lastAllCpus[cpu]
                );
            });
            resolve(cpuLoads);
        });
    });
}
 
function calculateCpuLoad(lastCpu, cpu) {
    const lastTimes = lastCpu.times;
    const times = cpu.times;
    const lastLoad =
        lastTimes.user + lastTimes.sys + lastTimes.nice + lastTimes.irq;
    const lastTick = lastLoad + lastTimes.idle;
    const load = times.user + times.sys + times.nice + times.irq;
    const tick = load + times.idle;
 
    return (load - lastLoad) / (tick - lastTick);
}
 
function memUsage() {
    return cgroup.memory.current();
}
 
function memLoad() {
    let max = os.totalmem();
    const cgroupMax = cgroup.memory.max();
    if (cgroupMax < max) {
        max = cgroupMax;
    }
    return memUsage() / max;
}
 
exports = {
    cpuNum,
    cpuUsage,
    cpuLoad,
    memUsage,
    memLoad
};
 
module.exports = exports;