| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5fdd3f4 commit a646a22
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -906,6 +906,10 @@ if (isMainThread) { | |||
| 906 | 906 | <!-- YAML | |
| 907 | 907 | added: v10.5.0 | |
| 908 | 908 | changes: | |
| 909 | + - version: REPLACEME | ||
| 910 | + pr-url: https://github.com/nodejs/node/pull/46832 | ||
| 911 | + description: Added support for a `name` option, which allows | ||
| 912 | + adding a name to worker title for debugging. | ||
| 909 | 913 | - version: v14.9.0 | |
| 910 | 914 | pr-url: https://github.com/nodejs/node/pull/34584 | |
| 911 | 915 | description: The `filename` parameter can be a WHATWG `URL` object using | |
@@ -1004,6 +1008,9 @@ changes: | |||
| 1004 | 1008 | used for generated code. | |
| 1005 | 1009 | * `stackSizeMb` {number} The default maximum stack size for the thread. | |
| 1006 | 1010 | Small values may lead to unusable Worker instances. **Default:** `4`. | |
| 1011 | + * `name` {string} An optional `name` to be appended to the worker title | ||
| 1012 | + for debuggin/identification purposes, making the final title as | ||
| 1013 | + `[worker ${id}] ${name}`. **Default:** `''`. | ||
| 1007 | 1014 | ||
| 1008 | 1015 | ### Event: `'error'` | |
| 1009 | 1016 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,7 @@ const { | |||
| 16 | 16 | SafeArrayIterator, | |
| 17 | 17 | SafeMap, | |
| 18 | 18 | String, | |
| 19 | + StringPrototypeTrim, | ||
| 19 | 20 | Symbol, | |
| 20 | 21 | SymbolFor, | |
| 21 | 22 | TypedArrayPrototypeFill, | |
@@ -57,7 +58,7 @@ const { | |||
| 57 | 58 | const { deserializeError } = require('internal/error_serdes'); | |
| 58 | 59 | const { fileURLToPath, isURLInstance, pathToFileURL } = require('internal/url'); | |
| 59 | 60 | const { kEmptyObject } = require('internal/util'); | |
| 60 | - const { validateArray } = require('internal/validators'); | ||
| 61 | + const { validateArray, validateString } = require('internal/validators'); | ||
| 61 | 62 | ||
| 62 | 63 | const { | |
| 63 | 64 | ownsProcessState, | |
@@ -188,12 +189,19 @@ class Worker extends EventEmitter { | |||
| 188 | 189 | options.env); | |
| 189 | 190 | } | |
| 190 | 191 | ||
| 192 | + let name = ''; | ||
| 193 | + if (options.name) { | ||
| 194 | + validateString(options.name, 'options.name'); | ||
| 195 | + name = StringPrototypeTrim(options.name); | ||
| 196 | + } | ||
| 197 | + | ||
| 191 | 198 | // Set up the C++ handle for the worker, as well as some internal wiring. | |
| 192 | 199 | this[kHandle] = new WorkerImpl(url, | |
| 193 | 200 | env === process.env ? null : env, | |
| 194 | 201 | options.execArgv, | |
| 195 | 202 | parseResourceLimits(options.resourceLimits), | |
| 196 | - !!(options.trackUnmanagedFds ?? true)); | ||
| 203 | + !!(options.trackUnmanagedFds ?? true), | ||
| 204 | + name); | ||
| 197 | 205 | if (this[kHandle].invalidExecArgv) { | |
| 198 | 206 | throw new ERR_WORKER_INVALID_EXEC_ARGV(this[kHandle].invalidExecArgv); | |
| 199 | 207 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -529,11 +529,17 @@ NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle( | |||
| 529 | 529 | Environment* env, | |
| 530 | 530 | ThreadId thread_id, | |
| 531 | 531 | const char* url) { | |
| 532 | + return GetInspectorParentHandle(env, thread_id, url, ""); | ||
| 533 | + } | ||
| 534 | + | ||
| 535 | + NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle( | ||
| 536 | + Environment* env, ThreadId thread_id, const char* url, const char* name) { | ||
| 532 | 537 | CHECK_NOT_NULL(env); | |
| 538 | + if (name == nullptr) name = ""; | ||
| 533 | 539 | CHECK_NE(thread_id.id, static_cast<uint64_t>(-1)); | |
| 534 | 540 | #if HAVE_INSPECTOR | |
| 535 | 541 | return std::make_unique<InspectorParentHandleImpl>( | |
| 536 | - env->inspector_agent()->GetParentHandle(thread_id.id, url)); | ||
| 542 | + env->inspector_agent()->GetParentHandle(thread_id.id, url, name)); | ||
| 537 | 543 | #else | |
| 538 | 544 | return {}; | |
| 539 | 545 | #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 | |
|---|---|---|---|
@@ -952,17 +952,17 @@ void Agent::SetParentHandle( | |||
| 952 | 952 | } | |
| 953 | 953 | ||
| 954 | 954 | std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle( | |
| 955 | - uint64_t thread_id, const std::string& url) { | ||
| 955 | + uint64_t thread_id, const std::string& url, const std::string& name) { | ||
| 956 | 956 | if (!parent_env_->should_create_inspector() && !client_) { | |
| 957 | 957 | ThrowUninitializedInspectorError(parent_env_); | |
| 958 | 958 | return std::unique_ptr<ParentInspectorHandle>{}; | |
| 959 | 959 | } | |
| 960 | 960 | ||
| 961 | 961 | CHECK_NOT_NULL(client_); | |
| 962 | 962 | if (!parent_handle_) { | |
| 963 | - return client_->getWorkerManager()->NewParentHandle(thread_id, url); | ||
| 963 | + return client_->getWorkerManager()->NewParentHandle(thread_id, url, name); | ||
| 964 | 964 | } else { | |
| 965 | - return parent_handle_->NewParentInspectorHandle(thread_id, url); | ||
| 965 | + return parent_handle_->NewParentInspectorHandle(thread_id, url, name); | ||
| 966 | 966 | } | |
| 967 | 967 | } | |
| 968 | 968 | ||
| 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 | |
|---|---|---|---|
@@ -696,6 +696,12 @@ NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle( | |||
| 696 | 696 | ThreadId child_thread_id, | |
| 697 | 697 | const char* child_url); | |
| 698 | 698 | ||
| 699 | + NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle( | ||
| 700 | + Environment* parent_env, | ||
| 701 | + ThreadId child_thread_id, | ||
| 702 | + const char* child_url, | ||
| 703 | + const char* name); | ||
| 704 | + | ||
| 699 | 705 | struct StartExecutionCallbackInfo { | |
| 700 | 706 | v8::Local<v8::Object> process_object; | |
| 701 | 707 | v8::Local<v8::Function> native_require; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,6 +49,7 @@ constexpr double kMB = 1024 * 1024; | |||
| 49 | 49 | Worker::Worker(Environment* env, | |
| 50 | 50 | Local<Object> wrap, | |
| 51 | 51 | const std::string& url, | |
| 52 | + const std::string& name, | ||
| 52 | 53 | std::shared_ptr<PerIsolateOptions> per_isolate_opts, | |
| 53 | 54 | std::vector<std::string>&& exec_argv, | |
| 54 | 55 | std::shared_ptr<KVStore> env_vars, | |
@@ -58,6 +59,7 @@ Worker::Worker(Environment* env, | |||
| 58 | 59 | exec_argv_(exec_argv), | |
| 59 | 60 | platform_(env->isolate_data()->platform()), | |
| 60 | 61 | thread_id_(AllocateEnvironmentThreadId()), | |
| 62 | + name_(name), | ||
| 61 | 63 | env_vars_(env_vars), | |
| 62 | 64 | snapshot_data_(snapshot_data) { | |
| 63 | 65 | Debug(this, "Creating new worker instance with thread id %llu", | |
@@ -82,8 +84,8 @@ Worker::Worker(Environment* env, | |||
| 82 | 84 | Number::New(env->isolate(), static_cast<double>(thread_id_.id))) | |
| 83 | 85 | .Check(); | |
| 84 | 86 | ||
| 85 | - inspector_parent_handle_ = GetInspectorParentHandle( | ||
| 86 | - env, thread_id_, url.c_str()); | ||
| 87 | + inspector_parent_handle_ = | ||
| 88 | + GetInspectorParentHandle(env, thread_id_, url.c_str(), name.c_str()); | ||
| 87 | 89 | ||
| 88 | 90 | argv_ = std::vector<std::string>{env->argv()[0]}; | |
| 89 | 91 | // Mark this Worker object as weak until we actually start the thread. | |
@@ -264,11 +266,10 @@ size_t Worker::NearHeapLimit(void* data, size_t current_heap_limit, | |||
| 264 | 266 | } | |
| 265 | 267 | ||
| 266 | 268 | void Worker::Run() { | |
| 267 | - std::string name = "WorkerThread "; | ||
| 268 | - name += std::to_string(thread_id_.id); | ||
| 269 | + std::string trace_name = "[worker " + std::to_string(thread_id_.id) + "]" + | ||
| 270 | + (name_ == "" ? "" : " " + name_); | ||
| 269 | 271 | TRACE_EVENT_METADATA1( | |
| 270 | - "__metadata", "thread_name", "name", | ||
| 271 | - TRACE_STR_COPY(name.c_str())); | ||
| 272 | + "__metadata", "thread_name", "name", TRACE_STR_COPY(trace_name.c_str())); | ||
| 272 | 273 | CHECK_NOT_NULL(platform_); | |
| 273 | 274 | ||
| 274 | 275 | Debug(this, "Creating isolate for worker with id %llu", thread_id_.id); | |
@@ -467,6 +468,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 467 | 468 | } | |
| 468 | 469 | ||
| 469 | 470 | std::string url; | |
| 471 | + std::string name; | ||
| 470 | 472 | std::shared_ptr<PerIsolateOptions> per_isolate_opts = nullptr; | |
| 471 | 473 | std::shared_ptr<KVStore> env_vars = nullptr; | |
| 472 | 474 | ||
@@ -479,6 +481,12 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 479 | 481 | url.append(value.out(), value.length()); | |
| 480 | 482 | } | |
| 481 | 483 | ||
| 484 | + if (!args[5]->IsNullOrUndefined()) { | ||
| 485 | + Utf8Value value( | ||
| 486 | + isolate, args[5]->ToString(env->context()).FromMaybe(Local<String>())); | ||
| 487 | + name.append(value.out(), value.length()); | ||
| 488 | + } | ||
| 489 | + | ||
| 482 | 490 | if (args[1]->IsNull()) { | |
| 483 | 491 | // Means worker.env = { ...process.env }. | |
| 484 | 492 | env_vars = env->env_vars()->Clone(isolate); | |
@@ -589,6 +597,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 589 | 597 | Worker* worker = new Worker(env, | |
| 590 | 598 | args.This(), | |
| 591 | 599 | url, | |
| 600 | + name, | ||
| 592 | 601 | per_isolate_opts, | |
| 593 | 602 | std::move(exec_argv_out), | |
| 594 | 603 | env_vars, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,6 +30,7 @@ class Worker : public AsyncWrap { | |||
| 30 | 30 | Worker(Environment* env, | |
| 31 | 31 | v8::Local<v8::Object> wrap, | |
| 32 | 32 | const std::string& url, | |
| 33 | + const std::string& name, | ||
| 33 | 34 | std::shared_ptr<PerIsolateOptions> per_isolate_opts, | |
| 34 | 35 | std::vector<std::string>&& exec_argv, | |
| 35 | 36 | std::shared_ptr<KVStore> env_vars, | |
@@ -99,6 +100,8 @@ class Worker : public AsyncWrap { | |||
| 99 | 100 | ExitCode exit_code_ = ExitCode::kNoFailure; | |
| 100 | 101 | ThreadId thread_id_; | |
| 101 | 102 | uintptr_t stack_base_ = 0; | |
| 103 | + // Optional name used for debugging in inspector and trace events. | ||
| 104 | + std::string name_; | ||
| 102 | 105 | ||
| 103 | 106 | // Custom resource constraints: | |
| 104 | 107 | double resource_limits_[kTotalResourceLimitCount]; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments