Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: handling v8 thread pool of zero #42524

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1368,11 +1368,8 @@ 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 the value provided is larger than V8's maximum, then the largest value
will be chosen.
If set to `0` then Node.js will choose an appropriate size of the thread pool
based on the number of online processors.

### `--zero-fill-buffers`

Expand Down
15 changes: 15 additions & 0 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ static void PlatformWorkerThread(void* data) {
}
}

static int GetActualThreadPoolSize(int thread_pool_size) {
if (thread_pool_size < 1) {
uv_cpu_info_t* cpu_info;
int count;

if (uv_cpu_info(&cpu_info, &count) == 0) {
uv_free_cpu_info(cpu_info, count);
thread_pool_size = count - 1;
}
}
Comment on lines +53 to +57
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest waiting for the upgrade to libuv 1.44 and replacing this with uv_available_parallelism(), it's specifically for use cases such as this one.

uv_cpu_info() isn't really appropriate because it doesn't know how many processors are available to the process, only how many are online.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uv_available_parallelism() looks more suited to this case indeed. I'll wait for the upgrade to land.

return std::max(thread_pool_size, 1);
}

} // namespace

class WorkerThreadsTaskRunner::DelayedTaskScheduler {
Expand Down Expand Up @@ -340,6 +353,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
15 changes: 15 additions & 0 deletions test/parallel/test-worker-v8-pool-size-0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Flags: --v8-pool-size=0
'use strict';

const common = require('../common');
const assert = require('assert');
const { Worker, isMainThread } = require('worker_threads');

if (isMainThread) {
const w = new Worker(__filename);
w.on('online', common.mustCall());
w.on('exit', common.mustCall((code) => assert.strictEqual(code, 0)));
process.on('exit', (code) => {
assert.strictEqual(code, 0);
});
}