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