mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
4 files changed
+28
No files matched your search
@@ -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
|
||||
@@ -1340,3 +1353,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
|
||||
@@ -45,6 +45,7 @@ const {
|
||||
const { validateInt32 } = require('internal/validators');
|
||||
|
||||
const {
|
||||
getAvailableParallelism,
|
||||
getCPUs,
|
||||
getFreeMem,
|
||||
getHomeDirectory: _getHomeDirectory,
|
||||
@@ -100,6 +101,7 @@ const getOSVersion = () => version;
|
||||
*/
|
||||
const getMachine = () => machine;
|
||||
|
||||
getAvailableParallelism[SymbolToPrimitive] = () => getAvailableParallelism();
|
||||
getFreeMem[SymbolToPrimitive] = () => getFreeMem();
|
||||
getHostname[SymbolToPrimitive] = () => getHostname();
|
||||
getOSVersion[SymbolToPrimitive] = () => getOSVersion();
|
||||
@@ -364,6 +366,7 @@ function userInfo(options) {
|
||||
|
||||
module.exports = {
|
||||
arch,
|
||||
availableParallelism: getAvailableParallelism,
|
||||
cpus,
|
||||
endianness,
|
||||
freemem: getFreeMem,
|
||||
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -417,6 +423,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
|
||||
registry->Register(GetUserInfo);
|
||||
registry->Register(SetPriority);
|
||||
registry->Register(GetPriority);
|
||||
registry->Register(GetAvailableParallelism);
|
||||
registry->Register(GetOSInformation);
|
||||
}
|
||||
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -264,3 +266,5 @@ if (common.isWindows) {
|
||||
} else {
|
||||
assert.strictEqual(devNull, '/dev/null');
|
||||
}
|
||||
|
||||
assert.ok(os.availableParallelism() > 0);
|
||||
Reference in new issue
Block a user