GeezerVison is showing github.com — workers,trace_events: set thread name for workers · nodejs/node@dfb5cf6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/env-inl.h +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -598,11 +598,11 @@ inline bool Environment::is_main_thread() const { | 598 | 598 | return thread_id_ == 0; | 599 | 599 | } | 600 | 600 |
| 601 | | -inline double Environment::thread_id() const { | | 601 | +inline uint64_t Environment::thread_id() const { | 602 | 602 | return thread_id_; | 603 | 603 | } | 604 | 604 |
| 605 | | -inline void Environment::set_thread_id(double id) { | | 605 | +inline void Environment::set_thread_id(uint64_t id) { | 606 | 606 | thread_id_ = id; | 607 | 607 | } | 608 | 608 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -726,8 +726,8 @@ class Environment { | 726 | 726 | bool is_stopping_worker() const; | 727 | 727 |
| 728 | 728 | 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); | 731 | 731 | inline worker::Worker* worker_context() const; | 732 | 732 | inline void set_worker_context(worker::Worker* context); | 733 | 733 | inline void add_sub_worker_context(worker::Worker* context); | @@ -881,7 +881,7 @@ class Environment { | 881 | 881 | std::unordered_map<std::string, uint64_t> performance_marks_; | 882 | 882 |
| 883 | 883 | bool can_call_into_js_ = true; | 884 | | - double thread_id_ = 0; | | 884 | + uint64_t thread_id_ = 0; | 885 | 885 | std::unordered_set<worker::Worker*> sub_worker_contexts_; | 886 | 886 |
| 887 | 887 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_worker.cc +12-3Lines changed: 12 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | #include "async_wrap.h" | 10 | 10 | #include "async_wrap-inl.h" | 11 | 11 |
| | 12 | +#include <string> | | 13 | + | 12 | 14 | using v8::ArrayBuffer; | 13 | 15 | using v8::Context; | 14 | 16 | using v8::Function; | @@ -30,7 +32,7 @@ namespace worker { | 30 | 32 |
| 31 | 33 | namespace { | 32 | 34 |
| 33 | | -double next_thread_id = 1; | | 35 | +uint64_t next_thread_id = 1; | 34 | 36 | Mutex next_thread_id_mutex; | 35 | 37 |
| 36 | 38 | } // anonymous namespace | @@ -44,7 +46,8 @@ Worker::Worker(Environment* env, Local<Object> wrap) | 44 | 46 | } | 45 | 47 | wrap->Set(env->context(), | 46 | 48 | env->thread_id_string(), | 47 | | - Number::New(env->isolate(), thread_id_)).FromJust(); | | 49 | + Number::New(env->isolate(), | | 50 | + static_cast<double>(thread_id_))).FromJust(); | 48 | 51 |
| 49 | 52 | // Set up everything that needs to be set up in the parent environment. | 50 | 53 | parent_port_ = MessagePort::New(env, env->context()); | @@ -112,6 +115,11 @@ bool Worker::is_stopped() const { | 112 | 115 | } | 113 | 116 |
| 114 | 117 | 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())); | 115 | 123 | MultiIsolatePlatform* platform = isolate_data_->platform(); | 116 | 124 | CHECK_NE(platform, nullptr); | 117 | 125 |
| @@ -418,7 +426,8 @@ void InitWorker(Local<Object> target, | 418 | 426 | auto thread_id_string = FIXED_ONE_BYTE_STRING(env->isolate(), "threadId"); | 419 | 427 | target->Set(env->context(), | 420 | 428 | 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(); | 422 | 431 | } | 423 | 432 |
| 424 | 433 | } // anonymous namespace | |
Collapse file Copy file name to clipboardExpand all lines: src/node_worker.h +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -62,7 +62,7 @@ class Worker : public AsyncWrap { | 62 | 62 |
| 63 | 63 | bool thread_joined_ = true; | 64 | 64 | int exit_code_ = 0; | 65 | | - double thread_id_ = -1; | | 65 | + uint64_t thread_id_ = -1; | 66 | 66 |
| 67 | 67 | std::unique_ptr<MessagePortData> child_port_data_; | 68 | 68 |
| |
Collapse file Copy file name to clipboard +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
|