Skip to content

Commit

Permalink
src: set an appropriate thread pool size if given --v8-pool-size=0
Browse files Browse the repository at this point in the history
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 <daeyeon.dev@gmail.com>
PR-URL: #45513
Fixes: #42523
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
  • Loading branch information
daeyeon authored and danielleadams committed Jan 3, 2023
1 parent dde8740 commit 0328208
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
9 changes: 5 additions & 4 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
9 changes: 9 additions & 0 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<WorkerThreadsTaskRunner>(thread_pool_size);
}
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-v8-flag-pool-size-0.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 0328208

Please sign in to comment.