mirror of https://github.com/nodejs/node.git
src: make ELDHistogram a HandleWrap
This simplifies the implementation of ELDHistogram a bit, and more generally allows us to have weak JS references associated with `HandleWrap`s. PR-URL: https://github.com/nodejs/node/pull/29317 Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
1e01f3f022
commit
0af62aae07
|
@ -34,6 +34,7 @@ namespace node {
|
||||||
#define NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \
|
#define NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \
|
||||||
V(NONE) \
|
V(NONE) \
|
||||||
V(DNSCHANNEL) \
|
V(DNSCHANNEL) \
|
||||||
|
V(ELDHISTOGRAM) \
|
||||||
V(FILEHANDLE) \
|
V(FILEHANDLE) \
|
||||||
V(FILEHANDLECLOSEREQ) \
|
V(FILEHANDLECLOSEREQ) \
|
||||||
V(FSEVENTWRAP) \
|
V(FSEVENTWRAP) \
|
||||||
|
|
|
@ -84,6 +84,17 @@ void HandleWrap::Close(Local<Value> close_callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void HandleWrap::MakeWeak() {
|
||||||
|
persistent().SetWeak(
|
||||||
|
this,
|
||||||
|
[](const v8::WeakCallbackInfo<HandleWrap>& data) {
|
||||||
|
HandleWrap* handle_wrap = data.GetParameter();
|
||||||
|
handle_wrap->persistent().Reset();
|
||||||
|
handle_wrap->Close();
|
||||||
|
}, v8::WeakCallbackType::kParameter);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void HandleWrap::MarkAsInitialized() {
|
void HandleWrap::MarkAsInitialized() {
|
||||||
env()->handle_wrap_queue()->PushBack(this);
|
env()->handle_wrap_queue()->PushBack(this);
|
||||||
state_ = kInitialized;
|
state_ = kInitialized;
|
||||||
|
@ -115,15 +126,14 @@ void HandleWrap::OnClose(uv_handle_t* handle) {
|
||||||
HandleScope scope(env->isolate());
|
HandleScope scope(env->isolate());
|
||||||
Context::Scope context_scope(env->context());
|
Context::Scope context_scope(env->context());
|
||||||
|
|
||||||
// The wrap object should still be there.
|
|
||||||
CHECK_EQ(wrap->persistent().IsEmpty(), false);
|
|
||||||
CHECK_EQ(wrap->state_, kClosing);
|
CHECK_EQ(wrap->state_, kClosing);
|
||||||
|
|
||||||
wrap->state_ = kClosed;
|
wrap->state_ = kClosed;
|
||||||
|
|
||||||
wrap->OnClose();
|
wrap->OnClose();
|
||||||
|
|
||||||
if (wrap->object()->Has(env->context(), env->handle_onclose_symbol())
|
if (!wrap->persistent().IsEmpty() &&
|
||||||
|
wrap->object()->Has(env->context(), env->handle_onclose_symbol())
|
||||||
.FromMaybe(false)) {
|
.FromMaybe(false)) {
|
||||||
wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
|
wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,6 +76,8 @@ class HandleWrap : public AsyncWrap {
|
||||||
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
|
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
|
||||||
Environment* env);
|
Environment* env);
|
||||||
|
|
||||||
|
void MakeWeak(); // This hides BaseObject::MakeWeak()
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
HandleWrap(Environment* env,
|
HandleWrap(Environment* env,
|
||||||
v8::Local<v8::Object> object,
|
v8::Local<v8::Object> object,
|
||||||
|
|
|
@ -477,31 +477,18 @@ static void ELDHistogramNew(const FunctionCallbackInfo<Value>& args) {
|
||||||
ELDHistogram::ELDHistogram(
|
ELDHistogram::ELDHistogram(
|
||||||
Environment* env,
|
Environment* env,
|
||||||
Local<Object> wrap,
|
Local<Object> wrap,
|
||||||
int32_t resolution) : BaseObject(env, wrap),
|
int32_t resolution) : HandleWrap(env,
|
||||||
|
wrap,
|
||||||
|
reinterpret_cast<uv_handle_t*>(&timer_),
|
||||||
|
AsyncWrap::PROVIDER_ELDHISTOGRAM),
|
||||||
Histogram(1, 3.6e12),
|
Histogram(1, 3.6e12),
|
||||||
resolution_(resolution) {
|
resolution_(resolution) {
|
||||||
MakeWeak();
|
MakeWeak();
|
||||||
timer_ = new uv_timer_t();
|
uv_timer_init(env->event_loop(), &timer_);
|
||||||
uv_timer_init(env->event_loop(), timer_);
|
|
||||||
timer_->data = this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ELDHistogram::CloseTimer() {
|
void ELDHistogram::DelayIntervalCallback(uv_timer_t* req) {
|
||||||
if (timer_ == nullptr)
|
ELDHistogram* histogram = ContainerOf(&ELDHistogram::timer_, req);
|
||||||
return;
|
|
||||||
|
|
||||||
env()->CloseHandle(timer_, [](uv_timer_t* handle) { delete handle; });
|
|
||||||
timer_ = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
ELDHistogram::~ELDHistogram() {
|
|
||||||
Disable();
|
|
||||||
CloseTimer();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ELDHistogramDelayInterval(uv_timer_t* req) {
|
|
||||||
ELDHistogram* histogram =
|
|
||||||
reinterpret_cast<ELDHistogram*>(req->data);
|
|
||||||
histogram->RecordDelta();
|
histogram->RecordDelta();
|
||||||
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
|
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
|
||||||
"min", histogram->Min());
|
"min", histogram->Min());
|
||||||
|
@ -537,21 +524,21 @@ bool ELDHistogram::RecordDelta() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ELDHistogram::Enable() {
|
bool ELDHistogram::Enable() {
|
||||||
if (enabled_) return false;
|
if (enabled_ || IsHandleClosing()) return false;
|
||||||
enabled_ = true;
|
enabled_ = true;
|
||||||
prev_ = 0;
|
prev_ = 0;
|
||||||
uv_timer_start(timer_,
|
uv_timer_start(&timer_,
|
||||||
ELDHistogramDelayInterval,
|
DelayIntervalCallback,
|
||||||
resolution_,
|
resolution_,
|
||||||
resolution_);
|
resolution_);
|
||||||
uv_unref(reinterpret_cast<uv_handle_t*>(timer_));
|
uv_unref(reinterpret_cast<uv_handle_t*>(&timer_));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ELDHistogram::Disable() {
|
bool ELDHistogram::Disable() {
|
||||||
if (!enabled_) return false;
|
if (!enabled_ || IsHandleClosing()) return false;
|
||||||
enabled_ = false;
|
enabled_ = false;
|
||||||
uv_timer_stop(timer_);
|
uv_timer_stop(&timer_);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -123,14 +123,12 @@ class GCPerformanceEntry : public PerformanceEntry {
|
||||||
PerformanceGCKind gckind_;
|
PerformanceGCKind gckind_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ELDHistogram : public BaseObject, public Histogram {
|
class ELDHistogram : public HandleWrap, public Histogram {
|
||||||
public:
|
public:
|
||||||
ELDHistogram(Environment* env,
|
ELDHistogram(Environment* env,
|
||||||
Local<Object> wrap,
|
Local<Object> wrap,
|
||||||
int32_t resolution);
|
int32_t resolution);
|
||||||
|
|
||||||
~ELDHistogram() override;
|
|
||||||
|
|
||||||
bool RecordDelta();
|
bool RecordDelta();
|
||||||
bool Enable();
|
bool Enable();
|
||||||
bool Disable();
|
bool Disable();
|
||||||
|
@ -149,13 +147,13 @@ class ELDHistogram : public BaseObject, public Histogram {
|
||||||
SET_SELF_SIZE(ELDHistogram)
|
SET_SELF_SIZE(ELDHistogram)
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CloseTimer();
|
static void DelayIntervalCallback(uv_timer_t* req);
|
||||||
|
|
||||||
bool enabled_ = false;
|
bool enabled_ = false;
|
||||||
int32_t resolution_ = 0;
|
int32_t resolution_ = 0;
|
||||||
int64_t exceeds_ = 0;
|
int64_t exceeds_ = 0;
|
||||||
uint64_t prev_ = 0;
|
uint64_t prev_ = 0;
|
||||||
uv_timer_t* timer_;
|
uv_timer_t timer_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace performance
|
} // namespace performance
|
||||||
|
|
|
@ -50,6 +50,7 @@ const { getSystemErrorName } = require('util');
|
||||||
delete providers.KEYPAIRGENREQUEST;
|
delete providers.KEYPAIRGENREQUEST;
|
||||||
delete providers.HTTPCLIENTREQUEST;
|
delete providers.HTTPCLIENTREQUEST;
|
||||||
delete providers.HTTPINCOMINGMESSAGE;
|
delete providers.HTTPINCOMINGMESSAGE;
|
||||||
|
delete providers.ELDHISTOGRAM;
|
||||||
|
|
||||||
const objKeys = Object.keys(providers);
|
const objKeys = Object.keys(providers);
|
||||||
if (objKeys.length > 0)
|
if (objKeys.length > 0)
|
||||||
|
|
Loading…
Reference in New Issue