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.
5 #include "content/child/web_process_memory_dump_impl.h"
7 #include "base/trace_event/process_memory_dump.h"
8 #include "content/child/web_memory_allocator_dump_impl.h"
12 WebProcessMemoryDumpImpl::WebProcessMemoryDumpImpl()
13 : owned_process_memory_dump_(
14 new base::trace_event::ProcessMemoryDump(nullptr)),
15 process_memory_dump_(owned_process_memory_dump_
.get()) {
18 WebProcessMemoryDumpImpl::WebProcessMemoryDumpImpl(
19 base::trace_event::ProcessMemoryDump
* process_memory_dump
)
20 : process_memory_dump_(process_memory_dump
) {
23 WebProcessMemoryDumpImpl::~WebProcessMemoryDumpImpl() {
26 blink::WebMemoryAllocatorDump
*
27 WebProcessMemoryDumpImpl::createMemoryAllocatorDump(
28 const blink::WebString
& absolute_name
) {
29 // Get a MemoryAllocatorDump from the base/ object.
30 base::trace_event::MemoryAllocatorDump
* memory_allocator_dump
=
31 process_memory_dump_
->CreateAllocatorDump(absolute_name
.utf8());
33 return createWebMemoryAllocatorDump(memory_allocator_dump
);
36 blink::WebMemoryAllocatorDump
*
37 WebProcessMemoryDumpImpl::createMemoryAllocatorDump(
38 const blink::WebString
& absolute_name
,
39 blink::WebMemoryAllocatorDumpGuid guid
) {
40 // Get a MemoryAllocatorDump from the base/ object with given guid.
41 base::trace_event::MemoryAllocatorDump
* memory_allocator_dump
=
42 process_memory_dump_
->CreateAllocatorDump(
44 base::trace_event::MemoryAllocatorDumpGuid(guid
));
45 return createWebMemoryAllocatorDump(memory_allocator_dump
);
48 blink::WebMemoryAllocatorDump
*
49 WebProcessMemoryDumpImpl::createWebMemoryAllocatorDump(
50 base::trace_event::MemoryAllocatorDump
* memory_allocator_dump
) {
51 if (!memory_allocator_dump
)
54 // Wrap it and return to blink.
55 WebMemoryAllocatorDumpImpl
* web_memory_allocator_dump_impl
=
56 new WebMemoryAllocatorDumpImpl(memory_allocator_dump
);
58 // memory_allocator_dumps_ will take ownership of
59 // |web_memory_allocator_dumpd_impl|.
60 memory_allocator_dumps_
.set(memory_allocator_dump
,
61 make_scoped_ptr(web_memory_allocator_dump_impl
));
62 return web_memory_allocator_dump_impl
;
65 blink::WebMemoryAllocatorDump
* WebProcessMemoryDumpImpl::getMemoryAllocatorDump(
66 const blink::WebString
& absolute_name
) const {
67 // Retrieve the base MemoryAllocatorDump object and then reverse lookup
69 base::trace_event::MemoryAllocatorDump
* memory_allocator_dump
=
70 process_memory_dump_
->GetAllocatorDump(absolute_name
.utf8());
71 if (!memory_allocator_dump
)
74 // The only case of (memory_allocator_dump && !web_memory_allocator_dump)
75 // is something from blink trying to get a MAD that was created from chromium,
76 // which is an odd use case.
77 blink::WebMemoryAllocatorDump
* web_memory_allocator_dump
=
78 memory_allocator_dumps_
.get(memory_allocator_dump
);
79 DCHECK(web_memory_allocator_dump
);
80 return web_memory_allocator_dump
;
83 void WebProcessMemoryDumpImpl::clear() {
84 // Clear all the WebMemoryAllocatorDump wrappers.
85 memory_allocator_dumps_
.clear();
87 // Clear the actual MemoryAllocatorDump objects from the underlying PMD.
88 process_memory_dump_
->Clear();
91 void WebProcessMemoryDumpImpl::takeAllDumpsFrom(
92 blink::WebProcessMemoryDump
* other
) {
93 auto other_impl
= static_cast<WebProcessMemoryDumpImpl
*>(other
);
94 // WebProcessMemoryDumpImpl is a container of WebMemoryAllocatorDump(s) which
95 // in turn are wrappers of base::trace_event::MemoryAllocatorDump(s).
96 // In order to expose the move and ownership transfer semantics of the
97 // underlying ProcessMemoryDump, we need to:
99 // 1) Move and transfer the ownership of the wrapped
100 // base::trace_event::MemoryAllocatorDump(s) instances.
101 process_memory_dump_
->TakeAllDumpsFrom(other_impl
->process_memory_dump_
);
103 // 2) Move and transfer the ownership of the WebMemoryAllocatorDump wrappers.
104 const size_t expected_final_size
= memory_allocator_dumps_
.size() +
105 other_impl
->memory_allocator_dumps_
.size();
106 while (!other_impl
->memory_allocator_dumps_
.empty()) {
107 auto first_entry
= other_impl
->memory_allocator_dumps_
.begin();
108 base::trace_event::MemoryAllocatorDump
* memory_allocator_dump
=
110 memory_allocator_dumps_
.set(
111 memory_allocator_dump
,
112 other_impl
->memory_allocator_dumps_
.take_and_erase(first_entry
).Pass());
114 DCHECK_EQ(expected_final_size
, memory_allocator_dumps_
.size());
115 DCHECK(other_impl
->memory_allocator_dumps_
.empty());
118 void WebProcessMemoryDumpImpl::AddOwnershipEdge(
119 blink::WebMemoryAllocatorDumpGuid source
,
120 blink::WebMemoryAllocatorDumpGuid target
,
122 process_memory_dump_
->AddOwnershipEdge(
123 base::trace_event::MemoryAllocatorDumpGuid(source
),
124 base::trace_event::MemoryAllocatorDumpGuid(target
), importance
);
127 void WebProcessMemoryDumpImpl::AddOwnershipEdge(
128 blink::WebMemoryAllocatorDumpGuid source
,
129 blink::WebMemoryAllocatorDumpGuid target
) {
130 process_memory_dump_
->AddOwnershipEdge(
131 base::trace_event::MemoryAllocatorDumpGuid(source
),
132 base::trace_event::MemoryAllocatorDumpGuid(target
));
135 } // namespace content