From 19b671506cc44e1210f6fd6e8b3ac284f22cfdde Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 5 Mar 2019 02:32:08 +0100 Subject: [PATCH] src: move v8 stats buffers out of Environment Moves state that is specific to the `v8` binding into the `v8` binding implementation as a cleanup. PR-URL: https://github.com/nodejs/node/pull/32538 Reviewed-By: James M Snell --- src/env-inl.h | 33 -------------------- src/env.h | 18 +---------- src/node_v8.cc | 82 +++++++++++++++++++++++++++++++++----------------- 3 files changed, 55 insertions(+), 78 deletions(-) diff --git a/src/env-inl.h b/src/env-inl.h index 957649541da4c7..091b98d0adb51a 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -580,39 +580,6 @@ inline double Environment::get_default_trigger_async_id() { return default_trigger_async_id; } -inline double* Environment::heap_statistics_buffer() const { - CHECK_NOT_NULL(heap_statistics_buffer_); - return static_cast(heap_statistics_buffer_->Data()); -} - -inline void Environment::set_heap_statistics_buffer( - std::shared_ptr backing_store) { - CHECK(!heap_statistics_buffer_); // Should be set only once. - heap_statistics_buffer_ = std::move(backing_store); -} - -inline double* Environment::heap_space_statistics_buffer() const { - CHECK(heap_space_statistics_buffer_); - return static_cast(heap_space_statistics_buffer_->Data()); -} - -inline void Environment::set_heap_space_statistics_buffer( - std::shared_ptr backing_store) { - CHECK(!heap_space_statistics_buffer_); // Should be set only once. - heap_space_statistics_buffer_ = std::move(backing_store); -} - -inline double* Environment::heap_code_statistics_buffer() const { - CHECK(heap_code_statistics_buffer_); - return static_cast(heap_code_statistics_buffer_->Data()); -} - -inline void Environment::set_heap_code_statistics_buffer( - std::shared_ptr backing_store) { - CHECK(!heap_code_statistics_buffer_); // Should be set only once. - heap_code_statistics_buffer_ = std::move(backing_store); -} - inline char* Environment::http_parser_buffer() const { return http_parser_buffer_; } diff --git a/src/env.h b/src/env.h index 7c2ef3b502d8e4..7534907c0c6f0a 100644 --- a/src/env.h +++ b/src/env.h @@ -1007,18 +1007,6 @@ class Environment : public MemoryRetainer { inline uint32_t get_next_script_id(); inline uint32_t get_next_function_id(); - inline double* heap_statistics_buffer() const; - inline void set_heap_statistics_buffer( - std::shared_ptr backing_store); - - inline double* heap_space_statistics_buffer() const; - inline void set_heap_space_statistics_buffer( - std::shared_ptr backing_store); - - inline double* heap_code_statistics_buffer() const; - inline void set_heap_code_statistics_buffer( - std::shared_ptr backing_store); - inline char* http_parser_buffer() const; inline void set_http_parser_buffer(char* buffer); inline bool http_parser_buffer_in_use() const; @@ -1377,14 +1365,10 @@ class Environment : public MemoryRetainer { int handle_cleanup_waiting_ = 0; int request_waiting_ = 0; - std::shared_ptr heap_statistics_buffer_; - std::shared_ptr heap_space_statistics_buffer_; - std::shared_ptr heap_code_statistics_buffer_; - char* http_parser_buffer_ = nullptr; bool http_parser_buffer_in_use_ = false; - EnabledDebugList enabled_debug_list_; + AliasedFloat64Array fs_stats_field_array_; AliasedBigUint64Array fs_stats_field_bigint_array_; diff --git a/src/node_v8.cc b/src/node_v8.cc index a59c2170dae068..a0f477430419f9 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -20,7 +20,9 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. #include "node.h" +#include "base_object-inl.h" #include "env-inl.h" +#include "memory_tracker-inl.h" #include "util-inl.h" #include "v8.h" @@ -28,6 +30,7 @@ namespace node { using v8::Array; using v8::ArrayBuffer; +using v8::BackingStore; using v8::Context; using v8::FunctionCallbackInfo; using v8::HeapCodeStatistics; @@ -59,7 +62,7 @@ using v8::Value; V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex) #define V(a, b, c) +1 -static const size_t kHeapStatisticsPropertiesCount = +static constexpr size_t kHeapStatisticsPropertiesCount = HEAP_STATISTICS_PROPERTIES(V); #undef V @@ -71,10 +74,28 @@ static const size_t kHeapStatisticsPropertiesCount = V(3, physical_space_size, kPhysicalSpaceSizeIndex) #define V(a, b, c) +1 -static const size_t kHeapSpaceStatisticsPropertiesCount = +static constexpr size_t kHeapSpaceStatisticsPropertiesCount = HEAP_SPACE_STATISTICS_PROPERTIES(V); #undef V +class BindingData : public BaseObject { + public: + BindingData(Environment* env, Local obj) : BaseObject(env, obj) {} + + std::shared_ptr heap_statistics_buffer; + std::shared_ptr heap_space_statistics_buffer; + std::shared_ptr heap_code_statistics_buffer; + + void MemoryInfo(MemoryTracker* tracker) const override { + tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer); + tracker->TrackField("heap_space_statistics_buffer", + heap_space_statistics_buffer); + tracker->TrackField("heap_code_statistics_buffer", + heap_code_statistics_buffer); + } + SET_SELF_SIZE(BindingData) + SET_MEMORY_INFO_NAME(BindingData) +}; #define HEAP_CODE_STATISTICS_PROPERTIES(V) \ V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \ @@ -96,10 +117,11 @@ void CachedDataVersionTag(const FunctionCallbackInfo& args) { void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); + BindingData* data = Unwrap(args.Data()); HeapStatistics s; - env->isolate()->GetHeapStatistics(&s); - double* const buffer = env->heap_statistics_buffer(); + args.GetIsolate()->GetHeapStatistics(&s); + double* const buffer = + static_cast(data->heap_statistics_buffer->Data()); #define V(index, name, _) buffer[index] = static_cast(s.name()); HEAP_STATISTICS_PROPERTIES(V) #undef V @@ -107,18 +129,20 @@ void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo& args) { void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); + BindingData* data = Unwrap(args.Data()); HeapSpaceStatistics s; - Isolate* const isolate = env->isolate(); - double* buffer = env->heap_space_statistics_buffer(); - size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces(); + Isolate* const isolate = args.GetIsolate(); + size_t number_of_heap_spaces = isolate->NumberOfHeapSpaces(); + + double* const buffer = + static_cast(data->heap_space_statistics_buffer->Data()); for (size_t i = 0; i < number_of_heap_spaces; i++) { isolate->GetHeapSpaceStatistics(&s, i); size_t const property_offset = i * kHeapSpaceStatisticsPropertiesCount; -#define V(index, name, _) buffer[property_offset + index] = \ - static_cast(s.name()); - HEAP_SPACE_STATISTICS_PROPERTIES(V) +#define V(index, name, _) \ + buffer[property_offset + index] = static_cast(s.name()); + HEAP_SPACE_STATISTICS_PROPERTIES(V) #undef V } } @@ -126,10 +150,11 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo& args) { void UpdateHeapCodeStatisticsArrayBuffer( const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); + BindingData* data = Unwrap(args.Data()); HeapCodeStatistics s; - env->isolate()->GetHeapCodeAndMetadataStatistics(&s); - double* const buffer = env->heap_code_statistics_buffer(); + args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s); + double* const buffer = + static_cast(data->heap_code_statistics_buffer->Data()); #define V(index, name, _) buffer[index] = static_cast(s.name()); HEAP_CODE_STATISTICS_PROPERTIES(V) #undef V @@ -148,6 +173,9 @@ void Initialize(Local target, Local context, void* priv) { Environment* env = Environment::GetCurrent(context); + Environment::BindingScope binding_scope(env); + if (!binding_scope) return; + BindingData* binding_data = binding_scope.data; env->SetMethodNoSideEffect(target, "cachedDataVersionTag", CachedDataVersionTag); @@ -158,11 +186,11 @@ void Initialize(Local target, UpdateHeapStatisticsArrayBuffer); const size_t heap_statistics_buffer_byte_length = - sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount; + sizeof(double) * kHeapStatisticsPropertiesCount; Local heap_statistics_ab = ArrayBuffer::New(env->isolate(), heap_statistics_buffer_byte_length); - env->set_heap_statistics_buffer(heap_statistics_ab->GetBackingStore()); + binding_data->heap_statistics_buffer = heap_statistics_ab->GetBackingStore(); target->Set(env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsArrayBuffer"), @@ -182,19 +210,17 @@ void Initialize(Local target, UpdateHeapCodeStatisticsArrayBuffer); const size_t heap_code_statistics_buffer_byte_length = - sizeof(*env->heap_code_statistics_buffer()) - * kHeapCodeStatisticsPropertiesCount; + sizeof(double) * kHeapCodeStatisticsPropertiesCount; Local heap_code_statistics_ab = ArrayBuffer::New(env->isolate(), heap_code_statistics_buffer_byte_length); - env->set_heap_code_statistics_buffer( - heap_code_statistics_ab->GetBackingStore()); + binding_data->heap_code_statistics_buffer = + heap_code_statistics_ab->GetBackingStore(); target->Set(env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsArrayBuffer"), - heap_code_statistics_ab) - .Check(); + heap_code_statistics_ab).Check(); #define V(i, _, name) \ target->Set(env->context(), \ @@ -236,20 +262,20 @@ void Initialize(Local target, UpdateHeapSpaceStatisticsBuffer); const size_t heap_space_statistics_buffer_byte_length = - sizeof(*env->heap_space_statistics_buffer()) * + sizeof(double) * kHeapSpaceStatisticsPropertiesCount * number_of_heap_spaces; Local heap_space_statistics_ab = ArrayBuffer::New(env->isolate(), heap_space_statistics_buffer_byte_length); - env->set_heap_space_statistics_buffer( - heap_space_statistics_ab->GetBackingStore()); + binding_data->heap_space_statistics_buffer = + heap_space_statistics_ab->GetBackingStore(); + target->Set(env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "heapSpaceStatisticsArrayBuffer"), - heap_space_statistics_ab) - .Check(); + heap_space_statistics_ab).Check(); #define V(i, _, name) \ target->Set(env->context(), \