-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
143 lines (141 loc) · 3.46 KB
/
index.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
(function(){
"use strict";
const
os = require("os"),
fs = require("fs");
module.exports = (callback) => {
let cpus = os.cpus();
if (cpus && cpus.length)
callback(null, cpus);
else getCPUNumber(
(err, n) => {
if (err) callback(err);
// gather other info
}
);
};
const getCPUNumber =
module.exports.getCPUNumber =
module.exports.getVirtualCPUNumber = (callback) => {
let
cpus = os.cpus();
if (cpus && cpus.length)
callback(null, cpus.length);
else
getPresentCPUNumber(
(err, n) => {
if (err)
getCPUNumberFromDir(
(err, n) => {
if (err)
getCPUNumberFromProcStat(
(err, n) => {
if (err)
getCPUNumberFromProcCPUInfo(
(err, n) => {
callback(err, n);
}
);
else
callback(err, n);
}
);
else
callback(err, n);
}
);
else
callback(err, n);
}
);
};
function getCPUNumberFromRange(err, data, callback){
let s = data+'';
if (err || s.substring(0,2) !== "0-" || isNaN(s.substring(2)))
callback(new Error("Expected '0-' followed by a number, but found '"+s+"'"));
else
callback(null, 1 + +s.substring(2));
}
const getPossibleCPUNumber =
module.exports.getCPUNumber.fromSysDevicesPossible = (callback) => {
fs.readFile(
"/sys/devices/system/cpu/possible",
(err, data) => {
if (err) callback(err);
else getCPUNumberFromRange(err, data, callback);
}
);
};
const getPresentCPUNumber =
module.exports.getCPUNumber.fromSysDevicesPresent = (callback) => {
fs.readFile(
"/sys/devices/system/cpu/present",
(err, data) => {
if (err) callback(err);
else getCPUNumberFromRange(err, data, callback);
}
);
};
const getCPUNumberFromDir =
module.exports.getCPUNumber.fromSysDevicesCPUDir = (callback) => {
fs.readdir(
"/sys/devices/system/cpu",
(err, files) => {
if (err) return callback(err);
let n=0;
for (var i = 0; i < files.length; i++)
if (files[i].substring(0,3) === "cpu" && !isNaN(files[i].substring(3)))
n++;
callback(n ? null : new Error("No CPUs found"), n);
}
);
};
//module.exports.getPhysicalCPUNumber
// It is very remotely possible that proc stat succeeded, but reading the models has failed, or more likely visa versa (such as in Android 8), so these are second attempts, basically ported from linux-core.c of libuv
const getCPUNumberFromProcStat =
module.exports.getCPUNumber.fromProcStat = (callback) => {
fs.readFile(
"/proc/stat",
(err, data) => {
if (err) callback(err);
else parseProcStatCPUNumber(data, callback);
}
);
};
const parseProcStatCPUNumber = (data, callback) => {
let
lines = (data+"").split("\n"),
n = 0;
if (lines[0].substring(0,4) !== "cpu ")return callback(new Error("expected /proc/stat to start with 'cpu ' but found '"+lines[0].substring(0,4)+"'"));
for (var i = 1; i < lines.length; i++)
if (lines[i].substring(0,3) === "cpu")
n++;
callback(n ? null : new Error("No CPUs found"), n);
};
const getCPUNumberFromProcCPUInfo =
module.exports.getCPUNumber.fromProcCPUInfo = (callback) => {
callback(new Error("Not yet implemented"));
};
const getProcStat =
module.exports.getProcStat = (callback) => {
fs.readFile(
"/proc/stat",
(err, data) => {
if (err) callback(err);
else parseProcStatCPUNumber(
data,
(err, n) => {
if (err) return callback(err);
let cpus = [];
//TODO: for(var i = 0; i < n; i++)
callback(new Error("Not yet implemented"));
}
);
}
);
}
const getProcCPUInfo =
module.exports.getProcCPUInfo = (callback) => {
callback(new Error("Not yet implemented"));
};
})();