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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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); |
68 pmd1->AsValueInto(traced_value.get()); | 68 pmd1->AsValueInto(traced_value.get()); |
69 | 69 |
70 // Check that the pmd can be reused and behaves as expected. | 70 // Check that the pmd can be reused and behaves as expected. |
71 auto mad1 = pmd1->CreateAllocatorDump("mad1"); | 71 auto* mad1 = pmd1->CreateAllocatorDump("mad1"); |
72 auto mad3 = pmd1->CreateAllocatorDump("mad3"); | 72 auto* mad3 = pmd1->CreateAllocatorDump("mad3"); |
73 auto shared_mad1 = pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid1); | 73 auto* shared_mad1 = pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid1); |
74 auto shared_mad2 = | 74 auto* shared_mad2 = |
75 pmd1->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2); | 75 pmd1->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2); |
76 ASSERT_EQ(4u, pmd1->allocator_dumps().size()); | 76 ASSERT_EQ(4u, pmd1->allocator_dumps().size()); |
77 ASSERT_EQ(mad1, pmd1->GetAllocatorDump("mad1")); | 77 ASSERT_EQ(mad1, pmd1->GetAllocatorDump("mad1")); |
78 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2")); | 78 ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2")); |
79 ASSERT_EQ(mad3, pmd1->GetAllocatorDump("mad3")); | 79 ASSERT_EQ(mad3, pmd1->GetAllocatorDump("mad3")); |
80 ASSERT_EQ(shared_mad1, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1)); | 80 ASSERT_EQ(shared_mad1, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1)); |
81 ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags()); | 81 ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags()); |
82 ASSERT_EQ(shared_mad2, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2)); | 82 ASSERT_EQ(shared_mad2, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2)); |
83 ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad2->flags()); | 83 ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad2->flags()); |
84 | 84 |
(...skipping 10 matching lines...) Expand all Loading... |
95 TraceEventMemoryOverhead overhead; | 95 TraceEventMemoryOverhead overhead; |
96 | 96 |
97 scoped_refptr<MemoryDumpSessionState> session_state = | 97 scoped_refptr<MemoryDumpSessionState> session_state = |
98 new MemoryDumpSessionState; | 98 new MemoryDumpSessionState; |
99 session_state->SetStackFrameDeduplicator( | 99 session_state->SetStackFrameDeduplicator( |
100 WrapUnique(new StackFrameDeduplicator)); | 100 WrapUnique(new StackFrameDeduplicator)); |
101 session_state->SetTypeNameDeduplicator( | 101 session_state->SetTypeNameDeduplicator( |
102 WrapUnique(new TypeNameDeduplicator)); | 102 WrapUnique(new TypeNameDeduplicator)); |
103 std::unique_ptr<ProcessMemoryDump> pmd1( | 103 std::unique_ptr<ProcessMemoryDump> pmd1( |
104 new ProcessMemoryDump(session_state.get(), kDetailedDumpArgs)); | 104 new ProcessMemoryDump(session_state.get(), kDetailedDumpArgs)); |
105 auto mad1_1 = pmd1->CreateAllocatorDump("pmd1/mad1"); | 105 auto* mad1_1 = pmd1->CreateAllocatorDump("pmd1/mad1"); |
106 auto mad1_2 = pmd1->CreateAllocatorDump("pmd1/mad2"); | 106 auto* mad1_2 = pmd1->CreateAllocatorDump("pmd1/mad2"); |
107 pmd1->AddOwnershipEdge(mad1_1->guid(), mad1_2->guid()); | 107 pmd1->AddOwnershipEdge(mad1_1->guid(), mad1_2->guid()); |
108 pmd1->DumpHeapUsage(metrics_by_context, overhead, "pmd1/heap_dump1"); | 108 pmd1->DumpHeapUsage(metrics_by_context, overhead, "pmd1/heap_dump1"); |
109 pmd1->DumpHeapUsage(metrics_by_context, overhead, "pmd1/heap_dump2"); | 109 pmd1->DumpHeapUsage(metrics_by_context, overhead, "pmd1/heap_dump2"); |
110 | 110 |
111 std::unique_ptr<ProcessMemoryDump> pmd2( | 111 std::unique_ptr<ProcessMemoryDump> pmd2( |
112 new ProcessMemoryDump(session_state.get(), kDetailedDumpArgs)); | 112 new ProcessMemoryDump(session_state.get(), kDetailedDumpArgs)); |
113 auto mad2_1 = pmd2->CreateAllocatorDump("pmd2/mad1"); | 113 auto* mad2_1 = pmd2->CreateAllocatorDump("pmd2/mad1"); |
114 auto mad2_2 = pmd2->CreateAllocatorDump("pmd2/mad2"); | 114 auto* mad2_2 = pmd2->CreateAllocatorDump("pmd2/mad2"); |
115 pmd2->AddOwnershipEdge(mad2_1->guid(), mad2_2->guid()); | 115 pmd2->AddOwnershipEdge(mad2_1->guid(), mad2_2->guid()); |
116 pmd2->DumpHeapUsage(metrics_by_context, overhead, "pmd2/heap_dump1"); | 116 pmd2->DumpHeapUsage(metrics_by_context, overhead, "pmd2/heap_dump1"); |
117 pmd2->DumpHeapUsage(metrics_by_context, overhead, "pmd2/heap_dump2"); | 117 pmd2->DumpHeapUsage(metrics_by_context, overhead, "pmd2/heap_dump2"); |
118 | 118 |
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().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()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 } | 165 } |
166 | 166 |
167 TEST(ProcessMemoryDumpTest, Suballocations) { | 167 TEST(ProcessMemoryDumpTest, Suballocations) { |
168 std::unique_ptr<ProcessMemoryDump> pmd( | 168 std::unique_ptr<ProcessMemoryDump> pmd( |
169 new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); | 169 new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); |
170 const std::string allocator_dump_name = "fakealloc/allocated_objects"; | 170 const std::string allocator_dump_name = "fakealloc/allocated_objects"; |
171 pmd->CreateAllocatorDump(allocator_dump_name); | 171 pmd->CreateAllocatorDump(allocator_dump_name); |
172 | 172 |
173 // Create one allocation with an auto-assigned guid and mark it as a | 173 // Create one allocation with an auto-assigned guid and mark it as a |
174 // suballocation of "fakealloc/allocated_objects". | 174 // suballocation of "fakealloc/allocated_objects". |
175 auto pic1_dump = pmd->CreateAllocatorDump("picturemanager/picture1"); | 175 auto* pic1_dump = pmd->CreateAllocatorDump("picturemanager/picture1"); |
176 pmd->AddSuballocation(pic1_dump->guid(), allocator_dump_name); | 176 pmd->AddSuballocation(pic1_dump->guid(), allocator_dump_name); |
177 | 177 |
178 // Same here, but this time create an allocation with an explicit guid. | 178 // Same here, but this time create an allocation with an explicit guid. |
179 auto pic2_dump = pmd->CreateAllocatorDump("picturemanager/picture2", | 179 auto* pic2_dump = pmd->CreateAllocatorDump("picturemanager/picture2", |
180 MemoryAllocatorDumpGuid(0x42)); | 180 MemoryAllocatorDumpGuid(0x42)); |
181 pmd->AddSuballocation(pic2_dump->guid(), allocator_dump_name); | 181 pmd->AddSuballocation(pic2_dump->guid(), allocator_dump_name); |
182 | 182 |
183 // Now check that AddSuballocation() has created anonymous child dumps under | 183 // Now check that AddSuballocation() has created anonymous child dumps under |
184 // "fakealloc/allocated_objects". | 184 // "fakealloc/allocated_objects". |
185 auto anon_node_1_it = pmd->allocator_dumps().find( | 185 auto anon_node_1_it = pmd->allocator_dumps().find( |
186 allocator_dump_name + "/__" + pic1_dump->guid().ToString()); | 186 allocator_dump_name + "/__" + pic1_dump->guid().ToString()); |
187 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_1_it); | 187 ASSERT_NE(pmd->allocator_dumps().end(), anon_node_1_it); |
188 | 188 |
189 auto anon_node_2_it = | 189 auto anon_node_2_it = |
(...skipping 16 matching lines...) Expand all Loading... |
206 std::unique_ptr<TracedValue> traced_value(new TracedValue); | 206 std::unique_ptr<TracedValue> traced_value(new TracedValue); |
207 pmd->AsValueInto(traced_value.get()); | 207 pmd->AsValueInto(traced_value.get()); |
208 | 208 |
209 pmd.reset(); | 209 pmd.reset(); |
210 } | 210 } |
211 | 211 |
212 TEST(ProcessMemoryDumpTest, GlobalAllocatorDumpTest) { | 212 TEST(ProcessMemoryDumpTest, GlobalAllocatorDumpTest) { |
213 std::unique_ptr<ProcessMemoryDump> pmd( | 213 std::unique_ptr<ProcessMemoryDump> pmd( |
214 new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); | 214 new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); |
215 MemoryAllocatorDumpGuid shared_mad_guid(1); | 215 MemoryAllocatorDumpGuid shared_mad_guid(1); |
216 auto shared_mad1 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid); | 216 auto* shared_mad1 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid); |
217 ASSERT_EQ(shared_mad_guid, shared_mad1->guid()); | 217 ASSERT_EQ(shared_mad_guid, shared_mad1->guid()); |
218 ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad1->flags()); | 218 ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad1->flags()); |
219 | 219 |
220 auto shared_mad2 = pmd->GetSharedGlobalAllocatorDump(shared_mad_guid); | 220 auto* shared_mad2 = pmd->GetSharedGlobalAllocatorDump(shared_mad_guid); |
221 ASSERT_EQ(shared_mad1, shared_mad2); | 221 ASSERT_EQ(shared_mad1, shared_mad2); |
222 ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad1->flags()); | 222 ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad1->flags()); |
223 | 223 |
224 auto shared_mad3 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid); | 224 auto* shared_mad3 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid); |
225 ASSERT_EQ(shared_mad1, shared_mad3); | 225 ASSERT_EQ(shared_mad1, shared_mad3); |
226 ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad1->flags()); | 226 ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad1->flags()); |
227 | 227 |
228 auto shared_mad4 = pmd->CreateSharedGlobalAllocatorDump(shared_mad_guid); | 228 auto* shared_mad4 = pmd->CreateSharedGlobalAllocatorDump(shared_mad_guid); |
229 ASSERT_EQ(shared_mad1, shared_mad4); | 229 ASSERT_EQ(shared_mad1, shared_mad4); |
230 ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags()); | 230 ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags()); |
231 | 231 |
232 auto shared_mad5 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid); | 232 auto* shared_mad5 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid); |
233 ASSERT_EQ(shared_mad1, shared_mad5); | 233 ASSERT_EQ(shared_mad1, shared_mad5); |
234 ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags()); | 234 ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags()); |
235 } | 235 } |
236 | 236 |
237 TEST(ProcessMemoryDumpTest, BackgroundModeTest) { | 237 TEST(ProcessMemoryDumpTest, BackgroundModeTest) { |
238 MemoryDumpArgs background_args = {MemoryDumpLevelOfDetail::BACKGROUND}; | 238 MemoryDumpArgs background_args = {MemoryDumpLevelOfDetail::BACKGROUND}; |
239 std::unique_ptr<ProcessMemoryDump> pmd( | 239 std::unique_ptr<ProcessMemoryDump> pmd( |
240 new ProcessMemoryDump(nullptr, background_args)); | 240 new ProcessMemoryDump(nullptr, background_args)); |
241 ProcessMemoryDump::is_black_hole_non_fatal_for_testing_ = true; | 241 ProcessMemoryDump::is_black_hole_non_fatal_for_testing_ = true; |
242 SetAllocatorDumpNameWhitelistForTesting(kTestDumpNameWhitelist); | 242 SetAllocatorDumpNameWhitelistForTesting(kTestDumpNameWhitelist); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 static_cast<char*>(base::AlignedAlloc(kVeryLargeMemorySize, page_size))); | 297 static_cast<char*>(base::AlignedAlloc(kVeryLargeMemorySize, page_size))); |
298 memset(memory2.get(), 0, kVeryLargeMemorySize); | 298 memset(memory2.get(), 0, kVeryLargeMemorySize); |
299 size_t res2 = ProcessMemoryDump::CountResidentBytes(memory2.get(), | 299 size_t res2 = ProcessMemoryDump::CountResidentBytes(memory2.get(), |
300 kVeryLargeMemorySize); | 300 kVeryLargeMemorySize); |
301 ASSERT_EQ(res2, kVeryLargeMemorySize); | 301 ASSERT_EQ(res2, kVeryLargeMemorySize); |
302 } | 302 } |
303 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) | 303 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) |
304 | 304 |
305 } // namespace trace_event | 305 } // namespace trace_event |
306 } // namespace base | 306 } // namespace base |
OLD | NEW |