Skip to content

Commit

Permalink
simplify cputcount
Browse files Browse the repository at this point in the history
  • Loading branch information
DeMoorJasper committed Feb 2, 2021
1 parent 5d9143b commit cea9ee3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 74 deletions.
4 changes: 2 additions & 2 deletions packages/core/workers/src/WorkerFarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import ThrowableDiagnostic, {anyToDiagnostic} from '@parcel/diagnostic';
import {escapeMarkdown} from '@parcel/utils';
import Worker, {type WorkerCall} from './Worker';
import cpuCount from './cpuCount';
import {getCoreCount} from './cpuCount';
import Handle from './Handle';
import {child} from './childState';
import {detectBackend} from './backend';
Expand Down Expand Up @@ -559,7 +559,7 @@ export default class WorkerFarm extends EventEmitter {
static getNumWorkers(): number {
return process.env.PARCEL_WORKERS
? parseInt(process.env.PARCEL_WORKERS, 10)
: cpuCount();
: getCoreCount();
}

static isWorker(): boolean {
Expand Down
65 changes: 3 additions & 62 deletions packages/core/workers/src/cpuCount.js
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);
}
24 changes: 14 additions & 10 deletions packages/core/workers/test/cpuCount.test.js
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);
});
}
});

0 comments on commit cea9ee3

Please sign in to comment.