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: https://github.com/nodejs/node/pull/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> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
This commit is contained in:
parent
3ff1cb955f
commit
99e6ecbb17
@ -598,11 +598,11 @@ inline bool Environment::is_main_thread() const {
|
|||||||
return thread_id_ == 0;
|
return thread_id_ == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline double Environment::thread_id() const {
|
inline uint64_t Environment::thread_id() const {
|
||||||
return thread_id_;
|
return thread_id_;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Environment::set_thread_id(double id) {
|
inline void Environment::set_thread_id(uint64_t id) {
|
||||||
thread_id_ = id;
|
thread_id_ = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -726,8 +726,8 @@ class Environment {
|
|||||||
bool is_stopping_worker() const;
|
bool is_stopping_worker() const;
|
||||||
|
|
||||||
inline bool is_main_thread() const;
|
inline bool is_main_thread() const;
|
||||||
inline double thread_id() const;
|
inline uint64_t thread_id() const;
|
||||||
inline void set_thread_id(double id);
|
inline void set_thread_id(uint64_t id);
|
||||||
inline worker::Worker* worker_context() const;
|
inline worker::Worker* worker_context() const;
|
||||||
inline void set_worker_context(worker::Worker* context);
|
inline void set_worker_context(worker::Worker* context);
|
||||||
inline void add_sub_worker_context(worker::Worker* context);
|
inline void add_sub_worker_context(worker::Worker* context);
|
||||||
@ -881,7 +881,7 @@ class Environment {
|
|||||||
std::unordered_map<std::string, uint64_t> performance_marks_;
|
std::unordered_map<std::string, uint64_t> performance_marks_;
|
||||||
|
|
||||||
bool can_call_into_js_ = true;
|
bool can_call_into_js_ = true;
|
||||||
double thread_id_ = 0;
|
uint64_t thread_id_ = 0;
|
||||||
std::unordered_set<worker::Worker*> sub_worker_contexts_;
|
std::unordered_set<worker::Worker*> sub_worker_contexts_;
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
#include "async_wrap.h"
|
#include "async_wrap.h"
|
||||||
#include "async_wrap-inl.h"
|
#include "async_wrap-inl.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
using v8::ArrayBuffer;
|
using v8::ArrayBuffer;
|
||||||
using v8::Context;
|
using v8::Context;
|
||||||
using v8::Function;
|
using v8::Function;
|
||||||
@ -30,7 +32,7 @@ namespace worker {
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
double next_thread_id = 1;
|
uint64_t next_thread_id = 1;
|
||||||
Mutex next_thread_id_mutex;
|
Mutex next_thread_id_mutex;
|
||||||
|
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
@ -44,7 +46,8 @@ Worker::Worker(Environment* env, Local<Object> wrap)
|
|||||||
}
|
}
|
||||||
wrap->Set(env->context(),
|
wrap->Set(env->context(),
|
||||||
env->thread_id_string(),
|
env->thread_id_string(),
|
||||||
Number::New(env->isolate(), thread_id_)).FromJust();
|
Number::New(env->isolate(),
|
||||||
|
static_cast<double>(thread_id_))).FromJust();
|
||||||
|
|
||||||
// Set up everything that needs to be set up in the parent environment.
|
// Set up everything that needs to be set up in the parent environment.
|
||||||
parent_port_ = MessagePort::New(env, env->context());
|
parent_port_ = MessagePort::New(env, env->context());
|
||||||
@ -112,6 +115,11 @@ bool Worker::is_stopped() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Worker::Run() {
|
void Worker::Run() {
|
||||||
|
std::string name = "WorkerThread ";
|
||||||
|
name += std::to_string(thread_id_);
|
||||||
|
TRACE_EVENT_METADATA1(
|
||||||
|
"__metadata", "thread_name", "name",
|
||||||
|
TRACE_STR_COPY(name.c_str()));
|
||||||
MultiIsolatePlatform* platform = isolate_data_->platform();
|
MultiIsolatePlatform* platform = isolate_data_->platform();
|
||||||
CHECK_NE(platform, nullptr);
|
CHECK_NE(platform, nullptr);
|
||||||
|
|
||||||
@ -418,7 +426,8 @@ void InitWorker(Local<Object> target,
|
|||||||
auto thread_id_string = FIXED_ONE_BYTE_STRING(env->isolate(), "threadId");
|
auto thread_id_string = FIXED_ONE_BYTE_STRING(env->isolate(), "threadId");
|
||||||
target->Set(env->context(),
|
target->Set(env->context(),
|
||||||
thread_id_string,
|
thread_id_string,
|
||||||
Number::New(env->isolate(), env->thread_id())).FromJust();
|
Number::New(env->isolate(),
|
||||||
|
static_cast<double>(env->thread_id()))).FromJust();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
|
@ -62,7 +62,7 @@ class Worker : public AsyncWrap {
|
|||||||
|
|
||||||
bool thread_joined_ = true;
|
bool thread_joined_ = true;
|
||||||
int exit_code_ = 0;
|
int exit_code_ = 0;
|
||||||
double thread_id_ = -1;
|
uint64_t thread_id_ = -1;
|
||||||
|
|
||||||
std::unique_ptr<MessagePortData> child_port_data_;
|
std::unique_ptr<MessagePortData> child_port_data_;
|
||||||
|
|
||||||
|
33
test/parallel/test-trace-events-worker-metadata.js
Normal file
33
test/parallel/test-trace-events-worker-metadata.js
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
// Flags: --experimental-worker
|
||||||
|
'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, '/')}')`;
|
||||||
|
const FILE_NAME = 'node_trace.1.log';
|
||||||
|
const tmpdir = require('../common/tmpdir');
|
||||||
|
tmpdir.refresh();
|
||||||
|
process.chdir(tmpdir.path);
|
||||||
|
|
||||||
|
const proc = cp.spawn(process.execPath,
|
||||||
|
[ '--experimental-worker',
|
||||||
|
'--trace-event-categories', 'node',
|
||||||
|
'-e', CODE ]);
|
||||||
|
proc.once('exit', common.mustCall(() => {
|
||||||
|
assert(common.fileExists(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 === 'WorkerThread 1'));
|
||||||
|
}));
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
// Do nothing here.
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user