Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dfb5cf6

Browse filesBrowse files
jasnelltargos
authored andcommitted
workers,trace_events: set thread name for workers
Set the thread name for workers in trace events. Also, use uint64_t for thread_id_ because there's really no reason for those to be doubles PR-URL: #21246 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
1 parent a3fd1cd commit dfb5cf6
Copy full SHA for dfb5cf6

File tree

Expand file treeCollapse file tree

5 files changed

+51
-9
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+51
-9
lines changed
Open diff view settings
Collapse file

‎src/env-inl.h‎

Copy file name to clipboardExpand all lines: src/env-inl.h
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,11 @@ inline bool Environment::is_main_thread() const {
598598
return thread_id_ == 0;
599599
}
600600

601-
inline double Environment::thread_id() const {
601+
inline uint64_t Environment::thread_id() const {
602602
return thread_id_;
603603
}
604604

605-
inline void Environment::set_thread_id(double id) {
605+
inline void Environment::set_thread_id(uint64_t id) {
606606
thread_id_ = id;
607607
}
608608

Collapse file

‎src/env.h‎

Copy file name to clipboardExpand all lines: src/env.h
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,8 @@ class Environment {
726726
bool is_stopping_worker() const;
727727

728728
inline bool is_main_thread() const;
729-
inline double thread_id() const;
730-
inline void set_thread_id(double id);
729+
inline uint64_t thread_id() const;
730+
inline void set_thread_id(uint64_t id);
731731
inline worker::Worker* worker_context() const;
732732
inline void set_worker_context(worker::Worker* context);
733733
inline void add_sub_worker_context(worker::Worker* context);
@@ -881,7 +881,7 @@ class Environment {
881881
std::unordered_map<std::string, uint64_t> performance_marks_;
882882

883883
bool can_call_into_js_ = true;
884-
double thread_id_ = 0;
884+
uint64_t thread_id_ = 0;
885885
std::unordered_set<worker::Worker*> sub_worker_contexts_;
886886

887887

Collapse file

‎src/node_worker.cc‎

Copy file name to clipboardExpand all lines: src/node_worker.cc
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "async_wrap.h"
1010
#include "async_wrap-inl.h"
1111

12+
#include <string>
13+
1214
using v8::ArrayBuffer;
1315
using v8::Context;
1416
using v8::Function;
@@ -30,7 +32,7 @@ namespace worker {
3032

3133
namespace {
3234

33-
double next_thread_id = 1;
35+
uint64_t next_thread_id = 1;
3436
Mutex next_thread_id_mutex;
3537

3638
} // anonymous namespace
@@ -44,7 +46,8 @@ Worker::Worker(Environment* env, Local<Object> wrap)
4446
}
4547
wrap->Set(env->context(),
4648
env->thread_id_string(),
47-
Number::New(env->isolate(), thread_id_)).FromJust();
49+
Number::New(env->isolate(),
50+
static_cast<double>(thread_id_))).FromJust();
4851

4952
// Set up everything that needs to be set up in the parent environment.
5053
parent_port_ = MessagePort::New(env, env->context());
@@ -112,6 +115,11 @@ bool Worker::is_stopped() const {
112115
}
113116

114117
void Worker::Run() {
118+
std::string name = "WorkerThread ";
119+
name += std::to_string(thread_id_);
120+
TRACE_EVENT_METADATA1(
121+
"__metadata", "thread_name", "name",
122+
TRACE_STR_COPY(name.c_str()));
115123
MultiIsolatePlatform* platform = isolate_data_->platform();
116124
CHECK_NE(platform, nullptr);
117125

@@ -418,7 +426,8 @@ void InitWorker(Local<Object> target,
418426
auto thread_id_string = FIXED_ONE_BYTE_STRING(env->isolate(), "threadId");
419427
target->Set(env->context(),
420428
thread_id_string,
421-
Number::New(env->isolate(), env->thread_id())).FromJust();
429+
Number::New(env->isolate(),
430+
static_cast<double>(env->thread_id()))).FromJust();
422431
}
423432

424433
} // anonymous namespace
Collapse file

‎src/node_worker.h‎

Copy file name to clipboardExpand all lines: src/node_worker.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Worker : public AsyncWrap {
6262

6363
bool thread_joined_ = true;
6464
int exit_code_ = 0;
65-
double thread_id_ = -1;
65+
uint64_t thread_id_ = -1;
6666

6767
std::unique_ptr<MessagePortData> child_port_data_;
6868

Collapse file
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Flags: --experimental-worker
2+
'use strict';
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const cp = require('child_process');
6+
const fs = require('fs');
7+
const { isMainThread } = require('worker_threads');
8+
9+
if (isMainThread) {
10+
const CODE = 'const { Worker } = require(\'worker_threads\'); ' +
11+
`new Worker('${__filename}')`;
12+
const FILE_NAME = 'node_trace.1.log';
13+
const tmpdir = require('../common/tmpdir');
14+
tmpdir.refresh();
15+
process.chdir(tmpdir.path);
16+
17+
const proc = cp.spawn(process.execPath,
18+
[ '--experimental-worker',
19+
'--trace-event-categories', 'node',
20+
'-e', CODE ]);
21+
proc.once('exit', common.mustCall(() => {
22+
assert(common.fileExists(FILE_NAME));
23+
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
24+
const traces = JSON.parse(data.toString()).traceEvents;
25+
assert(traces.length > 0);
26+
assert(traces.some((trace) =>
27+
trace.cat === '__metadata' && trace.name === 'thread_name' &&
28+
trace.args.name === 'WorkerThread 1'));
29+
}));
30+
}));
31+
} else {
32+
// Do nothing here.
33+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.