diff --git a/src/node_locks.cc b/src/node_locks.cc index f26f112f35..9c871569c4 100644 --- a/src/node_locks.cc +++ b/src/node_locks.cc @@ -23,12 +23,10 @@ using v8::Isolate; using v8::Local; using v8::LocalVector; using v8::MaybeLocal; -using v8::NewStringType; using v8::Object; using v8::ObjectTemplate; using v8::Promise; using v8::PropertyAttribute; -using v8::String; using v8::Value; // Reject two promises and return `false` on failure. @@ -51,19 +49,26 @@ Lock::Lock(Environment* env, released_promise_.Reset(env_->isolate(), released); } +void Lock::MemoryInfo(node::MemoryTracker* tracker) const { + tracker->TrackFieldWithSize("name", name_.size()); + tracker->TrackField("client_id", client_id_); + tracker->TrackField("waiting_promise", waiting_promise_); + tracker->TrackField("released_promise", released_promise_); +} + LockRequest::LockRequest(Environment* env, Local waiting, Local released, Local callback, const std::u16string& name, Lock::Mode mode, - const std::string& client_id, + std::string client_id, bool steal, bool if_available) : env_(env), name_(name), mode_(mode), - client_id_(client_id), + client_id_(std::move(client_id)), steal_(steal), if_available_(if_available) { waiting_promise_.Reset(env_->isolate(), waiting); @@ -85,6 +90,7 @@ Local GetLockInfoTemplate(Environment* env) { return tmpl; } +// The request here can be either a Lock or a LockRequest. static MaybeLocal CreateLockInfoObject(Environment* env, const auto& request) { auto tmpl = GetLockInfoTemplate(env); @@ -573,22 +579,13 @@ void LockManager::Request(const FunctionCallbackInfo& args) { CHECK(args[4]->IsBoolean()); // ifAvailable CHECK(args[5]->IsFunction()); // callback - Local resource_name_str = args[0].As(); - TwoByteValue resource_name_utf16(isolate, resource_name_str); - std::u16string resource_name( - reinterpret_cast(*resource_name_utf16), - resource_name_utf16.length()); - String::Utf8Value client_id_utf8(isolate, args[1]); - std::string client_id(*client_id_utf8); - String::Utf8Value mode_utf8(isolate, args[2]); - std::string mode_str(*mode_utf8); + TwoByteValue resource_name(isolate, args[0]); + Utf8Value client_id(isolate, args[1]); + Utf8Value mode(isolate, args[2]); bool steal = args[3]->BooleanValue(isolate); bool if_available = args[4]->BooleanValue(isolate); Local callback = args[5].As(); - Lock::Mode lock_mode = - mode_str == "shared" ? Lock::Mode::Shared : Lock::Mode::Exclusive; - Local waiting_promise; Local released_promise; @@ -611,15 +608,17 @@ void LockManager::Request(const FunctionCallbackInfo& args) { env->AddCleanupHook(LockManager::OnEnvironmentCleanup, env); } - auto lock_request = std::make_unique(env, - waiting_promise, - released_promise, - callback, - resource_name, - lock_mode, - client_id, - steal, - if_available); + auto lock_request = std::make_unique( + env, + waiting_promise, + released_promise, + callback, + resource_name.ToU16String(), + mode.ToStringView() == "shared" ? Lock::Mode::Shared + : Lock::Mode::Exclusive, + client_id.ToString(), + steal, + if_available); // Steal requests get priority by going to front of queue if (steal) { manager->pending_queue_.emplace_front(std::move(lock_request)); @@ -842,6 +841,10 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(OnIfAvailableReject); } +void LockHolder::MemoryInfo(node::MemoryTracker* tracker) const { + tracker->TrackField("lock", lock_); +} + BaseObjectPtr LockHolder::Create(Environment* env, std::shared_ptr lock) { Local obj; diff --git a/src/node_locks.h b/src/node_locks.h index 68a2d84ff3..cee1d26a42 100644 --- a/src/node_locks.h +++ b/src/node_locks.h @@ -15,7 +15,7 @@ namespace node::worker::locks { -class Lock final { +class Lock final : public MemoryRetainer { public: enum class Mode { Shared, Exclusive }; @@ -53,6 +53,10 @@ class Lock final { return released_promise_.Get(env_->isolate()); } + void MemoryInfo(node::MemoryTracker* tracker) const override; + SET_MEMORY_INFO_NAME(Lock) + SET_SELF_SIZE(Lock) + private: Environment* env_; std::u16string name_; @@ -79,7 +83,7 @@ class LockHolder final : public BaseObject { std::shared_ptr lock() const { return lock_; } - SET_NO_MEMORY_INFO() + void MemoryInfo(node::MemoryTracker* tracker) const override; SET_MEMORY_INFO_NAME(LockHolder) SET_SELF_SIZE(LockHolder) @@ -101,7 +105,7 @@ class LockRequest final { v8::Local callback, const std::u16string& name, Lock::Mode mode, - const std::string& client_id, + std::string client_id, bool steal, bool if_available); ~LockRequest() = default; diff --git a/src/util.h b/src/util.h index c361cefa9a..6dfccf3a34 100644 --- a/src/util.h +++ b/src/util.h @@ -562,6 +562,15 @@ class Utf8Value : public MaybeStackBuffer { class TwoByteValue : public MaybeStackBuffer { public: explicit TwoByteValue(v8::Isolate* isolate, v8::Local value); + + inline std::u16string ToU16String() const { + return std::u16string(reinterpret_cast(out()), length()); + } + + inline std::u16string_view ToU16StringView() const { + return std::u16string_view(reinterpret_cast(out()), + length()); + } }; class BufferValue : public MaybeStackBuffer {