1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gin/v8_isolate_memory_dump_provider.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "base/trace_event/memory_dump_manager.h"
10 #include "base/trace_event/process_memory_dump.h"
11 #include "gin/public/isolate_holder.h"
12 #include "v8/include/v8.h"
16 V8IsolateMemoryDumpProvider::V8IsolateMemoryDumpProvider(
17 IsolateHolder
* isolate_holder
)
18 : isolate_holder_(isolate_holder
) {
19 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
20 this, base::ThreadTaskRunnerHandle::Get());
23 V8IsolateMemoryDumpProvider::~V8IsolateMemoryDumpProvider() {
24 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
28 // Called at trace dump point time. Creates a snapshot with the memory counters
29 // for the current isolate.
30 bool V8IsolateMemoryDumpProvider::OnMemoryDump(
31 const base::trace_event::MemoryDumpArgs
& args
,
32 base::trace_event::ProcessMemoryDump
* process_memory_dump
) {
33 // TODO(ssid): Use MemoryDumpArgs to create light dumps when requested
34 // (crbug.com/499731).
36 if (isolate_holder_
->access_mode() == IsolateHolder::kUseLocker
) {
37 v8::Locker
locked(isolate_holder_
->isolate());
38 DumpHeapStatistics(process_memory_dump
);
40 DumpHeapStatistics(process_memory_dump
);
45 void V8IsolateMemoryDumpProvider::DumpHeapStatistics(
46 base::trace_event::ProcessMemoryDump
* process_memory_dump
) {
47 std::string dump_base_name
=
48 base::StringPrintf("v8/isolate_%p", isolate_holder_
->isolate());
50 // Dump statistics of the heap's spaces.
51 std::string space_name_prefix
= dump_base_name
+ "/heap_spaces";
52 v8::HeapStatistics heap_statistics
;
53 isolate_holder_
->isolate()->GetHeapStatistics(&heap_statistics
);
55 size_t known_spaces_used_size
= 0;
56 size_t known_spaces_size
= 0;
57 size_t number_of_spaces
= isolate_holder_
->isolate()->NumberOfHeapSpaces();
58 for (size_t space
= 0; space
< number_of_spaces
; space
++) {
59 v8::HeapSpaceStatistics space_statistics
;
60 isolate_holder_
->isolate()->GetHeapSpaceStatistics(&space_statistics
,
62 const size_t space_size
= space_statistics
.space_size();
63 const size_t space_used_size
= space_statistics
.space_used_size();
65 known_spaces_size
+= space_size
;
66 known_spaces_used_size
+= space_used_size
;
68 std::string space_dump_name
=
69 space_name_prefix
+ "/" + space_statistics
.space_name();
70 auto space_dump
= process_memory_dump
->CreateAllocatorDump(space_dump_name
);
71 space_dump
->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize
,
72 base::trace_event::MemoryAllocatorDump::kUnitsBytes
,
75 auto space_allocated_dump
= process_memory_dump
->CreateAllocatorDump(
76 space_dump_name
+ "/allocated_objects");
77 space_allocated_dump
->AddScalar(
78 base::trace_event::MemoryAllocatorDump::kNameSize
,
79 base::trace_event::MemoryAllocatorDump::kUnitsBytes
, space_used_size
);
82 // Compute the rest of the memory, not accounted by the spaces above.
83 std::string other_spaces_name
= space_name_prefix
+ "/other_spaces";
84 auto other_dump
= process_memory_dump
->CreateAllocatorDump(other_spaces_name
);
85 other_dump
->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize
,
86 base::trace_event::MemoryAllocatorDump::kUnitsBytes
,
87 heap_statistics
.total_heap_size() - known_spaces_size
);
89 auto other_allocated_dump
= process_memory_dump
->CreateAllocatorDump(
90 other_spaces_name
+ "/allocated_objects");
91 other_allocated_dump
->AddScalar(
92 base::trace_event::MemoryAllocatorDump::kNameSize
,
93 base::trace_event::MemoryAllocatorDump::kUnitsBytes
,
94 heap_statistics
.used_heap_size() - known_spaces_used_size
);
96 // Dump statistics of the heap's live objects from last GC.
97 std::string object_name_prefix
= dump_base_name
+ "/heap_objects";
98 bool did_dump_object_stats
= false;
99 const size_t object_types
=
100 isolate_holder_
->isolate()->NumberOfTrackedHeapObjectTypes();
101 for (size_t type_index
= 0; type_index
< object_types
; type_index
++) {
102 v8::HeapObjectStatistics object_statistics
;
103 if (!isolate_holder_
->isolate()->GetHeapObjectStatisticsAtLastGC(
104 &object_statistics
, type_index
))
107 std::string dump_name
=
108 object_name_prefix
+ "/" + object_statistics
.object_type();
109 if (object_statistics
.object_sub_type()[0] != '\0')
110 dump_name
+= std::string("/") + object_statistics
.object_sub_type();
111 auto object_dump
= process_memory_dump
->CreateAllocatorDump(dump_name
);
113 object_dump
->AddScalar(
114 base::trace_event::MemoryAllocatorDump::kNameObjectsCount
,
115 base::trace_event::MemoryAllocatorDump::kUnitsObjects
,
116 object_statistics
.object_count());
117 object_dump
->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize
,
118 base::trace_event::MemoryAllocatorDump::kUnitsBytes
,
119 object_statistics
.object_size());
120 did_dump_object_stats
= true;
123 if (process_memory_dump
->GetAllocatorDump(object_name_prefix
+
125 auto code_kind_dump
= process_memory_dump
->CreateAllocatorDump(
126 object_name_prefix
+ "/CODE_TYPE/CODE_KIND");
127 auto code_age_dump
= process_memory_dump
->CreateAllocatorDump(
128 object_name_prefix
+ "/CODE_TYPE/CODE_AGE");
129 process_memory_dump
->AddOwnershipEdge(code_kind_dump
->guid(),
130 code_age_dump
->guid());
133 if (did_dump_object_stats
) {
134 process_memory_dump
->AddOwnershipEdge(
135 process_memory_dump
->CreateAllocatorDump(object_name_prefix
)->guid(),
136 process_memory_dump
->CreateAllocatorDump(space_name_prefix
)->guid());