-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: add support for worker name in inspector and trace_events
Fixes: #41589 PR-URL: #46832 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
- Loading branch information
1 parent
09c5e6a
commit f7423bd
Showing
14 changed files
with
144 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const { Session } = require('inspector'); | ||
const { parentPort } = require('worker_threads'); | ||
|
||
const session = new Session(); | ||
|
||
parentPort.once('message', () => {}); // Prevent the worker from exiting. | ||
|
||
session.connectToMainThread(); | ||
|
||
session.on( | ||
'NodeWorker.attachedToWorker', | ||
({ params: { workerInfo } }) => { | ||
// send the worker title to the main thread | ||
parentPort.postMessage(workerInfo.title); | ||
} | ||
); | ||
session.post('NodeWorker.enable', { waitForDebuggerOnStart: false }); |
31 changes: 31 additions & 0 deletions
31
test/parallel/test-trace-events-worker-metadata-with-name.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const cp = require('child_process'); | ||
const fs = require('fs'); | ||
const { isMainThread } = require('worker_threads'); | ||
|
||
if (isMainThread) { | ||
const CODE = 'const { Worker } = require(\'worker_threads\'); ' + | ||
`new Worker('${__filename.replace(/\\/g, '/')}', { name: 'foo' })`; | ||
const FILE_NAME = 'node_trace.1.log'; | ||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
process.chdir(tmpdir.path); | ||
|
||
const proc = cp.spawn(process.execPath, | ||
[ '--trace-event-categories', 'node', | ||
'-e', CODE ]); | ||
proc.once('exit', common.mustCall(() => { | ||
assert(fs.existsSync(FILE_NAME)); | ||
fs.readFile(FILE_NAME, common.mustCall((err, data) => { | ||
const traces = JSON.parse(data.toString()).traceEvents; | ||
assert(traces.length > 0); | ||
assert(traces.some((trace) => | ||
trace.cat === '__metadata' && trace.name === 'thread_name' && | ||
trace.args.name === '[worker 1] foo')); | ||
})); | ||
})); | ||
} else { | ||
// Do nothing here. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
common.skipIfInspectorDisabled(); | ||
common.skipIfWorker(); // This test requires both main and worker threads. | ||
|
||
const assert = require('assert'); | ||
const { Worker, isMainThread } = require('worker_threads'); | ||
|
||
if (isMainThread) { | ||
const name = 'Hello Thread'; | ||
const expectedTitle = `[worker 1] ${name}`; | ||
const worker = new Worker(fixtures.path('worker-name.js'), { | ||
name, | ||
}); | ||
worker.once('message', common.mustCall((message) => { | ||
assert.strictEqual(message, expectedTitle); | ||
worker.postMessage('done'); | ||
})); | ||
} |