Avi Drissman | 468e51b6 | 2022-09-13 20:47:01 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 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 | |
Lei Zhang | 8815c18 | 2022-11-03 14:40:27 | [diff] [blame] | 10 | #include "base/check_op.h" |
Peter Boström | f5d5155 | 2024-01-13 05:46:29 | [diff] [blame] | 11 | #include "base/notreached.h" |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
Sean Maher | e672a66 | 2023-01-09 21:42:28 | [diff] [blame] | 13 | #include "base/task/single_thread_task_runner.h" |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 14 | #include "base/trace_event/memory_dump_manager.h" |
| 15 | #include "base/trace_event/process_memory_dump.h" |
| 16 | #include "gin/public/isolate_holder.h" |
Dan Elphick | 05acd60 | 2021-08-30 15:22:07 | [diff] [blame] | 17 | #include "v8/include/v8-isolate.h" |
| 18 | #include "v8/include/v8-locker.h" |
| 19 | #include "v8/include/v8-statistics.h" |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 20 | |
| 21 | namespace gin { |
| 22 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 23 | V8IsolateMemoryDumpProvider::V8IsolateMemoryDumpProvider( |
Siddhartha | 331b5f0d | 2017-12-12 14:47:05 | [diff] [blame] | 24 | IsolateHolder* isolate_holder, |
| 25 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 26 | : isolate_holder_(isolate_holder) { |
Siddhartha | 331b5f0d | 2017-12-12 14:47:05 | [diff] [blame] | 27 | DCHECK(task_runner); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 28 | base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
Siddhartha | 331b5f0d | 2017-12-12 14:47:05 | [diff] [blame] | 29 | this, "V8Isolate", task_runner); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | V8IsolateMemoryDumpProvider::~V8IsolateMemoryDumpProvider() { |
| 33 | base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
| 34 | this); |
| 35 | } |
| 36 | |
| 37 | // Called at trace dump point time. Creates a snapshot with the memory counters |
| 38 | // for the current isolate. |
| 39 | bool V8IsolateMemoryDumpProvider::OnMemoryDump( |
ssid | 90694aeec | 2015-08-06 13:01:30 | [diff] [blame] | 40 | const base::trace_event::MemoryDumpArgs& args, |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 41 | base::trace_event::ProcessMemoryDump* process_memory_dump) { |
ssid | 90694aeec | 2015-08-06 13:01:30 | [diff] [blame] | 42 | // TODO(ssid): Use MemoryDumpArgs to create light dumps when requested |
| 43 | // (crbug.com/499731). |
| 44 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 45 | if (isolate_holder_->access_mode() == IsolateHolder::kUseLocker) { |
| 46 | v8::Locker locked(isolate_holder_->isolate()); |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 47 | DumpHeapStatistics(args, process_memory_dump); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 48 | } else { |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 49 | DumpHeapStatistics(args, process_memory_dump); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame] | 54 | namespace { |
| 55 | |
| 56 | // Dump statistics related to code/bytecode when memory-infra.v8.code_stats is |
| 57 | // enabled. |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 58 | void DumpCodeStatistics(base::trace_event::MemoryAllocatorDump* dump, |
| 59 | IsolateHolder* isolate_holder) { |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame] | 60 | // Collecting code statistics is an expensive operation (~10 ms) when |
| 61 | // compared to other v8 metrics (< 1 ms). So, dump them only when |
| 62 | // memory-infra.v8.code_stats is enabled. |
| 63 | // TODO(primiano): This information should be plumbed through TraceConfig. |
| 64 | // See crbug.com/616441. |
| 65 | bool dump_code_stats = false; |
| 66 | TRACE_EVENT_CATEGORY_GROUP_ENABLED( |
| 67 | TRACE_DISABLED_BY_DEFAULT("memory-infra.v8.code_stats"), |
| 68 | &dump_code_stats); |
| 69 | if (!dump_code_stats) |
| 70 | return; |
| 71 | |
| 72 | v8::HeapCodeStatistics code_statistics; |
| 73 | if (!isolate_holder->isolate()->GetHeapCodeAndMetadataStatistics( |
| 74 | &code_statistics)) { |
| 75 | return; |
| 76 | } |
| 77 | |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 78 | dump->AddScalar("code_and_metadata_size", |
| 79 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 80 | code_statistics.code_and_metadata_size()); |
| 81 | dump->AddScalar("bytecode_and_metadata_size", |
| 82 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 83 | code_statistics.bytecode_and_metadata_size()); |
| 84 | dump->AddScalar("external_script_source_size", |
| 85 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 86 | code_statistics.external_script_source_size()); |
Corentin Pescheloche | 959ce8b | 2021-11-08 21:49:47 | [diff] [blame] | 87 | dump->AddScalar("cpu_profiler_metadata_size", |
| 88 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 89 | code_statistics.cpu_profiler_metadata_size()); |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame] | 90 | } |
| 91 | |
Ulan Degenbaev | bfe5871 | 2017-12-15 19:40:54 | [diff] [blame] | 92 | // Dump the number of native and detached contexts. |
| 93 | // The result looks as follows in the Chrome trace viewer: |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 94 | // ======================================== |
| 95 | // Component object_count |
Ulan Degenbaev | bfe5871 | 2017-12-15 19:40:54 | [diff] [blame] | 96 | // - v8 |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 97 | // - main |
Ulan Degenbaev | bfe5871 | 2017-12-15 19:40:54 | [diff] [blame] | 98 | // - contexts |
| 99 | // - detached_context 10 |
| 100 | // - native_context 20 |
Ulan Degenbaev | dc12d16a | 2018-09-06 15:12:16 | [diff] [blame] | 101 | // - workers |
| 102 | // - contexts |
| 103 | // - detached_context |
| 104 | // - isolate_0x1234 10 |
| 105 | // - native_context |
| 106 | // - isolate_0x1234 20 |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 107 | // ======================================== |
Ulan Degenbaev | bfe5871 | 2017-12-15 19:40:54 | [diff] [blame] | 108 | void DumpContextStatistics( |
| 109 | base::trace_event::ProcessMemoryDump* process_memory_dump, |
| 110 | std::string dump_base_name, |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 111 | std::string dump_name_suffix, |
Ulan Degenbaev | bfe5871 | 2017-12-15 19:40:54 | [diff] [blame] | 112 | size_t number_of_detached_contexts, |
| 113 | size_t number_of_native_contexts) { |
Ulan Degenbaev | dc12d16a | 2018-09-06 15:12:16 | [diff] [blame] | 114 | std::string dump_name_prefix = dump_base_name + "/contexts"; |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 115 | std::string native_context_name = |
| 116 | dump_name_prefix + "/native_context" + dump_name_suffix; |
Ulan Degenbaev | bfe5871 | 2017-12-15 19:40:54 | [diff] [blame] | 117 | auto* native_context_dump = |
| 118 | process_memory_dump->CreateAllocatorDump(native_context_name); |
| 119 | native_context_dump->AddScalar( |
| 120 | "object_count", base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 121 | number_of_native_contexts); |
Ulan Degenbaev | dc12d16a | 2018-09-06 15:12:16 | [diff] [blame] | 122 | std::string detached_context_name = |
| 123 | dump_name_prefix + "/detached_context" + dump_name_suffix; |
Ulan Degenbaev | bfe5871 | 2017-12-15 19:40:54 | [diff] [blame] | 124 | auto* detached_context_dump = |
| 125 | process_memory_dump->CreateAllocatorDump(detached_context_name); |
| 126 | detached_context_dump->AddScalar( |
| 127 | "object_count", base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 128 | number_of_detached_contexts); |
| 129 | } |
| 130 | |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 131 | std::string IsolateTypeString(IsolateHolder::IsolateType isolate_type) { |
| 132 | switch (isolate_type) { |
| 133 | case IsolateHolder::IsolateType::kBlinkMainThread: |
| 134 | return "main"; |
| 135 | case IsolateHolder::IsolateType::kBlinkWorkerThread: |
| 136 | return "workers"; |
| 137 | case IsolateHolder::IsolateType::kTest: |
Peter Boström | 01ab59a | 2024-08-15 02:39:49 | [diff] [blame] | 138 | NOTREACHED(); |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 139 | case IsolateHolder::IsolateType::kUtility: |
| 140 | return "utility"; |
| 141 | } |
Peter Boström | 01ab59a | 2024-08-15 02:39:49 | [diff] [blame] | 142 | NOTREACHED(); |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | bool CanHaveMultipleIsolates(IsolateHolder::IsolateType isolate_type) { |
| 146 | switch (isolate_type) { |
| 147 | case IsolateHolder::IsolateType::kBlinkMainThread: |
| 148 | return false; |
| 149 | case IsolateHolder::IsolateType::kBlinkWorkerThread: |
| 150 | return true; |
| 151 | case IsolateHolder::IsolateType::kTest: |
Peter Boström | 01ab59a | 2024-08-15 02:39:49 | [diff] [blame] | 152 | NOTREACHED(); |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 153 | case IsolateHolder::IsolateType::kUtility: |
| 154 | // PDFium and ProxyResolver create one isolate per process. |
| 155 | return false; |
| 156 | } |
Peter Boström | 01ab59a | 2024-08-15 02:39:49 | [diff] [blame] | 157 | NOTREACHED(); |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 158 | } |
| 159 | |
Corentin Pescheloche | 959ce8b | 2021-11-08 21:49:47 | [diff] [blame] | 160 | } // namespace |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame] | 161 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 162 | void V8IsolateMemoryDumpProvider::DumpHeapStatistics( |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 163 | const base::trace_event::MemoryDumpArgs& args, |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 164 | base::trace_event::ProcessMemoryDump* process_memory_dump) { |
Ho Cheung | adbf3fb | 2023-09-08 02:01:11 | [diff] [blame] | 165 | if (args.determinism == base::trace_event::MemoryDumpDeterminism::kForceGc) { |
Ulan Degenbaev | 4f2f1a1 | 2019-09-24 14:42:57 | [diff] [blame] | 166 | // Force GC in V8 using the same API as DevTools uses in "collectGarbage". |
| 167 | isolate_holder_->isolate()->LowMemoryNotification(); |
| 168 | } |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 169 | std::string isolate_name = base::StringPrintf( |
| 170 | "isolate_0x%" PRIXPTR, |
ssid | 4205d7b | 2016-06-15 20:36:03 | [diff] [blame] | 171 | reinterpret_cast<uintptr_t>(isolate_holder_->isolate())); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 172 | |
Dominik Inführ | 524999c3 | 2022-08-25 20:01:47 | [diff] [blame] | 173 | // Dump statistics of the heap's spaces. |
| 174 | v8::HeapStatistics heap_statistics; |
| 175 | // The total heap sizes should be sampled before the individual space sizes |
| 176 | // because of concurrent allocation. DCHECKs below rely on this order. |
| 177 | isolate_holder_->isolate()->GetHeapStatistics(&heap_statistics); |
| 178 | |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 179 | IsolateHolder::IsolateType isolate_type = isolate_holder_->isolate_type(); |
| 180 | std::string dump_base_name = "v8/" + IsolateTypeString(isolate_type); |
| 181 | std::string dump_name_suffix = |
| 182 | CanHaveMultipleIsolates(isolate_type) ? "/" + isolate_name : ""; |
| 183 | |
| 184 | std::string space_name_prefix = dump_base_name + "/heap"; |
| 185 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 186 | size_t known_spaces_size = 0; |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 187 | size_t known_spaces_physical_size = 0; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 188 | size_t number_of_spaces = isolate_holder_->isolate()->NumberOfHeapSpaces(); |
| 189 | for (size_t space = 0; space < number_of_spaces; space++) { |
| 190 | v8::HeapSpaceStatistics space_statistics; |
| 191 | isolate_holder_->isolate()->GetHeapSpaceStatistics(&space_statistics, |
| 192 | space); |
| 193 | const size_t space_size = space_statistics.space_size(); |
| 194 | const size_t space_used_size = space_statistics.space_used_size(); |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 195 | const size_t space_physical_size = space_statistics.physical_space_size(); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 196 | |
| 197 | known_spaces_size += space_size; |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 198 | known_spaces_physical_size += space_physical_size; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 199 | |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 200 | std::string space_dump_name = dump_base_name + "/heap/" + |
| 201 | space_statistics.space_name() + |
| 202 | dump_name_suffix; |
| 203 | |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 204 | auto* space_dump = |
| 205 | process_memory_dump->CreateAllocatorDump(space_dump_name); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 206 | space_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 207 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 208 | space_physical_size); |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 209 | space_dump->AddScalar("virtual_size", |
| 210 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 211 | space_size); |
| 212 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 213 | space_dump->AddScalar("allocated_objects_size", |
| 214 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 215 | space_used_size); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 216 | } |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 217 | |
Dominik Inführ | fc96914 | 2022-08-30 08:25:37 | [diff] [blame] | 218 | // Sanity check that all spaces are accounted for in GetHeapSpaceStatistics. |
Ulan Degenbaev | 9d6e209b | 2020-09-03 09:12:30 | [diff] [blame] | 219 | // Background threads may be running and allocating concurrently, so the sum |
Dominik Inführ | fc96914 | 2022-08-30 08:25:37 | [diff] [blame] | 220 | // of space sizes may exceed the total heap size that was sampled earlier. |
Ulan Degenbaev | 9d6e209b | 2020-09-03 09:12:30 | [diff] [blame] | 221 | DCHECK_LE(heap_statistics.total_heap_size(), known_spaces_size); |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 222 | |
ssid | bce6ee4 | 2015-11-05 02:35:39 | [diff] [blame] | 223 | // If V8 zaps garbage, all the memory mapped regions become resident, |
| 224 | // so we add an extra dump to avoid mismatches w.r.t. the total |
| 225 | // resident values. |
| 226 | if (heap_statistics.does_zap_garbage()) { |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 227 | auto* zap_dump = process_memory_dump->CreateAllocatorDump( |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 228 | dump_base_name + "/zapped_for_debug" + dump_name_suffix); |
Dominik Inführ | fc96914 | 2022-08-30 08:25:37 | [diff] [blame] | 229 | size_t zapped_size_for_debugging = |
| 230 | known_spaces_size >= known_spaces_physical_size |
| 231 | ? known_spaces_size - known_spaces_physical_size |
| 232 | : 0; |
ssid | bce6ee4 | 2015-11-05 02:35:39 | [diff] [blame] | 233 | zap_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 234 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
Dominik Inführ | fc96914 | 2022-08-30 08:25:37 | [diff] [blame] | 235 | zapped_size_for_debugging); |
ssid | bce6ee4 | 2015-11-05 02:35:39 | [diff] [blame] | 236 | } |
| 237 | |
jochen | 07eb7579 | 2016-04-09 20:34:02 | [diff] [blame] | 238 | // Dump statistics about malloced memory. |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 239 | std::string malloc_name = dump_base_name + "/malloc" + dump_name_suffix; |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 240 | auto* malloc_dump = process_memory_dump->CreateAllocatorDump(malloc_name); |
jochen | 07eb7579 | 2016-04-09 20:34:02 | [diff] [blame] | 241 | malloc_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 242 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 243 | heap_statistics.malloced_memory()); |
jochen | 077e9d7 | 2016-07-19 19:22:38 | [diff] [blame] | 244 | malloc_dump->AddScalar("peak_size", |
| 245 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 246 | heap_statistics.peak_malloced_memory()); |
jochen | 07eb7579 | 2016-04-09 20:34:02 | [diff] [blame] | 247 | const char* system_allocator_name = |
| 248 | base::trace_event::MemoryDumpManager::GetInstance() |
| 249 | ->system_allocator_pool_name(); |
| 250 | if (system_allocator_name) { |
| 251 | process_memory_dump->AddSuballocation(malloc_dump->guid(), |
| 252 | system_allocator_name); |
| 253 | } |
| 254 | |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 255 | DumpContextStatistics(process_memory_dump, dump_base_name, dump_name_suffix, |
Ulan Degenbaev | bfe5871 | 2017-12-15 19:40:54 | [diff] [blame] | 256 | heap_statistics.number_of_detached_contexts(), |
| 257 | heap_statistics.number_of_native_contexts()); |
| 258 | |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 259 | auto* code_stats_dump = process_memory_dump->CreateAllocatorDump( |
| 260 | dump_base_name + "/code_stats" + dump_name_suffix); |
mythria | 3554066 | 2016-06-03 15:02:05 | [diff] [blame] | 261 | |
rmcilroy | 6b6b89a | 2016-08-25 15:12:57 | [diff] [blame] | 262 | // Dump statistics related to code and bytecode if requested. |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 263 | DumpCodeStatistics(code_stats_dump, isolate_holder_); |
rmcilroy | 6b6b89a | 2016-08-25 15:12:57 | [diff] [blame] | 264 | |
Dominik Inführ | e35b641 | 2020-03-09 21:32:56 | [diff] [blame] | 265 | // Dump statistics for global handles. |
| 266 | auto* global_handles_dump = process_memory_dump->CreateAllocatorDump( |
| 267 | dump_base_name + "/global_handles" + dump_name_suffix); |
| 268 | global_handles_dump->AddScalar( |
| 269 | base::trace_event::MemoryAllocatorDump::kNameSize, |
| 270 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 271 | heap_statistics.total_global_handles_size()); |
Dominik Inführ | af9fe22e | 2021-02-05 14:15:46 | [diff] [blame] | 272 | global_handles_dump->AddScalar( |
| 273 | "allocated_objects_size", |
| 274 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 275 | heap_statistics.used_global_handles_size()); |
| 276 | if (system_allocator_name) { |
| 277 | process_memory_dump->AddSuballocation(global_handles_dump->guid(), |
| 278 | system_allocator_name); |
| 279 | } |
Dominik Inführ | e35b641 | 2020-03-09 21:32:56 | [diff] [blame] | 280 | |
ssid | 3011f20 | 2016-06-01 20:24:26 | [diff] [blame] | 281 | // Dump object statistics only for detailed dumps. |
| 282 | if (args.level_of_detail != |
Ho Cheung | adbf3fb | 2023-09-08 02:01:11 | [diff] [blame] | 283 | base::trace_event::MemoryDumpLevelOfDetail::kDetailed) { |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 284 | return; |
ssid | 3011f20 | 2016-06-01 20:24:26 | [diff] [blame] | 285 | } |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 286 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 287 | // Dump statistics of the heap's live objects from last GC. |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 288 | // TODO(primiano): these should not be tracked in the same trace event as they |
| 289 | // report stats for the last GC (not the current state). See crbug.com/498779. |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 290 | std::string object_name_prefix = |
| 291 | dump_base_name + "/heap_objects_at_last_gc" + dump_name_suffix; |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 292 | bool did_dump_object_stats = false; |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 293 | const size_t object_types = |
| 294 | isolate_holder_->isolate()->NumberOfTrackedHeapObjectTypes(); |
| 295 | for (size_t type_index = 0; type_index < object_types; type_index++) { |
| 296 | v8::HeapObjectStatistics object_statistics; |
| 297 | if (!isolate_holder_->isolate()->GetHeapObjectStatisticsAtLastGC( |
| 298 | &object_statistics, type_index)) |
| 299 | continue; |
| 300 | |
| 301 | std::string dump_name = |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 302 | object_name_prefix + "/" + object_statistics.object_type(); |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 303 | if (object_statistics.object_sub_type()[0] != '\0') |
| 304 | dump_name += std::string("/") + object_statistics.object_sub_type(); |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 305 | auto* object_dump = process_memory_dump->CreateAllocatorDump(dump_name); |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 306 | |
| 307 | object_dump->AddScalar( |
bratell | 10f2e3c | 2015-09-10 16:28:43 | [diff] [blame] | 308 | base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 309 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 310 | object_statistics.object_count()); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 311 | object_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 312 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 313 | object_statistics.object_size()); |
| 314 | did_dump_object_stats = true; |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 315 | } |
| 316 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 317 | if (process_memory_dump->GetAllocatorDump(object_name_prefix + |
| 318 | "/CODE_TYPE")) { |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 319 | auto* code_kind_dump = process_memory_dump->CreateAllocatorDump( |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 320 | object_name_prefix + "/CODE_TYPE/CODE_KIND"); |
vmpstr | 1ee03c4c0 | 2016-06-30 21:40:56 | [diff] [blame] | 321 | auto* code_age_dump = process_memory_dump->CreateAllocatorDump( |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 322 | object_name_prefix + "/CODE_TYPE/CODE_AGE"); |
| 323 | process_memory_dump->AddOwnershipEdge(code_kind_dump->guid(), |
| 324 | code_age_dump->guid()); |
| 325 | } |
| 326 | |
| 327 | if (did_dump_object_stats) { |
| 328 | process_memory_dump->AddOwnershipEdge( |
| 329 | process_memory_dump->CreateAllocatorDump(object_name_prefix)->guid(), |
Ulan Degenbaev | 1d31633 | 2018-09-03 14:19:08 | [diff] [blame] | 330 | process_memory_dump->GetOrCreateAllocatorDump(space_name_prefix) |
| 331 | ->guid()); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 332 | } |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 333 | } |
| 334 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 335 | } // namespace gin |