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" |
11 #include "base/process/process_metrics.h" | 11 #include "base/process/process_metrics.h" |
| 12 #include "base/trace_event/heap_profiler_allocation_register.h" |
12 #include "base/trace_event/memory_allocator_dump_guid.h" | 13 #include "base/trace_event/memory_allocator_dump_guid.h" |
13 #include "base/trace_event/memory_infra_background_whitelist.h" | 14 #include "base/trace_event/memory_infra_background_whitelist.h" |
14 #include "base/trace_event/trace_event_argument.h" | 15 #include "base/trace_event/trace_event_argument.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 namespace trace_event { | 19 namespace trace_event { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad2->flags()); | 84 ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad2->flags()); |
84 | 85 |
85 traced_value.reset(new TracedValue); | 86 traced_value.reset(new TracedValue); |
86 pmd1->AsValueInto(traced_value.get()); | 87 pmd1->AsValueInto(traced_value.get()); |
87 | 88 |
88 pmd1.reset(); | 89 pmd1.reset(); |
89 } | 90 } |
90 | 91 |
91 TEST(ProcessMemoryDumpTest, TakeAllDumpsFrom) { | 92 TEST(ProcessMemoryDumpTest, TakeAllDumpsFrom) { |
92 std::unique_ptr<TracedValue> traced_value(new TracedValue); | 93 std::unique_ptr<TracedValue> traced_value(new TracedValue); |
93 hash_map<AllocationContext, AllocationMetrics> metrics_by_context; | 94 AllocationRegister allocation_register(10, 10); |
94 metrics_by_context[AllocationContext()] = { 1, 1 }; | 95 allocation_register.Insert("", 100, AllocationContext()); |
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->CreateDeduplicators(); |
100 WrapUnique(new StackFrameDeduplicator)); | |
101 session_state->SetTypeNameDeduplicator( | |
102 WrapUnique(new TypeNameDeduplicator)); | |
103 std::unique_ptr<ProcessMemoryDump> pmd1( | 100 std::unique_ptr<ProcessMemoryDump> pmd1( |
104 new ProcessMemoryDump(session_state.get(), kDetailedDumpArgs)); | 101 new ProcessMemoryDump(session_state.get(), kDetailedDumpArgs)); |
105 auto* mad1_1 = pmd1->CreateAllocatorDump("pmd1/mad1"); | 102 auto* mad1_1 = pmd1->CreateAllocatorDump("pmd1/mad1"); |
106 auto* mad1_2 = pmd1->CreateAllocatorDump("pmd1/mad2"); | 103 auto* mad1_2 = pmd1->CreateAllocatorDump("pmd1/mad2"); |
107 pmd1->AddOwnershipEdge(mad1_1->guid(), mad1_2->guid()); | 104 pmd1->AddOwnershipEdge(mad1_1->guid(), mad1_2->guid()); |
108 pmd1->DumpHeapUsage(metrics_by_context, overhead, "pmd1/heap_dump1"); | 105 pmd1->DumpHeapUsage(allocation_register, "pmd1/heap_dump1"); |
109 pmd1->DumpHeapUsage(metrics_by_context, overhead, "pmd1/heap_dump2"); | 106 pmd1->DumpHeapUsage(allocation_register, "pmd1/heap_dump2"); |
110 | 107 |
111 std::unique_ptr<ProcessMemoryDump> pmd2( | 108 std::unique_ptr<ProcessMemoryDump> pmd2( |
112 new ProcessMemoryDump(session_state.get(), kDetailedDumpArgs)); | 109 new ProcessMemoryDump(session_state.get(), kDetailedDumpArgs)); |
113 auto* mad2_1 = pmd2->CreateAllocatorDump("pmd2/mad1"); | 110 auto* mad2_1 = pmd2->CreateAllocatorDump("pmd2/mad1"); |
114 auto* mad2_2 = pmd2->CreateAllocatorDump("pmd2/mad2"); | 111 auto* mad2_2 = pmd2->CreateAllocatorDump("pmd2/mad2"); |
115 pmd2->AddOwnershipEdge(mad2_1->guid(), mad2_2->guid()); | 112 pmd2->AddOwnershipEdge(mad2_1->guid(), mad2_2->guid()); |
116 pmd2->DumpHeapUsage(metrics_by_context, overhead, "pmd2/heap_dump1"); | 113 pmd2->DumpHeapUsage(allocation_register, "pmd2/heap_dump1"); |
117 pmd2->DumpHeapUsage(metrics_by_context, overhead, "pmd2/heap_dump2"); | 114 pmd2->DumpHeapUsage(allocation_register, "pmd2/heap_dump2"); |
118 | 115 |
119 MemoryAllocatorDumpGuid shared_mad_guid1(1); | 116 MemoryAllocatorDumpGuid shared_mad_guid1(1); |
120 MemoryAllocatorDumpGuid shared_mad_guid2(2); | 117 MemoryAllocatorDumpGuid shared_mad_guid2(2); |
121 auto* shared_mad1 = pmd2->CreateSharedGlobalAllocatorDump(shared_mad_guid1); | 118 auto* shared_mad1 = pmd2->CreateSharedGlobalAllocatorDump(shared_mad_guid1); |
122 auto* shared_mad2 = | 119 auto* shared_mad2 = |
123 pmd2->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2); | 120 pmd2->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2); |
124 | 121 |
125 pmd1->TakeAllDumpsFrom(pmd2.get()); | 122 pmd1->TakeAllDumpsFrom(pmd2.get()); |
126 | 123 |
127 // Make sure that pmd2 is empty but still usable after it has been emptied. | 124 // Make sure that pmd2 is empty but still usable after it has been emptied. |
128 ASSERT_TRUE(pmd2->allocator_dumps().empty()); | 125 ASSERT_TRUE(pmd2->allocator_dumps().empty()); |
129 ASSERT_TRUE(pmd2->allocator_dumps_edges().empty()); | 126 ASSERT_TRUE(pmd2->allocator_dumps_edges().empty()); |
130 ASSERT_TRUE(pmd2->heap_dumps().empty()); | 127 ASSERT_TRUE(pmd2->heap_dumps().empty()); |
131 pmd2->CreateAllocatorDump("pmd2/this_mad_stays_with_pmd2"); | 128 pmd2->CreateAllocatorDump("pmd2/this_mad_stays_with_pmd2"); |
132 ASSERT_EQ(1u, pmd2->allocator_dumps().size()); | 129 ASSERT_EQ(1u, pmd2->allocator_dumps().size()); |
133 ASSERT_EQ(1u, pmd2->allocator_dumps().count("pmd2/this_mad_stays_with_pmd2")); | 130 ASSERT_EQ(1u, pmd2->allocator_dumps().count("pmd2/this_mad_stays_with_pmd2")); |
134 pmd2->AddOwnershipEdge(MemoryAllocatorDumpGuid(42), | 131 pmd2->AddOwnershipEdge(MemoryAllocatorDumpGuid(42), |
135 MemoryAllocatorDumpGuid(4242)); | 132 MemoryAllocatorDumpGuid(4242)); |
136 | 133 |
137 // Check that calling AsValueInto() doesn't cause a crash. | 134 // Check that calling AsValueInto() doesn't cause a crash. |
138 pmd2->AsValueInto(traced_value.get()); | 135 pmd2->AsValueInto(traced_value.get()); |
139 | 136 |
140 // Free the |pmd2| to check that the memory ownership of the two MAD(s) | 137 // Free the |pmd2| to check that the memory ownership of the two MAD(s) |
141 // has been transferred to |pmd1|. | 138 // has been transferred to |pmd1|. |
142 pmd2.reset(); | 139 pmd2.reset(); |
143 | 140 |
144 // Now check that |pmd1| has been effectively merged. | 141 // Now check that |pmd1| has been effectively merged. |
145 ASSERT_EQ(6u, pmd1->allocator_dumps().size()); | 142 // Note that DumpHeapUsage() adds an implicit dump for AllocationRegister's |
| 143 // memory overhead. |
| 144 ASSERT_EQ(10u, pmd1->allocator_dumps().size()); |
146 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad1")); | 145 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad1")); |
147 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2")); | 146 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2")); |
148 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd2/mad1")); | 147 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd2/mad1")); |
149 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2")); | 148 ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2")); |
150 ASSERT_EQ(2u, pmd1->allocator_dumps_edges().size()); | 149 ASSERT_EQ(2u, pmd1->allocator_dumps_edges().size()); |
151 ASSERT_EQ(shared_mad1, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1)); | 150 ASSERT_EQ(shared_mad1, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1)); |
152 ASSERT_EQ(shared_mad2, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2)); | 151 ASSERT_EQ(shared_mad2, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2)); |
153 ASSERT_TRUE(MemoryAllocatorDump::Flags::WEAK & shared_mad2->flags()); | 152 ASSERT_TRUE(MemoryAllocatorDump::Flags::WEAK & shared_mad2->flags()); |
154 ASSERT_EQ(4u, pmd1->heap_dumps().size()); | 153 ASSERT_EQ(4u, pmd1->heap_dumps().size()); |
155 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump1") != nullptr); | 154 ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump1") != nullptr); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 static_cast<char*>(base::AlignedAlloc(kVeryLargeMemorySize, page_size))); | 296 static_cast<char*>(base::AlignedAlloc(kVeryLargeMemorySize, page_size))); |
298 memset(memory2.get(), 0, kVeryLargeMemorySize); | 297 memset(memory2.get(), 0, kVeryLargeMemorySize); |
299 size_t res2 = ProcessMemoryDump::CountResidentBytes(memory2.get(), | 298 size_t res2 = ProcessMemoryDump::CountResidentBytes(memory2.get(), |
300 kVeryLargeMemorySize); | 299 kVeryLargeMemorySize); |
301 ASSERT_EQ(res2, kVeryLargeMemorySize); | 300 ASSERT_EQ(res2, kVeryLargeMemorySize); |
302 } | 301 } |
303 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) | 302 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) |
304 | 303 |
305 } // namespace trace_event | 304 } // namespace trace_event |
306 } // namespace base | 305 } // namespace base |
OLD | NEW |