Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

# Reset this number to 0 on major V8 upgrades.
# Increment by one for each non-official patch applied to deps/v8.
'v8_embedder_string': '-node.14',
'v8_embedder_string': '-node.15',

##### V8 defaults for Node.js #####

Expand Down
7 changes: 7 additions & 0 deletions deps/v8/include/v8-isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,13 @@ class V8_EXPORT Isolate {
*/
void GetHeapStatistics(HeapStatistics* heap_statistics);

/**
* Get total allocated bytes since isolate creation.
* This should be used only by Node.JS, since it's a temporary method
* to avoid breaking ABI on HeapStatistics.
*/
uint64_t GetTotalAllocatedBytes();

/**
* Returns the number of spaces in the heap.
*/
Expand Down
8 changes: 0 additions & 8 deletions deps/v8/include/v8-statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,6 @@ class V8_EXPORT HeapStatistics {
size_t number_of_native_contexts() { return number_of_native_contexts_; }
size_t number_of_detached_contexts() { return number_of_detached_contexts_; }

/**
* Returns the total number of bytes allocated since the Isolate was created.
* This includes all heap objects allocated in any space (new, old, code,
* etc.).
*/
uint64_t total_allocated_bytes() { return total_allocated_bytes_; }

/**
* Returns a 0/1 boolean, which signifies whether the V8 overwrite heap
* garbage with a bit pattern.
Expand All @@ -182,7 +175,6 @@ class V8_EXPORT HeapStatistics {
size_t number_of_detached_contexts_;
size_t total_global_handles_size_;
size_t used_global_handles_size_;
uint64_t total_allocated_bytes_;

friend class V8;
friend class Isolate;
Expand Down
9 changes: 6 additions & 3 deletions deps/v8/src/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6584,8 +6584,7 @@ HeapStatistics::HeapStatistics()
peak_malloced_memory_(0),
does_zap_garbage_(false),
number_of_native_contexts_(0),
number_of_detached_contexts_(0),
total_allocated_bytes_(0) {}
number_of_detached_contexts_(0) {}

HeapSpaceStatistics::HeapSpaceStatistics()
: space_name_(nullptr),
Expand Down Expand Up @@ -10439,7 +10438,6 @@ void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
heap_statistics->number_of_native_contexts_ = heap->NumberOfNativeContexts();
heap_statistics->number_of_detached_contexts_ =
heap->NumberOfDetachedContexts();
heap_statistics->total_allocated_bytes_ = heap->GetTotalAllocatedBytes();
heap_statistics->does_zap_garbage_ = i::heap::ShouldZapGarbage();

#if V8_ENABLE_WEBASSEMBLY
Expand All @@ -10450,6 +10448,11 @@ void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
#endif // V8_ENABLE_WEBASSEMBLY
}

uint64_t Isolate::GetTotalAllocatedBytes() {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(this);
return i_isolate->heap()->GetTotalAllocatedBytes();
}

size_t Isolate::NumberOfHeapSpaces() {
return i::LAST_SPACE - i::FIRST_SPACE + 1;
}
Expand Down
146 changes: 0 additions & 146 deletions deps/v8/test/cctest/test-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17656,152 +17656,6 @@ TEST(GetHeapSpaceStatistics) {
CHECK_EQ(total_physical_size, heap_statistics.total_physical_size());
}

UNINITIALIZED_TEST(GetHeapTotalAllocatedBytes) {
// This test is incompatible with concurrent allocation, which may occur
// while collecting the statistics and break the final `CHECK_EQ`s.
if (i::v8_flags.stress_concurrent_allocation) return;

v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);

const uint32_t number_of_elements = 1;
const uint32_t allocation_size = i::FixedArray::SizeFor(number_of_elements);
const uint32_t trusted_allocation_size =
i::TrustedFixedArray::SizeFor(number_of_elements);
const uint32_t lo_number_of_elements = 256 * 1024;
const uint32_t lo_allocation_size =
i::FixedArray::SizeFor(lo_number_of_elements);
const uint32_t trusted_lo_allocation_size =
i::TrustedFixedArray::SizeFor(lo_number_of_elements);
const uint32_t expected_allocation_size =
allocation_size * 2 + lo_allocation_size * 2 + trusted_allocation_size +
trusted_lo_allocation_size;

{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
LocalContext env(isolate);
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);

v8::HeapStatistics heap_stats_before;
isolate->GetHeapStatistics(&heap_stats_before);
size_t initial_allocated = heap_stats_before.total_allocated_bytes();

i::MaybeHandle<i::FixedArray> young_alloc =
i_isolate->factory()->TryNewFixedArray(number_of_elements,
i::AllocationType::kYoung);
USE(young_alloc);
i::MaybeHandle<i::FixedArray> old_alloc =
i_isolate->factory()->TryNewFixedArray(number_of_elements,
i::AllocationType::kOld);
USE(old_alloc);
i::Handle<i::TrustedFixedArray> trusted_alloc =
i_isolate->factory()->NewTrustedFixedArray(number_of_elements,
i::AllocationType::kTrusted);
USE(trusted_alloc);
i::MaybeHandle<i::FixedArray> old_lo_alloc =
i_isolate->factory()->TryNewFixedArray(lo_number_of_elements,
i::AllocationType::kOld);
USE(old_lo_alloc);

{
v8::HandleScope inner_handle_scope(isolate);
auto young_lo_alloc = i_isolate->factory()->TryNewFixedArray(
lo_number_of_elements, i::AllocationType::kYoung);
USE(young_lo_alloc);
}

auto trusted_lo_alloc = i_isolate->factory()->NewTrustedFixedArray(
lo_number_of_elements, i::AllocationType::kTrusted);
USE(trusted_lo_alloc);

v8::HeapStatistics heap_stats_after;
isolate->GetHeapStatistics(&heap_stats_after);
uint64_t final_allocated = heap_stats_after.total_allocated_bytes();

CHECK_GT(final_allocated, initial_allocated);
uint64_t allocated_diff = final_allocated - initial_allocated;
CHECK_GE(allocated_diff, expected_allocation_size);

// This either tests counting happening when a LAB freed and validate
// there's no double counting on evacuated/promoted objects.
v8::internal::heap::InvokeAtomicMajorGC(i_isolate->heap());

v8::HeapStatistics heap_stats_after_gc;
isolate->GetHeapStatistics(&heap_stats_after_gc);
uint64_t total_allocation_after_gc =
heap_stats_after_gc.total_allocated_bytes();

CHECK_EQ(total_allocation_after_gc, final_allocated);
}

isolate->Dispose();
}

#if V8_CAN_CREATE_SHARED_HEAP_BOOL

UNINITIALIZED_TEST(GetHeapTotalAllocatedBytesSharedSpaces) {
// This test is incompatible with concurrent allocation, which may occur
// while collecting the statistics and break the final `CHECK_EQ`s.
if (i::v8_flags.stress_concurrent_allocation) return;
if (COMPRESS_POINTERS_IN_MULTIPLE_CAGES_BOOL) return;

i::v8_flags.shared_heap = true;
i::FlagList::EnforceFlagImplications();

v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);

{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
LocalContext env(isolate);

v8::HeapStatistics heap_stats_before;
isolate->GetHeapStatistics(&heap_stats_before);
size_t initial_allocated = heap_stats_before.total_allocated_bytes();

i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);

const uint32_t number_of_elements = 1;
const uint32_t allocation_size = i::FixedArray::SizeFor(number_of_elements);
const uint32_t trusted_allocation_size =
i::TrustedFixedArray::SizeFor(number_of_elements);
const uint32_t lo_number_of_elements = 256 * 1024;
const uint32_t lo_allocation_size =
i::FixedArray::SizeFor(lo_number_of_elements);
const uint32_t expected_allocation_size =
allocation_size + trusted_allocation_size + lo_allocation_size;

i::MaybeHandle<i::FixedArray> shared_alloc =
i_isolate->factory()->TryNewFixedArray(number_of_elements,
i::AllocationType::kSharedOld);
USE(shared_alloc);
i::Handle<i::TrustedFixedArray> shared_trusted_alloc =
i_isolate->factory()->NewTrustedFixedArray(
number_of_elements, i::AllocationType::kSharedTrusted);
USE(shared_trusted_alloc);
i::MaybeHandle<i::FixedArray> shared_lo_alloc =
i_isolate->factory()->TryNewFixedArray(lo_number_of_elements,
i::AllocationType::kSharedOld);
USE(shared_lo_alloc);

v8::HeapStatistics heap_stats_after;
isolate->GetHeapStatistics(&heap_stats_after);
uint64_t final_allocated = heap_stats_after.total_allocated_bytes();

CHECK_GT(final_allocated, initial_allocated);
uint64_t allocated_diff = final_allocated - initial_allocated;
CHECK_GE(allocated_diff, expected_allocation_size);
}

isolate->Dispose();
}

#endif // V8_CAN_CREATE_SHARED_HEAP_BOOL

TEST(NumberOfNativeContexts) {
static const size_t kNumTestContexts = 10;
i::Isolate* isolate = CcTest::i_isolate();
Expand Down
5 changes: 3 additions & 2 deletions doc/api/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ used memory size of V8 global handles.
buffers and external strings.

`total_allocated_bytes` The value of total allocated bytes since the Isolate
creation
creation.

<!-- eslint-skip -->

Expand All @@ -271,7 +271,8 @@ creation
number_of_detached_contexts: 0,
total_global_handles_size: 8192,
used_global_handles_size: 3296,
external_memory: 318824
external_memory: 318824,
total_allocated_bytes: 45224088
}
```

Expand Down
4 changes: 2 additions & 2 deletions lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const {
stopCpuProfile: _stopCpuProfile,
isStringOneByteRepresentation: _isStringOneByteRepresentation,
updateHeapStatisticsBuffer,
getTotalAllocatedBytes,
updateHeapSpaceStatisticsBuffer,
updateHeapCodeStatisticsBuffer,
setHeapSnapshotNearHeapLimit: _setHeapSnapshotNearHeapLimit,
Expand All @@ -136,7 +137,6 @@ const {
kTotalGlobalHandlesSizeIndex,
kUsedGlobalHandlesSizeIndex,
kExternalMemoryIndex,
kTotalAllocatedBytes,

// Properties for heap spaces statistics buffer extraction.
kHeapSpaces,
Expand Down Expand Up @@ -247,7 +247,7 @@ function getHeapStatistics() {
total_global_handles_size: buffer[kTotalGlobalHandlesSizeIndex],
used_global_handles_size: buffer[kUsedGlobalHandlesSizeIndex],
external_memory: buffer[kExternalMemoryIndex],
total_allocated_bytes: buffer[kTotalAllocatedBytes],
total_allocated_bytes: getTotalAllocatedBytes(),
};
}

Expand Down
15 changes: 13 additions & 2 deletions src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ using v8::Value;
V(10, number_of_detached_contexts, kNumberOfDetachedContextsIndex) \
V(11, total_global_handles_size, kTotalGlobalHandlesSizeIndex) \
V(12, used_global_handles_size, kUsedGlobalHandlesSizeIndex) \
V(13, external_memory, kExternalMemoryIndex) \
V(14, total_allocated_bytes, kTotalAllocatedBytes)
V(13, external_memory, kExternalMemoryIndex)

#define V(a, b, c) +1
static constexpr size_t kHeapStatisticsPropertiesCount =
Expand Down Expand Up @@ -213,6 +212,12 @@ void UpdateHeapStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
#undef V
}

void GetTotalAllocatedBytes(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
uint64_t allocated_bytes = isolate->GetTotalAllocatedBytes();
args.GetReturnValue().Set(Number::New(isolate, allocated_bytes));
}


void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {
BindingData* data = Realm::GetBindingData<BindingData>(args);
Expand Down Expand Up @@ -693,6 +698,11 @@ void Initialize(Local<Object> target,
"updateHeapStatisticsBuffer",
UpdateHeapStatisticsBuffer);

SetMethod(context,
target,
"getTotalAllocatedBytes",
GetTotalAllocatedBytes);

SetMethod(context,
target,
"updateHeapCodeStatisticsBuffer",
Expand Down Expand Up @@ -774,6 +784,7 @@ void Initialize(Local<Object> target,
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(CachedDataVersionTag);
registry->Register(UpdateHeapStatisticsBuffer);
registry->Register(GetTotalAllocatedBytes);
registry->Register(UpdateHeapCodeStatisticsBuffer);
registry->Register(UpdateHeapSpaceStatisticsBuffer);
registry->Register(SetFlagsFromString);
Expand Down
2 changes: 1 addition & 1 deletion src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ void Worker::GetHeapStatistics(const FunctionCallbackInfo<Value>& args) {
Number::New(isolate, heap_stats->total_global_handles_size()),
Number::New(isolate, heap_stats->used_global_handles_size()),
Number::New(isolate, heap_stats->external_memory()),
Number::New(isolate, heap_stats->total_allocated_bytes())};
Number::New(isolate, isolate->GetTotalAllocatedBytes())};

Local<Object> obj;
if (!NewDictionaryInstanceNullProto(
Expand Down
Loading