Skip to content

Commit fe81545

Browse files
caiolimaV8 LUCI CQ
authored andcommitted
[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}
1 parent 768b63d commit fe81545

7 files changed

Lines changed: 198 additions & 8 deletions

File tree

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;

src/api/api.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6340,7 +6340,8 @@ HeapStatistics::HeapStatistics()
63406340
peak_malloced_memory_(0),
63416341
does_zap_garbage_(false),
63426342
number_of_native_contexts_(0),
6343-
number_of_detached_contexts_(0) {}
6343+
number_of_detached_contexts_(0),
6344+
total_allocated_bytes_(0) {}
63446345

63456346
HeapSpaceStatistics::HeapSpaceStatistics()
63466347
: space_name_(nullptr),
@@ -10209,6 +10210,7 @@ void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
1020910210
heap_statistics->number_of_native_contexts_ = heap->NumberOfNativeContexts();
1021010211
heap_statistics->number_of_detached_contexts_ =
1021110212
heap->NumberOfDetachedContexts();
10213+
heap_statistics->total_allocated_bytes_ = heap->GetTotalAllocatedBytes();
1021210214
heap_statistics->does_zap_garbage_ = i::heap::ShouldZapGarbage();
1021310215

1021410216
#if V8_ENABLE_WEBASSEMBLY

src/heap/heap-allocator.cc

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,42 @@ AllocationResult HeapAllocator::AllocateRawLargeInternal(
8686
int size_in_bytes, AllocationType allocation, AllocationOrigin origin,
8787
AllocationAlignment alignment, AllocationHint hint) {
8888
DCHECK_GT(size_in_bytes, heap_->MaxRegularHeapObjectSize(allocation));
89+
AllocationResult allocation_result;
8990
switch (allocation) {
9091
case AllocationType::kYoung:
91-
return new_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
92+
allocation_result =
93+
new_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
94+
break;
9295
case AllocationType::kOld:
93-
return lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
96+
allocation_result =
97+
lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
98+
break;
9499
case AllocationType::kCode:
95-
return code_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
100+
allocation_result =
101+
code_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
102+
break;
96103
case AllocationType::kSharedOld:
97-
return shared_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
104+
allocation_result =
105+
shared_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
106+
break;
98107
case AllocationType::kTrusted:
99-
return trusted_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
108+
allocation_result =
109+
trusted_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
110+
break;
100111
case AllocationType::kSharedTrusted:
101-
return shared_trusted_lo_space()->AllocateRaw(local_heap_, size_in_bytes,
102-
hint);
112+
allocation_result = shared_trusted_lo_space()->AllocateRaw(
113+
local_heap_, size_in_bytes, hint);
114+
break;
103115
case AllocationType::kMap:
104116
case AllocationType::kReadOnly:
105117
case AllocationType::kSharedMap:
106118
UNREACHABLE();
107119
}
120+
if (!allocation_result.IsFailure()) {
121+
int allocated_size = ALIGN_TO_ALLOCATION_ALIGNMENT(size_in_bytes);
122+
heap_->AddTotalAllocatedBytes(allocated_size);
123+
}
124+
return allocation_result;
108125
}
109126

110127
namespace {

src/heap/heap.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7867,6 +7867,10 @@ int Heap::NextStackTraceId() {
78677867
return last_id;
78687868
}
78697869

7870+
uint64_t Heap::GetTotalAllocatedBytes() {
7871+
return total_allocated_bytes_.load(std::memory_order_relaxed);
7872+
}
7873+
78707874
EmbedderStackStateScope::EmbedderStackStateScope(
78717875
Heap* heap, EmbedderStackStateOrigin origin, StackState stack_state)
78727876
: heap_(heap),

src/heap/heap.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,11 @@ class Heap final {
17031703
bool ShouldUseBackgroundThreads() const;
17041704
bool ShouldUseIncrementalMarking() const;
17051705

1706+
void AddTotalAllocatedBytes(size_t size) {
1707+
total_allocated_bytes_.fetch_add(size, std::memory_order_relaxed);
1708+
}
1709+
uint64_t GetTotalAllocatedBytes();
1710+
17061711
HeapAllocator* allocator() { return heap_allocator_; }
17071712
const HeapAllocator* allocator() const { return heap_allocator_; }
17081713

@@ -2543,6 +2548,8 @@ class Heap final {
25432548
// no value was provided this will be 0.
25442549
uint64_t physical_memory_;
25452550

2551+
std::atomic<uint64_t> total_allocated_bytes_ = 0;
2552+
25462553
perfetto::NamedTrack tracing_track_;
25472554
perfetto::NamedTrack loading_track_;
25482555

src/heap/main-allocator.cc

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

295+
// This is going to overestimate a bit of the total allocated bytes, since the
296+
// LAB was not used yet. However the leftover compared to the LAB itself is
297+
// quite small, so it seems tolerable.
298+
if (local_heap_) {
299+
local_heap_->heap()->AddTotalAllocatedBytes(end - start);
300+
}
295301
allocation_info().Reset(start, end);
296302
extended_limit_ = extended_end;
297303

test/cctest/test-api.cc

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

0 commit comments

Comments
 (0)