| 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/memory_allocator_dump_guid.h" | 12 #include "base/trace_event/memory_allocator_dump_guid.h" |
| 13 #include "base/trace_event/memory_infra_background_whitelist.h" | 13 #include "base/trace_event/memory_infra_background_whitelist.h" |
| 14 #include "base/trace_event/sharded_allocation_register.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 |
| 22 const MemoryDumpArgs kDetailedDumpArgs = {MemoryDumpLevelOfDetail::DETAILED}; | 23 const MemoryDumpArgs kDetailedDumpArgs = {MemoryDumpLevelOfDetail::DETAILED}; |
| 23 const char* const kTestDumpNameWhitelist[] = { | 24 const char* const kTestDumpNameWhitelist[] = { |
| (...skipping 59 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 std::unordered_map<AllocationContext, AllocationMetrics> metrics_by_context; | 94 ShardedAllocationRegister allocation_register; |
| 94 metrics_by_context[AllocationContext()] = { 1, 1 }; | 95 allocation_register.Insert("", 100, AllocationContext()); |
| 95 TraceEventMemoryOverhead overhead; | |
| 96 | 96 |
| 97 scoped_refptr<HeapProfilerSerializationState> | 97 scoped_refptr<HeapProfilerSerializationState> |
| 98 heap_profiler_serialization_state = new HeapProfilerSerializationState; | 98 heap_profiler_serialization_state = new HeapProfilerSerializationState; |
| 99 heap_profiler_serialization_state->SetStackFrameDeduplicator( | 99 heap_profiler_serialization_state->CreateDeduplicators(); |
| 100 WrapUnique(new StackFrameDeduplicator)); | |
| 101 heap_profiler_serialization_state->SetTypeNameDeduplicator( | |
| 102 WrapUnique(new TypeNameDeduplicator)); | |
| 103 std::unique_ptr<ProcessMemoryDump> pmd1(new ProcessMemoryDump( | 100 std::unique_ptr<ProcessMemoryDump> pmd1(new ProcessMemoryDump( |
| 104 heap_profiler_serialization_state.get(), kDetailedDumpArgs)); | 101 heap_profiler_serialization_state.get(), kDetailedDumpArgs)); |
| 102 |
| 105 auto* mad1_1 = pmd1->CreateAllocatorDump("pmd1/mad1"); | 103 auto* mad1_1 = pmd1->CreateAllocatorDump("pmd1/mad1"); |
| 106 auto* mad1_2 = pmd1->CreateAllocatorDump("pmd1/mad2"); | 104 auto* mad1_2 = pmd1->CreateAllocatorDump("pmd1/mad2"); |
| 107 pmd1->AddOwnershipEdge(mad1_1->guid(), mad1_2->guid()); | 105 pmd1->AddOwnershipEdge(mad1_1->guid(), mad1_2->guid()); |
| 108 pmd1->DumpHeapUsage(metrics_by_context, overhead, "pmd1/heap_dump1"); | 106 pmd1->DumpHeapUsage(allocation_register, "pmd1/heap_dump1"); |
| 109 pmd1->DumpHeapUsage(metrics_by_context, overhead, "pmd1/heap_dump2"); | 107 pmd1->DumpHeapUsage(allocation_register, "pmd1/heap_dump2"); |
| 110 | 108 |
| 111 std::unique_ptr<ProcessMemoryDump> pmd2(new ProcessMemoryDump( | 109 std::unique_ptr<ProcessMemoryDump> pmd2(new ProcessMemoryDump( |
| 112 heap_profiler_serialization_state.get(), kDetailedDumpArgs)); | 110 heap_profiler_serialization_state.get(), kDetailedDumpArgs)); |
| 113 auto* mad2_1 = pmd2->CreateAllocatorDump("pmd2/mad1"); | 111 auto* mad2_1 = pmd2->CreateAllocatorDump("pmd2/mad1"); |
| 114 auto* mad2_2 = pmd2->CreateAllocatorDump("pmd2/mad2"); | 112 auto* mad2_2 = pmd2->CreateAllocatorDump("pmd2/mad2"); |
| 115 pmd2->AddOwnershipEdge(mad2_1->guid(), mad2_2->guid()); | 113 pmd2->AddOwnershipEdge(mad2_1->guid(), mad2_2->guid()); |
| 116 pmd2->DumpHeapUsage(metrics_by_context, overhead, "pmd2/heap_dump1"); | 114 pmd2->DumpHeapUsage(allocation_register, "pmd2/heap_dump1"); |
| 117 pmd2->DumpHeapUsage(metrics_by_context, overhead, "pmd2/heap_dump2"); | 115 pmd2->DumpHeapUsage(allocation_register, "pmd2/heap_dump2"); |
| 118 | 116 |
| 119 MemoryAllocatorDumpGuid shared_mad_guid1(1); | 117 MemoryAllocatorDumpGuid shared_mad_guid1(1); |
| 120 MemoryAllocatorDumpGuid shared_mad_guid2(2); | 118 MemoryAllocatorDumpGuid shared_mad_guid2(2); |
| 121 auto* shared_mad1 = pmd2->CreateSharedGlobalAllocatorDump(shared_mad_guid1); | 119 auto* shared_mad1 = pmd2->CreateSharedGlobalAllocatorDump(shared_mad_guid1); |
| 122 auto* shared_mad2 = | 120 auto* shared_mad2 = |
| 123 pmd2->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2); | 121 pmd2->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2); |
| 124 | 122 |
| 125 pmd1->TakeAllDumpsFrom(pmd2.get()); | 123 pmd1->TakeAllDumpsFrom(pmd2.get()); |
| 126 | 124 |
| 127 // Make sure that pmd2 is empty but still usable after it has been emptied. | 125 // Make sure that pmd2 is empty but still usable after it has been emptied. |
| 128 ASSERT_TRUE(pmd2->allocator_dumps().empty()); | 126 ASSERT_TRUE(pmd2->allocator_dumps().empty()); |
| 129 ASSERT_TRUE(pmd2->allocator_dumps_edges().empty()); | 127 ASSERT_TRUE(pmd2->allocator_dumps_edges().empty()); |
| 130 ASSERT_TRUE(pmd2->heap_dumps().empty()); | 128 ASSERT_TRUE(pmd2->heap_dumps().empty()); |
| 131 pmd2->CreateAllocatorDump("pmd2/this_mad_stays_with_pmd2"); | 129 pmd2->CreateAllocatorDump("pmd2/this_mad_stays_with_pmd2"); |
| 132 ASSERT_EQ(1u, pmd2->allocator_dumps().size()); | 130 ASSERT_EQ(1u, pmd2->allocator_dumps().size()); |
| 133 ASSERT_EQ(1u, pmd2->allocator_dumps().count("pmd2/this_mad_stays_with_pmd2")); | 131 ASSERT_EQ(1u, pmd2->allocator_dumps().count("pmd2/this_mad_stays_with_pmd2")); |
| 134 pmd2->AddOwnershipEdge(MemoryAllocatorDumpGuid(42), | 132 pmd2->AddOwnershipEdge(MemoryAllocatorDumpGuid(42), |
| 135 MemoryAllocatorDumpGuid(4242)); | 133 MemoryAllocatorDumpGuid(4242)); |
| 136 | 134 |
| 137 // Check that calling AsValueInto() doesn't cause a crash. | 135 // Check that calling AsValueInto() doesn't cause a crash. |
| 138 pmd2->AsValueInto(traced_value.get()); | 136 pmd2->AsValueInto(traced_value.get()); |
| 139 | 137 |
| 140 // Free the |pmd2| to check that the memory ownership of the two MAD(s) | 138 // Free the |pmd2| to check that the memory ownership of the two MAD(s) |
| 141 // has been transferred to |pmd1|. | 139 // has been transferred to |pmd1|. |
| 142 pmd2.reset(); | 140 pmd2.reset(); |
| 143 | 141 |
| 144 // Now check that |pmd1| has been effectively merged. | 142 // Now check that |pmd1| has been effectively merged. |
| 145 ASSERT_EQ(6u, pmd1->allocator_dumps().size()); | 143 // Note that DumpHeapUsage() adds an implicit dump for AllocationRegister's |
| 144 // memory overhead. |
| 145 ASSERT_EQ(10u, 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().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); |
| (...skipping 141 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 |