-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d9143b
commit cea9ee3
Showing
3 changed files
with
19 additions
and
74 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,66 +1,7 @@ | ||
// @flow | ||
import os from 'os'; | ||
import {execSync} from 'child_process'; | ||
|
||
const exec = (command: string): string => { | ||
try { | ||
let stdout = execSync(command, { | ||
encoding: 'utf8', | ||
// This prevents the command from outputting to the console | ||
stdio: [null, null, null], | ||
}); | ||
return stdout.trim(); | ||
} catch (e) { | ||
return ''; | ||
} | ||
}; | ||
|
||
export function detectRealCores(): number { | ||
let platform = os.platform(); | ||
let amount = 0; | ||
|
||
if (platform === 'linux') { | ||
amount = parseInt( | ||
exec('lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l'), | ||
10, | ||
); | ||
} else if (platform === 'darwin') { | ||
amount = parseInt(exec('sysctl -n hw.physicalcpu_max'), 10); | ||
} else if (platform === 'win32') { | ||
const str = exec('wmic cpu get NumberOfCores').match(/\d+/g); | ||
if (str !== null) { | ||
amount = parseInt(str.filter(n => n !== '')[0], 10); | ||
} | ||
} | ||
|
||
if (!amount || amount <= 0) { | ||
throw new Error('Could not detect cpu count!'); | ||
} | ||
|
||
return amount; | ||
} | ||
|
||
let cores; | ||
export default function getCores(bypassCache?: boolean = false): number { | ||
// Do not re-run commands if we already have the count... | ||
if (cores && !bypassCache) { | ||
return cores; | ||
} | ||
|
||
try { | ||
cores = detectRealCores(); | ||
} catch (e) { | ||
// Guess the amount of real cores | ||
cores = os | ||
.cpus() | ||
.filter((cpu, index) => !cpu.model.includes('Intel') || index % 2 === 1) | ||
.length; | ||
} | ||
|
||
// Another fallback | ||
if (!cores) { | ||
cores = 1; | ||
} | ||
|
||
return cores; | ||
export function getCoreCount(limit: number = 8): number { | ||
let coreCount = Math.ceil(os.cpus().length / 2); | ||
return Math.max(Math.min(coreCount, limit), 1); | ||
} |
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 |
---|---|---|
@@ -1,19 +1,23 @@ | ||
import assert from 'assert'; | ||
import os from 'os'; | ||
|
||
import getCores, {detectRealCores} from '../src/cpuCount'; | ||
import {getCoreCount} from '../src/cpuCount'; | ||
|
||
describe('cpuCount', function() { | ||
it('Should be able to detect real cpu count', () => { | ||
// Windows not supported as getting the cpu count takes a couple seconds... | ||
if (os.platform() === 'win32') return; | ||
let cpus = os.cpus().length; | ||
|
||
let cores = detectRealCores(); | ||
describe('cpuCount', function() { | ||
it('getCoreCount should return more than 0', () => { | ||
let cores = getCoreCount(); | ||
assert(cores > 0); | ||
}); | ||
|
||
it('getCores should return more than 0', () => { | ||
let cores = getCores(true); | ||
assert(cores > 0); | ||
}); | ||
if (cpus > 2) { | ||
it('Should be able to limit coreCount', () => { | ||
let allCores = getCoreCount(); | ||
let limitedCores = getCoreCount(1); | ||
|
||
assert(allCores > limitedCores); | ||
assert.equal(limitedCores, 1); | ||
}); | ||
} | ||
}); |