From be2cbccf003d110cad00317090072788021efa56 Mon Sep 17 00:00:00 2001 From: Kyle Farnung Date: Mon, 22 Jan 2018 11:43:35 -0800 Subject: [PATCH] http2,perf_hooks: perf state using AliasedBuffer Update performance_state to use AliasedBuffer and update usage sites. PR-URL: https://github.com/nodejs/node/pull/18300 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- src/env-inl.h | 5 ++--- src/env.h | 4 ++-- src/node_http2.cc | 3 ++- src/node_perf.cc | 35 +++++++++++++++++------------------ src/node_perf_common.h | 31 +++++++++++++++++++++++++++---- 5 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/env-inl.h b/src/env-inl.h index 4a6e73cc38807e..2a004f58153c21 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -344,7 +344,7 @@ inline Environment::Environment(IsolateData* isolate_data, AssignToContext(context, ContextInfo("")); destroy_async_id_list_.reserve(512); - performance_state_ = Calloc(1); + performance_state_.reset(new performance::performance_state(isolate())); performance_state_->milestones[ performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT] = PERFORMANCE_NOW(); @@ -377,7 +377,6 @@ inline Environment::~Environment() { delete[] heap_statistics_buffer_; delete[] heap_space_statistics_buffer_; delete[] http_parser_buffer_; - free(performance_state_); } inline v8::Isolate* Environment::isolate() const { @@ -583,7 +582,7 @@ void Environment::SetUnrefImmediate(native_immediate_callback cb, } inline performance::performance_state* Environment::performance_state() { - return performance_state_; + return performance_state_.get(); } inline std::map* Environment::performance_marks() { diff --git a/src/env.h b/src/env.h index a1f505c4fe1da1..198cda1d52caa1 100644 --- a/src/env.h +++ b/src/env.h @@ -48,7 +48,7 @@ struct nghttp2_rcbuf; namespace node { namespace performance { -struct performance_state; +class performance_state; } namespace loader { @@ -758,7 +758,7 @@ class Environment { int should_not_abort_scope_counter_ = 0; - performance::performance_state* performance_state_ = nullptr; + std::unique_ptr performance_state_; std::map performance_marks_; #if HAVE_INSPECTOR diff --git a/src/node_http2.cc b/src/node_http2.cc index cd28f57ffe2d06..bd7eeee8655e52 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -554,7 +554,8 @@ Http2Session::~Http2Session() { } inline bool HasHttp2Observer(Environment* env) { - uint32_t* observers = env->performance_state()->observers; + AliasedBuffer& observers = + env->performance_state()->observers; return observers[performance::NODE_PERFORMANCE_ENTRY_TYPE_HTTP2] != 0; } diff --git a/src/node_perf.cc b/src/node_perf.cc index 34e6a35000ef1d..4c3dfbe4eb7208 100644 --- a/src/node_perf.cc +++ b/src/node_perf.cc @@ -85,9 +85,9 @@ void PerformanceEntry::Notify(Environment* env, PerformanceEntryType type, Local object) { Context::Scope scope(env->context()); - uint32_t* observers = env->performance_state()->observers; - if (observers != nullptr && - type != NODE_PERFORMANCE_ENTRY_TYPE_INVALID && + AliasedBuffer& observers = + env->performance_state()->observers; + if (type != NODE_PERFORMANCE_ENTRY_TYPE_INVALID && observers[type]) { node::MakeCallback(env->isolate(), env->process_object(), @@ -130,7 +130,8 @@ void Measure(const FunctionCallbackInfo& args) { Utf8Value startMark(env->isolate(), args[1]); Utf8Value endMark(env->isolate(), args[2]); - double* milestones = env->performance_state()->milestones; + AliasedBuffer& milestones = + env->performance_state()->milestones; uint64_t startTimestamp = timeOrigin; uint64_t start = GetPerformanceMark(env, *startMark); @@ -165,7 +166,8 @@ void Measure(const FunctionCallbackInfo& args) { void MarkMilestone(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); Local context = env->context(); - double* milestones = env->performance_state()->milestones; + AliasedBuffer& milestones = + env->performance_state()->milestones; PerformanceMilestone milestone = static_cast( args[0]->Int32Value(context).ToChecked()); @@ -187,7 +189,8 @@ void PerformanceGCCallback(Environment* env, void* ptr) { HandleScope scope(env->isolate()); Local context = env->context(); - uint32_t* observers = env->performance_state()->observers; + AliasedBuffer& observers = + env->performance_state()->observers; if (observers[NODE_PERFORMANCE_ENTRY_TYPE_GC]) { Local obj = entry->ToObject(); v8::PropertyAttribute attr = @@ -289,8 +292,8 @@ void TimerFunctionCall(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(ret.ToLocalChecked()); } - - uint32_t* observers = env->performance_state()->observers; + AliasedBuffer& observers = + env->performance_state()->observers; if (!observers[NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION]) return; @@ -323,16 +326,12 @@ void Init(Local target, performance_state* state = env->performance_state(); auto state_ab = ArrayBuffer::New(isolate, state, sizeof(*state)); - #define SET_STATE_TYPEDARRAY(name, type, field) \ - target->Set(context, \ - FIXED_ONE_BYTE_STRING(isolate, (name)), \ - type::New(state_ab, \ - offsetof(performance_state, field), \ - arraysize(state->field))) \ - .FromJust() - SET_STATE_TYPEDARRAY("observerCounts", v8::Uint32Array, observers); - SET_STATE_TYPEDARRAY("milestones", v8::Float64Array, milestones); - #undef SET_STATE_TYPEDARRAY + target->Set(context, + FIXED_ONE_BYTE_STRING(isolate, "observerCounts"), + state->observers.GetJSArray()).FromJust(); + target->Set(context, + FIXED_ONE_BYTE_STRING(isolate, "milestones"), + state->milestones.GetJSArray()).FromJust(); Local performanceEntryString = FIXED_ONE_BYTE_STRING(isolate, "PerformanceEntry"); diff --git a/src/node_perf_common.h b/src/node_perf_common.h index 713f126d7f70d9..435a4cffe5a753 100644 --- a/src/node_perf_common.h +++ b/src/node_perf_common.h @@ -61,10 +61,33 @@ enum PerformanceEntryType { node::performance::NODE_PERFORMANCE_MILESTONE_##n); \ } while (0); -struct performance_state { - // doubles first so that they are always sizeof(double)-aligned - double milestones[NODE_PERFORMANCE_MILESTONE_INVALID]; - uint32_t observers[NODE_PERFORMANCE_ENTRY_TYPE_INVALID]; +class performance_state { + public: + explicit performance_state(v8::Isolate* isolate) : + root( + isolate, + sizeof(performance_state_internal)), + milestones( + isolate, + offsetof(performance_state_internal, milestones), + NODE_PERFORMANCE_MILESTONE_INVALID, + root), + observers( + isolate, + offsetof(performance_state_internal, observers), + NODE_PERFORMANCE_ENTRY_TYPE_INVALID, + root) {} + + AliasedBuffer root; + AliasedBuffer milestones; + AliasedBuffer observers; + + private: + struct performance_state_internal { + // doubles first so that they are always sizeof(double)-aligned + double milestones[NODE_PERFORMANCE_MILESTONE_INVALID]; + uint32_t observers[NODE_PERFORMANCE_ENTRY_TYPE_INVALID]; + }; }; } // namespace performance