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