-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(config): physical cpu core counting (#1110)
- Loading branch information
Showing
5 changed files
with
56 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint-disable security/detect-child-process */ | ||
const { cpus, EOL, platform } = require("os"); | ||
const { execSync } = require("child_process"); | ||
|
||
/** | ||
* @author Frazer Smith | ||
* @description Count number of physical CPU cores a system has, | ||
* taking into account Intel Hyper-Threading. | ||
* @see https://github.com/nodejs/node/issues/7730 | ||
* @returns {number} Number of physical cores the system has. | ||
*/ | ||
function coreCount() { | ||
const config = { encoding: "utf8" }; | ||
|
||
switch (platform()) { | ||
case "darwin": | ||
return parseInt( | ||
execSync("sysctl -n hw.physicalcpu_max", config).trim(), | ||
10 | ||
); | ||
case "linux": | ||
return parseInt( | ||
execSync( | ||
'lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l', | ||
config | ||
).trim(), | ||
10 | ||
); | ||
case "win32": | ||
return execSync("WMIC CPU Get NumberOfCores", config) | ||
.split(EOL) | ||
.map((line) => parseInt(line, 10)) | ||
.filter((value) => !Number.isNaN(Number(value))) | ||
.reduce((sum, number) => sum + number, 0); | ||
default: | ||
return cpus().filter((cpu, index) => { | ||
const hasHyperthreading = cpu.model.includes("Intel"); | ||
const isOdd = index % 2 === 1; | ||
return !hasHyperthreading || isOdd; | ||
}).length; | ||
} | ||
} | ||
|
||
module.exports = coreCount; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const util = require("."); | ||
|
||
describe("Core-Count Util", () => { | ||
test("Should return count of physical cores", () => { | ||
const response = util(); | ||
expect(response).toEqual(expect.any(Number)); | ||
}); | ||
}); |