ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "gin/v8_isolate_memory_dump_provider.h" |
| 6 | |
ssid | 4205d7b | 2016-06-15 20:36:03 | [diff] [blame] | 7 | #include <inttypes.h> |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 10 | #include "base/strings/stringprintf.h" |
gab | 54909b72 | 2016-05-11 18:34:11 | [diff] [blame] | 11 | #include "base/threading/thread_task_runner_handle.h" |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 12 | #include "base/trace_event/memory_dump_manager.h" |
| 13 | #include "base/trace_event/process_memory_dump.h" |
| 14 | #include "gin/public/isolate_holder.h" |
| 15 | #include "v8/include/v8.h" |
| 16 | |
| 17 | namespace gin { |
| 18 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 19 | V8IsolateMemoryDumpProvider::V8IsolateMemoryDumpProvider( |
| 20 | IsolateHolder* isolate_holder) |
| 21 | : isolate_holder_(isolate_holder) { |
| 22 | base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
primiano | 186d6bfe | 2015-10-30 13:21:40 | [diff] [blame] | 23 | this, "V8Isolate", base::ThreadTaskRunnerHandle::Get()); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | V8IsolateMemoryDumpProvider::~V8IsolateMemoryDumpProvider() { |
| 27 | base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
| 28 | this); |
| 29 | } |
| 30 | |
| 31 | // Called at trace dump point time. Creates a snapshot with the memory counters |
| 32 | // for the current isolate. |
| 33 | bool V8IsolateMemoryDumpProvider::OnMemoryDump( |
ssid | 90694aeec | 2015-08-06 13:01:30 | [diff] [blame] | 34 | const base::trace_event::MemoryDumpArgs& args, |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 35 | base::trace_event::ProcessMemoryDump* process_memory_dump) { |
ssid | 90694aeec | 2015-08-06 13:01:30 | [diff] [blame] | 36 | // TODO(ssid): Use MemoryDumpArgs to create light dumps when requested |
| 37 | // (crbug.com/499731). |
| 38 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 39 | if (isolate_holder_->access_mode() == IsolateHolder::kUseLocker) { |
| 40 | v8::Locker locked(isolate_holder_->isolate()); |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 41 | DumpHeapStatistics(args, process_memory_dump); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 42 | } else { |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 43 | DumpHeapStatistics(args, process_memory_dump); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame] | 48 | namespace { |
| 49 | |
| 50 | // Dump statistics related to code/bytecode when memory-infra.v8.code_stats is |
| 51 | // enabled. |
| 52 | void DumpCodeStatistics( |
| 53 | base::trace_event::MemoryAllocatorDump* heap_spaces_dump, |
| 54 | IsolateHolder* isolate_holder) { |
| 55 | // Collecting code statistics is an expensive operation (~10 ms) when |
| 56 | // compared to other v8 metrics (< 1 ms). So, dump them only when |
| 57 | // memory-infra.v8.code_stats is enabled. |
| 58 | // TODO(primiano): This information should be plumbed through TraceConfig. |
| 59 | // See crbug.com/616441. |
| 60 | bool dump_code_stats = false; |
| 61 | TRACE_EVENT_CATEGORY_GROUP_ENABLED( |
| 62 | TRACE_DISABLED_BY_DEFAULT("memory-infra.v8.code_stats"), |
| 63 | &dump_code_stats); |
| 64 | if (!dump_code_stats) |
| 65 | return; |
| 66 | |
| 67 | v8::HeapCodeStatistics code_statistics; |
| 68 | if (!isolate_holder->isolate()->GetHeapCodeAndMetadataStatistics( |
| 69 | &code_statistics)) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | heap_spaces_dump->AddScalar( |
| 74 | "code_and_metadata_size", |
| 75 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 76 | code_statistics.code_and_metadata_size()); |
| 77 | heap_spaces_dump->AddScalar( |
| 78 | "bytecode_and_metadata_size", |
| 79 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 80 | code_statistics.bytecode_and_metadata_size()); |
| 81 | } |
| 82 | |
| 83 | } // namespace anonymous |
| 84 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 85 | void V8IsolateMemoryDumpProvider::DumpHeapStatistics( |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 86 | const base::trace_event::MemoryDumpArgs& args, |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 87 | base::trace_event::ProcessMemoryDump* process_memory_dump) { |
ssid | 4205d7b | 2016-06-15 20:36:03 | [diff] [blame] | 88 | std::string dump_base_name = base::StringPrintf( |
| 89 | "v8/isolate_0x%" PRIXPTR, |
| 90 | reinterpret_cast<uintptr_t>(isolate_holder_->isolate())); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 91 | |
| 92 | // Dump statistics of the heap's spaces. |
| 93 | std::string space_name_prefix = dump_base_name + "/heap_spaces"; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 94 | v8::HeapStatistics heap_statistics; |
| 95 | isolate_holder_->isolate()->GetHeapStatistics(&heap_statistics); |
| 96 | |
| 97 | size_t known_spaces_used_size = 0; |
| 98 | size_t known_spaces_size = 0; |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 99 | size_t known_spaces_physical_size = 0; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 100 | size_t number_of_spaces = isolate_holder_->isolate()->NumberOfHeapSpaces(); |
| 101 | for (size_t space = 0; space < number_of_spaces; space++) { |
| 102 | v8::HeapSpaceStatistics space_statistics; |
| 103 | isolate_holder_->isolate()->GetHeapSpaceStatistics(&space_statistics, |
| 104 | space); |
| 105 | const size_t space_size = space_statistics.space_size(); |
| 106 | const size_t space_used_size = space_statistics.space_used_size(); |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 107 | const size_t space_physical_size = space_statistics.physical_space_size(); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 108 | |
| 109 | known_spaces_size += space_size; |
| 110 | known_spaces_used_size += space_used_size; |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 111 | known_spaces_physical_size += space_physical_size; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 112 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 113 | std::string space_dump_name = |
| 114 | space_name_prefix + "/" + space_statistics.space_name(); |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 115 | auto* space_dump = |
| 116 | process_memory_dump->CreateAllocatorDump(space_dump_name); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 117 | space_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 118 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 119 | space_physical_size); |
| 120 | |
| 121 | space_dump->AddScalar("virtual_size", |
| 122 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 123 | space_size); |
| 124 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 125 | space_dump->AddScalar("allocated_objects_size", |
| 126 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 127 | space_used_size); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 128 | } |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 129 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 130 | // Compute the rest of the memory, not accounted by the spaces above. |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 131 | std::string other_spaces_name = space_name_prefix + "/other_spaces"; |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 132 | auto* other_dump = |
| 133 | process_memory_dump->CreateAllocatorDump(other_spaces_name); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 134 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 135 | other_dump->AddScalar( |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 136 | base::trace_event::MemoryAllocatorDump::kNameSize, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 137 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 138 | heap_statistics.total_physical_size() - known_spaces_physical_size); |
| 139 | |
| 140 | other_dump->AddScalar( |
| 141 | "allocated_objects_size", |
| 142 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 143 | heap_statistics.used_heap_size() - known_spaces_used_size); |
| 144 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 145 | other_dump->AddScalar("virtual_size", |
| 146 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 147 | heap_statistics.total_heap_size() - known_spaces_size); |
| 148 | |
ssid | bce6ee4 | 2015-11-05 02:35:39 | [diff] [blame] | 149 | // If V8 zaps garbage, all the memory mapped regions become resident, |
| 150 | // so we add an extra dump to avoid mismatches w.r.t. the total |
| 151 | // resident values. |
| 152 | if (heap_statistics.does_zap_garbage()) { |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 153 | auto* zap_dump = process_memory_dump->CreateAllocatorDump( |
ssid | bce6ee4 | 2015-11-05 02:35:39 | [diff] [blame] | 154 | dump_base_name + "/zapped_for_debug"); |
| 155 | zap_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 156 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 157 | heap_statistics.total_heap_size() - |
| 158 | heap_statistics.total_physical_size()); |
| 159 | } |
| 160 | |
jochen | 07eb7579 | 2016-04-09 20:34:02 | [diff] [blame] | 161 | // Dump statistics about malloced memory. |
| 162 | std::string malloc_name = dump_base_name + "/malloc"; |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 163 | auto* malloc_dump = process_memory_dump->CreateAllocatorDump(malloc_name); |
jochen | 07eb7579 | 2016-04-09 20:34:02 | [diff] [blame] | 164 | malloc_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 165 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 166 | heap_statistics.malloced_memory()); |
jochen | 077e9d7 | 2016-07-19 19:22:38 | [diff] [blame] | 167 | malloc_dump->AddScalar("peak_size", |
| 168 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 169 | heap_statistics.peak_malloced_memory()); |
jochen | 07eb7579 | 2016-04-09 20:34:02 | [diff] [blame] | 170 | const char* system_allocator_name = |
| 171 | base::trace_event::MemoryDumpManager::GetInstance() |
| 172 | ->system_allocator_pool_name(); |
| 173 | if (system_allocator_name) { |
| 174 | process_memory_dump->AddSuballocation(malloc_dump->guid(), |
| 175 | system_allocator_name); |
| 176 | } |
| 177 | |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame] | 178 | // Add an empty row for the heap_spaces. This is to keep the shape of the |
| 179 | // dump stable, whether code stats are enabled or not. |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 180 | auto* heap_spaces_dump = |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame] | 181 | process_memory_dump->CreateAllocatorDump(space_name_prefix); |
| 182 | |
rmcilroy | 6b6b89a | 2016-08-25 15:12:57 | [diff] [blame^] | 183 | // Dump statistics related to code and bytecode if requested. |
| 184 | DumpCodeStatistics(heap_spaces_dump, isolate_holder_); |
| 185 | |
ssid | 3011f20 | 2016-06-01 20:24:26 | [diff] [blame] | 186 | // Dump object statistics only for detailed dumps. |
| 187 | if (args.level_of_detail != |
| 188 | base::trace_event::MemoryDumpLevelOfDetail::DETAILED) { |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 189 | return; |
ssid | 3011f20 | 2016-06-01 20:24:26 | [diff] [blame] | 190 | } |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 191 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 192 | // Dump statistics of the heap's live objects from last GC. |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 193 | // TODO(primiano): these should not be tracked in the same trace event as they |
| 194 | // report stats for the last GC (not the current state). See crbug.com/498779. |
| 195 | std::string object_name_prefix = dump_base_name + "/heap_objects_at_last_gc"; |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 196 | bool did_dump_object_stats = false; |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 197 | const size_t object_types = |
| 198 | isolate_holder_->isolate()->NumberOfTrackedHeapObjectTypes(); |
| 199 | for (size_t type_index = 0; type_index < object_types; type_index++) { |
| 200 | v8::HeapObjectStatistics object_statistics; |
| 201 | if (!isolate_holder_->isolate()->GetHeapObjectStatisticsAtLastGC( |
| 202 | &object_statistics, type_index)) |
| 203 | continue; |
| 204 | |
| 205 | std::string dump_name = |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 206 | object_name_prefix + "/" + object_statistics.object_type(); |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 207 | if (object_statistics.object_sub_type()[0] != '\0') |
| 208 | dump_name += std::string("/") + object_statistics.object_sub_type(); |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 209 | auto* object_dump = process_memory_dump->CreateAllocatorDump(dump_name); |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 210 | |
| 211 | object_dump->AddScalar( |
bratell | 10f2e3c | 2015-09-10 16:28:43 | [diff] [blame] | 212 | base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 213 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 214 | object_statistics.object_count()); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 215 | object_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 216 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 217 | object_statistics.object_size()); |
| 218 | did_dump_object_stats = true; |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 219 | } |
| 220 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 221 | if (process_memory_dump->GetAllocatorDump(object_name_prefix + |
| 222 | "/CODE_TYPE")) { |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 223 | auto* code_kind_dump = process_memory_dump->CreateAllocatorDump( |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 224 | object_name_prefix + "/CODE_TYPE/CODE_KIND"); |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 225 | auto* code_age_dump = process_memory_dump->CreateAllocatorDump( |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 226 | object_name_prefix + "/CODE_TYPE/CODE_AGE"); |
| 227 | process_memory_dump->AddOwnershipEdge(code_kind_dump->guid(), |
| 228 | code_age_dump->guid()); |
| 229 | } |
| 230 | |
| 231 | if (did_dump_object_stats) { |
| 232 | process_memory_dump->AddOwnershipEdge( |
| 233 | process_memory_dump->CreateAllocatorDump(object_name_prefix)->guid(), |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame] | 234 | heap_spaces_dump->guid()); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 235 | } |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 236 | } |
| 237 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 238 | } // namespace gin |