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());
32 if (!memory_allocator_dump
)
35 // Wrap it and return to blink.
36 WebMemoryAllocatorDumpImpl
* web_memory_allocator_dump_impl
=
37 new WebMemoryAllocatorDumpImpl(memory_allocator_dump
);
39 // memory_allocator_dumps_ will take ownership of
40 // |web_memory_allocator_dumpd_impl|.
41 memory_allocator_dumps_
.set(memory_allocator_dump
,
42 make_scoped_ptr(web_memory_allocator_dump_impl
));
44 return web_memory_allocator_dump_impl
;
47 blink::WebMemoryAllocatorDump
* WebProcessMemoryDumpImpl::getMemoryAllocatorDump(
48 const blink::WebString
& absolute_name
) const {
49 // Retrieve the base MemoryAllocatorDump object and then reverse lookup
51 base::trace_event::MemoryAllocatorDump
* memory_allocator_dump
=
52 process_memory_dump_
->GetAllocatorDump(absolute_name
.utf8());
53 if (!memory_allocator_dump
)
56 // The only case of (memory_allocator_dump && !web_memory_allocator_dump)
57 // is something from blink trying to get a MAD that was created from chromium,
58 // which is an odd use case.
59 blink::WebMemoryAllocatorDump
* web_memory_allocator_dump
=
60 memory_allocator_dumps_
.get(memory_allocator_dump
);
61 DCHECK(web_memory_allocator_dump
);
62 return web_memory_allocator_dump
;
65 void WebProcessMemoryDumpImpl::clear() {
66 // Clear all the WebMemoryAllocatorDump wrappers.
67 memory_allocator_dumps_
.clear();
69 // Clear the actual MemoryAllocatorDump objects from the underlying PMD.
70 process_memory_dump_
->Clear();
73 void WebProcessMemoryDumpImpl::takeAllDumpsFrom(
74 blink::WebProcessMemoryDump
* other
) {
75 auto other_impl
= static_cast<WebProcessMemoryDumpImpl
*>(other
);
76 // WebProcessMemoryDumpImpl is a container of WebMemoryAllocatorDump(s) which
77 // in turn are wrappers of base::trace_event::MemoryAllocatorDump(s).
78 // In order to expose the move and ownership transfer semantics of the
79 // underlying ProcessMemoryDump, we need to:
81 // 1) Move and transfer the ownership of the wrapped
82 // base::trace_event::MemoryAllocatorDump(s) instances.
83 process_memory_dump_
->TakeAllDumpsFrom(other_impl
->process_memory_dump_
);
85 // 2) Move and transfer the ownership of the WebMemoryAllocatorDump wrappers.
86 const size_t expected_final_size
= memory_allocator_dumps_
.size() +
87 other_impl
->memory_allocator_dumps_
.size();
88 while (!other_impl
->memory_allocator_dumps_
.empty()) {
89 auto first_entry
= other_impl
->memory_allocator_dumps_
.begin();
90 base::trace_event::MemoryAllocatorDump
* memory_allocator_dump
=
92 memory_allocator_dumps_
.set(
93 memory_allocator_dump
,
94 other_impl
->memory_allocator_dumps_
.take_and_erase(first_entry
).Pass());
96 DCHECK_EQ(expected_final_size
, memory_allocator_dumps_
.size());
97 DCHECK(other_impl
->memory_allocator_dumps_
.empty());
100 } // namespace content