-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify cpuCount utility and add limiting functionality #5673
Open
DeMoorJasper
wants to merge
5
commits into
v2
Choose a base branch
from
simplify-cpu-count
base: v2
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cea9ee3
simplify cputcount
DeMoorJasper 4b859e6
Merge branch 'v2' into simplify-cpu-count
DeMoorJasper ddb7d33
Merge branch 'v2' into simplify-cpu-count
DeMoorJasper 63079e0
Merge branch 'v2' into simplify-cpu-count
DeMoorJasper a6c0723
change limit based on platform
DeMoorJasper File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,14 @@ | ||
// @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 ''; | ||
let cachedCoreCount = 0; | ||
const platform = os.platform(); | ||
export function getCoreCount( | ||
limit: number = platform === 'win32' ? 4 : 8, | ||
): number { | ||
if (!cachedCoreCount) { | ||
cachedCoreCount = Math.ceil(os.cpus().length / 2); | ||
} | ||
}; | ||
|
||
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; | ||
return Math.max(Math.min(cachedCoreCount, 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); | ||
}); | ||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not an OS-relevant issue. The parcel doesn't use the threads it spawns. Why do you want to create so many threads which Parcel doesn't use?
#5072 (comment)
Other than workers 0 and 1, the others almost do nothing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought this was a filesystem limitation of windows
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a separate issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The smarter way is to spawn threads proportional to the work that Parcel wants to do. For example:
So, if there are 100 files and we have 16 cores, it will spawn only 5 cores.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but we don't (always) know this beforehand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use the information when it is available. When not known, we can just use a constant:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is
fileNumbers
?On an initial build (where there's the most work), we don't know.
For subsequent builds, we only know the filecount of the previous build, not how much changed (= how much work there is).
So I would say this isn't possible to know at all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So maybe it is better to perform an initial analysis using all threads. Then after that, it can use a suitable number of workers for the actual build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would probably slow it down more than it would speed up?