-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): configure physical cores, logical_cores or fixed number (…
…#10257) Gatsby 2 now utilises multi-core builds using jest-worker. By default Gatsby creates a pool of workers equal to the number of physical cores on your machine, see build-html.js. In some scenarios it may be appropriate to tell Gatsby to use a different method to calculate the number of worker pools.
- Loading branch information
1 parent
3519c43
commit c51440e
Showing
8 changed files
with
109 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
title: Multi-core builds | ||
--- | ||
|
||
Gatsby now performs the static HTML generation phase of the overall [Page HTML Generation](/docs/html-generation/) process using multi-core parallel pools of workers. This helps speed up builds by distributing build generation tasks across multiple cores on your machine. | ||
|
||
By default, Gatsby creates a pool of workers equal to the number of physical cores on your machine. See [build-html.js](/docs/html-generation/#build-htmljs). | ||
|
||
In some scenarios, it may be appropriate to tell Gatsby to use a different method to calculate the number of worker pools. | ||
|
||
**For example**, if you are running a Cloud server (like AWS EC2), your DevOps engineers may want to control the number of worker pools to improve the efficiency of server resource usage. | ||
|
||
## Warning | ||
|
||
You could negatively impact performance if you use this variable incorrectly. The default Gatsby setting (no env variable or `physical_cores`) is the safest option. | ||
|
||
## Setup | ||
|
||
Set the `GATSBY_CPU_COUNT` environment variable whilst running the `gatsby build` command. | ||
|
||
`GATSBY_CPU_COUNT=physical_cores` - (default) calculate the number of worker pools based on the number of physical CPU cores on your machine. | ||
|
||
`GATSBY_CPU_COUNT=logical_cores` - calculate the number worker of pools based on the number of logical CPU cores on your machine. | ||
|
||
`GATSBY_CPU_COUNT=2` - calculate the number worker pools based on a definite number. | ||
|
||
## More information | ||
|
||
Understanding how processors work is complex and out of scope for this documentation. | ||
|
||
In brief, some processors use _Simultaneous Multithreading (SMT)_, sometimes known as _Hyper-Threading_, which is the process of a CPU splitting each of its physical cores into virtual/logical cores. | ||
|
||
SMT _can_ help to increase performance of some workloads by allowing each physical core to run two streams of work at once. | ||
|
||
However, sometimes latency can be increased. As logical cores share the same physical CPU core, sometimes more memory is required for each worker in the pool and more time is needed to spawn worker processes. |
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
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,55 @@ | ||
/** | ||
* Calculate CPU core count | ||
* @param {boolean} [useEnvVar=false] Use the 'GATSBY_CPU_COUNT' env var to calculate the requested type of CPU cores | ||
* @returns {number} Count of the requested type of CPU cores. Defaults to number of physical cores or 1 | ||
*/ | ||
|
||
const cpuCoreCount = (useEnvVar = false) => { | ||
try { | ||
let coreCount = require(`physical-cpu-count`) || 1 | ||
|
||
if (!useEnvVar) { | ||
// Return the physical CPU count, | ||
// or default to 1 if we can't detect | ||
return coreCount | ||
} | ||
|
||
if (typeof process.env.GATSBY_CPU_COUNT !== `undefined`) { | ||
const coreCountArg = | ||
Number(process.env.GATSBY_CPU_COUNT) || process.env.GATSBY_CPU_COUNT | ||
|
||
switch (typeof coreCountArg) { | ||
case `string`: | ||
// Leave at Default CPU count if coreCountArg === `physical_cores` | ||
|
||
// CPU count === logical CPU count | ||
// throw error if we have a problem counting logical cores | ||
if (coreCountArg === `logical_cores`) { | ||
coreCount = require(`os`).cpus().length | ||
|
||
if (typeof coreCount !== `number`) { | ||
throw new Error( | ||
`process.env.GATSBY_CPU_COUNT is set to 'logical_cores' but there was a problem finding the number of logical cores` | ||
) | ||
} | ||
} | ||
break | ||
|
||
case `number`: | ||
// CPU count === passed in count | ||
coreCount = coreCountArg | ||
break | ||
|
||
default: | ||
break | ||
} | ||
} | ||
|
||
return coreCount | ||
} catch (err) { | ||
console.error(err) | ||
throw new Error(`There has been a problem counting the number of CPU cores`) | ||
} | ||
} | ||
|
||
module.exports = cpuCoreCount |
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