diff --git a/lib/v8.js b/lib/v8.js index 5841f204986fd7..11ca7fa640f27b 100644 --- a/lib/v8.js +++ b/lib/v8.js @@ -73,12 +73,12 @@ class Deserializer extends _Deserializer { } const { cachedDataVersionTag, setFlagsFromString: _setFlagsFromString, - heapStatisticsArrayBuffer, - heapSpaceStatisticsArrayBuffer, - heapCodeStatisticsArrayBuffer, - updateHeapStatisticsArrayBuffer, - updateHeapSpaceStatisticsArrayBuffer, - updateHeapCodeStatisticsArrayBuffer, + heapStatisticsBuffer, + heapSpaceStatisticsBuffer, + heapCodeStatisticsBuffer, + updateHeapStatisticsBuffer, + updateHeapSpaceStatisticsBuffer, + updateHeapCodeStatisticsBuffer, // Properties for heap statistics buffer extraction. kTotalHeapSizeIndex, @@ -95,7 +95,6 @@ const { // Properties for heap spaces statistics buffer extraction. kHeapSpaces, - kHeapSpaceStatisticsPropertiesCount, kSpaceSizeIndex, kSpaceUsedSizeIndex, kSpaceAvailableSizeIndex, @@ -109,15 +108,6 @@ const { const kNumberOfHeapSpaces = kHeapSpaces.length; -const heapStatisticsBuffer = - new Float64Array(heapStatisticsArrayBuffer); - -const heapSpaceStatisticsBuffer = - new Float64Array(heapSpaceStatisticsArrayBuffer); - -const heapCodeStatisticsBuffer = - new Float64Array(heapCodeStatisticsArrayBuffer); - function setFlagsFromString(flags) { validateString(flags, 'flags'); _setFlagsFromString(flags); @@ -126,7 +116,7 @@ function setFlagsFromString(flags) { function getHeapStatistics() { const buffer = heapStatisticsBuffer; - updateHeapStatisticsArrayBuffer(); + updateHeapStatisticsBuffer(); return { 'total_heap_size': buffer[kTotalHeapSizeIndex], @@ -146,16 +136,15 @@ function getHeapStatistics() { function getHeapSpaceStatistics() { const heapSpaceStatistics = new Array(kNumberOfHeapSpaces); const buffer = heapSpaceStatisticsBuffer; - updateHeapSpaceStatisticsArrayBuffer(); for (let i = 0; i < kNumberOfHeapSpaces; i++) { - const propertyOffset = i * kHeapSpaceStatisticsPropertiesCount; + updateHeapSpaceStatisticsBuffer(i); heapSpaceStatistics[i] = { space_name: kHeapSpaces[i], - space_size: buffer[propertyOffset + kSpaceSizeIndex], - space_used_size: buffer[propertyOffset + kSpaceUsedSizeIndex], - space_available_size: buffer[propertyOffset + kSpaceAvailableSizeIndex], - physical_space_size: buffer[propertyOffset + kPhysicalSpaceSizeIndex] + space_size: buffer[kSpaceSizeIndex], + space_used_size: buffer[kSpaceUsedSizeIndex], + space_available_size: buffer[kSpaceAvailableSizeIndex], + physical_space_size: buffer[kPhysicalSpaceSizeIndex] }; } @@ -165,7 +154,7 @@ function getHeapSpaceStatistics() { function getHeapCodeStatistics() { const buffer = heapCodeStatisticsBuffer; - updateHeapCodeStatisticsArrayBuffer(); + updateHeapCodeStatisticsBuffer(); return { 'code_and_metadata_size': buffer[kCodeAndMetadataSizeIndex], 'bytecode_and_metadata_size': buffer[kBytecodeAndMetadataSizeIndex], diff --git a/src/node_v8.cc b/src/node_v8.cc index a0f477430419f9..7174d9ae7f830b 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -29,8 +29,6 @@ namespace node { using v8::Array; -using v8::ArrayBuffer; -using v8::BackingStore; using v8::Context; using v8::FunctionCallbackInfo; using v8::HeapCodeStatistics; @@ -78,13 +76,29 @@ static constexpr size_t kHeapSpaceStatisticsPropertiesCount = HEAP_SPACE_STATISTICS_PROPERTIES(V); #undef V +#define HEAP_CODE_STATISTICS_PROPERTIES(V) \ + V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \ + V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex) \ + V(2, external_script_source_size, kExternalScriptSourceSizeIndex) + +#define V(a, b, c) +1 +static const size_t kHeapCodeStatisticsPropertiesCount = + HEAP_CODE_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; + BindingData(Environment* env, Local obj) + : BaseObject(env, obj), + heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount), + heap_space_statistics_buffer(env->isolate(), + kHeapSpaceStatisticsPropertiesCount), + heap_code_statistics_buffer(env->isolate(), + kHeapCodeStatisticsPropertiesCount) {} + + AliasedFloat64Array heap_statistics_buffer; + AliasedFloat64Array heap_space_statistics_buffer; + AliasedFloat64Array heap_code_statistics_buffer; void MemoryInfo(MemoryTracker* tracker) const override { tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer); @@ -97,15 +111,6 @@ class BindingData : public BaseObject { SET_MEMORY_INFO_NAME(BindingData) }; -#define HEAP_CODE_STATISTICS_PROPERTIES(V) \ - V(0, code_and_metadata_size, kCodeAndMetadataSizeIndex) \ - V(1, bytecode_and_metadata_size, kBytecodeAndMetadataSizeIndex) \ - V(2, external_script_source_size, kExternalScriptSourceSizeIndex) - -#define V(a, b, c) +1 -static const size_t kHeapCodeStatisticsPropertiesCount = - HEAP_CODE_STATISTICS_PROPERTIES(V); -#undef V void CachedDataVersionTag(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); @@ -115,13 +120,11 @@ void CachedDataVersionTag(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(result); } - -void UpdateHeapStatisticsArrayBuffer(const FunctionCallbackInfo& args) { +void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo& args) { BindingData* data = Unwrap(args.Data()); HeapStatistics s; args.GetIsolate()->GetHeapStatistics(&s); - double* const buffer = - static_cast(data->heap_statistics_buffer->Data()); + AliasedFloat64Array& buffer = data->heap_statistics_buffer; #define V(index, name, _) buffer[index] = static_cast(s.name()); HEAP_STATISTICS_PROPERTIES(V) #undef V @@ -132,29 +135,23 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo& args) { BindingData* data = Unwrap(args.Data()); HeapSpaceStatistics s; Isolate* const isolate = args.GetIsolate(); - size_t number_of_heap_spaces = isolate->NumberOfHeapSpaces(); + CHECK(args[0]->IsUint32()); + size_t space_index = static_cast(args[0].As()->Value()); + isolate->GetHeapSpaceStatistics(&s, space_index); - double* const buffer = - static_cast(data->heap_space_statistics_buffer->Data()); + AliasedFloat64Array& buffer = data->heap_space_statistics_buffer; - 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[index] = static_cast(s.name()); + HEAP_SPACE_STATISTICS_PROPERTIES(V) #undef V - } } - -void UpdateHeapCodeStatisticsArrayBuffer( - const FunctionCallbackInfo& args) { +void UpdateHeapCodeStatisticsBuffer(const FunctionCallbackInfo& args) { BindingData* data = Unwrap(args.Data()); HeapCodeStatistics s; args.GetIsolate()->GetHeapCodeAndMetadataStatistics(&s); - double* const buffer = - static_cast(data->heap_code_statistics_buffer->Data()); + AliasedFloat64Array& buffer = data->heap_code_statistics_buffer; + #define V(index, name, _) buffer[index] = static_cast(s.name()); HEAP_CODE_STATISTICS_PROPERTIES(V) #undef V @@ -181,20 +178,14 @@ void Initialize(Local target, CachedDataVersionTag); // Export symbols used by v8.getHeapStatistics() - env->SetMethod(target, - "updateHeapStatisticsArrayBuffer", - UpdateHeapStatisticsArrayBuffer); + env->SetMethod( + target, "updateHeapStatisticsBuffer", UpdateHeapStatisticsBuffer); - const size_t heap_statistics_buffer_byte_length = - sizeof(double) * kHeapStatisticsPropertiesCount; - - Local heap_statistics_ab = - ArrayBuffer::New(env->isolate(), heap_statistics_buffer_byte_length); - binding_data->heap_statistics_buffer = heap_statistics_ab->GetBackingStore(); - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), - "heapStatisticsArrayBuffer"), - heap_statistics_ab).Check(); + target + ->Set(env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"), + binding_data->heap_statistics_buffer.GetJSArray()) + .Check(); #define V(i, _, name) \ target->Set(env->context(), \ @@ -205,22 +196,14 @@ void Initialize(Local target, #undef V // Export symbols used by v8.getHeapCodeStatistics() - env->SetMethod(target, - "updateHeapCodeStatisticsArrayBuffer", - UpdateHeapCodeStatisticsArrayBuffer); - - const size_t heap_code_statistics_buffer_byte_length = - sizeof(double) * kHeapCodeStatisticsPropertiesCount; + env->SetMethod( + target, "updateHeapCodeStatisticsBuffer", UpdateHeapCodeStatisticsBuffer); - Local heap_code_statistics_ab = - ArrayBuffer::New(env->isolate(), - heap_code_statistics_buffer_byte_length); - 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(); + target + ->Set(env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"), + binding_data->heap_code_statistics_buffer.GetJSArray()) + .Check(); #define V(i, _, name) \ target->Set(env->context(), \ @@ -230,14 +213,6 @@ void Initialize(Local target, HEAP_CODE_STATISTICS_PROPERTIES(V) #undef V - // Export symbols used by v8.getHeapSpaceStatistics() - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), - "kHeapSpaceStatisticsPropertiesCount"), - Uint32::NewFromUnsigned(env->isolate(), - kHeapSpaceStatisticsPropertiesCount)) - .Check(); - size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces(); // Heap space names are extracted once and exposed to JavaScript to @@ -258,24 +233,15 @@ void Initialize(Local target, number_of_heap_spaces)).Check(); env->SetMethod(target, - "updateHeapSpaceStatisticsArrayBuffer", + "updateHeapSpaceStatisticsBuffer", UpdateHeapSpaceStatisticsBuffer); - const size_t heap_space_statistics_buffer_byte_length = - sizeof(double) * - kHeapSpaceStatisticsPropertiesCount * - number_of_heap_spaces; - - Local heap_space_statistics_ab = - ArrayBuffer::New(env->isolate(), - heap_space_statistics_buffer_byte_length); - 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(); + target + ->Set(env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), + "heapSpaceStatisticsBuffer"), + binding_data->heap_space_statistics_buffer.GetJSArray()) + .Check(); #define V(i, _, name) \ target->Set(env->context(), \