| 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 "platform/PartitionAllocMemoryDumpProvider.h" | 5 #include "platform/PartitionAllocMemoryDumpProvider.h" |
| 6 | 6 |
| 7 #include "base/trace_event/heap_profiler_allocation_context.h" |
| 8 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" |
| 9 #include "base/trace_event/heap_profiler_allocation_register.h" |
| 10 #include "base/trace_event/heap_profiler_heap_dump_writer.h" |
| 11 #include "base/trace_event/process_memory_dump.h" |
| 12 #include "base/trace_event/trace_event_argument.h" |
| 13 #include "base/trace_event/trace_event_memory_overhead.h" |
| 7 #include "public/platform/WebMemoryAllocatorDump.h" | 14 #include "public/platform/WebMemoryAllocatorDump.h" |
| 8 #include "public/platform/WebProcessMemoryDump.h" | 15 #include "public/platform/WebProcessMemoryDump.h" |
| 9 #include "wtf/Partitions.h" | 16 #include "wtf/Partitions.h" |
| 10 #include "wtf/text/WTFString.h" | 17 #include "wtf/text/WTFString.h" |
| 11 | 18 |
| 12 namespace blink { | 19 namespace blink { |
| 13 | 20 |
| 14 namespace { | 21 namespace { |
| 15 | 22 |
| 16 using namespace WTF; | 23 using namespace WTF; |
| 17 | 24 |
| 25 void reportAllocation(void* address, size_t size, const char* typeName) |
| 26 { |
| 27 PartitionAllocMemoryDumpProvider::instance()->insert(address, size, typeName
); |
| 28 } |
| 29 |
| 30 void reportFree(void* address) |
| 31 { |
| 32 PartitionAllocMemoryDumpProvider::instance()->remove(address); |
| 33 } |
| 34 |
| 18 const char kPartitionAllocDumpName[] = "partition_alloc"; | 35 const char kPartitionAllocDumpName[] = "partition_alloc"; |
| 19 const char kPartitionsDumpName[] = "partitions"; | 36 const char kPartitionsDumpName[] = "partitions"; |
| 20 | 37 |
| 21 String getPartitionDumpName(const char* partitionName) | 38 String getPartitionDumpName(const char* partitionName) |
| 22 { | 39 { |
| 23 return String::format("%s/%s/%s", kPartitionAllocDumpName, kPartitionsDumpNa
me, partitionName); | 40 return String::format("%s/%s/%s", kPartitionAllocDumpName, kPartitionsDumpNa
me, partitionName); |
| 24 } | 41 } |
| 25 | 42 |
| 26 // This class is used to invert the dependency of PartitionAlloc on the | 43 // This class is used to invert the dependency of PartitionAlloc on the |
| 27 // PartitionAllocMemoryDumpProvider. This implements an interface that will | 44 // PartitionAllocMemoryDumpProvider. This implements an interface that will |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 } // namespace | 104 } // namespace |
| 88 | 105 |
| 89 PartitionAllocMemoryDumpProvider* PartitionAllocMemoryDumpProvider::instance() | 106 PartitionAllocMemoryDumpProvider* PartitionAllocMemoryDumpProvider::instance() |
| 90 { | 107 { |
| 91 DEFINE_STATIC_LOCAL(PartitionAllocMemoryDumpProvider, instance, ()); | 108 DEFINE_STATIC_LOCAL(PartitionAllocMemoryDumpProvider, instance, ()); |
| 92 return &instance; | 109 return &instance; |
| 93 } | 110 } |
| 94 | 111 |
| 95 bool PartitionAllocMemoryDumpProvider::onMemoryDump(WebMemoryDumpLevelOfDetail l
evelOfDetail, WebProcessMemoryDump* memoryDump) | 112 bool PartitionAllocMemoryDumpProvider::onMemoryDump(WebMemoryDumpLevelOfDetail l
evelOfDetail, WebProcessMemoryDump* memoryDump) |
| 96 { | 113 { |
| 114 if (levelOfDetail == WebMemoryDumpLevelOfDetail::Detailed && m_isHeapProfili
ngEnabled) { |
| 115 base::trace_event::ProcessMemoryDump* pmd = memoryDump->getProcessMemory
Dump(); |
| 116 base::trace_event::TraceEventMemoryOverhead overhead; |
| 117 base::hash_map<base::trace_event::AllocationContext, size_t> bytesByCont
ext; |
| 118 { |
| 119 MutexLocker locker(m_allocationRegisterMutex); |
| 120 for (const auto& allocSize : *m_allocationRegister) |
| 121 bytesByContext[allocSize.context] += allocSize.size; |
| 122 |
| 123 m_allocationRegister->EstimateTraceMemoryOverhead(&overhead); |
| 124 } |
| 125 |
| 126 scoped_refptr<base::trace_event::TracedValue> heapDump = ExportHeapDump(
bytesByContext, pmd->session_state()->stack_frame_deduplicator(), pmd->session_s
tate()->type_name_deduplicator()); |
| 127 pmd->AddHeapDump("partition_alloc", heapDump); |
| 128 overhead.DumpInto("tracing/heap_profiler", pmd); |
| 129 } |
| 130 |
| 97 PartitionStatsDumperImpl partitionStatsDumper(memoryDump, levelOfDetail); | 131 PartitionStatsDumperImpl partitionStatsDumper(memoryDump, levelOfDetail); |
| 98 | 132 |
| 99 WebMemoryAllocatorDump* partitionsDump = memoryDump->createMemoryAllocatorDu
mp( | 133 WebMemoryAllocatorDump* partitionsDump = memoryDump->createMemoryAllocatorDu
mp( |
| 100 String::format("%s/%s", kPartitionAllocDumpName, kPartitionsDumpName)); | 134 String::format("%s/%s", kPartitionAllocDumpName, kPartitionsDumpName)); |
| 101 | 135 |
| 102 // This method calls memoryStats.partitionsDumpBucketStats with memory stati
stics. | 136 // This method calls memoryStats.partitionsDumpBucketStats with memory stati
stics. |
| 103 WTF::Partitions::dumpMemoryStats(levelOfDetail == WebMemoryDumpLevelOfDetail
::Light, &partitionStatsDumper); | 137 WTF::Partitions::dumpMemoryStats(levelOfDetail == WebMemoryDumpLevelOfDetail
::Light, &partitionStatsDumper); |
| 104 | 138 |
| 105 WebMemoryAllocatorDump* allocatedObjectsDump = memoryDump->createMemoryAlloc
atorDump(String(Partitions::kAllocatedObjectPoolName)); | 139 WebMemoryAllocatorDump* allocatedObjectsDump = memoryDump->createMemoryAlloc
atorDump(String(Partitions::kAllocatedObjectPoolName)); |
| 106 allocatedObjectsDump->addScalar("size", "bytes", partitionStatsDumper.totalA
ctiveBytes()); | 140 allocatedObjectsDump->addScalar("size", "bytes", partitionStatsDumper.totalA
ctiveBytes()); |
| 107 memoryDump->addOwnershipEdge(allocatedObjectsDump->guid(), partitionsDump->g
uid()); | 141 memoryDump->addOwnershipEdge(allocatedObjectsDump->guid(), partitionsDump->g
uid()); |
| 108 | 142 |
| 109 return true; | 143 return true; |
| 110 } | 144 } |
| 111 | 145 |
| 112 PartitionAllocMemoryDumpProvider::PartitionAllocMemoryDumpProvider() | 146 PartitionAllocMemoryDumpProvider::PartitionAllocMemoryDumpProvider() |
| 147 : m_allocationRegister(adoptPtr(new base::trace_event::AllocationRegister())
) |
| 148 , m_isHeapProfilingEnabled(false) |
| 113 { | 149 { |
| 114 } | 150 } |
| 115 | 151 |
| 116 PartitionAllocMemoryDumpProvider::~PartitionAllocMemoryDumpProvider() | 152 PartitionAllocMemoryDumpProvider::~PartitionAllocMemoryDumpProvider() |
| 117 { | 153 { |
| 118 } | 154 } |
| 119 | 155 |
| 120 void PartitionAllocMemoryDumpProvider::onHeapProfilingEnabled(AllocationHook* al
locationHook, FreeHook* freeHook) | 156 void PartitionAllocMemoryDumpProvider::onHeapProfilingEnabled(bool enabled) |
| 121 { | 157 { |
| 122 // Make PartitionAlloc call |allocationHook| and |freeHook| for every | 158 if (enabled) { |
| 123 // subsequent allocation and free (or not if the pointers are null). | 159 PartitionAllocHooks::setAllocationHook(reportAllocation); |
| 124 PartitionAllocHooks::setAllocationHook(allocationHook); | 160 PartitionAllocHooks::setFreeHook(reportFree); |
| 125 PartitionAllocHooks::setFreeHook(freeHook); | 161 } else { |
| 162 PartitionAllocHooks::setAllocationHook(nullptr); |
| 163 PartitionAllocHooks::setFreeHook(nullptr); |
| 164 } |
| 165 m_isHeapProfilingEnabled = enabled; |
| 166 } |
| 167 |
| 168 void PartitionAllocMemoryDumpProvider::insert(void* address, size_t size, const
char* typeName) |
| 169 { |
| 170 base::trace_event::AllocationContext context = base::trace_event::Allocation
ContextTracker::GetContextSnapshot(); |
| 171 context.type_name = typeName; |
| 172 MutexLocker locker(m_allocationRegisterMutex); |
| 173 m_allocationRegister->Insert(address, size, context); |
| 174 } |
| 175 |
| 176 void PartitionAllocMemoryDumpProvider::remove(void* address) |
| 177 { |
| 178 MutexLocker locker(m_allocationRegisterMutex); |
| 179 m_allocationRegister->Remove(address); |
| 126 } | 180 } |
| 127 | 181 |
| 128 } // namespace blink | 182 } // namespace blink |
| OLD | NEW |