Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b2facef

Browse filesBrowse files
cjihrigjuanarbol
authored andcommitted
os: add availableParallelism()
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 <moshe@atlow.co.il> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 48e3ad3 commit b2facef
Copy full SHA for b2facef

File tree

Expand file treeCollapse file tree

4 files changed

+28
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+28
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/os.md‎

Copy file name to clipboardExpand all lines: doc/api/os.md
+14Lines changed: 14 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ The operating system-specific end-of-line marker.
2626
* `\n` on POSIX
2727
* `\r\n` on Windows
2828

29+
## `os.availableParallelism()`
30+
31+
<!-- YAML
32+
added: REPLACEME
33+
-->
34+
35+
* Returns: {integer}
36+
37+
Returns an estimate of the default amount of parallelism a program should use.
38+
Always returns a value greater than zero.
39+
40+
This function is a small wrapper about libuv's [`uv_available_parallelism()`][].
41+
2942
## `os.arch()`
3043

3144
<!-- YAML
@@ -1338,3 +1351,4 @@ The following process scheduling constants are exported by
13381351
[`process.arch`]: process.md#processarch
13391352
[`process.platform`]: process.md#processplatform
13401353
[`uname(3)`]: https://linux.die.net/man/3/uname
1354+
[`uv_available_parallelism()`]: https://docs.libuv.org/en/v1.x/misc.html#c.uv_available_parallelism
Collapse file

‎lib/os.js‎

Copy file name to clipboardExpand all lines: lib/os.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
const { validateInt32 } = require('internal/validators');
4646

4747
const {
48+
getAvailableParallelism,
4849
getCPUs,
4950
getFreeMem,
5051
getHomeDirectory: _getHomeDirectory,
@@ -100,6 +101,7 @@ const getOSVersion = () => version;
100101
*/
101102
const getMachine = () => machine;
102103

104+
getAvailableParallelism[SymbolToPrimitive] = () => getAvailableParallelism();
103105
getFreeMem[SymbolToPrimitive] = () => getFreeMem();
104106
getHostname[SymbolToPrimitive] = () => getHostname();
105107
getOSVersion[SymbolToPrimitive] = () => getOSVersion();
@@ -364,6 +366,7 @@ function userInfo(options) {
364366

365367
module.exports = {
366368
arch,
369+
availableParallelism: getAvailableParallelism,
367370
cpus,
368371
endianness,
369372
freemem: getFreeMem,
Collapse file

‎src/node_os.cc‎

Copy file name to clipboardExpand all lines: src/node_os.cc
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ static void GetPriority(const FunctionCallbackInfo<Value>& args) {
380380
args.GetReturnValue().Set(priority);
381381
}
382382

383+
static void GetAvailableParallelism(const FunctionCallbackInfo<Value>& args) {
384+
unsigned int parallelism = uv_available_parallelism();
385+
args.GetReturnValue().Set(parallelism);
386+
}
383387

384388
void Initialize(Local<Object> target,
385389
Local<Value> unused,
@@ -397,6 +401,8 @@ void Initialize(Local<Object> target,
397401
SetMethod(context, target, "getUserInfo", GetUserInfo);
398402
SetMethod(context, target, "setPriority", SetPriority);
399403
SetMethod(context, target, "getPriority", GetPriority);
404+
SetMethod(
405+
context, target, "getAvailableParallelism", GetAvailableParallelism);
400406
SetMethod(context, target, "getOSInformation", GetOSInformation);
401407
target
402408
->Set(context,
@@ -417,6 +423,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
417423
registry->Register(GetUserInfo);
418424
registry->Register(SetPriority);
419425
registry->Register(GetPriority);
426+
registry->Register(GetAvailableParallelism);
420427
registry->Register(GetOSInformation);
421428
}
422429

Collapse file

‎test/parallel/test-os.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-os.js
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ if (!common.isIBMi) {
255255
is.number(os.uptime(), 'uptime');
256256
}
257257

258+
is.number(+os.availableParallelism, 'availableParallelism');
259+
is.number(os.availableParallelism(), 'availableParallelism');
258260
is.number(+os.freemem, 'freemem');
259261
is.number(os.freemem(), 'freemem');
260262

@@ -264,3 +266,5 @@ if (common.isWindows) {
264266
} else {
265267
assert.strictEqual(devNull, '/dev/null');
266268
}
269+
270+
assert.ok(os.availableParallelism() > 0);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.