blob: 6b26c3129621b94749c913171e0e85a76955ab37 [file] [log] [blame]
ssid83aa5be2015-05-08 12:03:261// 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.
4
5#include "gin/v8_isolate_memory_dump_provider.h"
6
avi90e658dd2015-12-21 07:16:197#include <stddef.h>
8
ssid83aa5be2015-05-08 12:03:269#include "base/strings/stringprintf.h"
gab54909b722016-05-11 18:34:1110#include "base/threading/thread_task_runner_handle.h"
ssid83aa5be2015-05-08 12:03:2611#include "base/trace_event/memory_dump_manager.h"
12#include "base/trace_event/process_memory_dump.h"
13#include "gin/public/isolate_holder.h"
14#include "v8/include/v8.h"
15
16namespace gin {
17
ssid83aa5be2015-05-08 12:03:2618V8IsolateMemoryDumpProvider::V8IsolateMemoryDumpProvider(
19 IsolateHolder* isolate_holder)
20 : isolate_holder_(isolate_holder) {
21 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
primiano186d6bfe2015-10-30 13:21:4022 this, "V8Isolate", base::ThreadTaskRunnerHandle::Get());
ssid83aa5be2015-05-08 12:03:2623}
24
25V8IsolateMemoryDumpProvider::~V8IsolateMemoryDumpProvider() {
26 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
27 this);
28}
29
30// Called at trace dump point time. Creates a snapshot with the memory counters
31// for the current isolate.
32bool V8IsolateMemoryDumpProvider::OnMemoryDump(
ssid90694aeec2015-08-06 13:01:3033 const base::trace_event::MemoryDumpArgs& args,
ssidf51216b02015-06-04 19:46:2334 base::trace_event::ProcessMemoryDump* process_memory_dump) {
ssid90694aeec2015-08-06 13:01:3035 // TODO(ssid): Use MemoryDumpArgs to create light dumps when requested
36 // (crbug.com/499731).
37
ssid83aa5be2015-05-08 12:03:2638 if (isolate_holder_->access_mode() == IsolateHolder::kUseLocker) {
39 v8::Locker locked(isolate_holder_->isolate());
ssid2888a242015-08-07 23:08:4240 DumpHeapStatistics(args, process_memory_dump);
ssid83aa5be2015-05-08 12:03:2641 } else {
ssid2888a242015-08-07 23:08:4242 DumpHeapStatistics(args, process_memory_dump);
ssid83aa5be2015-05-08 12:03:2643 }
44 return true;
45}
46
mythria35540662016-06-03 15:02:0547namespace {
48
49// Dump statistics related to code/bytecode when memory-infra.v8.code_stats is
50// enabled.
51void DumpCodeStatistics(
52 base::trace_event::MemoryAllocatorDump* heap_spaces_dump,
53 IsolateHolder* isolate_holder) {
54 // Collecting code statistics is an expensive operation (~10 ms) when
55 // compared to other v8 metrics (< 1 ms). So, dump them only when
56 // memory-infra.v8.code_stats is enabled.
57 // TODO(primiano): This information should be plumbed through TraceConfig.
58 // See crbug.com/616441.
59 bool dump_code_stats = false;
60 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
61 TRACE_DISABLED_BY_DEFAULT("memory-infra.v8.code_stats"),
62 &dump_code_stats);
63 if (!dump_code_stats)
64 return;
65
66 v8::HeapCodeStatistics code_statistics;
67 if (!isolate_holder->isolate()->GetHeapCodeAndMetadataStatistics(
68 &code_statistics)) {
69 return;
70 }
71
72 heap_spaces_dump->AddScalar(
73 "code_and_metadata_size",
74 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
75 code_statistics.code_and_metadata_size());
76 heap_spaces_dump->AddScalar(
77 "bytecode_and_metadata_size",
78 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
79 code_statistics.bytecode_and_metadata_size());
80}
81
82} // namespace anonymous
83
sside36cfaf2015-06-12 16:42:2084void V8IsolateMemoryDumpProvider::DumpHeapStatistics(
ssid2888a242015-08-07 23:08:4285 const base::trace_event::MemoryDumpArgs& args,
sside36cfaf2015-06-12 16:42:2086 base::trace_event::ProcessMemoryDump* process_memory_dump) {
87 std::string dump_base_name =
88 base::StringPrintf("v8/isolate_%p", isolate_holder_->isolate());
89
90 // Dump statistics of the heap's spaces.
91 std::string space_name_prefix = dump_base_name + "/heap_spaces";
ssid83aa5be2015-05-08 12:03:2692 v8::HeapStatistics heap_statistics;
93 isolate_holder_->isolate()->GetHeapStatistics(&heap_statistics);
94
95 size_t known_spaces_used_size = 0;
96 size_t known_spaces_size = 0;
primiano553bea9a2015-09-09 16:37:3597 size_t known_spaces_physical_size = 0;
ssid83aa5be2015-05-08 12:03:2698 size_t number_of_spaces = isolate_holder_->isolate()->NumberOfHeapSpaces();
99 for (size_t space = 0; space < number_of_spaces; space++) {
100 v8::HeapSpaceStatistics space_statistics;
101 isolate_holder_->isolate()->GetHeapSpaceStatistics(&space_statistics,
102 space);
103 const size_t space_size = space_statistics.space_size();
104 const size_t space_used_size = space_statistics.space_used_size();
primiano553bea9a2015-09-09 16:37:35105 const size_t space_physical_size = space_statistics.physical_space_size();
ssid83aa5be2015-05-08 12:03:26106
107 known_spaces_size += space_size;
108 known_spaces_used_size += space_used_size;
primiano553bea9a2015-09-09 16:37:35109 known_spaces_physical_size += space_physical_size;
ssid83aa5be2015-05-08 12:03:26110
sside36cfaf2015-06-12 16:42:20111 std::string space_dump_name =
112 space_name_prefix + "/" + space_statistics.space_name();
113 auto space_dump = process_memory_dump->CreateAllocatorDump(space_dump_name);
114 space_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
ssid83aa5be2015-05-08 12:03:26115 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
primiano553bea9a2015-09-09 16:37:35116 space_physical_size);
117
118 space_dump->AddScalar("virtual_size",
119 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
sside36cfaf2015-06-12 16:42:20120 space_size);
121
primiano553bea9a2015-09-09 16:37:35122 space_dump->AddScalar("allocated_objects_size",
123 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
124 space_used_size);
ssid83aa5be2015-05-08 12:03:26125 }
ssidf51216b02015-06-04 19:46:23126
ssid83aa5be2015-05-08 12:03:26127 // Compute the rest of the memory, not accounted by the spaces above.
sside36cfaf2015-06-12 16:42:20128 std::string other_spaces_name = space_name_prefix + "/other_spaces";
129 auto other_dump = process_memory_dump->CreateAllocatorDump(other_spaces_name);
sside36cfaf2015-06-12 16:42:20130
primiano553bea9a2015-09-09 16:37:35131 other_dump->AddScalar(
sside36cfaf2015-06-12 16:42:20132 base::trace_event::MemoryAllocatorDump::kNameSize,
ssid83aa5be2015-05-08 12:03:26133 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
primiano553bea9a2015-09-09 16:37:35134 heap_statistics.total_physical_size() - known_spaces_physical_size);
135
136 other_dump->AddScalar(
137 "allocated_objects_size",
138 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
ssid83aa5be2015-05-08 12:03:26139 heap_statistics.used_heap_size() - known_spaces_used_size);
140
primiano553bea9a2015-09-09 16:37:35141 other_dump->AddScalar("virtual_size",
142 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
143 heap_statistics.total_heap_size() - known_spaces_size);
144
ssidbce6ee42015-11-05 02:35:39145 // If V8 zaps garbage, all the memory mapped regions become resident,
146 // so we add an extra dump to avoid mismatches w.r.t. the total
147 // resident values.
148 if (heap_statistics.does_zap_garbage()) {
149 auto zap_dump = process_memory_dump->CreateAllocatorDump(
150 dump_base_name + "/zapped_for_debug");
151 zap_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
152 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
153 heap_statistics.total_heap_size() -
154 heap_statistics.total_physical_size());
155 }
156
jochen07eb75792016-04-09 20:34:02157 // Dump statistics about malloced memory.
158 std::string malloc_name = dump_base_name + "/malloc";
159 auto malloc_dump = process_memory_dump->CreateAllocatorDump(malloc_name);
160 malloc_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
161 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
162 heap_statistics.malloced_memory());
163 const char* system_allocator_name =
164 base::trace_event::MemoryDumpManager::GetInstance()
165 ->system_allocator_pool_name();
166 if (system_allocator_name) {
167 process_memory_dump->AddSuballocation(malloc_dump->guid(),
168 system_allocator_name);
169 }
170
mythria35540662016-06-03 15:02:05171 // Add an empty row for the heap_spaces. This is to keep the shape of the
172 // dump stable, whether code stats are enabled or not.
173 auto heap_spaces_dump =
174 process_memory_dump->CreateAllocatorDump(space_name_prefix);
175
ssid3011f202016-06-01 20:24:26176 // Dump object statistics only for detailed dumps.
177 if (args.level_of_detail !=
178 base::trace_event::MemoryDumpLevelOfDetail::DETAILED) {
ssid2888a242015-08-07 23:08:42179 return;
ssid3011f202016-06-01 20:24:26180 }
ssid2888a242015-08-07 23:08:42181
sside36cfaf2015-06-12 16:42:20182 // Dump statistics of the heap's live objects from last GC.
primiano553bea9a2015-09-09 16:37:35183 // TODO(primiano): these should not be tracked in the same trace event as they
184 // report stats for the last GC (not the current state). See crbug.com/498779.
185 std::string object_name_prefix = dump_base_name + "/heap_objects_at_last_gc";
sside36cfaf2015-06-12 16:42:20186 bool did_dump_object_stats = false;
ssidf51216b02015-06-04 19:46:23187 const size_t object_types =
188 isolate_holder_->isolate()->NumberOfTrackedHeapObjectTypes();
189 for (size_t type_index = 0; type_index < object_types; type_index++) {
190 v8::HeapObjectStatistics object_statistics;
191 if (!isolate_holder_->isolate()->GetHeapObjectStatisticsAtLastGC(
192 &object_statistics, type_index))
193 continue;
194
195 std::string dump_name =
sside36cfaf2015-06-12 16:42:20196 object_name_prefix + "/" + object_statistics.object_type();
ssidf51216b02015-06-04 19:46:23197 if (object_statistics.object_sub_type()[0] != '\0')
198 dump_name += std::string("/") + object_statistics.object_sub_type();
sside36cfaf2015-06-12 16:42:20199 auto object_dump = process_memory_dump->CreateAllocatorDump(dump_name);
ssidf51216b02015-06-04 19:46:23200
201 object_dump->AddScalar(
bratell10f2e3c2015-09-10 16:28:43202 base::trace_event::MemoryAllocatorDump::kNameObjectCount,
ssidf51216b02015-06-04 19:46:23203 base::trace_event::MemoryAllocatorDump::kUnitsObjects,
204 object_statistics.object_count());
sside36cfaf2015-06-12 16:42:20205 object_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
206 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
207 object_statistics.object_size());
208 did_dump_object_stats = true;
ssidf51216b02015-06-04 19:46:23209 }
210
sside36cfaf2015-06-12 16:42:20211 if (process_memory_dump->GetAllocatorDump(object_name_prefix +
212 "/CODE_TYPE")) {
213 auto code_kind_dump = process_memory_dump->CreateAllocatorDump(
214 object_name_prefix + "/CODE_TYPE/CODE_KIND");
215 auto code_age_dump = process_memory_dump->CreateAllocatorDump(
216 object_name_prefix + "/CODE_TYPE/CODE_AGE");
217 process_memory_dump->AddOwnershipEdge(code_kind_dump->guid(),
218 code_age_dump->guid());
219 }
220
221 if (did_dump_object_stats) {
222 process_memory_dump->AddOwnershipEdge(
223 process_memory_dump->CreateAllocatorDump(object_name_prefix)->guid(),
mythria35540662016-06-03 15:02:05224 heap_spaces_dump->guid());
sside36cfaf2015-06-12 16:42:20225 }
mythria35540662016-06-03 15:02:05226
227 // Dump statistics related to code and bytecode if requested.
228 DumpCodeStatistics(heap_spaces_dump, isolate_holder_);
ssidf51216b02015-06-04 19:46:23229}
230
ssid83aa5be2015-05-08 12:03:26231} // namespace gin