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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/memory/aligned_memory.h" | 9 #include "base/memory/aligned_memory.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 pmd1->AddOwnershipEdge(MemoryAllocatorDumpGuid(42), | 47 pmd1->AddOwnershipEdge(MemoryAllocatorDumpGuid(42), |
48 MemoryAllocatorDumpGuid(4242)); | 48 MemoryAllocatorDumpGuid(4242)); |
49 | 49 |
50 MemoryAllocatorDumpGuid shared_mad_guid1(1); | 50 MemoryAllocatorDumpGuid shared_mad_guid1(1); |
51 MemoryAllocatorDumpGuid shared_mad_guid2(2); | 51 MemoryAllocatorDumpGuid shared_mad_guid2(2); |
52 pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid1); | 52 pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid1); |
53 pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid2); | 53 pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid2); |
54 | 54 |
55 pmd1->Clear(); | 55 pmd1->Clear(); |
56 ASSERT_TRUE(pmd1->allocator_dumps().empty()); | 56 ASSERT_TRUE(pmd1->allocator_dumps().empty()); |
57 ASSERT_TRUE(pmd1->allocator_dumps_edges().empty()); | 57 ASSERT_TRUE(pmd1->allocator_dumps_edges_for_testing().empty()); |
58 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad1")); | 58 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad1")); |
59 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2")); | 59 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2")); |
60 ASSERT_FALSE(pmd1->has_process_totals()); | 60 ASSERT_FALSE(pmd1->has_process_totals()); |
61 ASSERT_FALSE(pmd1->has_process_mmaps()); | 61 ASSERT_FALSE(pmd1->has_process_mmaps()); |
62 ASSERT_TRUE(pmd1->process_mmaps()->vm_regions().empty()); | 62 ASSERT_TRUE(pmd1->process_mmaps()->vm_regions().empty()); |
63 ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1)); | 63 ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1)); |
64 ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2)); | 64 ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2)); |
65 | 65 |
66 // Check that calling AsValueInto() doesn't cause a crash. | 66 // Check that calling AsValueInto() doesn't cause a crash. |
67 std::unique_ptr<TracedValue> traced_value(new TracedValue); | 67 std::unique_ptr<TracedValue> traced_value(new TracedValue); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 MemoryAllocatorDumpGuid shared_mad_guid1(1); | 119 MemoryAllocatorDumpGuid shared_mad_guid1(1); |
120 MemoryAllocatorDumpGuid shared_mad_guid2(2); | 120 MemoryAllocatorDumpGuid shared_mad_guid2(2); |
121 auto* shared_mad1 = pmd2->CreateSharedGlobalAllocatorDump(shared_mad_guid1); | 121 auto* shared_mad1 = pmd2->CreateSharedGlobalAllocatorDump(shared_mad_guid1); |
122 auto* shared_mad2 = | 122 auto* shared_mad2 = |
123 pmd2->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2); | 123 pmd2->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2); |
124 | 124 |
125 pmd1->TakeAllDumpsFrom(pmd2.get()); | 125 pmd1->TakeAllDumpsFrom(pmd2.get()); |
126 | 126 |
127 // Make sure that pmd2 is empty but still usable after it has been emptied. | 127 // Make sure that pmd2 is empty but still usable after it has been emptied. |
128 ASSERT_TRUE(pmd2->allocator_dumps().empty()); | 128 ASSERT_TRUE(pmd2->allocator_dumps().empty()); |
129 ASSERT_TRUE(pmd2->allocator_dumps_edges().empty()); | 129 ASSERT_TRUE(pmd2->allocator_dumps_edges_for_testing().empty()); |
130 ASSERT_TRUE(pmd2->heap_dumps().empty()); | 130 ASSERT_TRUE(pmd2->heap_dumps().empty()); |
131 pmd2->CreateAllocatorDump("pmd2/this_mad_stays_with_pmd2"); | 131 pmd2->CreateAllocatorDump("pmd2/this_mad_stays_with_pmd2"); |
132 ASSERT_EQ(1u, pmd2->allocator_dumps().size()); | 132 ASSERT_EQ(1u, pmd2->allocator_dumps().size()); |
133 ASSERT_EQ(1u, pmd2->allocator_dumps().count("pmd2/this_mad_stays_with_pmd2")); | 133 ASSERT_EQ(1u, pmd2->allocator_dumps().count("pmd2/this_mad_stays_with_pmd2")); |
134 pmd2->AddOwnershipEdge(MemoryAllocatorDumpGuid(42), | 134 pmd2->AddOwnershipEdge(MemoryAllocatorDumpGuid(42), |
135 MemoryAllocatorDumpGuid(4242)); | 135 MemoryAllocatorDumpGuid(4242)); |
136 | 136 |
137 // Check that calling AsValueInto() doesn't cause a crash. | 137 // Check that calling AsValueInto() doesn't cause a crash. |
138 pmd2->AsValueInto(traced_value.get()); | 138 pmd2->AsValueInto(traced_value.get()); |
139 | 139 |
140 // Free the |pmd2| to check that the memory ownership of the two MAD(s) | 140 // Free the |pmd2| to check that the memory ownership of the two MAD(s) |
141 // has been transferred to |pmd1|. | 141 // has been transferred to |pmd1|. |
142 pmd2.reset(); | 142 pmd2.reset(); |
143 | 143 |
144 // Now check that |pmd1| has been effectively merged. | 144 // Now check that |pmd1| has been effectively merged. |
145 ASSERT_EQ(6u, pmd1->allocator_dumps().size()); | 145 ASSERT_EQ(6u, pmd1->allocator_dumps().size()); |
146 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad1")); | 146 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad1")); |
147 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2")); | 147 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2")); |
148 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd2/mad1")); | 148 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd2/mad1")); |
149 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2")); | 149 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2")); |
150 ASSERT_EQ(2u, pmd1->allocator_dumps_edges().size()); | 150 ASSERT_EQ(2u, pmd1->allocator_dumps_edges_for_testing().size()); |
151 ASSERT_EQ(shared_mad1, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1)); | 151 ASSERT_EQ(shared_mad1, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1)); |
152 ASSERT_EQ(shared_mad2, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2)); | 152 ASSERT_EQ(shared_mad2, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2)); |
153 ASSERT_TRUE(MemoryAllocatorDump::Flags::WEAK & shared_mad2->flags()); | 153 ASSERT_TRUE(MemoryAllocatorDump::Flags::WEAK & shared_mad2->flags()); |
154 ASSERT_EQ(4u, pmd1->heap_dumps().size()); | 154 ASSERT_EQ(4u, pmd1->heap_dumps().size()); |
155 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump1") != nullptr); | 155 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump1") != nullptr); |
156 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump2") != nullptr); | 156 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump2") != nullptr); |
157 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd2/heap_dump1") != nullptr); | 157 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd2/heap_dump1") != nullptr); |
158 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd2/heap_dump2") != nullptr); | 158 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd2/heap_dump2") != nullptr); |
159 | 159 |
160 // Check that calling AsValueInto() doesn't cause a crash. | 160 // Check that calling AsValueInto() doesn't cause a crash. |
161 traced_value.reset(new TracedValue); | 161 traced_value.reset(new TracedValue); |
162 pmd1->AsValueInto(traced_value.get()); | 162 pmd1->AsValueInto(traced_value.get()); |
163 | 163 |
164 pmd1.reset(); | 164 pmd1.reset(); |
165 } | 165 } |
166 | 166 |
| 167 TEST(ProcessMemoryDumpTest, OverrideOwnershipEdge) { |
| 168 std::unique_ptr<ProcessMemoryDump> pmd( |
| 169 new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); |
| 170 |
| 171 auto* shm_dump1 = pmd->CreateAllocatorDump("shared_mem/seg1"); |
| 172 auto* shm_dump2 = pmd->CreateAllocatorDump("shared_mem/seg2"); |
| 173 auto* shm_dump3 = pmd->CreateAllocatorDump("shared_mem/seg3"); |
| 174 auto* shm_dump4 = pmd->CreateAllocatorDump("shared_mem/seg4"); |
| 175 |
| 176 // Create one allocation with an auto-assigned guid and mark it as a |
| 177 // suballocation of "fakealloc/allocated_objects". |
| 178 auto* child1_dump = pmd->CreateAllocatorDump("shared_mem/child/seg1"); |
| 179 pmd->AddOverridableOwnershipEdge(child1_dump->guid(), shm_dump1->guid(), |
| 180 0 /* importance */); |
| 181 auto* child2_dump = pmd->CreateAllocatorDump("shared_mem/child/seg2"); |
| 182 pmd->AddOwnershipEdge(child2_dump->guid(), shm_dump2->guid(), |
| 183 3 /* importance */); |
| 184 MemoryAllocatorDumpGuid shared_mad_guid(1); |
| 185 pmd->CreateSharedGlobalAllocatorDump(shared_mad_guid); |
| 186 pmd->AddOverridableOwnershipEdge(shm_dump3->guid(), shared_mad_guid, |
| 187 0 /* importance */); |
| 188 auto* child4_dump = pmd->CreateAllocatorDump("shared_mem/child/seg4"); |
| 189 pmd->AddOverridableOwnershipEdge(child4_dump->guid(), shm_dump4->guid(), |
| 190 4 /* importance */); |
| 191 |
| 192 const ProcessMemoryDump::AllocatorDumpEdgesMap& edges = |
| 193 pmd->allocator_dumps_edges_for_testing(); |
| 194 EXPECT_EQ(4u, edges.size()); |
| 195 EXPECT_EQ(shm_dump1->guid(), edges.find(child1_dump->guid())->second.target); |
| 196 EXPECT_EQ(0, edges.find(child1_dump->guid())->second.importance); |
| 197 EXPECT_TRUE(edges.find(child1_dump->guid())->second.overridable); |
| 198 EXPECT_EQ(shm_dump2->guid(), edges.find(child2_dump->guid())->second.target); |
| 199 EXPECT_EQ(3, edges.find(child2_dump->guid())->second.importance); |
| 200 EXPECT_FALSE(edges.find(child2_dump->guid())->second.overridable); |
| 201 EXPECT_EQ(shared_mad_guid, edges.find(shm_dump3->guid())->second.target); |
| 202 EXPECT_EQ(0, edges.find(shm_dump3->guid())->second.importance); |
| 203 EXPECT_TRUE(edges.find(shm_dump3->guid())->second.overridable); |
| 204 EXPECT_EQ(shm_dump4->guid(), edges.find(child4_dump->guid())->second.target); |
| 205 EXPECT_EQ(4, edges.find(child4_dump->guid())->second.importance); |
| 206 EXPECT_TRUE(edges.find(child4_dump->guid())->second.overridable); |
| 207 |
| 208 // These should override old edges: |
| 209 pmd->AddOwnershipEdge(child1_dump->guid(), shm_dump1->guid(), |
| 210 1 /* importance */); |
| 211 pmd->AddOwnershipEdge(shm_dump3->guid(), shared_mad_guid, 2 /* importance */); |
| 212 // This should not change the old edges. |
| 213 pmd->AddOverridableOwnershipEdge(child2_dump->guid(), shm_dump2->guid(), |
| 214 0 /* importance */); |
| 215 pmd->AddOwnershipEdge(child4_dump->guid(), shm_dump4->guid(), |
| 216 0 /* importance */); |
| 217 |
| 218 EXPECT_EQ(4u, edges.size()); |
| 219 EXPECT_EQ(shm_dump1->guid(), edges.find(child1_dump->guid())->second.target); |
| 220 EXPECT_EQ(1, edges.find(child1_dump->guid())->second.importance); |
| 221 EXPECT_FALSE(edges.find(child1_dump->guid())->second.overridable); |
| 222 EXPECT_EQ(shm_dump2->guid(), edges.find(child2_dump->guid())->second.target); |
| 223 EXPECT_EQ(3, edges.find(child2_dump->guid())->second.importance); |
| 224 EXPECT_FALSE(edges.find(child2_dump->guid())->second.overridable); |
| 225 EXPECT_EQ(shared_mad_guid, edges.find(shm_dump3->guid())->second.target); |
| 226 EXPECT_EQ(2, edges.find(shm_dump3->guid())->second.importance); |
| 227 EXPECT_FALSE(edges.find(shm_dump3->guid())->second.overridable); |
| 228 EXPECT_EQ(shm_dump4->guid(), edges.find(child4_dump->guid())->second.target); |
| 229 EXPECT_EQ(0, edges.find(child4_dump->guid())->second.importance); |
| 230 EXPECT_FALSE(edges.find(child4_dump->guid())->second.overridable); |
| 231 } |
| 232 |
167 TEST(ProcessMemoryDumpTest, Suballocations) { | 233 TEST(ProcessMemoryDumpTest, Suballocations) { |
168 std::unique_ptr<ProcessMemoryDump> pmd( | 234 std::unique_ptr<ProcessMemoryDump> pmd( |
169 new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); | 235 new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); |
170 const std::string allocator_dump_name = "fakealloc/allocated_objects"; | 236 const std::string allocator_dump_name = "fakealloc/allocated_objects"; |
171 pmd->CreateAllocatorDump(allocator_dump_name); | 237 pmd->CreateAllocatorDump(allocator_dump_name); |
172 | 238 |
173 // Create one allocation with an auto-assigned guid and mark it as a | 239 // Create one allocation with an auto-assigned guid and mark it as a |
174 // suballocation of "fakealloc/allocated_objects". | 240 // suballocation of "fakealloc/allocated_objects". |
175 auto* pic1_dump = pmd->CreateAllocatorDump("picturemanager/picture1"); | 241 auto* pic1_dump = pmd->CreateAllocatorDump("picturemanager/picture1"); |
176 pmd->AddSuballocation(pic1_dump->guid(), allocator_dump_name); | 242 pmd->AddSuballocation(pic1_dump->guid(), allocator_dump_name); |
177 | 243 |
178 // Same here, but this time create an allocation with an explicit guid. | 244 // Same here, but this time create an allocation with an explicit guid. |
179 auto* pic2_dump = pmd->CreateAllocatorDump("picturemanager/picture2", | 245 auto* pic2_dump = pmd->CreateAllocatorDump("picturemanager/picture2", |
180 MemoryAllocatorDumpGuid(0x42)); | 246 MemoryAllocatorDumpGuid(0x42)); |
181 pmd->AddSuballocation(pic2_dump->guid(), allocator_dump_name); | 247 pmd->AddSuballocation(pic2_dump->guid(), allocator_dump_name); |
182 | 248 |
183 // Now check that AddSuballocation() has created anonymous child dumps under | 249 // Now check that AddSuballocation() has created anonymous child dumps under |
184 // "fakealloc/allocated_objects". | 250 // "fakealloc/allocated_objects". |
185 auto anon_node_1_it = pmd->allocator_dumps().find( | 251 auto anon_node_1_it = pmd->allocator_dumps().find( |
186 allocator_dump_name + "/__" + pic1_dump->guid().ToString()); | 252 allocator_dump_name + "/__" + pic1_dump->guid().ToString()); |
187 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_1_it); | 253 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_1_it); |
188 | 254 |
189 auto anon_node_2_it = | 255 auto anon_node_2_it = |
190 pmd->allocator_dumps().find(allocator_dump_name + "/__42"); | 256 pmd->allocator_dumps().find(allocator_dump_name + "/__42"); |
191 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_2_it); | 257 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_2_it); |
192 | 258 |
193 // Finally check that AddSuballocation() has created also the | 259 // Finally check that AddSuballocation() has created also the |
194 // edges between the pictures and the anonymous allocator child dumps. | 260 // edges between the pictures and the anonymous allocator child dumps. |
195 bool found_edge[2]{false, false}; | 261 bool found_edge[2]{false, false}; |
196 for (const auto& e : pmd->allocator_dumps_edges()) { | 262 for (const auto& e : pmd->allocator_dumps_edges_for_testing()) { |
197 found_edge[0] |= (e.source == pic1_dump->guid() && | 263 found_edge[0] |= (e.first == pic1_dump->guid() && |
198 e.target == anon_node_1_it->second->guid()); | 264 e.second.target == anon_node_1_it->second->guid()); |
199 found_edge[1] |= (e.source == pic2_dump->guid() && | 265 found_edge[1] |= (e.first == pic2_dump->guid() && |
200 e.target == anon_node_2_it->second->guid()); | 266 e.second.target == anon_node_2_it->second->guid()); |
201 } | 267 } |
202 ASSERT_TRUE(found_edge[0]); | 268 ASSERT_TRUE(found_edge[0]); |
203 ASSERT_TRUE(found_edge[1]); | 269 ASSERT_TRUE(found_edge[1]); |
204 | 270 |
205 // Check that calling AsValueInto() doesn't cause a crash. | 271 // Check that calling AsValueInto() doesn't cause a crash. |
206 std::unique_ptr<TracedValue> traced_value(new TracedValue); | 272 std::unique_ptr<TracedValue> traced_value(new TracedValue); |
207 pmd->AsValueInto(traced_value.get()); | 273 pmd->AsValueInto(traced_value.get()); |
208 | 274 |
209 pmd.reset(); | 275 pmd.reset(); |
210 } | 276 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 static_cast<char*>(base::AlignedAlloc(kVeryLargeMemorySize, page_size))); | 363 static_cast<char*>(base::AlignedAlloc(kVeryLargeMemorySize, page_size))); |
298 memset(memory2.get(), 0, kVeryLargeMemorySize); | 364 memset(memory2.get(), 0, kVeryLargeMemorySize); |
299 size_t res2 = ProcessMemoryDump::CountResidentBytes(memory2.get(), | 365 size_t res2 = ProcessMemoryDump::CountResidentBytes(memory2.get(), |
300 kVeryLargeMemorySize); | 366 kVeryLargeMemorySize); |
301 ASSERT_EQ(res2, kVeryLargeMemorySize); | 367 ASSERT_EQ(res2, kVeryLargeMemorySize); |
302 } | 368 } |
303 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) | 369 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) |
304 | 370 |
305 } // namespace trace_event | 371 } // namespace trace_event |
306 } // namespace base | 372 } // namespace base |
OLD | NEW |