-
Notifications
You must be signed in to change notification settings - Fork 30k
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
workers: implement --max-worker-threads command line option #32606
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,8 @@ | |
#pragma GCC diagnostic pop | ||
#endif | ||
|
||
#include "uv.h" | ||
|
||
#include <cassert> | ||
#include <climits> // PATH_MAX | ||
#include <csignal> | ||
|
@@ -764,6 +766,31 @@ class PersistentToLocal { | |
} | ||
}; | ||
|
||
class CPUInfo { | ||
public: | ||
CPUInfo() { | ||
if (uv_cpu_info(&info_, &count_) != 0) { | ||
info_ = nullptr; | ||
count_ = 0; | ||
} | ||
} | ||
~CPUInfo() { | ||
if (info_ != nullptr) | ||
uv_free_cpu_info(info_, count_); | ||
} | ||
int count() const { return count_; } | ||
operator bool() const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think we usually try to put empty lines in between methods. Could you also run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sigh...
Yeah, when I update this I'll switch over to the linux box and tweak the formatting. |
||
return info_ != nullptr; | ||
} | ||
const uv_cpu_info_t& operator[](int idx) const { | ||
return info_[idx]; | ||
} | ||
|
||
private: | ||
jasnell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uv_cpu_info_t* info_ = nullptr; | ||
int count_ = 0; | ||
}; | ||
|
||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||||
// Flags: --expose-internals --max-worker-threads=-1 | ||||||
'use strict'; | ||||||
|
||||||
// Check that when --max-worker-threads is negative, | ||||||
// the option value is auto-calculated based on the | ||||||
// number of CPUs | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: (here and below)
Suggested change
|
||||||
|
||||||
require('../common'); | ||||||
const { getOptionValue } = require('internal/options'); | ||||||
const { cpus } = require('os'); | ||||||
const assert = require('assert'); | ||||||
|
||||||
// Make sure the flag is actually set | ||||||
assert(process.execArgv.indexOf('--max-worker-threads=-1') > -1); | ||||||
|
||||||
const kWorkerThreadsMultiplier = 4; | ||||||
const maxWorkerThreads = getOptionValue('--max-worker-threads'); | ||||||
|
||||||
assert.strictEqual(cpus().length * kWorkerThreadsMultiplier, maxWorkerThreads); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Flags: --max-worker-threads=0 | ||
'use strict'; | ||
const common = require('../common'); | ||
const { Worker } = require('worker_threads'); | ||
const { cpus } = require('os'); | ||
|
||
const cpu_count = cpus().length; | ||
|
||
// This may need to be updated in the future if there are | ||
// any other warnings expected. For now, no warnings are | ||
// emitted with this code. | ||
process.on('warning', common.mustNotCall()); | ||
|
||
const expr = 'setInterval(() => {}, 10);'; | ||
|
||
function makeWorker(workers) { | ||
return new Promise((res) => { | ||
const worker = new Worker(expr, { eval: true }); | ||
worker.on('online', res); | ||
workers.push(worker); | ||
}); | ||
} | ||
|
||
async function doTest() { | ||
const workers = []; | ||
const list = []; | ||
for (let n = 0; n < cpu_count; n++) | ||
list.push(makeWorker(workers)); | ||
await Promise.all(list); | ||
workers.forEach((i) => i.terminate()); | ||
} | ||
jasnell marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: simplification and a bit fewer promises (and same applies to the other test if you decide to change) function makeWorker() {
return new Promise((res) => {
const worker = new Worker(expr, { eval: true });
worker.once('online', () => res(worker));
});
}
async function doTest() {
const promises = [];
for (let n = 0; n < cpu_count; n++)
promises.push(makeWorker());
return Promise.all(promises)
.then((workers) => workers.forEach((i) => i.terminate())
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we do that, then I’d go one step further and use async function makeWorker() {
const worker = new Worker(expr, { eval: true });
await once(worker, 'online');
return worker;
} 🙂 |
||
|
||
doTest().then(common.mustCall()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { spawn } = require('child_process'); | ||
const { Worker } = require('worker_threads'); | ||
|
||
if (process.argv[2] === 'child') { | ||
|
||
// Checks that warning is emitted in the main thread, | ||
// even tho the offending Worker is created from within | ||
// another worker. | ||
common.expectWarning({ | ||
Warning: [ | ||
'Too many active worker threads (2). Performance may be degraded. ' + | ||
'(--max-worker-threads=1)' | ||
] }); | ||
|
||
const expr = ` | ||
setInterval(() => {}, 10); | ||
const { Worker, parentPort } = require('worker_threads'); | ||
const expr = 'setInterval(() => {}, 10);'; | ||
const worker = new Worker(expr, { eval: true }); | ||
worker.on('online', () => { | ||
worker.terminate(); | ||
parentPort.postMessage({}); | ||
}); | ||
`; | ||
|
||
const worker = new Worker(expr, { eval: true }); | ||
worker.on('message', () => worker.terminate()); | ||
|
||
} else { | ||
const child = spawn( | ||
process.execPath, | ||
['--max-worker-threads=1', __filename, 'child'], | ||
{ stdio: 'inherit' }); | ||
child.on('close', common.mustCall((code) => assert(!code))); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.