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