8000 Use os.availableParallelism() instead of os.cpus().length (#5091) · Hashah1/rushstack@e34cf0f · GitHub
[go: up one dir, main page]

Skip to content

Commit e34cf0f

Browse files
authored
Use os.availableParallelism() instead of os.cpus().length (microsoft#5091)
* Use os.availableParallelism() * trim CPU model --------- Co-authored-by: David Michon <dmichon-msft@users.noreply.github.com>
1 parent 4985365 commit e34cf0f

File tree

12 files changed

+67
-12
lines changed

12 files changed

+67
-12
lines changed

apps/heft/src/cli/HeftActionRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export class HeftActionRunner {
195195
this._terminal = options.terminal;
196196
this._metricsCollector = options.metricsCollector;
197197

198-
const numberOfCores: number = os.cpus().length;
198+
const numberOfCores: number = os.availableParallelism?.() ?? os.cpus().length;
199199

200200
// If an explicit parallelism number wasn't provided, then choose a sensible
201201
// default.

apps/heft/src/metrics/MetricsCollector.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ export class MetricsCollector {
143143

144144
const { taskTotalExecutionMs } = filledPerformanceData;
145145

146+
const cpus: os.CpuInfo[] = os.cpus();
147+
146148
const metricData: IMetricsData = {
147149
command: command,
148150
encounteredError: filledPerformanceData.encounteredError,
@@ -151,8 +153,10 @@ export class MetricsCollector {
151153
totalUptimeMs: process.uptime() * 1000,
152154
machineOs: process.platform,
153155
machineArch: process.arch,
154-
machineCores: os.cpus().length,
155-
machineProcessor: os.cpus()[0].model,
156+
machineCores: cpus.length,
157+
// The Node.js model is sometimes padded, for example:
158+
// "AMD Ryzen 7 3700X 8-Core Processor
159+
machineProcessor: cpus[0].model.trim(),
156160
machineTotalMemoryMB: os.totalmem(),
157161
commandParameters: parameters || {}
158162
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush",
5+
"comment": "Prefer `os.availableParallelism()` to `os.cpus().length`.",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/heft",
5+
"comment": "Prefer `os.availableParallelism()` to `os.cpus().length`.",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@rushstack/heft"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/module-minifier",
5+
"comment": "Prefer `os.availableParallelism()` to `os.cpus().length`.",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@rushstack/module-minifier"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/package-extractor",
5+
"comment": "Prefer `os.availableParallelism()` to `os.cpus().length`.",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@rushstack/package-extractor"
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/webpack4-module-minifier-plugin",
5+
"comment": "Prefer `os.availableParallelism()` to `os.cpus().length`.",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@rushstack/webpack4-module-minifier-plugin"
10+
}

libraries/module-minifier/src/WorkerPoolMinifier.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// See LICENSE in the project root for license information.
33

44
import { createHash } from 'crypto';
5-
import { cpus } from 'os';
5+
import os from 'os';
66
import type { ResourceLimits } from 'worker_threads';
77

88
import serialize from 'serialize-javascript';
@@ -24,7 +24,7 @@ import type {
2424
export interface IWorkerPoolMinifierOptions {
2525
/**
2626
* Maximum number of worker threads to use. Will never use more than there are modules to process.
27-
* Defaults to os.cpus().length
27+
* Defaults to os.availableParallelism()
2828
*/
2929
maxThreads?: number;
3030
/**
@@ -62,7 +62,7 @@ export class WorkerPoolMinifier implements IModuleMinifier {
6262

6363
public constructor(options: IWorkerPoolMinifierOptions) {
6464
const {
65-
maxThreads = cpus().length,
65+
maxThreads = os.availableParallelism?.() ?? os.cpus().length,
6666
terserOptions = {},
6767
verbose = false,
6868
workerResourceLimits

libraries/package-extractor/src/scripts/createLinks/utilities/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { TARGET_ROOT_SCRIPT_RELATIVE_PATH_TEMPLATE_STRING as TargetRootScri
88
/**
99
* The maximum number of concurrent operations to perform.
1010
*/
11-
export const MAX_CONCURRENCY: number = os.cpus().length * 2;
11+
export const MAX_CONCURRENCY: number = (os.availableParallelism?.() ?? os.cpus().length) * 2;
1212

1313
/**
1414
* The name of the action to create symlinks.

libraries/rush-lib/src/cli/parsing/ParseParallelism.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as os from 'os';
99
*/
1010
export function parseParallelism(
1111
rawParallelism: string | undefined,
12-
numberOfCores: number = os.cpus().length
12+
numberOfCores: number = os.availableParallelism?.() ?? os.cpus().length
1313
): number {
1414
if (rawParallelism) {
1515
if (rawParallelism === 'max') {

libraries/rush-lib/src/logic/Telemetry.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,15 @@ export class Telemetry {
156156
if (!this._enabled) {
157157
return;
158158
}
159+
const cpus: os.CpuInfo[] = os.cpus();
159160
const data: ITelemetryData = {
160161
...telemetryData,
161162
machineInfo: telemetryData.machineInfo || {
162163
machineArchitecture: os.arch(),
163164
// The Node.js model is sometimes padded, for example:
164165
// "AMD Ryzen 7 3700X 8-Core Processor "
165-
machineCpu: os.cpus()[0].model.trim(),
166-
machineCores: os.cpus().length,
166+
machineCpu: cpus[0].model.trim(),
167+
machineCores: cpus.length,
167168
machineTotalMemoryMiB: Math.round(os.totalmem() / ONE_MEGABYTE_IN_BYTES),
168169
machineFreeMemoryMiB: Math.round(os.freemem() / ONE_MEGABYTE_IN_BYTES)
169170
},

webpack/webpack4-module-minifier-plugin/src/ParallelCompiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4-
import { cpus } from 'os';
4+
import os from 'os';
55
import { resolve } from 'path';
66
import type { Worker } from 'worker_threads';
77

@@ -61,7 +61,7 @@ export async function runParallel(options: IParallelWebpackOptions): Promise<voi
6161
const configArray: Configuration[] = Array.isArray(rawConfig) ? rawConfig : [rawConfig];
6262
const configCount: number = configArray.length;
6363

64-
const totalCpus: number = cpus().length;
64+
const totalCpus: number = os.availableParallelism?.() ?? os.cpus().length;
6565

6666
// TODO: Use all cores if not minifying
6767
const {

0 commit comments

Comments
 (0)
0