| OLD | NEW |
| (Empty) |
| 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 "content/child/web_process_memory_dump_impl.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/memory/discardable_memory.h" | |
| 10 #include "base/trace_event/heap_profiler_heap_dump_writer.h" | |
| 11 #include "base/trace_event/process_memory_dump.h" | |
| 12 #include "base/trace_event/trace_event_argument.h" | |
| 13 #include "base/trace_event/trace_event_memory_overhead.h" | |
| 14 #include "content/child/web_memory_allocator_dump_impl.h" | |
| 15 #include "skia/ext/skia_trace_memory_dump_impl.h" | |
| 16 | |
| 17 namespace content { | |
| 18 | |
| 19 WebProcessMemoryDumpImpl::WebProcessMemoryDumpImpl() | |
| 20 : owned_process_memory_dump_( | |
| 21 new base::trace_event::ProcessMemoryDump(nullptr)), | |
| 22 process_memory_dump_(owned_process_memory_dump_.get()), | |
| 23 level_of_detail_(base::trace_event::MemoryDumpLevelOfDetail::DETAILED) {} | |
| 24 | |
| 25 WebProcessMemoryDumpImpl::WebProcessMemoryDumpImpl( | |
| 26 base::trace_event::MemoryDumpLevelOfDetail level_of_detail, | |
| 27 base::trace_event::ProcessMemoryDump* process_memory_dump) | |
| 28 : process_memory_dump_(process_memory_dump), | |
| 29 level_of_detail_(level_of_detail) {} | |
| 30 | |
| 31 WebProcessMemoryDumpImpl::~WebProcessMemoryDumpImpl() { | |
| 32 } | |
| 33 | |
| 34 blink::WebMemoryAllocatorDump* | |
| 35 WebProcessMemoryDumpImpl::createMemoryAllocatorDump( | |
| 36 const blink::WebString& absolute_name) { | |
| 37 // Get a MemoryAllocatorDump from the base/ object. | |
| 38 base::trace_event::MemoryAllocatorDump* memory_allocator_dump = | |
| 39 process_memory_dump_->CreateAllocatorDump(absolute_name.utf8()); | |
| 40 | |
| 41 return createWebMemoryAllocatorDump(memory_allocator_dump); | |
| 42 } | |
| 43 | |
| 44 blink::WebMemoryAllocatorDump* | |
| 45 WebProcessMemoryDumpImpl::createMemoryAllocatorDump( | |
| 46 const blink::WebString& absolute_name, | |
| 47 blink::WebMemoryAllocatorDumpGuid guid) { | |
| 48 // Get a MemoryAllocatorDump from the base/ object with given guid. | |
| 49 base::trace_event::MemoryAllocatorDump* memory_allocator_dump = | |
| 50 process_memory_dump_->CreateAllocatorDump( | |
| 51 absolute_name.utf8(), | |
| 52 base::trace_event::MemoryAllocatorDumpGuid(guid)); | |
| 53 return createWebMemoryAllocatorDump(memory_allocator_dump); | |
| 54 } | |
| 55 | |
| 56 blink::WebMemoryAllocatorDump* | |
| 57 WebProcessMemoryDumpImpl::createWebMemoryAllocatorDump( | |
| 58 base::trace_event::MemoryAllocatorDump* memory_allocator_dump) { | |
| 59 if (!memory_allocator_dump) | |
| 60 return nullptr; | |
| 61 | |
| 62 // Wrap it and return to blink. | |
| 63 WebMemoryAllocatorDumpImpl* web_memory_allocator_dump_impl = | |
| 64 new WebMemoryAllocatorDumpImpl(memory_allocator_dump); | |
| 65 | |
| 66 // memory_allocator_dumps_ will take ownership of | |
| 67 // |web_memory_allocator_dumpd_impl|. | |
| 68 memory_allocator_dumps_.set(memory_allocator_dump, | |
| 69 make_scoped_ptr(web_memory_allocator_dump_impl)); | |
| 70 return web_memory_allocator_dump_impl; | |
| 71 } | |
| 72 | |
| 73 blink::WebMemoryAllocatorDump* WebProcessMemoryDumpImpl::getMemoryAllocatorDump( | |
| 74 const blink::WebString& absolute_name) const { | |
| 75 // Retrieve the base MemoryAllocatorDump object and then reverse lookup | |
| 76 // its wrapper. | |
| 77 base::trace_event::MemoryAllocatorDump* memory_allocator_dump = | |
| 78 process_memory_dump_->GetAllocatorDump(absolute_name.utf8()); | |
| 79 if (!memory_allocator_dump) | |
| 80 return nullptr; | |
| 81 | |
| 82 // The only case of (memory_allocator_dump && !web_memory_allocator_dump) | |
| 83 // is something from blink trying to get a MAD that was created from chromium, | |
| 84 // which is an odd use case. | |
| 85 blink::WebMemoryAllocatorDump* web_memory_allocator_dump = | |
| 86 memory_allocator_dumps_.get(memory_allocator_dump); | |
| 87 DCHECK(web_memory_allocator_dump); | |
| 88 return web_memory_allocator_dump; | |
| 89 } | |
| 90 | |
| 91 void WebProcessMemoryDumpImpl::clear() { | |
| 92 // Clear all the WebMemoryAllocatorDump wrappers. | |
| 93 memory_allocator_dumps_.clear(); | |
| 94 | |
| 95 // Clear the actual MemoryAllocatorDump objects from the underlying PMD. | |
| 96 process_memory_dump_->Clear(); | |
| 97 } | |
| 98 | |
| 99 void WebProcessMemoryDumpImpl::takeAllDumpsFrom( | |
| 100 blink::WebProcessMemoryDump* other) { | |
| 101 auto other_impl = static_cast<WebProcessMemoryDumpImpl*>(other); | |
| 102 // WebProcessMemoryDumpImpl is a container of WebMemoryAllocatorDump(s) which | |
| 103 // in turn are wrappers of base::trace_event::MemoryAllocatorDump(s). | |
| 104 // In order to expose the move and ownership transfer semantics of the | |
| 105 // underlying ProcessMemoryDump, we need to: | |
| 106 | |
| 107 // 1) Move and transfer the ownership of the wrapped | |
| 108 // base::trace_event::MemoryAllocatorDump(s) instances. | |
| 109 process_memory_dump_->TakeAllDumpsFrom(other_impl->process_memory_dump_); | |
| 110 | |
| 111 // 2) Move and transfer the ownership of the WebMemoryAllocatorDump wrappers. | |
| 112 const size_t expected_final_size = memory_allocator_dumps_.size() + | |
| 113 other_impl->memory_allocator_dumps_.size(); | |
| 114 while (!other_impl->memory_allocator_dumps_.empty()) { | |
| 115 auto first_entry = other_impl->memory_allocator_dumps_.begin(); | |
| 116 base::trace_event::MemoryAllocatorDump* memory_allocator_dump = | |
| 117 first_entry->first; | |
| 118 memory_allocator_dumps_.set( | |
| 119 memory_allocator_dump, | |
| 120 other_impl->memory_allocator_dumps_.take_and_erase(first_entry)); | |
| 121 } | |
| 122 DCHECK_EQ(expected_final_size, memory_allocator_dumps_.size()); | |
| 123 DCHECK(other_impl->memory_allocator_dumps_.empty()); | |
| 124 } | |
| 125 | |
| 126 void WebProcessMemoryDumpImpl::addOwnershipEdge( | |
| 127 blink::WebMemoryAllocatorDumpGuid source, | |
| 128 blink::WebMemoryAllocatorDumpGuid target, | |
| 129 int importance) { | |
| 130 process_memory_dump_->AddOwnershipEdge( | |
| 131 base::trace_event::MemoryAllocatorDumpGuid(source), | |
| 132 base::trace_event::MemoryAllocatorDumpGuid(target), importance); | |
| 133 } | |
| 134 | |
| 135 void WebProcessMemoryDumpImpl::addOwnershipEdge( | |
| 136 blink::WebMemoryAllocatorDumpGuid source, | |
| 137 blink::WebMemoryAllocatorDumpGuid target) { | |
| 138 process_memory_dump_->AddOwnershipEdge( | |
| 139 base::trace_event::MemoryAllocatorDumpGuid(source), | |
| 140 base::trace_event::MemoryAllocatorDumpGuid(target)); | |
| 141 } | |
| 142 | |
| 143 void WebProcessMemoryDumpImpl::addSuballocation( | |
| 144 blink::WebMemoryAllocatorDumpGuid source, | |
| 145 const blink::WebString& target_node_name) { | |
| 146 process_memory_dump_->AddSuballocation( | |
| 147 base::trace_event::MemoryAllocatorDumpGuid(source), | |
| 148 target_node_name.utf8()); | |
| 149 } | |
| 150 | |
| 151 SkTraceMemoryDump* WebProcessMemoryDumpImpl::createDumpAdapterForSkia( | |
| 152 const blink::WebString& dump_name_prefix) { | |
| 153 sk_trace_dump_list_.push_back(new skia::SkiaTraceMemoryDumpImpl( | |
| 154 dump_name_prefix.utf8(), level_of_detail_, process_memory_dump_)); | |
| 155 return sk_trace_dump_list_.back(); | |
| 156 } | |
| 157 | |
| 158 blink::WebMemoryAllocatorDump* | |
| 159 WebProcessMemoryDumpImpl::CreateDiscardableMemoryAllocatorDump( | |
| 160 const std::string& name, | |
| 161 base::DiscardableMemory* discardable) { | |
| 162 base::trace_event::MemoryAllocatorDump* dump = | |
| 163 discardable->CreateMemoryAllocatorDump(name.c_str(), | |
| 164 process_memory_dump_); | |
| 165 return createWebMemoryAllocatorDump(dump); | |
| 166 } | |
| 167 | |
| 168 void WebProcessMemoryDumpImpl::dumpHeapUsage( | |
| 169 const base::hash_map<base::trace_event::AllocationContext, size_t>& | |
| 170 bytes_by_context, | |
| 171 base::trace_event::TraceEventMemoryOverhead& overhead, | |
| 172 const char* allocator_name) { | |
| 173 scoped_refptr<base::trace_event::MemoryDumpSessionState> session_state = | |
| 174 process_memory_dump_->session_state(); | |
| 175 scoped_refptr<base::trace_event::TracedValue> heap_dump = ExportHeapDump( | |
| 176 bytes_by_context, | |
| 177 session_state->stack_frame_deduplicator(), | |
| 178 session_state->type_name_deduplicator()); | |
| 179 process_memory_dump_->AddHeapDump(allocator_name, heap_dump); | |
| 180 overhead.DumpInto("tracing/heap_profiler", process_memory_dump_); | |
| 181 } | |
| 182 | |
| 183 } // namespace content | |
| OLD | NEW |