diff --git a/common.gypi b/common.gypi index b915af23ad13a8..cefbc4318b7f7e 100644 --- a/common.gypi +++ b/common.gypi @@ -38,7 +38,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.14', + 'v8_embedder_string': '-node.15', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/include/v8-isolate.h b/deps/v8/include/v8-isolate.h index 16c61f605f9909..7ca0e7f618c21f 100644 --- a/deps/v8/include/v8-isolate.h +++ b/deps/v8/include/v8-isolate.h @@ -1001,6 +1001,13 @@ class V8_EXPORT Isolate { */ void GetHeapStatistics(HeapStatistics* heap_statistics); + /** + * Get total allocated bytes since isolate creation. + * This should be used only by Node.JS, since it's a temporary method + * to avoid breaking ABI on HeapStatistics. + */ + uint64_t GetTotalAllocatedBytes(); + /** * Returns the number of spaces in the heap. */ diff --git a/deps/v8/include/v8-statistics.h b/deps/v8/include/v8-statistics.h index 2dc10e0fbfd304..82b78f5ec65729 100644 --- a/deps/v8/include/v8-statistics.h +++ b/deps/v8/include/v8-statistics.h @@ -154,13 +154,6 @@ class V8_EXPORT HeapStatistics { size_t number_of_native_contexts() { return number_of_native_contexts_; } size_t number_of_detached_contexts() { return number_of_detached_contexts_; } - /** - * Returns the total number of bytes allocated since the Isolate was created. - * This includes all heap objects allocated in any space (new, old, code, - * etc.). - */ - uint64_t total_allocated_bytes() { return total_allocated_bytes_; } - /** * Returns a 0/1 boolean, which signifies whether the V8 overwrite heap * garbage with a bit pattern. @@ -182,7 +175,6 @@ class V8_EXPORT HeapStatistics { size_t number_of_detached_contexts_; size_t total_global_handles_size_; size_t used_global_handles_size_; - uint64_t total_allocated_bytes_; friend class V8; friend class Isolate; diff --git a/deps/v8/src/api/api.cc b/deps/v8/src/api/api.cc index b4a3fb1230a205..551fc1a2bc724c 100644 --- a/deps/v8/src/api/api.cc +++ b/deps/v8/src/api/api.cc @@ -6584,8 +6584,7 @@ HeapStatistics::HeapStatistics() peak_malloced_memory_(0), does_zap_garbage_(false), number_of_native_contexts_(0), - number_of_detached_contexts_(0), - total_allocated_bytes_(0) {} + number_of_detached_contexts_(0) {} HeapSpaceStatistics::HeapSpaceStatistics() : space_name_(nullptr), @@ -10439,7 +10438,6 @@ void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) { heap_statistics->number_of_native_contexts_ = heap->NumberOfNativeContexts(); heap_statistics->number_of_detached_contexts_ = heap->NumberOfDetachedContexts(); - heap_statistics->total_allocated_bytes_ = heap->GetTotalAllocatedBytes(); heap_statistics->does_zap_garbage_ = i::heap::ShouldZapGarbage(); #if V8_ENABLE_WEBASSEMBLY @@ -10450,6 +10448,11 @@ void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) { #endif // V8_ENABLE_WEBASSEMBLY } +uint64_t Isolate::GetTotalAllocatedBytes() { + i::Isolate* i_isolate = reinterpret_cast(this); + return i_isolate->heap()->GetTotalAllocatedBytes(); +} + size_t Isolate::NumberOfHeapSpaces() { return i::LAST_SPACE - i::FIRST_SPACE + 1; } diff --git a/deps/v8/test/cctest/test-api.cc b/deps/v8/test/cctest/test-api.cc index 8340387c1b42e7..0c6a25de324315 100644 --- a/deps/v8/test/cctest/test-api.cc +++ b/deps/v8/test/cctest/test-api.cc @@ -17656,152 +17656,6 @@ TEST(GetHeapSpaceStatistics) { CHECK_EQ(total_physical_size, heap_statistics.total_physical_size()); } -UNINITIALIZED_TEST(GetHeapTotalAllocatedBytes) { - // This test is incompatible with concurrent allocation, which may occur - // while collecting the statistics and break the final `CHECK_EQ`s. - if (i::v8_flags.stress_concurrent_allocation) return; - - v8::Isolate::CreateParams create_params; - create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); - v8::Isolate* isolate = v8::Isolate::New(create_params); - - const uint32_t number_of_elements = 1; - const uint32_t allocation_size = i::FixedArray::SizeFor(number_of_elements); - const uint32_t trusted_allocation_size = - i::TrustedFixedArray::SizeFor(number_of_elements); - const uint32_t lo_number_of_elements = 256 * 1024; - const uint32_t lo_allocation_size = - i::FixedArray::SizeFor(lo_number_of_elements); - const uint32_t trusted_lo_allocation_size = - i::TrustedFixedArray::SizeFor(lo_number_of_elements); - const uint32_t expected_allocation_size = - allocation_size * 2 + lo_allocation_size * 2 + trusted_allocation_size + - trusted_lo_allocation_size; - - { - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - LocalContext env(isolate); - i::Isolate* i_isolate = reinterpret_cast(isolate); - - v8::HeapStatistics heap_stats_before; - isolate->GetHeapStatistics(&heap_stats_before); - size_t initial_allocated = heap_stats_before.total_allocated_bytes(); - - i::MaybeHandle young_alloc = - i_isolate->factory()->TryNewFixedArray(number_of_elements, - i::AllocationType::kYoung); - USE(young_alloc); - i::MaybeHandle old_alloc = - i_isolate->factory()->TryNewFixedArray(number_of_elements, - i::AllocationType::kOld); - USE(old_alloc); - i::Handle trusted_alloc = - i_isolate->factory()->NewTrustedFixedArray(number_of_elements, - i::AllocationType::kTrusted); - USE(trusted_alloc); - i::MaybeHandle old_lo_alloc = - i_isolate->factory()->TryNewFixedArray(lo_number_of_elements, - i::AllocationType::kOld); - USE(old_lo_alloc); - - { - v8::HandleScope inner_handle_scope(isolate); - auto young_lo_alloc = i_isolate->factory()->TryNewFixedArray( - lo_number_of_elements, i::AllocationType::kYoung); - USE(young_lo_alloc); - } - - auto trusted_lo_alloc = i_isolate->factory()->NewTrustedFixedArray( - lo_number_of_elements, i::AllocationType::kTrusted); - USE(trusted_lo_alloc); - - v8::HeapStatistics heap_stats_after; - isolate->GetHeapStatistics(&heap_stats_after); - uint64_t final_allocated = heap_stats_after.total_allocated_bytes(); - - CHECK_GT(final_allocated, initial_allocated); - uint64_t allocated_diff = final_allocated - initial_allocated; - CHECK_GE(allocated_diff, expected_allocation_size); - - // This either tests counting happening when a LAB freed and validate - // there's no double counting on evacuated/promoted objects. - v8::internal::heap::InvokeAtomicMajorGC(i_isolate->heap()); - - v8::HeapStatistics heap_stats_after_gc; - isolate->GetHeapStatistics(&heap_stats_after_gc); - uint64_t total_allocation_after_gc = - heap_stats_after_gc.total_allocated_bytes(); - - CHECK_EQ(total_allocation_after_gc, final_allocated); - } - - isolate->Dispose(); -} - -#if V8_CAN_CREATE_SHARED_HEAP_BOOL - -UNINITIALIZED_TEST(GetHeapTotalAllocatedBytesSharedSpaces) { - // This test is incompatible with concurrent allocation, which may occur - // while collecting the statistics and break the final `CHECK_EQ`s. - if (i::v8_flags.stress_concurrent_allocation) return; - if (COMPRESS_POINTERS_IN_MULTIPLE_CAGES_BOOL) return; - - i::v8_flags.shared_heap = true; - i::FlagList::EnforceFlagImplications(); - - v8::Isolate::CreateParams create_params; - create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); - v8::Isolate* isolate = v8::Isolate::New(create_params); - - { - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope handle_scope(isolate); - LocalContext env(isolate); - - v8::HeapStatistics heap_stats_before; - isolate->GetHeapStatistics(&heap_stats_before); - size_t initial_allocated = heap_stats_before.total_allocated_bytes(); - - i::Isolate* i_isolate = reinterpret_cast(isolate); - - const uint32_t number_of_elements = 1; - const uint32_t allocation_size = i::FixedArray::SizeFor(number_of_elements); - const uint32_t trusted_allocation_size = - i::TrustedFixedArray::SizeFor(number_of_elements); - const uint32_t lo_number_of_elements = 256 * 1024; - const uint32_t lo_allocation_size = - i::FixedArray::SizeFor(lo_number_of_elements); - const uint32_t expected_allocation_size = - allocation_size + trusted_allocation_size + lo_allocation_size; - - i::MaybeHandle shared_alloc = - i_isolate->factory()->TryNewFixedArray(number_of_elements, - i::AllocationType::kSharedOld); - USE(shared_alloc); - i::Handle shared_trusted_alloc = - i_isolate->factory()->NewTrustedFixedArray( - number_of_elements, i::AllocationType::kSharedTrusted); - USE(shared_trusted_alloc); - i::MaybeHandle shared_lo_alloc = - i_isolate->factory()->TryNewFixedArray(lo_number_of_elements, - i::AllocationType::kSharedOld); - USE(shared_lo_alloc); - - v8::HeapStatistics heap_stats_after; - isolate->GetHeapStatistics(&heap_stats_after); - uint64_t final_allocated = heap_stats_after.total_allocated_bytes(); - - CHECK_GT(final_allocated, initial_allocated); - uint64_t allocated_diff = final_allocated - initial_allocated; - CHECK_GE(allocated_diff, expected_allocation_size); - } - - isolate->Dispose(); -} - -#endif // V8_CAN_CREATE_SHARED_HEAP_BOOL - TEST(NumberOfNativeContexts) { static const size_t kNumTestContexts = 10; i::Isolate* isolate = CcTest::i_isolate(); diff --git a/doc/api/v8.md b/doc/api/v8.md index ace8ea2d261d25..6a50c502f7166b 100644 --- a/doc/api/v8.md +++ b/doc/api/v8.md @@ -252,7 +252,7 @@ used memory size of V8 global handles. buffers and external strings. `total_allocated_bytes` The value of total allocated bytes since the Isolate -creation +creation. @@ -271,7 +271,8 @@ creation number_of_detached_contexts: 0, total_global_handles_size: 8192, used_global_handles_size: 3296, - external_memory: 318824 + external_memory: 318824, + total_allocated_bytes: 45224088 } ``` diff --git a/lib/v8.js b/lib/v8.js index 54395945aa1787..7f1789f1e04f99 100644 --- a/lib/v8.js +++ b/lib/v8.js @@ -117,6 +117,7 @@ const { stopCpuProfile: _stopCpuProfile, isStringOneByteRepresentation: _isStringOneByteRepresentation, updateHeapStatisticsBuffer, + getTotalAllocatedBytes, updateHeapSpaceStatisticsBuffer, updateHeapCodeStatisticsBuffer, setHeapSnapshotNearHeapLimit: _setHeapSnapshotNearHeapLimit, @@ -136,7 +137,6 @@ const { kTotalGlobalHandlesSizeIndex, kUsedGlobalHandlesSizeIndex, kExternalMemoryIndex, - kTotalAllocatedBytes, // Properties for heap spaces statistics buffer extraction. kHeapSpaces, @@ -247,7 +247,7 @@ function getHeapStatistics() { total_global_handles_size: buffer[kTotalGlobalHandlesSizeIndex], used_global_handles_size: buffer[kUsedGlobalHandlesSizeIndex], external_memory: buffer[kExternalMemoryIndex], - total_allocated_bytes: buffer[kTotalAllocatedBytes], + total_allocated_bytes: getTotalAllocatedBytes(), }; } diff --git a/src/node_v8.cc b/src/node_v8.cc index 0a0554d01f79af..da9b1282fd8a17 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -73,8 +73,7 @@ using v8::Value; V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex) \ V(11, total_global_handles_size, kTotalGlobalHandlesSizeIndex) \ V(12, used_global_handles_size, kUsedGlobalHandlesSizeIndex) \ - V(13, external_memory, kExternalMemoryIndex) \ - V(14, total_allocated_bytes, kTotalAllocatedBytes) + V(13, external_memory, kExternalMemoryIndex) #define V(a, b, c) +1 static constexpr size_t kHeapStatisticsPropertiesCount = @@ -213,6 +212,12 @@ void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo& args) { #undef V } +void GetTotalAllocatedBytes(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); + uint64_t allocated_bytes = isolate->GetTotalAllocatedBytes(); + args.GetReturnValue().Set(Number::New(isolate, allocated_bytes)); +} + void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo& args) { BindingData* data = Realm::GetBindingData(args); @@ -693,6 +698,11 @@ void Initialize(Local target, "updateHeapStatisticsBuffer", UpdateHeapStatisticsBuffer); + SetMethod(context, + target, + "getTotalAllocatedBytes", + GetTotalAllocatedBytes); + SetMethod(context, target, "updateHeapCodeStatisticsBuffer", @@ -774,6 +784,7 @@ void Initialize(Local target, void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(CachedDataVersionTag); registry->Register(UpdateHeapStatisticsBuffer); + registry->Register(GetTotalAllocatedBytes); registry->Register(UpdateHeapCodeStatisticsBuffer); registry->Register(UpdateHeapSpaceStatisticsBuffer); registry->Register(SetFlagsFromString); diff --git a/src/node_worker.cc b/src/node_worker.cc index d8cad06cd0655f..2fcabe5a003b51 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -1285,7 +1285,7 @@ void Worker::GetHeapStatistics(const FunctionCallbackInfo& args) { Number::New(isolate, heap_stats->total_global_handles_size()), Number::New(isolate, heap_stats->used_global_handles_size()), Number::New(isolate, heap_stats->external_memory()), - Number::New(isolate, heap_stats->total_allocated_bytes())}; + Number::New(isolate, isolate->GetTotalAllocatedBytes())}; Local obj; if (!NewDictionaryInstanceNullProto(