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