Skip to content

Commit 4cda026

Browse files
committed
v8: adding total_allocated_bytes to HeapStatistics
PR-URL: #60573 Reviewed-By: theanarkh <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: James M Snell <[email protected]> fixing tab error.
1 parent d5fdc00 commit 4cda026

File tree

12 files changed

+40
-161
lines changed

12 files changed

+40
-161
lines changed

common.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
# Reset this number to 0 on major V8 upgrades.
4040
# Increment by one for each non-official patch applied to deps/v8.
41-
'v8_embedder_string': '-node.31',
41+
'v8_embedder_string': '-node.32',
4242

4343
##### V8 defaults for Node.js #####
4444

deps/v8/include/v8-isolate.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,13 @@ class V8_EXPORT Isolate {
950950
*/
951951
void GetHeapStatistics(HeapStatistics* heap_statistics);
952952

953+
/**
954+
* Get total allocated bytes since isolate creation.
955+
* This should be used only by Node.JS, since it's a temporary method
956+
* to avoid breaking ABI on HeapStatistics.
957+
*/
958+
uint64_t GetTotalAllocatedBytes();
959+
953960
/**
954961
* Returns the number of spaces in the heap.
955962
*/

deps/v8/include/v8-statistics.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,6 @@ 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-
164157
/**
165158
* Returns a 0/1 boolean, which signifies whether the V8 overwrite heap
166159
* garbage with a bit pattern.
@@ -182,7 +175,6 @@ class V8_EXPORT HeapStatistics {
182175
size_t number_of_detached_contexts_;
183176
size_t total_global_handles_size_;
184177
size_t used_global_handles_size_;
185-
uint64_t total_allocated_bytes_;
186178

187179
friend class V8;
188180
friend class Isolate;

deps/v8/src/api/api.cc

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

65666565
HeapSpaceStatistics::HeapSpaceStatistics()
65676566
: space_name_(nullptr),
@@ -10354,7 +10353,6 @@ void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
1035410353
heap_statistics->number_of_native_contexts_ = heap->NumberOfNativeContexts();
1035510354
heap_statistics->number_of_detached_contexts_ =
1035610355
heap->NumberOfDetachedContexts();
10357-
heap_statistics->total_allocated_bytes_ = heap->GetTotalAllocatedBytes();
1035810356
heap_statistics->does_zap_garbage_ = i::heap::ShouldZapGarbage();
1035910357

1036010358
#if V8_ENABLE_WEBASSEMBLY
@@ -10365,6 +10363,11 @@ void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
1036510363
#endif // V8_ENABLE_WEBASSEMBLY
1036610364
}
1036710365

10366+
uint64_t Isolate::GetTotalAllocatedBytes() {
10367+
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(this);
10368+
return i_isolate->heap()->GetTotalAllocatedBytes();
10369+
}
10370+
1036810371
size_t Isolate::NumberOfHeapSpaces() {
1036910372
return i::LAST_SPACE - i::FIRST_SPACE + 1;
1037010373
}

deps/v8/src/heap/heap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,7 @@ class Heap final {
24142414
// actually finished.
24152415
bool is_full_gc_during_loading_ = false;
24162416

2417-
std::atomic<uint64_t> total_allocated_bytes_ = 0;
2417+
std::atomic<uint64_t> total_allocated_bytes_ = 0;
24182418

24192419
// Classes in "heap" can be friends.
24202420
friend class ActivateMemoryReducerTask;

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

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

doc/api/v8.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ Returns an object with the following properties:
197197
* `total_global_handles_size` {number}
198198
* `used_global_handles_size` {number}
199199
* `external_memory` {number}
200+
* `total_allocated_bytes` {number}
200201

201202
`total_heap_size` The value of total\_heap\_size is the number of bytes V8 has
202203
allocated for the heap. This can grow if used\_heap needs more memory.
@@ -250,6 +251,9 @@ used memory size of V8 global handles.
250251
`external_memory` The value of external\_memory is the memory size of array
251252
buffers and external strings.
252253

254+
`total_allocated_bytes` The value of total allocated bytes since the Isolate
255+
creation.
256+
253257
<!-- eslint-skip -->
254258

255259
```js
@@ -267,7 +271,8 @@ buffers and external strings.
267271
number_of_detached_contexts: 0,
268272
total_global_handles_size: 8192,
269273
used_global_handles_size: 3296,
270-
external_memory: 318824
274+
external_memory: 318824,
275+
total_allocated_bytes: 24970208
271276
}
272277
```
273278

lib/v8.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ const {
117117
stopCpuProfile: _stopCpuProfile,
118118
isStringOneByteRepresentation: _isStringOneByteRepresentation,
119119
updateHeapStatisticsBuffer,
120+
getTotalAllocatedBytes,
120121
updateHeapSpaceStatisticsBuffer,
121122
updateHeapCodeStatisticsBuffer,
122123
setHeapSnapshotNearHeapLimit: _setHeapSnapshotNearHeapLimit,
@@ -246,6 +247,7 @@ function getHeapStatistics() {
246247
total_global_handles_size: buffer[kTotalGlobalHandlesSizeIndex],
247248
used_global_handles_size: buffer[kUsedGlobalHandlesSizeIndex],
248249
external_memory: buffer[kExternalMemoryIndex],
250+
total_allocated_bytes: getTotalAllocatedBytes(),
249251
};
250252
}
251253

src/node_v8.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
212212
#undef V
213213
}
214214

215+
void GetTotalAllocatedBytes(const FunctionCallbackInfo<Value>& args) {
216+
Isolate* isolate = args.GetIsolate();
217+
uint64_t allocated_bytes = isolate->GetTotalAllocatedBytes();
218+
args.GetReturnValue().Set(Number::New(isolate, allocated_bytes));
219+
}
220+
215221

216222
void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
217223
BindingData* data = Realm::GetBindingData<BindingData>(args);
@@ -692,6 +698,11 @@ void Initialize(Local<Object> target,
692698
"updateHeapStatisticsBuffer",
693699
UpdateHeapStatisticsBuffer);
694700

701+
SetMethod(context,
702+
target,
703+
"getTotalAllocatedBytes",
704+
GetTotalAllocatedBytes);
705+
695706
SetMethod(context,
696707
target,
697708
"updateHeapCodeStatisticsBuffer",
@@ -773,6 +784,7 @@ void Initialize(Local<Object> target,
773784
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
774785
registry->Register(CachedDataVersionTag);
775786
registry->Register(UpdateHeapStatisticsBuffer);
787+
registry->Register(GetTotalAllocatedBytes);
776788
registry->Register(UpdateHeapCodeStatisticsBuffer);
777789
registry->Register(UpdateHeapSpaceStatisticsBuffer);
778790
registry->Register(SetFlagsFromString);

src/node_worker.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,7 @@ void Worker::GetHeapStatistics(const FunctionCallbackInfo<Value>& args) {
12631263
"total_global_handles_size",
12641264
"used_global_handles_size",
12651265
"external_memory",
1266+
"total_allocated_bytes",
12661267
};
12671268
tmpl = DictionaryTemplate::New(isolate, heap_stats_names);
12681269
env->set_heap_statistics_template(tmpl);
@@ -1283,7 +1284,8 @@ void Worker::GetHeapStatistics(const FunctionCallbackInfo<Value>& args) {
12831284
Number::New(isolate, heap_stats->number_of_detached_contexts()),
12841285
Number::New(isolate, heap_stats->total_global_handles_size()),
12851286
Number::New(isolate, heap_stats->used_global_handles_size()),
1286-
Number::New(isolate, heap_stats->external_memory())};
1287+
Number::New(isolate, heap_stats->external_memory()),
1288+
Number::New(isolate, isolate->GetTotalAllocatedBytes())};
12871289

12881290
Local<Object> obj;
12891291
if (!NewDictionaryInstanceNullProto(

0 commit comments

Comments
 (0)