Skip to content

Commit f9a83ff

Browse files
authored
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: #60429 Reviewed-By: Joyee Cheung <[email protected]>
1 parent e40b9b9 commit f9a83ff

File tree

7 files changed

+198
-8
lines changed

7 files changed

+198
-8
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
@@ -6557,7 +6557,8 @@ HeapStatistics::HeapStatistics()
65576557
peak_malloced_memory_(0),
65586558
does_zap_garbage_(false),
65596559
number_of_native_contexts_(0),
6560-
number_of_detached_contexts_(0) {}
6560+
number_of_detached_contexts_(0),
6561+
total_allocated_bytes_(0) {}
65616562

65626563
HeapSpaceStatistics::HeapSpaceStatistics()
65636564
: space_name_(nullptr),
@@ -10411,6 +10412,7 @@ void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
1041110412
heap_statistics->number_of_native_contexts_ = heap->NumberOfNativeContexts();
1041210413
heap_statistics->number_of_detached_contexts_ =
1041310414
heap->NumberOfDetachedContexts();
10415+
heap_statistics->total_allocated_bytes_ = heap->GetTotalAllocatedBytes();
1041410416
heap_statistics->does_zap_garbage_ = i::heap::ShouldZapGarbage();
1041510417

1041610418
#if V8_ENABLE_WEBASSEMBLY

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,42 @@ AllocationResult HeapAllocator::AllocateRawLargeInternal(
8585
int size_in_bytes, AllocationType allocation, AllocationOrigin origin,
8686
AllocationAlignment alignment, AllocationHint hint) {
8787
DCHECK_GT(size_in_bytes, heap_->MaxRegularHeapObjectSize(allocation));
88+
AllocationResult allocation_result;
8889
switch (allocation) {
8990
case AllocationType::kYoung:
90-
return new_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
91+
allocation_result =
92+
new_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
93+
break;
9194
case AllocationType::kOld:
92-
return lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
95+
allocation_result =
96+
lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
97+
break;
9398
case AllocationType::kCode:
94-
return code_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
99+
allocation_result =
100+
code_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
101+
break;
95102
case AllocationType::kSharedOld:
96-
return shared_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
103+
allocation_result =
104+
shared_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
105+
break;
97106
case AllocationType::kTrusted:
98-
return trusted_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
107+
allocation_result =
108+
trusted_lo_space()->AllocateRaw(local_heap_, size_in_bytes, hint);
109+
break;
99110
case AllocationType::kSharedTrusted:
100-
return shared_trusted_lo_space()->AllocateRaw(local_heap_, size_in_bytes,
101-
hint);
111+
allocation_result = shared_trusted_lo_space()->AllocateRaw(
112+
local_heap_, size_in_bytes, hint);
113+
break;
102114
case AllocationType::kMap:
103115
case AllocationType::kReadOnly:
104116
case AllocationType::kSharedMap:
105117
UNREACHABLE();
106118
}
119+
if (!allocation_result.IsFailure()) {
120+
int allocated_size = ALIGN_TO_ALLOCATION_ALIGNMENT(size_in_bytes);
121+
heap_->AddTotalAllocatedBytes(allocated_size);
122+
}
123+
return allocation_result;
107124
}
108125

109126
namespace {

deps/v8/src/heap/heap.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7839,6 +7839,10 @@ int Heap::NextStackTraceId() {
78397839
return last_id;
78407840
}
78417841

7842+
uint64_t Heap::GetTotalAllocatedBytes() {
7843+
return total_allocated_bytes_.load(std::memory_order_relaxed);
7844+
}
7845+
78427846
EmbedderStackStateScope::EmbedderStackStateScope(
78437847
Heap* heap, EmbedderStackStateOrigin origin, StackState stack_state)
78447848
: heap_(heap),

deps/v8/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

@@ -2498,6 +2503,8 @@ class Heap final {
24982503
// no value was provided this will be 0.
24992504
uint64_t physical_memory_;
25002505

2506+
std::atomic<uint64_t> total_allocated_bytes_ = 0;
2507+
25012508
#if defined(V8_USE_PERFETTO)
25022509
perfetto::NamedTrack tracing_track_;
25032510
#endif

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

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

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

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

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

0 commit comments

Comments
 (0)