Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit f787f88

Browse files
committed
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: nodejs/node#29317 Reviewed-By: James M Snell <[email protected]>
1 parent 9beec73 commit f787f88

File tree

6 files changed

+33
-34
lines changed

6 files changed

+33
-34
lines changed

src/async_wrap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace node {
3434
#define NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \
3535
V(NONE) \
3636
V(DNSCHANNEL) \
37+
V(ELDHISTOGRAM) \
3738
V(FILEHANDLE) \
3839
V(FILEHANDLECLOSEREQ) \
3940
V(FSEVENTWRAP) \

src/handle_wrap.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ void HandleWrap::Close(Local<Value> close_callback) {
8484
}
8585

8686

87+
void HandleWrap::MakeWeak() {
88+
persistent().SetWeak(
89+
this,
90+
[](const v8::WeakCallbackInfo<HandleWrap>& data) {
91+
HandleWrap* handle_wrap = data.GetParameter();
92+
handle_wrap->persistent().Reset();
93+
handle_wrap->Close();
94+
}, v8::WeakCallbackType::kParameter);
95+
}
96+
97+
8798
void HandleWrap::MarkAsInitialized() {
8899
env()->handle_wrap_queue()->PushBack(this);
89100
state_ = kInitialized;
@@ -119,15 +130,14 @@ void HandleWrap::OnClose(uv_handle_t* handle) {
119130
HandleScope scope(env->isolate());
120131
Context::Scope context_scope(env->context());
121132

122-
// The wrap object should still be there.
123-
CHECK_EQ(wrap->persistent().IsEmpty(), false);
124133
CHECK_EQ(wrap->state_, kClosing);
125134

126135
wrap->state_ = kClosed;
127136

128137
wrap->OnClose();
129138

130-
if (wrap->object()->Has(env->context(), env->handle_onclose_symbol())
139+
if (!wrap->persistent().IsEmpty() &&
140+
wrap->object()->Has(env->context(), env->handle_onclose_symbol())
131141
.FromMaybe(false)) {
132142
wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
133143
}

src/handle_wrap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class HandleWrap : public AsyncWrap {
7676
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
7777
Environment* env);
7878

79+
void MakeWeak(); // This hides BaseObject::MakeWeak()
80+
7981
protected:
8082
HandleWrap(Environment* env,
8183
v8::Local<v8::Object> object,

src/node_perf.cc

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -467,31 +467,18 @@ static void ELDHistogramNew(const FunctionCallbackInfo<Value>& args) {
467467
ELDHistogram::ELDHistogram(
468468
Environment* env,
469469
Local<Object> wrap,
470-
int32_t resolution) : BaseObject(env, wrap),
470+
int32_t resolution) : HandleWrap(env,
471+
wrap,
472+
reinterpret_cast<uv_handle_t*>(&timer_),
473+
AsyncWrap::PROVIDER_ELDHISTOGRAM),
471474
Histogram(1, 3.6e12),
472475
resolution_(resolution) {
473476
MakeWeak();
474-
timer_ = new uv_timer_t();
475-
uv_timer_init(env->event_loop(), timer_);
476-
timer_->data = this;
477+
uv_timer_init(env->event_loop(), &timer_);
477478
}
478479

479-
void ELDHistogram::CloseTimer() {
480-
if (timer_ == nullptr)
481-
return;
482-
483-
env()->CloseHandle(timer_, [](uv_timer_t* handle) { delete handle; });
484-
timer_ = nullptr;
485-
}
486-
487-
ELDHistogram::~ELDHistogram() {
488-
Disable();
489-
CloseTimer();
490-
}
491-
492-
void ELDHistogramDelayInterval(uv_timer_t* req) {
493-
ELDHistogram* histogram =
494-
reinterpret_cast<ELDHistogram*>(req->data);
480+
void ELDHistogram::DelayIntervalCallback(uv_timer_t* req) {
481+
ELDHistogram* histogram = ContainerOf(&ELDHistogram::timer_, req);
495482
histogram->RecordDelta();
496483
TRACE_COUNTER1(TRACING_CATEGORY_NODE2(perf, event_loop),
497484
"min", histogram->Min());
@@ -527,21 +514,21 @@ bool ELDHistogram::RecordDelta() {
527514
}
528515

529516
bool ELDHistogram::Enable() {
530-
if (enabled_) return false;
517+
if (enabled_ || IsHandleClosing()) return false;
531518
enabled_ = true;
532519
prev_ = 0;
533-
uv_timer_start(timer_,
534-
ELDHistogramDelayInterval,
520+
uv_timer_start(&timer_,
521+
DelayIntervalCallback,
535522
resolution_,
536523
resolution_);
537-
uv_unref(reinterpret_cast<uv_handle_t*>(timer_));
524+
uv_unref(reinterpret_cast<uv_handle_t*>(&timer_));
538525
return true;
539526
}
540527

541528
bool ELDHistogram::Disable() {
542-
if (!enabled_) return false;
529+
if (!enabled_ || IsHandleClosing()) return false;
543530
enabled_ = false;
544-
uv_timer_stop(timer_);
531+
uv_timer_stop(&timer_);
545532
return true;
546533
}
547534

src/node_perf.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,12 @@ class GCPerformanceEntry : public PerformanceEntry {
123123
PerformanceGCKind gckind_;
124124
};
125125

126-
class ELDHistogram : public BaseObject, public Histogram {
126+
class ELDHistogram : public HandleWrap, public Histogram {
127127
public:
128128
ELDHistogram(Environment* env,
129129
Local<Object> wrap,
130130
int32_t resolution);
131131

132-
~ELDHistogram() override;
133-
134132
bool RecordDelta();
135133
bool Enable();
136134
bool Disable();
@@ -149,13 +147,13 @@ class ELDHistogram : public BaseObject, public Histogram {
149147
SET_SELF_SIZE(ELDHistogram)
150148

151149
private:
152-
void CloseTimer();
150+
static void DelayIntervalCallback(uv_timer_t* req);
153151

154152
bool enabled_ = false;
155153
int32_t resolution_ = 0;
156154
int64_t exceeds_ = 0;
157155
uint64_t prev_ = 0;
158-
uv_timer_t* timer_;
156+
uv_timer_t timer_;
159157
};
160158

161159
} // namespace performance

test/sequential/test-async-wrap-getasyncid.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const { getSystemErrorName } = require('util');
5555
delete providers.KEYPAIRGENREQUEST;
5656
delete providers.HTTPCLIENTREQUEST;
5757
delete providers.HTTPINCOMINGMESSAGE;
58+
delete providers.ELDHISTOGRAM;
5859

5960
const objKeys = Object.keys(providers);
6061
if (objKeys.length > 0)

0 commit comments

Comments
 (0)