Skip to content

Commit d5fdc00

Browse files
committed
deps: V8: backport fe81545e6d14
Original commit message: [api] Adding total allocated bytes in HeapStatistics This change exposes total allocated bytes in v8::HeapStatistics API by introducing a new total_allocated_bytes() method that tracks all heap allocations since an Isolate creation. The implementation adds: - uint64_t total_allocated_bytes_ field to HeapStatistics. - An atomic total allocation counter is stored in the Heap class. - The counter is incremented whenever a RestLab is called. This approach can overestimate the total allocation for cases where the LAB is not fully used, but the leftover compared to the LAB itself is quite small, so it seems tolerable. Design doc reference: https://docs.google.com/document/d/1O4JPsoaxTQsX_7T5Fz4rsGeHMiM16jUrvDuq9FrtbNM Change-Id: Ic531698aaeb1578f943b7fdd346b9159ffd9b6c9 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6996467 Reviewed-by: Dominik Inführ <[email protected]> Reviewed-by: Michael Lippautz <[email protected]> Commit-Queue: Dmitry Bezhetskov <[email protected]> Cr-Commit-Position: refs/heads/main@{#103296} Refs: v8/v8@fe81545 Co-authored-by: Caio Lima <[email protected]> PR-URL: nodejs#60429 Reviewed-By: Joyee Cheung <[email protected]>
1 parent 6cd9bdc commit d5fdc00

File tree

7 files changed

+198
-7
lines changed

7 files changed

+198
-7
lines changed

deps/v8/include/v8-statistics.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ class V8_EXPORT HeapStatistics {
154154
size_t number_of_native_contexts() { return number_of_native_contexts_; }
155155
size_t number_of_detached_contexts() { return number_of_detached_contexts_; }
156156

157+
/**
158+
* Returns the total number of bytes allocated since the Isolate was created.
159+
* This includes all heap objects allocated in any space (new, old, code,
160+
* etc.).
161+
*/
162+
uint64_t total_allocated_bytes() { return total_allocated_bytes_; }
163+
157164
/**
158165
* Returns a 0/1 boolean, which signifies whether the V8 overwrite heap
159166
* garbage with a bit pattern.
@@ -175,6 +182,7 @@ class V8_EXPORT HeapStatistics {
175182
size_t number_of_detached_contexts_;
176183
size_t total_global_handles_size_;
177184
size_t used_global_handles_size_;
185+
uint64_t total_allocated_bytes_;
178186

179187
friend class V8;
180188
friend class Isolate;

deps/v8/src/api/api.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6560,7 +6560,8 @@ HeapStatistics::HeapStatistics()
65606560
peak_malloced_memory_(0),
65616561
does_zap_garbage_(false),
65626562
number_of_native_contexts_(0),
6563-
number_of_detached_contexts_(0) {}
6563+
number_of_detached_contexts_(0),
6564+
total_allocated_bytes_(0) {}
65646565

65656566
HeapSpaceStatistics::HeapSpaceStatistics()
65666567
: space_name_(nullptr),
@@ -10353,6 +10354,7 @@ void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
1035310354
heap_statistics->number_of_native_contexts_ = heap->NumberOfNativeContexts();
1035410355
heap_statistics->number_of_detached_contexts_ =
1035510356
heap->NumberOfDetachedContexts();
10357+
heap_statistics->total_allocated_bytes_ = heap->GetTotalAllocatedBytes();
1035610358
heap_statistics->does_zap_garbage_ = i::heap::ShouldZapGarbage();
1035710359

1035810360
#if V8_ENABLE_WEBASSEMBLY

deps/v8/src/heap/heap-allocator.cc

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,42 @@ AllocationResult HeapAllocator::AllocateRawLargeInternal(
6565
int size_in_bytes, AllocationType allocation, AllocationOrigin origin,
6666
AllocationAlignment alignment) {
6767
DCHECK_GT(size_in_bytes, heap_->MaxRegularHeapObjectSize(allocation));
68+
AllocationResult allocation_result;
6869
switch (allocation) {
6970
case AllocationType::kYoung:
70-
return new_lo_space()->AllocateRaw(local_heap_, size_in_bytes);
71+
allocation_result =
72+
new_lo_space()->AllocateRaw(local_heap_, size_in_bytes);
73+
break;
7174
case AllocationType::kOld:
72-
return lo_space()->AllocateRaw(local_heap_, size_in_bytes);
75+
allocation_result =
76+
lo_space()->AllocateRaw(local_heap_, size_in_bytes);
77+
break;
7378
case AllocationType::kCode:
74-
return code_lo_space()->AllocateRaw(local_heap_, size_in_bytes);
79+
allocation_result =
80+
code_lo_space()->AllocateRaw(local_heap_, size_in_bytes);
81+
break;
7582
case AllocationType::kSharedOld:
76-
return shared_lo_space()->AllocateRaw(local_heap_, size_in_bytes);
83+
allocation_result =
84+
shared_lo_space()->AllocateRaw(local_heap_, size_in_bytes);
85+
break;
7786
case AllocationType::kTrusted:
78-
return trusted_lo_space()->AllocateRaw(local_heap_, size_in_bytes);
87+
allocation_result =
88+
trusted_lo_space()->AllocateRaw(local_heap_, size_in_bytes);
89+
break;
7990
case AllocationType::kSharedTrusted:
80-
return shared_trusted_lo_space()->AllocateRaw(local_heap_, size_in_bytes);
91+
allocation_result = shared_trusted_lo_space()->AllocateRaw(
92+
local_heap_, size_in_bytes);
93+
break;
8194
case AllocationType::kMap:
8295
case AllocationType::kReadOnly:
8396
case AllocationType::kSharedMap:
8497
UNREACHABLE();
8598
}
99+
if (!allocation_result.IsFailure()) {
100+
int allocated_size = ALIGN_TO_ALLOCATION_ALIGNMENT(size_in_bytes);
101+
heap_->AddTotalAllocatedBytes(allocated_size);
102+
}
103+
return allocation_result;
86104
}
87105

88106
namespace {

deps/v8/src/heap/heap.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7446,6 +7446,10 @@ int Heap::NextStackTraceId() {
74467446
return last_id;
74477447
}
74487448

7449+
uint64_t Heap::GetTotalAllocatedBytes() {
7450+
return total_allocated_bytes_.load(std::memory_order_relaxed);
7451+
}
7452+
74497453
EmbedderStackStateScope::EmbedderStackStateScope(
74507454
Heap* heap, EmbedderStackStateOrigin origin, StackState stack_state)
74517455
: heap_(heap),

deps/v8/src/heap/heap.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,11 @@ class Heap final {
16351635
bool ShouldUseBackgroundThreads() const;
16361636
bool ShouldUseIncrementalMarking() const;
16371637

1638+
void AddTotalAllocatedBytes(size_t size) {
1639+
total_allocated_bytes_.fetch_add(size, std::memory_order_relaxed);
1640+
}
1641+
uint64_t GetTotalAllocatedBytes();
1642+
16381643
HeapAllocator* allocator() { return heap_allocator_; }
16391644
const HeapAllocator* allocator() const { return heap_allocator_; }
16401645

@@ -2409,6 +2414,8 @@ class Heap final {
24092414
// actually finished.
24102415
bool is_full_gc_during_loading_ = false;
24112416

2417+
std::atomic<uint64_t> total_allocated_bytes_ = 0;
2418+
24122419
// Classes in "heap" can be friends.
24132420
friend class ActivateMemoryReducerTask;
24142421
friend class AlwaysAllocateScope;

deps/v8/src/heap/main-allocator.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ void MainAllocator::ResetLab(Address start, Address end, Address extended_end) {
298298
MemoryChunkMetadata::UpdateHighWaterMark(top());
299299
}
300300

301+
// This is going to overestimate a bit of the total allocated bytes, since the
302+
// LAB was not used yet. However the leftover compared to the LAB itself is
303+
// quite small, so it seems tolerable.
304+
if (local_heap_) {
305+
local_heap_->heap()->AddTotalAllocatedBytes(end - start);
306+
}
301307
allocation_info().Reset(start, end);
302308

303309
if (SupportsPendingAllocation()) {

deps/v8/test/cctest/test-api.cc

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1749517641
TEST(NumberOfNativeContexts) {
1749617642
static const size_t kNumTestContexts = 10;
1749717643
i::Isolate* isolate = CcTest::i_isolate();

0 commit comments

Comments
 (0)