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 "base/trace_event/process_memory_dump.h"
7 #include "base/trace_event/process_memory_totals.h"
8 #include "base/trace_event/trace_event_argument.h"
11 namespace trace_event
{
14 const char kEdgeTypeOwnership
[] = "ownership";
16 std::string
GetSharedGlobalAllocatorDumpName(
17 const MemoryAllocatorDumpGuid
& guid
) {
18 return "global/" + guid
.ToString();
22 ProcessMemoryDump::ProcessMemoryDump(
23 const scoped_refptr
<MemoryDumpSessionState
>& session_state
)
24 : has_process_totals_(false),
25 has_process_mmaps_(false),
26 session_state_(session_state
) {
29 ProcessMemoryDump::~ProcessMemoryDump() {
32 MemoryAllocatorDump
* ProcessMemoryDump::CreateAllocatorDump(
33 const std::string
& absolute_name
) {
34 MemoryAllocatorDump
* mad
= new MemoryAllocatorDump(absolute_name
, this);
35 AddAllocatorDumpInternal(mad
); // Takes ownership of |mad|.
39 MemoryAllocatorDump
* ProcessMemoryDump::CreateAllocatorDump(
40 const std::string
& absolute_name
,
41 const MemoryAllocatorDumpGuid
& guid
) {
42 MemoryAllocatorDump
* mad
= new MemoryAllocatorDump(absolute_name
, this, guid
);
43 AddAllocatorDumpInternal(mad
); // Takes ownership of |mad|.
47 void ProcessMemoryDump::AddAllocatorDumpInternal(MemoryAllocatorDump
* mad
) {
48 DCHECK_EQ(0ul, allocator_dumps_
.count(mad
->absolute_name()));
49 allocator_dumps_storage_
.push_back(mad
);
50 allocator_dumps_
[mad
->absolute_name()] = mad
;
53 MemoryAllocatorDump
* ProcessMemoryDump::GetAllocatorDump(
54 const std::string
& absolute_name
) const {
55 auto it
= allocator_dumps_
.find(absolute_name
);
56 return it
== allocator_dumps_
.end() ? nullptr : it
->second
;
59 MemoryAllocatorDump
* ProcessMemoryDump::CreateSharedGlobalAllocatorDump(
60 const MemoryAllocatorDumpGuid
& guid
) {
61 // A shared allocator dump can be shared within a process and the guid could
62 // have been created already.
63 MemoryAllocatorDump
* allocator_dump
= GetSharedGlobalAllocatorDump(guid
);
64 return allocator_dump
? allocator_dump
65 : CreateAllocatorDump(
66 GetSharedGlobalAllocatorDumpName(guid
), guid
);
69 MemoryAllocatorDump
* ProcessMemoryDump::GetSharedGlobalAllocatorDump(
70 const MemoryAllocatorDumpGuid
& guid
) const {
71 return GetAllocatorDump(GetSharedGlobalAllocatorDumpName(guid
));
74 void ProcessMemoryDump::Clear() {
75 if (has_process_totals_
) {
76 process_totals_
.Clear();
77 has_process_totals_
= false;
80 if (has_process_mmaps_
) {
81 process_mmaps_
.Clear();
82 has_process_mmaps_
= false;
85 allocator_dumps_storage_
.clear();
86 allocator_dumps_
.clear();
87 allocator_dumps_edges_
.clear();
90 void ProcessMemoryDump::TakeAllDumpsFrom(ProcessMemoryDump
* other
) {
91 DCHECK(!other
->has_process_totals() && !other
->has_process_mmaps());
93 // Moves the ownership of all MemoryAllocatorDump(s) contained in |other|
94 // into this ProcessMemoryDump.
95 for (MemoryAllocatorDump
* mad
: other
->allocator_dumps_storage_
) {
96 // Check that we don't merge duplicates.
97 DCHECK_EQ(0ul, allocator_dumps_
.count(mad
->absolute_name()));
98 allocator_dumps_storage_
.push_back(mad
);
99 allocator_dumps_
[mad
->absolute_name()] = mad
;
101 other
->allocator_dumps_storage_
.weak_clear();
102 other
->allocator_dumps_
.clear();
104 // Move all the edges.
105 allocator_dumps_edges_
.insert(allocator_dumps_edges_
.end(),
106 other
->allocator_dumps_edges_
.begin(),
107 other
->allocator_dumps_edges_
.end());
108 other
->allocator_dumps_edges_
.clear();
111 void ProcessMemoryDump::AsValueInto(TracedValue
* value
) const {
112 if (has_process_totals_
) {
113 value
->BeginDictionary("process_totals");
114 process_totals_
.AsValueInto(value
);
115 value
->EndDictionary();
118 if (has_process_mmaps_
) {
119 value
->BeginDictionary("process_mmaps");
120 process_mmaps_
.AsValueInto(value
);
121 value
->EndDictionary();
124 if (allocator_dumps_storage_
.size() > 0) {
125 value
->BeginDictionary("allocators");
126 for (const MemoryAllocatorDump
* allocator_dump
: allocator_dumps_storage_
)
127 allocator_dump
->AsValueInto(value
);
128 value
->EndDictionary();
131 value
->BeginArray("allocators_graph");
132 for (const MemoryAllocatorDumpEdge
& edge
: allocator_dumps_edges_
) {
133 value
->BeginDictionary();
134 value
->SetString("source", edge
.source
.ToString());
135 value
->SetString("target", edge
.target
.ToString());
136 value
->SetInteger("importance", edge
.importance
);
137 value
->SetString("type", edge
.type
);
138 value
->EndDictionary();
143 void ProcessMemoryDump::AddOwnershipEdge(const MemoryAllocatorDumpGuid
& source
,
144 const MemoryAllocatorDumpGuid
& target
,
146 allocator_dumps_edges_
.push_back(
147 {source
, target
, importance
, kEdgeTypeOwnership
});
150 void ProcessMemoryDump::AddOwnershipEdge(
151 const MemoryAllocatorDumpGuid
& source
,
152 const MemoryAllocatorDumpGuid
& target
) {
153 AddOwnershipEdge(source
, target
, 0 /* importance */);
156 void ProcessMemoryDump::AddSuballocation(const MemoryAllocatorDumpGuid
& source
,
157 const std::string
& target_node_name
) {
158 std::string child_mad_name
= target_node_name
+ "/__" + source
.ToString();
159 MemoryAllocatorDump
* target_child_mad
= CreateAllocatorDump(child_mad_name
);
160 AddOwnershipEdge(source
, target_child_mad
->guid());
163 } // namespace trace_event