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 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 47 | void V8IsolateMemoryDumpProvider::DumpHeapStatistics( |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 48 | const base::trace_event::MemoryDumpArgs& args, |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 49 | base::trace_event::ProcessMemoryDump* process_memory_dump) { |
| 50 | std::string dump_base_name = |
| 51 | base::StringPrintf("v8/isolate_%p", isolate_holder_->isolate()); |
| 52 | |
| 53 | // Dump statistics of the heap's spaces. |
| 54 | std::string space_name_prefix = dump_base_name + "/heap_spaces"; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 55 | v8::HeapStatistics heap_statistics; |
| 56 | isolate_holder_->isolate()->GetHeapStatistics(&heap_statistics); |
| 57 | |
| 58 | size_t known_spaces_used_size = 0; |
| 59 | size_t known_spaces_size = 0; |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 60 | size_t known_spaces_physical_size = 0; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 61 | size_t number_of_spaces = isolate_holder_->isolate()->NumberOfHeapSpaces(); |
| 62 | for (size_t space = 0; space < number_of_spaces; space++) { |
| 63 | v8::HeapSpaceStatistics space_statistics; |
| 64 | isolate_holder_->isolate()->GetHeapSpaceStatistics(&space_statistics, |
| 65 | space); |
| 66 | const size_t space_size = space_statistics.space_size(); |
| 67 | const size_t space_used_size = space_statistics.space_used_size(); |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 68 | const size_t space_physical_size = space_statistics.physical_space_size(); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 69 | |
| 70 | known_spaces_size += space_size; |
| 71 | known_spaces_used_size += space_used_size; |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 72 | known_spaces_physical_size += space_physical_size; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 73 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 74 | std::string space_dump_name = |
| 75 | space_name_prefix + "/" + space_statistics.space_name(); |
| 76 | auto space_dump = process_memory_dump->CreateAllocatorDump(space_dump_name); |
| 77 | space_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 78 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 79 | space_physical_size); |
| 80 | |
| 81 | space_dump->AddScalar("virtual_size", |
| 82 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 83 | space_size); |
| 84 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 85 | space_dump->AddScalar("allocated_objects_size", |
| 86 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 87 | space_used_size); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 88 | } |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 89 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 90 | // Compute the rest of the memory, not accounted by the spaces above. |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 91 | std::string other_spaces_name = space_name_prefix + "/other_spaces"; |
| 92 | auto other_dump = process_memory_dump->CreateAllocatorDump(other_spaces_name); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 93 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 94 | other_dump->AddScalar( |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 95 | base::trace_event::MemoryAllocatorDump::kNameSize, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 96 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 97 | heap_statistics.total_physical_size() - known_spaces_physical_size); |
| 98 | |
| 99 | other_dump->AddScalar( |
| 100 | "allocated_objects_size", |
| 101 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 102 | heap_statistics.used_heap_size() - known_spaces_used_size); |
| 103 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 104 | other_dump->AddScalar("virtual_size", |
| 105 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 106 | heap_statistics.total_heap_size() - known_spaces_size); |
| 107 | |
ssid | bce6ee4 | 2015-11-05 02:35:39 | [diff] [blame] | 108 | // If V8 zaps garbage, all the memory mapped regions become resident, |
| 109 | // so we add an extra dump to avoid mismatches w.r.t. the total |
| 110 | // resident values. |
| 111 | if (heap_statistics.does_zap_garbage()) { |
| 112 | auto zap_dump = process_memory_dump->CreateAllocatorDump( |
| 113 | dump_base_name + "/zapped_for_debug"); |
| 114 | zap_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 115 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 116 | heap_statistics.total_heap_size() - |
| 117 | heap_statistics.total_physical_size()); |
| 118 | } |
| 119 | |
jochen | 07eb7579 | 2016-04-09 20:34:02 | [diff] [blame] | 120 | // Dump statistics about malloced memory. |
| 121 | std::string malloc_name = dump_base_name + "/malloc"; |
| 122 | auto malloc_dump = process_memory_dump->CreateAllocatorDump(malloc_name); |
| 123 | malloc_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 124 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 125 | heap_statistics.malloced_memory()); |
| 126 | const char* system_allocator_name = |
| 127 | base::trace_event::MemoryDumpManager::GetInstance() |
| 128 | ->system_allocator_pool_name(); |
| 129 | if (system_allocator_name) { |
| 130 | process_memory_dump->AddSuballocation(malloc_dump->guid(), |
| 131 | system_allocator_name); |
| 132 | } |
| 133 | |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 134 | // If light dump is requested, then object statistics are not dumped |
primiano | 82baa9af | 2015-09-14 20:44:25 | [diff] [blame] | 135 | if (args.level_of_detail == base::trace_event::MemoryDumpLevelOfDetail::LIGHT) |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 136 | return; |
| 137 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 138 | // Dump statistics of the heap's live objects from last GC. |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 139 | // TODO(primiano): these should not be tracked in the same trace event as they |
| 140 | // report stats for the last GC (not the current state). See crbug.com/498779. |
| 141 | std::string object_name_prefix = dump_base_name + "/heap_objects_at_last_gc"; |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 142 | bool did_dump_object_stats = false; |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 143 | const size_t object_types = |
| 144 | isolate_holder_->isolate()->NumberOfTrackedHeapObjectTypes(); |
| 145 | for (size_t type_index = 0; type_index < object_types; type_index++) { |
| 146 | v8::HeapObjectStatistics object_statistics; |
| 147 | if (!isolate_holder_->isolate()->GetHeapObjectStatisticsAtLastGC( |
| 148 | &object_statistics, type_index)) |
| 149 | continue; |
| 150 | |
| 151 | std::string dump_name = |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 152 | object_name_prefix + "/" + object_statistics.object_type(); |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 153 | if (object_statistics.object_sub_type()[0] != '\0') |
| 154 | dump_name += std::string("/") + object_statistics.object_sub_type(); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 155 | auto object_dump = process_memory_dump->CreateAllocatorDump(dump_name); |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 156 | |
| 157 | object_dump->AddScalar( |
bratell | 10f2e3c | 2015-09-10 16:28:43 | [diff] [blame] | 158 | base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 159 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 160 | object_statistics.object_count()); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 161 | object_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 162 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 163 | object_statistics.object_size()); |
| 164 | did_dump_object_stats = true; |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 165 | } |
| 166 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 167 | if (process_memory_dump->GetAllocatorDump(object_name_prefix + |
| 168 | "/CODE_TYPE")) { |
| 169 | auto code_kind_dump = process_memory_dump->CreateAllocatorDump( |
| 170 | object_name_prefix + "/CODE_TYPE/CODE_KIND"); |
| 171 | auto code_age_dump = process_memory_dump->CreateAllocatorDump( |
| 172 | object_name_prefix + "/CODE_TYPE/CODE_AGE"); |
| 173 | process_memory_dump->AddOwnershipEdge(code_kind_dump->guid(), |
| 174 | code_age_dump->guid()); |
| 175 | } |
| 176 | |
| 177 | if (did_dump_object_stats) { |
| 178 | process_memory_dump->AddOwnershipEdge( |
| 179 | process_memory_dump->CreateAllocatorDump(object_name_prefix)->guid(), |
| 180 | process_memory_dump->CreateAllocatorDump(space_name_prefix)->guid()); |
| 181 | } |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 182 | } |
| 183 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 184 | } // namespace gin |