@@ -17706,6 +17706,152 @@ TEST(GetHeapSpaceStatistics) {
1770617706 CHECK_EQ(total_physical_size, heap_statistics.total_physical_size());
1770717707}
1770817708
17709+ UNINITIALIZED_TEST(GetHeapTotalAllocatedBytes) {
17710+ // This test is incompatible with concurrent allocation, which may occur
17711+ // while collecting the statistics and break the final `CHECK_EQ`s.
17712+ if (i::v8_flags.stress_concurrent_allocation) return;
17713+
17714+ v8::Isolate::CreateParams create_params;
17715+ create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
17716+ v8::Isolate* isolate = v8::Isolate::New(create_params);
17717+
17718+ const uint32_t number_of_elements = 1;
17719+ const uint32_t allocation_size = i::FixedArray::SizeFor(number_of_elements);
17720+ const uint32_t trusted_allocation_size =
17721+ i::TrustedFixedArray::SizeFor(number_of_elements);
17722+ const uint32_t lo_number_of_elements = 256 * 1024;
17723+ const uint32_t lo_allocation_size =
17724+ i::FixedArray::SizeFor(lo_number_of_elements);
17725+ const uint32_t trusted_lo_allocation_size =
17726+ i::TrustedFixedArray::SizeFor(lo_number_of_elements);
17727+ const uint32_t expected_allocation_size =
17728+ allocation_size * 2 + lo_allocation_size * 2 + trusted_allocation_size +
17729+ trusted_lo_allocation_size;
17730+
17731+ {
17732+ v8::Isolate::Scope isolate_scope(isolate);
17733+ v8::HandleScope handle_scope(isolate);
17734+ LocalContext env(isolate);
17735+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
17736+
17737+ v8::HeapStatistics heap_stats_before;
17738+ isolate->GetHeapStatistics(&heap_stats_before);
17739+ size_t initial_allocated = heap_stats_before.total_allocated_bytes();
17740+
17741+ i::MaybeHandle<i::FixedArray> young_alloc =
17742+ i_isolate->factory()->TryNewFixedArray(number_of_elements,
17743+ i::AllocationType::kYoung);
17744+ USE(young_alloc);
17745+ i::MaybeHandle<i::FixedArray> old_alloc =
17746+ i_isolate->factory()->TryNewFixedArray(number_of_elements,
17747+ i::AllocationType::kOld);
17748+ USE(old_alloc);
17749+ i::Handle<i::TrustedFixedArray> trusted_alloc =
17750+ i_isolate->factory()->NewTrustedFixedArray(number_of_elements,
17751+ i::AllocationType::kTrusted);
17752+ USE(trusted_alloc);
17753+ i::MaybeHandle<i::FixedArray> old_lo_alloc =
17754+ i_isolate->factory()->TryNewFixedArray(lo_number_of_elements,
17755+ i::AllocationType::kOld);
17756+ USE(old_lo_alloc);
17757+
17758+ {
17759+ v8::HandleScope inner_handle_scope(isolate);
17760+ auto young_lo_alloc = i_isolate->factory()->TryNewFixedArray(
17761+ lo_number_of_elements, i::AllocationType::kYoung);
17762+ USE(young_lo_alloc);
17763+ }
17764+
17765+ auto trusted_lo_alloc = i_isolate->factory()->NewTrustedFixedArray(
17766+ lo_number_of_elements, i::AllocationType::kTrusted);
17767+ USE(trusted_lo_alloc);
17768+
17769+ v8::HeapStatistics heap_stats_after;
17770+ isolate->GetHeapStatistics(&heap_stats_after);
17771+ uint64_t final_allocated = heap_stats_after.total_allocated_bytes();
17772+
17773+ CHECK_GT(final_allocated, initial_allocated);
17774+ uint64_t allocated_diff = final_allocated - initial_allocated;
17775+ CHECK_GE(allocated_diff, expected_allocation_size);
17776+
17777+ // This either tests counting happening when a LAB freed and validate
17778+ // there's no double counting on evacuated/promoted objects.
17779+ v8::internal::heap::InvokeAtomicMajorGC(i_isolate->heap());
17780+
17781+ v8::HeapStatistics heap_stats_after_gc;
17782+ isolate->GetHeapStatistics(&heap_stats_after_gc);
17783+ uint64_t total_allocation_after_gc =
17784+ heap_stats_after_gc.total_allocated_bytes();
17785+
17786+ CHECK_EQ(total_allocation_after_gc, final_allocated);
17787+ }
17788+
17789+ isolate->Dispose();
17790+ }
17791+
17792+ #if V8_CAN_CREATE_SHARED_HEAP_BOOL
17793+
17794+ UNINITIALIZED_TEST(GetHeapTotalAllocatedBytesSharedSpaces) {
17795+ // This test is incompatible with concurrent allocation, which may occur
17796+ // while collecting the statistics and break the final `CHECK_EQ`s.
17797+ if (i::v8_flags.stress_concurrent_allocation) return;
17798+ if (COMPRESS_POINTERS_IN_MULTIPLE_CAGES_BOOL) return;
17799+
17800+ i::v8_flags.shared_heap = true;
17801+ i::FlagList::EnforceFlagImplications();
17802+
17803+ v8::Isolate::CreateParams create_params;
17804+ create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
17805+ v8::Isolate* isolate = v8::Isolate::New(create_params);
17806+
17807+ {
17808+ v8::Isolate::Scope isolate_scope(isolate);
17809+ v8::HandleScope handle_scope(isolate);
17810+ LocalContext env(isolate);
17811+
17812+ v8::HeapStatistics heap_stats_before;
17813+ isolate->GetHeapStatistics(&heap_stats_before);
17814+ size_t initial_allocated = heap_stats_before.total_allocated_bytes();
17815+
17816+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
17817+
17818+ const uint32_t number_of_elements = 1;
17819+ const uint32_t allocation_size = i::FixedArray::SizeFor(number_of_elements);
17820+ const uint32_t trusted_allocation_size =
17821+ i::TrustedFixedArray::SizeFor(number_of_elements);
17822+ const uint32_t lo_number_of_elements = 256 * 1024;
17823+ const uint32_t lo_allocation_size =
17824+ i::FixedArray::SizeFor(lo_number_of_elements);
17825+ const uint32_t expected_allocation_size =
17826+ allocation_size + trusted_allocation_size + lo_allocation_size;
17827+
17828+ i::MaybeHandle<i::FixedArray> shared_alloc =
17829+ i_isolate->factory()->TryNewFixedArray(number_of_elements,
17830+ i::AllocationType::kSharedOld);
17831+ USE(shared_alloc);
17832+ i::Handle<i::TrustedFixedArray> shared_trusted_alloc =
17833+ i_isolate->factory()->NewTrustedFixedArray(
17834+ number_of_elements, i::AllocationType::kSharedTrusted);
17835+ USE(shared_trusted_alloc);
17836+ i::MaybeHandle<i::FixedArray> shared_lo_alloc =
17837+ i_isolate->factory()->TryNewFixedArray(lo_number_of_elements,
17838+ i::AllocationType::kSharedOld);
17839+ USE(shared_lo_alloc);
17840+
17841+ v8::HeapStatistics heap_stats_after;
17842+ isolate->GetHeapStatistics(&heap_stats_after);
17843+ uint64_t final_allocated = heap_stats_after.total_allocated_bytes();
17844+
17845+ CHECK_GT(final_allocated, initial_allocated);
17846+ uint64_t allocated_diff = final_allocated - initial_allocated;
17847+ CHECK_GE(allocated_diff, expected_allocation_size);
17848+ }
17849+
17850+ isolate->Dispose();
17851+ }
17852+
17853+ #endif // V8_CAN_CREATE_SHARED_HEAP_BOOL
17854+
1770917855TEST(NumberOfNativeContexts) {
1771017856 static const size_t kNumTestContexts = 10;
1771117857 i::Isolate* isolate = CcTest::i_isolate();
0 commit comments