Skip to content

Commit 6523801

Browse files
Dominik InführCommit Bot
authored andcommitted
[heap] Report total size of global handles
Report the total size of global handles in GetHeapStatistics as well. This size includes used and free global handles. Change-Id: I08c0647d993a810a37ae9f332732de9551b5ea8d Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2083020 Commit-Queue: Dominik Inführ <dinfuehr@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#66562}
1 parent a1cb332 commit 6523801

7 files changed

Lines changed: 56 additions & 1 deletion

File tree

‎include/v8.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7578,6 +7578,7 @@ class V8_EXPORT HeapStatistics {
75787578
size_t total_heap_size_executable() { return total_heap_size_executable_; }
75797579
size_t total_physical_size() { return total_physical_size_; }
75807580
size_t total_available_size() { return total_available_size_; }
7581+
size_t total_global_handles_size() { return total_global_handles_size_; }
75817582
size_t used_heap_size() { return used_heap_size_; }
75827583
size_t heap_size_limit() { return heap_size_limit_; }
75837584
size_t malloced_memory() { return malloced_memory_; }
@@ -7605,6 +7606,7 @@ class V8_EXPORT HeapStatistics {
76057606
bool does_zap_garbage_;
76067607
size_t number_of_native_contexts_;
76077608
size_t number_of_detached_contexts_;
7609+
size_t total_global_handles_size_;
76087610

76097611
friend class V8;
76107612
friend class Isolate;

‎src/api/api.cc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8483,6 +8483,7 @@ void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
84838483
heap_statistics->total_physical_size_ = heap->CommittedPhysicalMemory();
84848484
heap_statistics->total_available_size_ = heap->Available();
84858485
heap_statistics->used_heap_size_ = heap->SizeOfObjects();
8486+
heap_statistics->total_global_handles_size_ = heap->TotalGlobalHandlesSize();
84868487

84878488
#ifndef V8_SHARED_RO_HEAP
84888489
i::ReadOnlySpace* ro_space = heap->read_only_space();

‎src/handles/global-handles.cc‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ class GlobalHandles::NodeSpace final {
186186
iterator begin() { return iterator(first_used_block_); }
187187
iterator end() { return iterator(nullptr); }
188188

189+
size_t TotalSize() const { return blocks_ * sizeof(NodeType) * kBlockSize; }
190+
189191
private:
190192
void PutNodesOnFreeList(BlockType* block);
191193
V8_INLINE void Free(NodeType* node);
@@ -194,6 +196,7 @@ class GlobalHandles::NodeSpace final {
194196
BlockType* first_block_ = nullptr;
195197
BlockType* first_used_block_ = nullptr;
196198
NodeType* first_free_ = nullptr;
199+
size_t blocks_ = 0;
197200
};
198201

199202
template <class NodeType>
@@ -210,6 +213,7 @@ template <class NodeType>
210213
NodeType* GlobalHandles::NodeSpace<NodeType>::Acquire(Object object) {
211214
if (first_free_ == nullptr) {
212215
first_block_ = new BlockType(global_handles_, this, first_block_);
216+
blocks_++;
213217
PutNodesOnFreeList(first_block_);
214218
}
215219
DCHECK_NOT_NULL(first_free_);
@@ -875,6 +879,10 @@ size_t GlobalHandles::NumberOfOnStackHandlesForTesting() {
875879
return on_stack_nodes_->NumberOfHandlesForTesting();
876880
}
877881

882+
size_t GlobalHandles::TotalSize() const {
883+
return regular_nodes_->TotalSize() + traced_nodes_->TotalSize();
884+
}
885+
878886
void GlobalHandles::SetStackStart(void* stack_start) {
879887
on_stack_nodes_->SetStackStart(stack_start);
880888
}

‎src/handles/global-handles.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ class V8_EXPORT_PRIVATE GlobalHandles final {
176176

177177
Isolate* isolate() const { return isolate_; }
178178

179+
size_t TotalSize() const;
180+
179181
// Number of global handles.
180182
size_t handles_count() const { return handles_count_; }
181183

‎src/heap/heap.cc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,10 @@ size_t Heap::SizeOfObjects() {
867867
return total;
868868
}
869869

870+
size_t Heap::TotalGlobalHandlesSize() {
871+
return isolate_->global_handles()->TotalSize();
872+
}
873+
870874
// static
871875
const char* Heap::GetSpaceName(AllocationSpace space) {
872876
switch (space) {

‎src/heap/heap.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,9 +1195,12 @@ class Heap {
11951195
// all available bytes. Check MaxHeapObjectSize() instead.
11961196
size_t Available();
11971197

1198-
// Returns of size of all objects residing in the heap.
1198+
// Returns size of all objects residing in the heap.
11991199
V8_EXPORT_PRIVATE size_t SizeOfObjects();
12001200

1201+
// Returns size of all global handles in the heap.
1202+
V8_EXPORT_PRIVATE size_t TotalGlobalHandlesSize();
1203+
12011204
void UpdateSurvivalStatistics(int start_new_space_size);
12021205

12031206
inline void IncrementPromotedObjectsSize(size_t object_size) {

‎test/cctest/test-global-handles.cc‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ void ConstructJSObject(v8::Isolate* isolate, v8::Global<v8::Object>* global) {
9898
CHECK(!global->IsEmpty());
9999
}
100100

101+
void ConstructJSObject(v8::Isolate* isolate,
102+
v8::TracedGlobal<v8::Object>* traced) {
103+
v8::HandleScope scope(isolate);
104+
v8::Local<v8::Object> object(v8::Object::New(isolate));
105+
CHECK(!object.IsEmpty());
106+
*traced = v8::TracedGlobal<v8::Object>(isolate, object);
107+
CHECK(!traced->IsEmpty());
108+
}
109+
101110
template <typename HandleContainer>
102111
void ConstructJSApiObject(v8::Isolate* isolate, v8::Local<v8::Context> context,
103112
HandleContainer* flag_and_persistent) {
@@ -667,5 +676,31 @@ TEST(MoveWeakGlobal) {
667676
InvokeMarkSweep();
668677
}
669678

679+
TEST(TotalSizeRegularNode) {
680+
CcTest::InitializeVM();
681+
v8::Isolate* isolate = CcTest::isolate();
682+
Isolate* i_isolate = CcTest::i_isolate();
683+
v8::HandleScope scope(isolate);
684+
685+
v8::Global<v8::Object>* global = new Global<v8::Object>();
686+
CHECK_EQ(i_isolate->global_handles()->TotalSize(), 0);
687+
ConstructJSObject(isolate, global);
688+
CHECK_GT(i_isolate->global_handles()->TotalSize(), 0);
689+
delete global;
690+
}
691+
692+
TEST(TotalSizeTracedNode) {
693+
CcTest::InitializeVM();
694+
v8::Isolate* isolate = CcTest::isolate();
695+
Isolate* i_isolate = CcTest::i_isolate();
696+
v8::HandleScope scope(isolate);
697+
698+
v8::TracedGlobal<v8::Object>* global = new TracedGlobal<v8::Object>();
699+
CHECK_EQ(i_isolate->global_handles()->TotalSize(), 0);
700+
ConstructJSObject(isolate, global);
701+
CHECK_GT(i_isolate->global_handles()->TotalSize(), 0);
702+
delete global;
703+
}
704+
670705
} // namespace internal
671706
} // namespace v8

0 commit comments

Comments
 (0)