From 0328208f6c9e52a55fee3fc1be23b61a53e92592 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Mon, 21 Nov 2022 11:14:28 +0900 Subject: [PATCH] src: set an appropriate thread pool size if given `--v8-pool-size=0` It doesn't terminate when any pending V8 tasks exist if no thread is in the pool. This allocates one thread at least for V8's background tasks if `--v8-pool-size=0` is given as a CLI option. Signed-off-by: Daeyeon Jeong PR-URL: https://github.com/nodejs/node/pull/45513 Fixes: https://github.com/nodejs/node/issues/42523 Reviewed-By: Ben Noordhuis Reviewed-By: Yagiz Nizipli Reviewed-By: Richard Lau Reviewed-By: Rafael Gonzaga Reviewed-By: Santiago Gimeno --- doc/api/cli.md | 9 +++++---- src/node_platform.cc | 9 +++++++++ test/parallel/test-v8-flag-pool-size-0.js | 10 ++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-v8-flag-pool-size-0.js diff --git a/doc/api/cli.md b/doc/api/cli.md index af92173ee7e756..0e6317cb4d3e3c 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1555,11 +1555,12 @@ added: v5.10.0 Set V8's thread pool size which will be used to allocate background jobs. -If set to `0` then V8 will choose an appropriate size of the thread pool based -on the number of online processors. +If set to `0` then Node.js will choose an appropriate size of the thread pool +based on an estimate of the amount of parallelism. -If the value provided is larger than V8's maximum, then the largest value -will be chosen. +The amount of parallelism refers to the number of computations that can be +carried out simultaneously in a given machine. In general, it's the same as the +amount of CPUs, but it may diverge in environments such as VMs or containers. ### `--watch` diff --git a/src/node_platform.cc b/src/node_platform.cc index 1d761c345f95e9..7dd0526e6ece5f 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -45,6 +45,13 @@ static void PlatformWorkerThread(void* data) { } } +static int GetActualThreadPoolSize(int thread_pool_size) { + if (thread_pool_size < 1) { + thread_pool_size = uv_available_parallelism() - 1; + } + return std::max(thread_pool_size, 1); +} + } // namespace class WorkerThreadsTaskRunner::DelayedTaskScheduler { @@ -340,6 +347,8 @@ NodePlatform::NodePlatform(int thread_pool_size, // current v8::Platform instance. SetTracingController(tracing_controller_); DCHECK_EQ(GetTracingController(), tracing_controller_); + + thread_pool_size = GetActualThreadPoolSize(thread_pool_size); worker_thread_task_runner_ = std::make_shared(thread_pool_size); } diff --git a/test/parallel/test-v8-flag-pool-size-0.js b/test/parallel/test-v8-flag-pool-size-0.js new file mode 100644 index 00000000000000..5f78e6006e0836 --- /dev/null +++ b/test/parallel/test-v8-flag-pool-size-0.js @@ -0,0 +1,10 @@ +// Flags: --v8-pool-size=0 --expose-gc + +'use strict'; + +require('../common'); + +// This verifies that V8 tasks scheduled by GC are handled on worker threads if +// `--v8-pool-size=0` is given. The worker threads are managed by Node.js' +// `v8::Platform` implementation. +globalThis.gc();