Skip to content

Commit

Permalink
os: add availableParallelism()
Browse files Browse the repository at this point in the history
This commit exposes uv_available_parallelism() as an alternative
to cpus().length. uv_available_parallelism() is inspired by
Rust's available_parallelism().

PR-URL: #45895
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
cjihrig authored and juanarbol committed Jan 31, 2023
1 parent 48e3ad3 commit b2facef
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions doc/api/os.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ The operating system-specific end-of-line marker.
* `\n` on POSIX
* `\r\n` on Windows

## `os.availableParallelism()`

<!-- YAML
added: REPLACEME
-->

* Returns: {integer}

Returns an estimate of the default amount of parallelism a program should use.
Always returns a value greater than zero.

This function is a small wrapper about libuv's [`uv_available_parallelism()`][].

## `os.arch()`

<!-- YAML
Expand Down Expand Up @@ -1338,3 +1351,4 @@ The following process scheduling constants are exported by
[`process.arch`]: process.md#processarch
[`process.platform`]: process.md#processplatform
[`uname(3)`]: https://linux.die.net/man/3/uname
[`uv_available_parallelism()`]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_available_parallelism
3 changes: 3 additions & 0 deletions lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const {
const { validateInt32 } = require('internal/validators');

const {
getAvailableParallelism,
getCPUs,
getFreeMem,
getHomeDirectory: _getHomeDirectory,
Expand Down Expand Up @@ -100,6 +101,7 @@ const getOSVersion = () => version;
*/
const getMachine = () => machine;

getAvailableParallelism[SymbolToPrimitive] = () => getAvailableParallelism();
getFreeMem[SymbolToPrimitive] = () => getFreeMem();
getHostname[SymbolToPrimitive] = () => getHostname();
getOSVersion[SymbolToPrimitive] = () => getOSVersion();
Expand Down Expand Up @@ -364,6 +366,7 @@ function userInfo(options) {

module.exports = {
arch,
availableParallelism: getAvailableParallelism,
cpus,
endianness,
freemem: getFreeMem,
Expand Down
7 changes: 7 additions & 0 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ static void GetPriority(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(priority);
}

static void GetAvailableParallelism(const FunctionCallbackInfo<Value>& args) {
unsigned int parallelism = uv_available_parallelism();
args.GetReturnValue().Set(parallelism);
}

void Initialize(Local<Object> target,
Local<Value> unused,
Expand All @@ -397,6 +401,8 @@ void Initialize(Local<Object> target,
SetMethod(context, target, "getUserInfo", GetUserInfo);
SetMethod(context, target, "setPriority", SetPriority);
SetMethod(context, target, "getPriority", GetPriority);
SetMethod(
context, target, "getAvailableParallelism", GetAvailableParallelism);
SetMethod(context, target, "getOSInformation", GetOSInformation);
target
->Set(context,
Expand All @@ -417,6 +423,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GetUserInfo);
registry->Register(SetPriority);
registry->Register(GetPriority);
registry->Register(GetAvailableParallelism);
registry->Register(GetOSInformation);
}

Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-os.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ if (!common.isIBMi) {
is.number(os.uptime(), 'uptime');
}

is.number(+os.availableParallelism, 'availableParallelism');
is.number(os.availableParallelism(), 'availableParallelism');
is.number(+os.freemem, 'freemem');
is.number(os.freemem(), 'freemem');

Expand All @@ -264,3 +266,5 @@ if (common.isWindows) {
} else {
assert.strictEqual(devNull, '/dev/null');
}

assert.ok(os.availableParallelism() > 0);

0 comments on commit b2facef

Please sign in to comment.