| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 09c5e6a commit f7423bd
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -900,6 +900,10 @@ if (isMainThread) { | |||
| 900 | 900 | <!-- YAML | |
| 901 | 901 | added: v10.5.0 | |
| 902 | 902 | changes: | |
| 903 | + - version: REPLACEME | ||
| 904 | + pr-url: https://github.com/nodejs/node/pull/46832 | ||
| 905 | + description: Added support for a `name` option, which allows | ||
| 906 | + adding a name to worker title for debugging. | ||
| 903 | 907 | - version: v14.9.0 | |
| 904 | 908 | pr-url: https://github.com/nodejs/node/pull/34584 | |
| 905 | 909 | description: The `filename` parameter can be a WHATWG `URL` object using | |
@@ -998,6 +1002,9 @@ changes: | |||
| 998 | 1002 | used for generated code. | |
| 999 | 1003 | * `stackSizeMb` {number} The default maximum stack size for the thread. | |
| 1000 | 1004 | Small values may lead to unusable Worker instances. **Default:** `4`. | |
| 1005 | + * `name` {string} An optional `name` to be appended to the worker title | ||
| 1006 | + for debuggin/identification purposes, making the final title as | ||
| 1007 | + `[worker ${id}] ${name}`. **Default:** `''`. | ||
| 1001 | 1008 | ||
| 1002 | 1009 | ### Event: `'error'` | |
| 1003 | 1010 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,7 @@ const { | |||
| 17 | 17 | SafeArrayIterator, | |
| 18 | 18 | SafeMap, | |
| 19 | 19 | String, | |
| 20 | + StringPrototypeTrim, | ||
| 20 | 21 | Symbol, | |
| 21 | 22 | SymbolFor, | |
| 22 | 23 | TypedArrayPrototypeFill, | |
@@ -56,7 +57,7 @@ const { | |||
| 56 | 57 | const { deserializeError } = require('internal/error_serdes'); | |
| 57 | 58 | const { fileURLToPath, isURLInstance, pathToFileURL } = require('internal/url'); | |
| 58 | 59 | const { kEmptyObject } = require('internal/util'); | |
| 59 | - const { validateArray } = require('internal/validators'); | ||
| 60 | + const { validateArray, validateString } = require('internal/validators'); | ||
| 60 | 61 | ||
| 61 | 62 | const { | |
| 62 | 63 | ownsProcessState, | |
@@ -184,12 +185,19 @@ class Worker extends EventEmitter { | |||
| 184 | 185 | options.env); | |
| 185 | 186 | } | |
| 186 | 187 | ||
| 188 | + let name = ''; | ||
| 189 | + if (options.name) { | ||
| 190 | + validateString(options.name, 'options.name'); | ||
| 191 | + name = StringPrototypeTrim(options.name); | ||
| 192 | + } | ||
| 193 | + | ||
| 187 | 194 | // Set up the C++ handle for the worker, as well as some internal wiring. | |
| 188 | 195 | this[kHandle] = new WorkerImpl(url, | |
| 189 | 196 | env === process.env ? null : env, | |
| 190 | 197 | options.execArgv, | |
| 191 | 198 | parseResourceLimits(options.resourceLimits), | |
| 192 | - !!(options.trackUnmanagedFds ?? true)); | ||
| 199 | + !!(options.trackUnmanagedFds ?? true), | ||
| 200 | + name); | ||
| 193 | 201 | if (this[kHandle].invalidExecArgv) { | |
| 194 | 202 | throw new ERR_WORKER_INVALID_EXEC_ARGV(this[kHandle].invalidExecArgv); | |
| 195 | 203 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -453,11 +453,17 @@ NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle( | |||
| 453 | 453 | Environment* env, | |
| 454 | 454 | ThreadId thread_id, | |
| 455 | 455 | const char* url) { | |
| 456 | + return GetInspectorParentHandle(env, thread_id, url, ""); | ||
| 457 | + } | ||
| 458 | + | ||
| 459 | + NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle( | ||
| 460 | + Environment* env, ThreadId thread_id, const char* url, const char* name) { | ||
| 456 | 461 | CHECK_NOT_NULL(env); | |
| 462 | + if (name == nullptr) name = ""; | ||
| 457 | 463 | CHECK_NE(thread_id.id, static_cast<uint64_t>(-1)); | |
| 458 | 464 | #if HAVE_INSPECTOR | |
| 459 | 465 | return std::make_unique<InspectorParentHandleImpl>( | |
| 460 | - env->inspector_agent()->GetParentHandle(thread_id.id, url)); | ||
| 466 | + env->inspector_agent()->GetParentHandle(thread_id.id, url, name)); | ||
| 461 | 467 | #else | |
| 462 | 468 | return {}; | |
| 463 | 469 | #endif | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,18 +14,20 @@ class WorkerStartedRequest : public Request { | |||
| 14 | 14 | uint64_t id, | |
| 15 | 15 | const std::string& url, | |
| 16 | 16 | std::shared_ptr<node::inspector::MainThreadHandle> worker_thread, | |
| 17 | - bool waiting) | ||
| 17 | + bool waiting, | ||
| 18 | + const std::string& name) | ||
| 18 | 19 | : id_(id), | |
| 19 | - info_(BuildWorkerTitle(id), url, worker_thread), | ||
| 20 | + info_(BuildWorkerTitle(id, name), url, worker_thread), | ||
| 20 | 21 | waiting_(waiting) {} | |
| 21 | 22 | void Call(MainThreadInterface* thread) override { | |
| 22 | 23 | auto manager = thread->inspector_agent()->GetWorkerManager(); | |
| 23 | 24 | manager->WorkerStarted(id_, info_, waiting_); | |
| 24 | 25 | } | |
| 25 | 26 | ||
| 26 | 27 | private: | |
| 27 | - static std::string BuildWorkerTitle(int id) { | ||
| 28 | - return "Worker " + std::to_string(id); | ||
| 28 | + static std::string BuildWorkerTitle(int id, const std::string& name) { | ||
| 29 | + return "[worker " + std::to_string(id) + "]" + | ||
| 30 | + (name == "" ? "" : " " + name); | ||
| 29 | 31 | } | |
| 30 | 32 | ||
| 31 | 33 | uint64_t id_; | |
@@ -57,11 +59,13 @@ ParentInspectorHandle::ParentInspectorHandle( | |||
| 57 | 59 | uint64_t id, | |
| 58 | 60 | const std::string& url, | |
| 59 | 61 | std::shared_ptr<MainThreadHandle> parent_thread, | |
| 60 | - bool wait_for_connect) | ||
| 62 | + bool wait_for_connect, | ||
| 63 | + const std::string& name) | ||
| 61 | 64 | : id_(id), | |
| 62 | 65 | url_(url), | |
| 63 | 66 | parent_thread_(parent_thread), | |
| 64 | - wait_(wait_for_connect) {} | ||
| 67 | + wait_(wait_for_connect), | ||
| 68 | + name_(name) {} | ||
| 65 | 69 | ||
| 66 | 70 | ParentInspectorHandle::~ParentInspectorHandle() { | |
| 67 | 71 | parent_thread_->Post( | |
@@ -71,7 +75,7 @@ ParentInspectorHandle::~ParentInspectorHandle() { | |||
| 71 | 75 | void ParentInspectorHandle::WorkerStarted( | |
| 72 | 76 | std::shared_ptr<MainThreadHandle> worker_thread, bool waiting) { | |
| 73 | 77 | std::unique_ptr<Request> request( | |
| 74 | - new WorkerStartedRequest(id_, url_, worker_thread, waiting)); | ||
| 78 | + new WorkerStartedRequest(id_, url_, worker_thread, waiting, name_)); | ||
| 75 | 79 | parent_thread_->Post(std::move(request)); | |
| 76 | 80 | } | |
| 77 | 81 | ||
@@ -97,9 +101,10 @@ void WorkerManager::WorkerStarted(uint64_t session_id, | |||
| 97 | 101 | } | |
| 98 | 102 | ||
| 99 | 103 | std::unique_ptr<ParentInspectorHandle> WorkerManager::NewParentHandle( | |
| 100 | - uint64_t thread_id, const std::string& url) { | ||
| 104 | + uint64_t thread_id, const std::string& url, const std::string& name) { | ||
| 101 | 105 | bool wait = !delegates_waiting_on_start_.empty(); | |
| 102 | - return std::make_unique<ParentInspectorHandle>(thread_id, url, thread_, wait); | ||
| 106 | + return std::make_unique<ParentInspectorHandle>( | ||
| 107 | + thread_id, url, thread_, wait, name); | ||
| 103 | 108 | } | |
| 104 | 109 | ||
| 105 | 110 | void WorkerManager::RemoveAttachDelegate(int id) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,14 +56,13 @@ class ParentInspectorHandle { | |||
| 56 | 56 | ParentInspectorHandle(uint64_t id, | |
| 57 | 57 | const std::string& url, | |
| 58 | 58 | std::shared_ptr<MainThreadHandle> parent_thread, | |
| 59 | - bool wait_for_connect); | ||
| 59 | + bool wait_for_connect, | ||
| 60 | + const std::string& name); | ||
| 60 | 61 | ~ParentInspectorHandle(); | |
| 61 | 62 | std::unique_ptr<ParentInspectorHandle> NewParentInspectorHandle( | |
| 62 | - uint64_t thread_id, const std::string& url) { | ||
| 63 | - return std::make_unique<ParentInspectorHandle>(thread_id, | ||
| 64 | - url, | ||
| 65 | - parent_thread_, | ||
| 66 | - wait_); | ||
| 63 | + uint64_t thread_id, const std::string& url, const std::string& name) { | ||
| 64 | + return std::make_unique<ParentInspectorHandle>( | ||
| 65 | + thread_id, url, parent_thread_, wait_, name); | ||
| 67 | 66 | } | |
| 68 | 67 | void WorkerStarted(std::shared_ptr<MainThreadHandle> worker_thread, | |
| 69 | 68 | bool waiting); | |
@@ -80,6 +79,7 @@ class ParentInspectorHandle { | |||
| 80 | 79 | std::string url_; | |
| 81 | 80 | std::shared_ptr<MainThreadHandle> parent_thread_; | |
| 82 | 81 | bool wait_; | |
| 82 | + std::string name_; | ||
| 83 | 83 | }; | |
| 84 | 84 | ||
| 85 | 85 | class WorkerManager : public std::enable_shared_from_this<WorkerManager> { | |
@@ -88,7 +88,7 @@ class WorkerManager : public std::enable_shared_from_this<WorkerManager> { | |||
| 88 | 88 | : thread_(thread) {} | |
| 89 | 89 | ||
| 90 | 90 | std::unique_ptr<ParentInspectorHandle> NewParentHandle( | |
| 91 | - uint64_t thread_id, const std::string& url); | ||
| 91 | + uint64_t thread_id, const std::string& url, const std::string& name); | ||
| 92 | 92 | void WorkerStarted(uint64_t session_id, const WorkerInfo& info, bool waiting); | |
| 93 | 93 | void WorkerFinished(uint64_t session_id); | |
| 94 | 94 | std::unique_ptr<WorkerManagerEventHandle> SetAutoAttach( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -948,17 +948,17 @@ void Agent::SetParentHandle( | |||
| 948 | 948 | } | |
| 949 | 949 | ||
| 950 | 950 | std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle( | |
| 951 | - uint64_t thread_id, const std::string& url) { | ||
| 951 | + uint64_t thread_id, const std::string& url, const std::string& name) { | ||
| 952 | 952 | if (!parent_env_->should_create_inspector() && !client_) { | |
| 953 | 953 | ThrowUninitializedInspectorError(parent_env_); | |
| 954 | 954 | return std::unique_ptr<ParentInspectorHandle>{}; | |
| 955 | 955 | } | |
| 956 | 956 | ||
| 957 | 957 | CHECK_NOT_NULL(client_); | |
| 958 | 958 | if (!parent_handle_) { | |
| 959 | - return client_->getWorkerManager()->NewParentHandle(thread_id, url); | ||
| 959 | + return client_->getWorkerManager()->NewParentHandle(thread_id, url, name); | ||
| 960 | 960 | } else { | |
| 961 | - return parent_handle_->NewParentInspectorHandle(thread_id, url); | ||
| 961 | + return parent_handle_->NewParentInspectorHandle(thread_id, url, name); | ||
| 962 | 962 | } | |
| 963 | 963 | } | |
| 964 | 964 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,7 +82,7 @@ class Agent { | |||
| 82 | 82 | ||
| 83 | 83 | void SetParentHandle(std::unique_ptr<ParentInspectorHandle> parent_handle); | |
| 84 | 84 | std::unique_ptr<ParentInspectorHandle> GetParentHandle( | |
| 85 | - uint64_t thread_id, const std::string& url); | ||
| 85 | + uint64_t thread_id, const std::string& url, const std::string& name); | ||
| 86 | 86 | ||
| 87 | 87 | // Called to create inspector sessions that can be used from the same thread. | |
| 88 | 88 | // The inspector responds by using the delegate to send messages back. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -590,6 +590,12 @@ NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle( | |||
| 590 | 590 | ThreadId child_thread_id, | |
| 591 | 591 | const char* child_url); | |
| 592 | 592 | ||
| 593 | + NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle( | ||
| 594 | + Environment* parent_env, | ||
| 595 | + ThreadId child_thread_id, | ||
| 596 | + const char* child_url, | ||
| 597 | + const char* name); | ||
| 598 | + | ||
| 593 | 599 | struct StartExecutionCallbackInfo { | |
| 594 | 600 | v8::Local<v8::Object> process_object; | |
| 595 | 601 | v8::Local<v8::Function> native_require; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,6 +48,7 @@ constexpr double kMB = 1024 * 1024; | |||
| 48 | 48 | Worker::Worker(Environment* env, | |
| 49 | 49 | Local<Object> wrap, | |
| 50 | 50 | const std::string& url, | |
| 51 | + const std::string& name, | ||
| 51 | 52 | std::shared_ptr<PerIsolateOptions> per_isolate_opts, | |
| 52 | 53 | std::vector<std::string>&& exec_argv, | |
| 53 | 54 | std::shared_ptr<KVStore> env_vars, | |
@@ -57,6 +58,7 @@ Worker::Worker(Environment* env, | |||
| 57 | 58 | exec_argv_(exec_argv), | |
| 58 | 59 | platform_(env->isolate_data()->platform()), | |
| 59 | 60 | thread_id_(AllocateEnvironmentThreadId()), | |
| 61 | + name_(name), | ||
| 60 | 62 | env_vars_(env_vars), | |
| 61 | 63 | snapshot_data_(snapshot_data) { | |
| 62 | 64 | Debug(this, "Creating new worker instance with thread id %llu", | |
@@ -81,8 +83,8 @@ Worker::Worker(Environment* env, | |||
| 81 | 83 | Number::New(env->isolate(), static_cast<double>(thread_id_.id))) | |
| 82 | 84 | .Check(); | |
| 83 | 85 | ||
| 84 | - inspector_parent_handle_ = GetInspectorParentHandle( | ||
| 85 | - env, thread_id_, url.c_str()); | ||
| 86 | + inspector_parent_handle_ = | ||
| 87 | + GetInspectorParentHandle(env, thread_id_, url.c_str(), name.c_str()); | ||
| 86 | 88 | ||
| 87 | 89 | argv_ = std::vector<std::string>{env->argv()[0]}; | |
| 88 | 90 | // Mark this Worker object as weak until we actually start the thread. | |
@@ -256,11 +258,10 @@ size_t Worker::NearHeapLimit(void* data, size_t current_heap_limit, | |||
| 256 | 258 | } | |
| 257 | 259 | ||
| 258 | 260 | void Worker::Run() { | |
| 259 | - std::string name = "WorkerThread "; | ||
| 260 | - name += std::to_string(thread_id_.id); | ||
| 261 | + std::string trace_name = "[worker " + std::to_string(thread_id_.id) + "]" + | ||
| 262 | + (name_ == "" ? "" : " " + name_); | ||
| 261 | 263 | TRACE_EVENT_METADATA1( | |
| 262 | - "__metadata", "thread_name", "name", | ||
| 263 | - TRACE_STR_COPY(name.c_str())); | ||
| 264 | + "__metadata", "thread_name", "name", TRACE_STR_COPY(trace_name.c_str())); | ||
| 264 | 265 | CHECK_NOT_NULL(platform_); | |
| 265 | 266 | ||
| 266 | 267 | Debug(this, "Creating isolate for worker with id %llu", thread_id_.id); | |
@@ -454,6 +455,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 454 | 455 | } | |
| 455 | 456 | ||
| 456 | 457 | std::string url; | |
| 458 | + std::string name; | ||
| 457 | 459 | std::shared_ptr<PerIsolateOptions> per_isolate_opts = nullptr; | |
| 458 | 460 | std::shared_ptr<KVStore> env_vars = nullptr; | |
| 459 | 461 | ||
@@ -466,6 +468,12 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 466 | 468 | url.append(value.out(), value.length()); | |
| 467 | 469 | } | |
| 468 | 470 | ||
| 471 | + if (!args[5]->IsNullOrUndefined()) { | ||
| 472 | + Utf8Value value( | ||
| 473 | + isolate, args[5]->ToString(env->context()).FromMaybe(Local<String>())); | ||
| 474 | + name.append(value.out(), value.length()); | ||
| 475 | + } | ||
| 476 | + | ||
| 469 | 477 | if (args[1]->IsNull()) { | |
| 470 | 478 | // Means worker.env = { ...process.env }. | |
| 471 | 479 | env_vars = env->env_vars()->Clone(isolate); | |
@@ -579,6 +587,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 579 | 587 | Worker* worker = new Worker(env, | |
| 580 | 588 | args.This(), | |
| 581 | 589 | url, | |
| 590 | + name, | ||
| 582 | 591 | per_isolate_opts, | |
| 583 | 592 | std::move(exec_argv_out), | |
| 584 | 593 | env_vars, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,6 +29,7 @@ class Worker : public AsyncWrap { | |||
| 29 | 29 | Worker(Environment* env, | |
| 30 | 30 | v8::Local<v8::Object> wrap, | |
| 31 | 31 | const std::string& url, | |
| 32 | + const std::string& name, | ||
| 32 | 33 | std::shared_ptr<PerIsolateOptions> per_isolate_opts, | |
| 33 | 34 | std::vector<std::string>&& exec_argv, | |
| 34 | 35 | std::shared_ptr<KVStore> env_vars, | |
@@ -98,6 +99,8 @@ class Worker : public AsyncWrap { | |||
| 98 | 99 | int exit_code_ = 0; | |
| 99 | 100 | ThreadId thread_id_; | |
| 100 | 101 | uintptr_t stack_base_ = 0; | |
| 102 | + // Optional name used for debugging in inspector and trace events. | ||
| 103 | + std::string name_; | ||
| 101 | 104 | ||
| 102 | 105 | // Custom resource constraints: | |
| 103 | 106 | double resource_limits_[kTotalResourceLimitCount]; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments