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().
  • Loading branch information
cjihrig committed Dec 18, 2022
1 parent ca2ec90 commit a65c309
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions doc/api/os.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ 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.

## `os.arch()`

<!-- YAML
Expand Down
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 a65c309

Please sign in to comment.