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 | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 7 | #include "base/strings/stringprintf.h" |
| 8 | #include "base/thread_task_runner_handle.h" |
| 9 | #include "base/trace_event/memory_dump_manager.h" |
| 10 | #include "base/trace_event/process_memory_dump.h" |
| 11 | #include "gin/public/isolate_holder.h" |
| 12 | #include "v8/include/v8.h" |
| 13 | |
| 14 | namespace gin { |
| 15 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 16 | V8IsolateMemoryDumpProvider::V8IsolateMemoryDumpProvider( |
| 17 | IsolateHolder* isolate_holder) |
| 18 | : isolate_holder_(isolate_holder) { |
| 19 | base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
| 20 | this, base::ThreadTaskRunnerHandle::Get()); |
| 21 | } |
| 22 | |
| 23 | V8IsolateMemoryDumpProvider::~V8IsolateMemoryDumpProvider() { |
| 24 | base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
| 25 | this); |
| 26 | } |
| 27 | |
| 28 | // Called at trace dump point time. Creates a snapshot with the memory counters |
| 29 | // for the current isolate. |
| 30 | bool V8IsolateMemoryDumpProvider::OnMemoryDump( |
ssid | 90694aeec | 2015-08-06 13:01:30 | [diff] [blame] | 31 | const base::trace_event::MemoryDumpArgs& args, |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 32 | base::trace_event::ProcessMemoryDump* process_memory_dump) { |
ssid | 90694aeec | 2015-08-06 13:01:30 | [diff] [blame] | 33 | // TODO(ssid): Use MemoryDumpArgs to create light dumps when requested |
| 34 | // (crbug.com/499731). |
| 35 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 36 | if (isolate_holder_->access_mode() == IsolateHolder::kUseLocker) { |
| 37 | v8::Locker locked(isolate_holder_->isolate()); |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 38 | DumpHeapStatistics(args, process_memory_dump); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 39 | } else { |
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 | } |
| 42 | return true; |
| 43 | } |
| 44 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 45 | void V8IsolateMemoryDumpProvider::DumpHeapStatistics( |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 46 | const base::trace_event::MemoryDumpArgs& args, |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 47 | base::trace_event::ProcessMemoryDump* process_memory_dump) { |
| 48 | std::string dump_base_name = |
| 49 | base::StringPrintf("v8/isolate_%p", isolate_holder_->isolate()); |
| 50 | |
| 51 | // Dump statistics of the heap's spaces. |
| 52 | std::string space_name_prefix = dump_base_name + "/heap_spaces"; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 53 | v8::HeapStatistics heap_statistics; |
| 54 | isolate_holder_->isolate()->GetHeapStatistics(&heap_statistics); |
| 55 | |
| 56 | size_t known_spaces_used_size = 0; |
| 57 | size_t known_spaces_size = 0; |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 58 | size_t known_spaces_physical_size = 0; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 59 | size_t number_of_spaces = isolate_holder_->isolate()->NumberOfHeapSpaces(); |
| 60 | for (size_t space = 0; space < number_of_spaces; space++) { |
| 61 | v8::HeapSpaceStatistics space_statistics; |
| 62 | isolate_holder_->isolate()->GetHeapSpaceStatistics(&space_statistics, |
| 63 | space); |
| 64 | const size_t space_size = space_statistics.space_size(); |
| 65 | const size_t space_used_size = space_statistics.space_used_size(); |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 66 | const size_t space_physical_size = space_statistics.physical_space_size(); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 67 | |
| 68 | known_spaces_size += space_size; |
| 69 | known_spaces_used_size += space_used_size; |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 70 | known_spaces_physical_size += space_physical_size; |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 71 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 72 | std::string space_dump_name = |
| 73 | space_name_prefix + "/" + space_statistics.space_name(); |
| 74 | auto space_dump = process_memory_dump->CreateAllocatorDump(space_dump_name); |
| 75 | space_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 76 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 77 | space_physical_size); |
| 78 | |
| 79 | space_dump->AddScalar("virtual_size", |
| 80 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 81 | space_size); |
| 82 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 83 | space_dump->AddScalar("allocated_objects_size", |
| 84 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 85 | space_used_size); |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 86 | } |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 87 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 88 | // Compute the rest of the memory, not accounted by the spaces above. |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 89 | std::string other_spaces_name = space_name_prefix + "/other_spaces"; |
| 90 | auto other_dump = process_memory_dump->CreateAllocatorDump(other_spaces_name); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 91 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 92 | other_dump->AddScalar( |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 93 | base::trace_event::MemoryAllocatorDump::kNameSize, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 94 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 95 | heap_statistics.total_physical_size() - known_spaces_physical_size); |
| 96 | |
| 97 | other_dump->AddScalar( |
| 98 | "allocated_objects_size", |
| 99 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 100 | heap_statistics.used_heap_size() - known_spaces_used_size); |
| 101 | |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 102 | other_dump->AddScalar("virtual_size", |
| 103 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 104 | heap_statistics.total_heap_size() - known_spaces_size); |
| 105 | |
ssid | 2888a24 | 2015-08-07 23:08:42 | [diff] [blame] | 106 | // If light dump is requested, then object statistics are not dumped |
| 107 | if (args.level_of_detail == |
| 108 | base::trace_event::MemoryDumpArgs::LevelOfDetail::LOW) |
| 109 | return; |
| 110 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 111 | // Dump statistics of the heap's live objects from last GC. |
primiano | 553bea9a | 2015-09-09 16:37:35 | [diff] [blame] | 112 | // TODO(primiano): these should not be tracked in the same trace event as they |
| 113 | // report stats for the last GC (not the current state). See crbug.com/498779. |
| 114 | std::string object_name_prefix = dump_base_name + "/heap_objects_at_last_gc"; |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 115 | bool did_dump_object_stats = false; |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 116 | const size_t object_types = |
| 117 | isolate_holder_->isolate()->NumberOfTrackedHeapObjectTypes(); |
| 118 | for (size_t type_index = 0; type_index < object_types; type_index++) { |
| 119 | v8::HeapObjectStatistics object_statistics; |
| 120 | if (!isolate_holder_->isolate()->GetHeapObjectStatisticsAtLastGC( |
| 121 | &object_statistics, type_index)) |
| 122 | continue; |
| 123 | |
| 124 | std::string dump_name = |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 125 | object_name_prefix + "/" + object_statistics.object_type(); |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 126 | if (object_statistics.object_sub_type()[0] != '\0') |
| 127 | dump_name += std::string("/") + object_statistics.object_sub_type(); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 128 | auto object_dump = process_memory_dump->CreateAllocatorDump(dump_name); |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 129 | |
| 130 | object_dump->AddScalar( |
bratell | 10f2e3c | 2015-09-10 16:28:43 | [diff] [blame^] | 131 | base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 132 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 133 | object_statistics.object_count()); |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 134 | object_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 135 | base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 136 | object_statistics.object_size()); |
| 137 | did_dump_object_stats = true; |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 138 | } |
| 139 | |
ssid | e36cfaf | 2015-06-12 16:42:20 | [diff] [blame] | 140 | if (process_memory_dump->GetAllocatorDump(object_name_prefix + |
| 141 | "/CODE_TYPE")) { |
| 142 | auto code_kind_dump = process_memory_dump->CreateAllocatorDump( |
| 143 | object_name_prefix + "/CODE_TYPE/CODE_KIND"); |
| 144 | auto code_age_dump = process_memory_dump->CreateAllocatorDump( |
| 145 | object_name_prefix + "/CODE_TYPE/CODE_AGE"); |
| 146 | process_memory_dump->AddOwnershipEdge(code_kind_dump->guid(), |
| 147 | code_age_dump->guid()); |
| 148 | } |
| 149 | |
| 150 | if (did_dump_object_stats) { |
| 151 | process_memory_dump->AddOwnershipEdge( |
| 152 | process_memory_dump->CreateAllocatorDump(object_name_prefix)->guid(), |
| 153 | process_memory_dump->CreateAllocatorDump(space_name_prefix)->guid()); |
| 154 | } |
ssid | f51216b0 | 2015-06-04 19:46:23 | [diff] [blame] | 155 | } |
| 156 | |
ssid | 83aa5be | 2015-05-08 12:03:26 | [diff] [blame] | 157 | } // namespace gin |