Skip to content

Commit 910600a

Browse files
daeyeondanielleadams
authored andcommitted
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 <[email protected]> PR-URL: #45513 Fixes: #42523 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]>
1 parent a8a8a87 commit 910600a

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

doc/api/cli.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -1555,11 +1555,12 @@ added: v5.10.0
15551555

15561556
Set V8's thread pool size which will be used to allocate background jobs.
15571557

1558-
If set to `0` then V8 will choose an appropriate size of the thread pool based
1559-
on the number of online processors.
1558+
If set to `0` then Node.js will choose an appropriate size of the thread pool
1559+
based on an estimate of the amount of parallelism.
15601560

1561-
If the value provided is larger than V8's maximum, then the largest value
1562-
will be chosen.
1561+
The amount of parallelism refers to the number of computations that can be
1562+
carried out simultaneously in a given machine. In general, it's the same as the
1563+
amount of CPUs, but it may diverge in environments such as VMs or containers.
15631564

15641565
### `--watch`
15651566

src/node_platform.cc

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ static void PlatformWorkerThread(void* data) {
4545
}
4646
}
4747

48+
static int GetActualThreadPoolSize(int thread_pool_size) {
49+
if (thread_pool_size < 1) {
50+
thread_pool_size = uv_available_parallelism() - 1;
51+
}
52+
return std::max(thread_pool_size, 1);
53+
}
54+
4855
} // namespace
4956

5057
class WorkerThreadsTaskRunner::DelayedTaskScheduler {
@@ -340,6 +347,8 @@ NodePlatform::NodePlatform(int thread_pool_size,
340347
// current v8::Platform instance.
341348
SetTracingController(tracing_controller_);
342349
DCHECK_EQ(GetTracingController(), tracing_controller_);
350+
351+
thread_pool_size = GetActualThreadPoolSize(thread_pool_size);
343352
worker_thread_task_runner_ =
344353
std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
345354
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Flags: --v8-pool-size=0 --expose-gc
2+
3+
'use strict';
4+
5+
require('../common');
6+
7+
// This verifies that V8 tasks scheduled by GC are handled on worker threads if
8+
// `--v8-pool-size=0` is given. The worker threads are managed by Node.js'
9+
// `v8::Platform` implementation.
10+
globalThis.gc();

0 commit comments

Comments
 (0)