OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/trace_event/process_memory_dump.h" | 5 #include "base/trace_event/process_memory_dump.h" |
6 | 6 |
7 #include "base/trace_event/process_memory_totals.h" | 7 #include "base/trace_event/process_memory_totals.h" |
8 #include "base/trace_event/trace_event_argument.h" | 8 #include "base/trace_event/trace_event_argument.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
11 namespace trace_event { | 11 namespace trace_event { |
12 | 12 |
| 13 namespace { |
| 14 const char kEdgeTypeOwnership[] = "ownership"; |
| 15 |
| 16 std::string GetSharedGlobalAllocatorDumpName( |
| 17 const MemoryAllocatorDumpGuid& guid) { |
| 18 return "global/" + guid.ToString(); |
| 19 } |
| 20 } // namespace |
| 21 |
13 ProcessMemoryDump::ProcessMemoryDump( | 22 ProcessMemoryDump::ProcessMemoryDump( |
14 const scoped_refptr<MemoryDumpSessionState>& session_state) | 23 const scoped_refptr<MemoryDumpSessionState>& session_state) |
15 : has_process_totals_(false), | 24 : has_process_totals_(false), |
16 has_process_mmaps_(false), | 25 has_process_mmaps_(false), |
17 session_state_(session_state) { | 26 session_state_(session_state) { |
18 } | 27 } |
19 | 28 |
20 ProcessMemoryDump::~ProcessMemoryDump() { | 29 ProcessMemoryDump::~ProcessMemoryDump() { |
21 } | 30 } |
22 | 31 |
23 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump( | 32 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump( |
24 const std::string& absolute_name) { | 33 const std::string& absolute_name) { |
25 MemoryAllocatorDump* mad = new MemoryAllocatorDump(absolute_name, this); | 34 MemoryAllocatorDump* mad = new MemoryAllocatorDump(absolute_name, this); |
26 DCHECK_EQ(0ul, allocator_dumps_.count(absolute_name)); | 35 AddAllocatorDumpInternal(mad); // Takes ownership of |mad|. |
| 36 return mad; |
| 37 } |
| 38 |
| 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|. |
| 44 return mad; |
| 45 } |
| 46 |
| 47 void ProcessMemoryDump::AddAllocatorDumpInternal(MemoryAllocatorDump* mad) { |
| 48 DCHECK_EQ(0ul, allocator_dumps_.count(mad->absolute_name())); |
27 allocator_dumps_storage_.push_back(mad); | 49 allocator_dumps_storage_.push_back(mad); |
28 allocator_dumps_[absolute_name] = mad; | 50 allocator_dumps_[mad->absolute_name()] = mad; |
29 return mad; | |
30 } | 51 } |
31 | 52 |
32 MemoryAllocatorDump* ProcessMemoryDump::GetAllocatorDump( | 53 MemoryAllocatorDump* ProcessMemoryDump::GetAllocatorDump( |
33 const std::string& absolute_name) const { | 54 const std::string& absolute_name) const { |
34 auto it = allocator_dumps_.find(absolute_name); | 55 auto it = allocator_dumps_.find(absolute_name); |
35 return it == allocator_dumps_.end() ? nullptr : it->second; | 56 return it == allocator_dumps_.end() ? nullptr : it->second; |
36 } | 57 } |
37 | 58 |
| 59 MemoryAllocatorDump* ProcessMemoryDump::CreateSharedGlobalAllocatorDump( |
| 60 const MemoryAllocatorDumpGuid& guid) { |
| 61 return CreateAllocatorDump(GetSharedGlobalAllocatorDumpName(guid), guid); |
| 62 } |
| 63 |
| 64 MemoryAllocatorDump* ProcessMemoryDump::GetSharedGlobalAllocatorDump( |
| 65 const MemoryAllocatorDumpGuid& guid) const { |
| 66 return GetAllocatorDump(GetSharedGlobalAllocatorDumpName(guid)); |
| 67 } |
| 68 |
| 69 void ProcessMemoryDump::Clear() { |
| 70 if (has_process_totals_) { |
| 71 process_totals_.Clear(); |
| 72 has_process_totals_ = false; |
| 73 } |
| 74 |
| 75 if (has_process_mmaps_) { |
| 76 process_mmaps_.Clear(); |
| 77 has_process_mmaps_ = false; |
| 78 } |
| 79 |
| 80 allocator_dumps_storage_.clear(); |
| 81 allocator_dumps_.clear(); |
| 82 allocator_dumps_edges_.clear(); |
| 83 } |
| 84 |
| 85 void ProcessMemoryDump::TakeAllDumpsFrom(ProcessMemoryDump* other) { |
| 86 DCHECK(!other->has_process_totals() && !other->has_process_mmaps()); |
| 87 |
| 88 // Moves the ownership of all MemoryAllocatorDump(s) contained in |other| |
| 89 // into this ProcessMemoryDump. |
| 90 for (MemoryAllocatorDump* mad : other->allocator_dumps_storage_) { |
| 91 // Check that we don't merge duplicates. |
| 92 DCHECK_EQ(0ul, allocator_dumps_.count(mad->absolute_name())); |
| 93 allocator_dumps_storage_.push_back(mad); |
| 94 allocator_dumps_[mad->absolute_name()] = mad; |
| 95 } |
| 96 other->allocator_dumps_storage_.weak_clear(); |
| 97 other->allocator_dumps_.clear(); |
| 98 |
| 99 // Move all the edges. |
| 100 allocator_dumps_edges_.insert(allocator_dumps_edges_.end(), |
| 101 other->allocator_dumps_edges_.begin(), |
| 102 other->allocator_dumps_edges_.end()); |
| 103 other->allocator_dumps_edges_.clear(); |
| 104 } |
| 105 |
38 void ProcessMemoryDump::AsValueInto(TracedValue* value) const { | 106 void ProcessMemoryDump::AsValueInto(TracedValue* value) const { |
39 // Build up the [dumper name] -> [value] dictionary. | |
40 if (has_process_totals_) { | 107 if (has_process_totals_) { |
41 value->BeginDictionary("process_totals"); | 108 value->BeginDictionary("process_totals"); |
42 process_totals_.AsValueInto(value); | 109 process_totals_.AsValueInto(value); |
43 value->EndDictionary(); | 110 value->EndDictionary(); |
44 } | 111 } |
| 112 |
45 if (has_process_mmaps_) { | 113 if (has_process_mmaps_) { |
46 value->BeginDictionary("process_mmaps"); | 114 value->BeginDictionary("process_mmaps"); |
47 process_mmaps_.AsValueInto(value); | 115 process_mmaps_.AsValueInto(value); |
48 value->EndDictionary(); | 116 value->EndDictionary(); |
49 } | 117 } |
| 118 |
50 if (allocator_dumps_storage_.size() > 0) { | 119 if (allocator_dumps_storage_.size() > 0) { |
51 value->BeginDictionary("allocators"); | 120 value->BeginDictionary("allocators"); |
52 for (const MemoryAllocatorDump* allocator_dump : allocator_dumps_storage_) | 121 for (const MemoryAllocatorDump* allocator_dump : allocator_dumps_storage_) |
53 allocator_dump->AsValueInto(value); | 122 allocator_dump->AsValueInto(value); |
54 value->EndDictionary(); | 123 value->EndDictionary(); |
55 } | 124 } |
| 125 |
| 126 value->BeginArray("allocators_graph"); |
| 127 for (const MemoryAllocatorDumpEdge& edge : allocator_dumps_edges_) { |
| 128 value->BeginDictionary(); |
| 129 value->SetString("source", edge.source.ToString()); |
| 130 value->SetString("target", edge.target.ToString()); |
| 131 value->SetInteger("importance", edge.importance); |
| 132 value->SetString("type", edge.type); |
| 133 value->EndDictionary(); |
| 134 } |
| 135 value->EndArray(); |
| 136 } |
| 137 |
| 138 void ProcessMemoryDump::AddOwnershipEdge(const MemoryAllocatorDumpGuid& source, |
| 139 const MemoryAllocatorDumpGuid& target, |
| 140 int importance) { |
| 141 allocator_dumps_edges_.push_back( |
| 142 {source, target, importance, kEdgeTypeOwnership}); |
| 143 } |
| 144 |
| 145 void ProcessMemoryDump::AddOwnershipEdge( |
| 146 const MemoryAllocatorDumpGuid& source, |
| 147 const MemoryAllocatorDumpGuid& target) { |
| 148 AddOwnershipEdge(source, target, 0 /* importance */); |
| 149 } |
| 150 |
| 151 void ProcessMemoryDump::AddSuballocation(const MemoryAllocatorDumpGuid& source, |
| 152 const std::string& target_node_name) { |
| 153 std::string child_mad_name = target_node_name + "/__" + source.ToString(); |
| 154 MemoryAllocatorDump* target_child_mad = CreateAllocatorDump(child_mad_name); |
| 155 AddOwnershipEdge(source, target_child_mad->guid()); |
56 } | 156 } |
57 | 157 |
58 } // namespace trace_event | 158 } // namespace trace_event |
59 } // namespace base | 159 } // namespace base |
OLD | NEW |