Skip to content

Commit

Permalink
Use os.availableParallelism() instead of os.cpus().length (#5091)
Browse files Browse the repository at this point in the history
* Use os.availableParallelism()

* trim CPU model

---------

Co-authored-by: David Michon <[email protected]>
  • Loading branch information
dmichon-msft and dmichon-msft authored Jan 30, 2025
1 parent 4985365 commit e34cf0f
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/heft/src/cli/HeftActionRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class HeftActionRunner {
this._terminal = options.terminal;
this._metricsCollector = options.metricsCollector;

const numberOfCores: number = os.cpus().length;
const numberOfCores: number = os.availableParallelism?.() ?? os.cpus().length;

// If an explicit parallelism number wasn't provided, then choose a sensible
// default.
Expand Down
8 changes: 6 additions & 2 deletions apps/heft/src/metrics/MetricsCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ export class MetricsCollector {

const { taskTotalExecutionMs } = filledPerformanceData;

const cpus: os.CpuInfo[] = os.cpus();

const metricData: IMetricsData = {
command: command,
encounteredError: filledPerformanceData.encounteredError,
Expand All @@ -151,8 +153,10 @@ export class MetricsCollector {
totalUptimeMs: process.uptime() * 1000,
machineOs: process.platform,
machineArch: process.arch,
machineCores: os.cpus().length,
machineProcessor: os.cpus()[0].model,
machineCores: cpus.length,
// The Node.js model is sometimes padded, for example:
// "AMD Ryzen 7 3700X 8-Core Processor
machineProcessor: cpus[0].model.trim(),
machineTotalMemoryMB: os.totalmem(),
commandParameters: parameters || {}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Prefer `os.availableParallelism()` to `os.cpus().length`.",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/heft",
"comment": "Prefer `os.availableParallelism()` to `os.cpus().length`.",
"type": "patch"
}
],
"packageName": "@rushstack/heft"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/module-minifier",
"comment": "Prefer `os.availableParallelism()` to `os.cpus().length`.",
"type": "patch"
}
],
"packageName": "@rushstack/module-minifier"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/package-extractor",
"comment": "Prefer `os.availableParallelism()` to `os.cpus().length`.",
"type": "patch"
}
],
"packageName": "@rushstack/package-extractor"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/webpack4-module-minifier-plugin",
"comment": "Prefer `os.availableParallelism()` to `os.cpus().length`.",
"type": "patch"
}
],
"packageName": "@rushstack/webpack4-module-minifier-plugin"
}
6 changes: 3 additions & 3 deletions libraries/module-minifier/src/WorkerPoolMinifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See LICENSE in the project root for license information.

import { createHash } from 'crypto';
import { cpus } from 'os';
import os from 'os';
import type { ResourceLimits } from 'worker_threads';

import serialize from 'serialize-javascript';
Expand All @@ -24,7 +24,7 @@ import type {
export interface IWorkerPoolMinifierOptions {
/**
* Maximum number of worker threads to use. Will never use more than there are modules to process.
* Defaults to os.cpus().length
* Defaults to os.availableParallelism()
*/
maxThreads?: number;
/**
Expand Down Expand Up @@ -62,7 +62,7 @@ export class WorkerPoolMinifier implements IModuleMinifier {

public constructor(options: IWorkerPoolMinifierOptions) {
const {
maxThreads = cpus().length,
maxThreads = os.availableParallelism?.() ?? os.cpus().length,
terserOptions = {},
verbose = false,
workerResourceLimits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { TARGET_ROOT_SCRIPT_RELATIVE_PATH_TEMPLATE_STRING as TargetRootScri
/**
* The maximum number of concurrent operations to perform.
*/
export const MAX_CONCURRENCY: number = os.cpus().length * 2;
export const MAX_CONCURRENCY: number = (os.availableParallelism?.() ?? os.cpus().length) * 2;

/**
* The name of the action to create symlinks.
Expand Down
2 changes: 1 addition & 1 deletion libraries/rush-lib/src/cli/parsing/ParseParallelism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as os from 'os';
*/
export function parseParallelism(
rawParallelism: string | undefined,
numberOfCores: number = os.cpus().length
numberOfCores: number = os.availableParallelism?.() ?? os.cpus().length
): number {
if (rawParallelism) {
if (rawParallelism === 'max') {
Expand Down
5 changes: 3 additions & 2 deletions libraries/rush-lib/src/logic/Telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,15 @@ export class Telemetry {
if (!this._enabled) {
return;
}
const cpus: os.CpuInfo[] = os.cpus();
const data: ITelemetryData = {
...telemetryData,
machineInfo: telemetryData.machineInfo || {
machineArchitecture: os.arch(),
// The Node.js model is sometimes padded, for example:
// "AMD Ryzen 7 3700X 8-Core Processor "
machineCpu: os.cpus()[0].model.trim(),
machineCores: os.cpus().length,
machineCpu: cpus[0].model.trim(),
machineCores: cpus.length,
machineTotalMemoryMiB: Math.round(os.totalmem() / ONE_MEGABYTE_IN_BYTES),
machineFreeMemoryMiB: Math.round(os.freemem() / ONE_MEGABYTE_IN_BYTES)
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import { cpus } from 'os';
import os from 'os';
import { resolve } from 'path';
import type { Worker } from 'worker_threads';

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

const totalCpus: number = cpus().length;
const totalCpus: number = os.availableParallelism?.() ?? os.cpus().length;

// TODO: Use all cores if not minifying
const {
Expand Down

0 comments on commit e34cf0f

Please sign in to comment.