forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.js
42 lines (35 loc) · 1.14 KB
/
cpu.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
/**
* @fileoverview CPU utility.
* @license Apache-2.0
*/
// https://www.npmjs.com/package/physical-cpu-count
import os from "os";
import childProcess from "child_process";
const cpus = os.cpus();
function exec(command) {
return childProcess.execSync(command, {encoding: 'utf8'});
}
var coreCount;
const platform = os.platform();
if (platform === "linux") {
const output = exec("lscpu -p | egrep -v \"^#\" | sort -u -t, -k 2,4 | wc -l");
coreCount = parseInt(output.trim(), 10);
} else if (platform === "darwin") {
const output = exec("sysctl -n hw.physicalcpu_max");
coreCount = parseInt(output.trim(), 10);
} else if (platform === "windows") {
const output = exec("WMIC CPU Get NumberOfCores");
coreCount = output.split(os.EOL)
.map(line => parseInt(line))
.filter(value => !isNaN(value))
.reduce((sum, number) => sum + number, 0);
} else {
const cores = cpus.filter(function (cpu, index) {
const hasHyperthreading = cpu.model.includes("Intel");
const isOdd = index % 2 === 1;
return !hasHyperthreading || isOdd;
});
coreCount = cores.length;
}
export const threadCount = cpus.length;
export { coreCount };