@@ -17492,6 +17492,152 @@ TEST(GetHeapSpaceStatistics) {
1749217492 CHECK_EQ(total_physical_size, heap_statistics.total_physical_size());
1749317493}
1749417494
17495+ UNINITIALIZED_TEST(GetHeapTotalAllocatedBytes) {
17496+ // This test is incompatible with concurrent allocation, which may occur
17497+ // while collecting the statistics and break the final `CHECK_EQ`s.
17498+ if (i::v8_flags.stress_concurrent_allocation) return;
17499+
17500+ v8::Isolate::CreateParams create_params;
17501+ create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
17502+ v8::Isolate* isolate = v8::Isolate::New(create_params);
17503+
17504+ const uint32_t number_of_elements = 1;
17505+ const uint32_t allocation_size = i::FixedArray::SizeFor(number_of_elements);
17506+ const uint32_t trusted_allocation_size =
17507+ i::TrustedFixedArray::SizeFor(number_of_elements);
17508+ const uint32_t lo_number_of_elements = 256 * 1024;
17509+ const uint32_t lo_allocation_size =
17510+ i::FixedArray::SizeFor(lo_number_of_elements);
17511+ const uint32_t trusted_lo_allocation_size =
17512+ i::TrustedFixedArray::SizeFor(lo_number_of_elements);
17513+ const uint32_t expected_allocation_size =
17514+ allocation_size * 2 + lo_allocation_size * 2 + trusted_allocation_size +
17515+ trusted_lo_allocation_size;
17516+
17517+ {
17518+ v8::Isolate::Scope isolate_scope(isolate);
17519+ v8::HandleScope handle_scope(isolate);
17520+ LocalContext env(isolate);
17521+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
17522+
17523+ v8::HeapStatistics heap_stats_before;
17524+ isolate->GetHeapStatistics(&heap_stats_before);
17525+ size_t initial_allocated = heap_stats_before.total_allocated_bytes();
17526+
17527+ i::MaybeHandle<i::FixedArray> young_alloc =
17528+ i_isolate->factory()->TryNewFixedArray(number_of_elements,
17529+ i::AllocationType::kYoung);
17530+ USE(young_alloc);
17531+ i::MaybeHandle<i::FixedArray> old_alloc =
17532+ i_isolate->factory()->TryNewFixedArray(number_of_elements,
17533+ i::AllocationType::kOld);
17534+ USE(old_alloc);
17535+ i::Handle<i::TrustedFixedArray> trusted_alloc =
17536+ i_isolate->factory()->NewTrustedFixedArray(number_of_elements,
17537+ i::AllocationType::kTrusted);
17538+ USE(trusted_alloc);
17539+ i::MaybeHandle<i::FixedArray> old_lo_alloc =
17540+ i_isolate->factory()->TryNewFixedArray(lo_number_of_elements,
17541+ i::AllocationType::kOld);
17542+ USE(old_lo_alloc);
17543+
17544+ {
17545+ v8::HandleScope inner_handle_scope(isolate);
17546+ auto young_lo_alloc = i_isolate->factory()->TryNewFixedArray(
17547+ lo_number_of_elements, i::AllocationType::kYoung);
17548+ USE(young_lo_alloc);
17549+ }
17550+
17551+ auto trusted_lo_alloc = i_isolate->factory()->NewTrustedFixedArray(
17552+ lo_number_of_elements, i::AllocationType::kTrusted);
17553+ USE(trusted_lo_alloc);
17554+
17555+ v8::HeapStatistics heap_stats_after;
17556+ isolate->GetHeapStatistics(&heap_stats_after);
17557+ uint64_t final_allocated = heap_stats_after.total_allocated_bytes();
17558+
17559+ CHECK_GT(final_allocated, initial_allocated);
17560+ uint64_t allocated_diff = final_allocated - initial_allocated;
17561+ CHECK_GE(allocated_diff, expected_allocation_size);
17562+
17563+ // This either tests counting happening when a LAB freed and validate
17564+ // there's no double counting on evacuated/promoted objects.
17565+ v8::internal::heap::InvokeAtomicMajorGC(i_isolate->heap());
17566+
17567+ v8::HeapStatistics heap_stats_after_gc;
17568+ isolate->GetHeapStatistics(&heap_stats_after_gc);
17569+ uint64_t total_allocation_after_gc =
17570+ heap_stats_after_gc.total_allocated_bytes();
17571+
17572+ CHECK_EQ(total_allocation_after_gc, final_allocated);
17573+ }
17574+
17575+ isolate->Dispose();
17576+ }
17577+
17578+ #if V8_CAN_CREATE_SHARED_HEAP_BOOL
17579+
17580+ UNINITIALIZED_TEST(GetHeapTotalAllocatedBytesSharedSpaces) {
17581+ // This test is incompatible with concurrent allocation, which may occur
17582+ // while collecting the statistics and break the final `CHECK_EQ`s.
17583+ if (i::v8_flags.stress_concurrent_allocation) return;
17584+ if (COMPRESS_POINTERS_IN_MULTIPLE_CAGES_BOOL) return;
17585+
17586+ i::v8_flags.shared_heap = true;
17587+ i::FlagList::EnforceFlagImplications();
17588+
17589+ v8::Isolate::CreateParams create_params;
17590+ create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
17591+ v8::Isolate* isolate = v8::Isolate::New(create_params);
17592+
17593+ {
17594+ v8::Isolate::Scope isolate_scope(isolate);
17595+ v8::HandleScope handle_scope(isolate);
17596+ LocalContext env(isolate);
17597+
17598+ v8::HeapStatistics heap_stats_before;
17599+ isolate->GetHeapStatistics(&heap_stats_before);
17600+ size_t initial_allocated = heap_stats_before.total_allocated_bytes();
17601+
17602+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
17603+
17604+ const uint32_t number_of_elements = 1;
17605+ const uint32_t allocation_size = i::FixedArray::SizeFor(number_of_elements);
17606+ const uint32_t trusted_allocation_size =
17607+ i::TrustedFixedArray::SizeFor(number_of_elements);
17608+ const uint32_t lo_number_of_elements = 256 * 1024;
17609+ const uint32_t lo_allocation_size =
17610+ i::FixedArray::SizeFor(lo_number_of_elements);
17611+ const uint32_t expected_allocation_size =
17612+ allocation_size + trusted_allocation_size + lo_allocation_size;
17613+
17614+ i::MaybeHandle<i::FixedArray> shared_alloc =
17615+ i_isolate->factory()->TryNewFixedArray(number_of_elements,
17616+ i::AllocationType::kSharedOld);
17617+ USE(shared_alloc);
17618+ i::Handle<i::TrustedFixedArray> shared_trusted_alloc =
17619+ i_isolate->factory()->NewTrustedFixedArray(
17620+ number_of_elements, i::AllocationType::kSharedTrusted);
17621+ USE(shared_trusted_alloc);
17622+ i::MaybeHandle<i::FixedArray> shared_lo_alloc =
17623+ i_isolate->factory()->TryNewFixedArray(lo_number_of_elements,
17624+ i::AllocationType::kSharedOld);
17625+ USE(shared_lo_alloc);
17626+
17627+ v8::HeapStatistics heap_stats_after;
17628+ isolate->GetHeapStatistics(&heap_stats_after);
17629+ uint64_t final_allocated = heap_stats_after.total_allocated_bytes();
17630+
17631+ CHECK_GT(final_allocated, initial_allocated);
17632+ uint64_t allocated_diff = final_allocated - initial_allocated;
17633+ CHECK_GE(allocated_diff, expected_allocation_size);
17634+ }
17635+
17636+ isolate->Dispose();
17637+ }
17638+
17639+ #endif // V8_CAN_CREATE_SHARED_HEAP_BOOL
17640+
1749517641TEST(NumberOfNativeContexts) {
1749617642 static const size_t kNumTestContexts = 10;
1749717643 i::Isolate* isolate = CcTest::i_isolate();
0 commit comments