Skip to content

Commit dfb5cf6

Browse 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 <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent a3fd1cd commit dfb5cf6

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

src/env-inl.h

+2-2
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

src/env.h

+3-3
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

src/node_worker.cc

+12-3
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

src/node_worker.h

+1-1
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

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)