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 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 9 | #include "base/strings/stringprintf.h" |
gab | 54909b72 | 2016-05-11 18:34:11 | [diff] [blame] | 10 | #include "base/threading/thread_task_runner_handle.h" |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 11 | #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 | |
| 16 | namespace gin { |
| 17 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 18 | V8IsolateMemoryDumpProvider::V8IsolateMemoryDumpProvider( |
| 19 | IsolateHolder* isolate_holder) |
| 20 | : isolate_holder_(isolate_holder) { |
| 21 | base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
primiano | 186d6bfe | 2015-10-30 13:21:40 | [diff] [blame] | 22 | this, "V8Isolate", base::ThreadTaskRunnerHandle::Get()); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | V8IsolateMemoryDumpProvider::~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. |
| 32 | bool V8IsolateMemoryDumpProvider::OnMemoryDump( |
ssid | 90694aeec | 2015-08-06 13:01:30 | [diff] [blame] | 33 | const base::trace_event::MemoryDumpArgs& args, |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 34 | base::trace_event::ProcessMemoryDump* process_memory_dump) { |
ssid | 90694aeec | 2015-08-06 13:01:30 | [diff] [blame] | 35 | // TODO(ssid): Use MemoryDumpArgs to create light dumps when requested |
| 36 | // (crbug.com/499731). |
| 37 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 38 | if (isolate_holder_->access_mode() == IsolateHolder::kUseLocker) { |
| 39 | v8::Locker locked(isolate_holder_->isolate()); |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 40 | DumpHeapStatistics(args, process_memory_dump); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 41 | } else { |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 42 | DumpHeapStatistics(args, process_memory_dump); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 43 | } |
| 44 | return true; |
| 45 | } |
| 46 | |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame^] | 47 | namespace { |
| 48 | |
| 49 | // Dump statistics related to code/bytecode when memory-infra.v8.code_stats is |
| 50 | // enabled. |
| 51 | void 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 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 84 | void V8IsolateMemoryDumpProvider::DumpHeapStatistics( |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 85 | const base::trace_event::MemoryDumpArgs& args, |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 86 | 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"; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 92 | 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; |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 97 | size_t known_spaces_physical_size = 0; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 98 | 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(); |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 105 | const size_t space_physical_size = space_statistics.physical_space_size(); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 106 | |
| 107 | known_spaces_size += space_size; |
| 108 | known_spaces_used_size += space_used_size; |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 109 | known_spaces_physical_size += space_physical_size; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 110 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 111 | 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, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 115 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 116 | space_physical_size); |
| 117 | |
| 118 | space_dump->AddScalar("virtual_size", |
| 119 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 120 | space_size); |
| 121 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 122 | space_dump->AddScalar("allocated_objects_size", |
| 123 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 124 | space_used_size); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 125 | } |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 126 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 127 | // Compute the rest of the memory, not accounted by the spaces above. |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 128 | std::string other_spaces_name = space_name_prefix + "/other_spaces"; |
| 129 | auto other_dump = process_memory_dump->CreateAllocatorDump(other_spaces_name); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 130 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 131 | other_dump->AddScalar( |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 132 | base::trace_event::MemoryAllocatorDump::kNameSize, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 133 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 134 | heap_statistics.total_physical_size() - known_spaces_physical_size); |
| 135 | |
| 136 | other_dump->AddScalar( |
| 137 | "allocated_objects_size", |
| 138 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 139 | heap_statistics.used_heap_size() - known_spaces_used_size); |
| 140 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 141 | other_dump->AddScalar("virtual_size", |
| 142 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 143 | heap_statistics.total_heap_size() - known_spaces_size); |
| 144 | |
ssid | bce6ee4 | 2015-11-05 02:35:39 | [diff] [blame] | 145 | // 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 | |
jochen | 07eb7579 | 2016-04-09 20:34:02 | [diff] [blame] | 157 | // 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 | |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame^] | 171 | // 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 | |
ssid | 3011f20 | 2016-06-01 20:24:26 | [diff] [blame] | 176 | // Dump object statistics only for detailed dumps. |
| 177 | if (args.level_of_detail != |
| 178 | base::trace_event::MemoryDumpLevelOfDetail::DETAILED) { |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 179 | return; |
ssid | 3011f20 | 2016-06-01 20:24:26 | [diff] [blame] | 180 | } |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 181 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 182 | // Dump statistics of the heap's live objects from last GC. |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 183 | // 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"; |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 186 | bool did_dump_object_stats = false; |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 187 | 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 = |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 196 | object_name_prefix + "/" + object_statistics.object_type(); |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 197 | if (object_statistics.object_sub_type()[0] != '\0') |
| 198 | dump_name += std::string("/") + object_statistics.object_sub_type(); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 199 | auto object_dump = process_memory_dump->CreateAllocatorDump(dump_name); |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 200 | |
| 201 | object_dump->AddScalar( |
bratell | 10f2e3c | 2015-09-10 16:28:43 | [diff] [blame] | 202 | base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 203 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 204 | object_statistics.object_count()); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 205 | 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; |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 209 | } |
| 210 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 211 | 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(), |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame^] | 224 | heap_spaces_dump->guid()); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 225 | } |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame^] | 226 | |
| 227 | // Dump statistics related to code and bytecode if requested. |
| 228 | DumpCodeStatistics(heap_spaces_dump, isolate_holder_); |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 229 | } |
| 230 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 231 | } // namespace gin |