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 <unordered_map> | 7 #include <unordered_map> |
8 | 8 |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/trace_event/heap_profiler_allocation_context.h" | |
11 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" | 10 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" |
12 #include "base/trace_event/heap_profiler_allocation_register.h" | 11 #include "base/trace_event/heap_profiler_allocation_register.h" |
13 #include "base/trace_event/process_memory_dump.h" | 12 #include "base/trace_event/process_memory_dump.h" |
14 #include "base/trace_event/trace_event_memory_overhead.h" | 13 #include "base/trace_event/trace_event_memory_overhead.h" |
15 #include "platform/wtf/allocator/Partitions.h" | 14 #include "platform/wtf/allocator/Partitions.h" |
16 #include "platform/wtf/text/WTFString.h" | 15 #include "platform/wtf/text/WTFString.h" |
17 | 16 |
18 namespace blink { | 17 namespace blink { |
19 | 18 |
20 namespace { | 19 namespace { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 DEFINE_STATIC_LOCAL(PartitionAllocMemoryDumpProvider, instance, ()); | 127 DEFINE_STATIC_LOCAL(PartitionAllocMemoryDumpProvider, instance, ()); |
129 return &instance; | 128 return &instance; |
130 } | 129 } |
131 | 130 |
132 bool PartitionAllocMemoryDumpProvider::OnMemoryDump( | 131 bool PartitionAllocMemoryDumpProvider::OnMemoryDump( |
133 const base::trace_event::MemoryDumpArgs& args, | 132 const base::trace_event::MemoryDumpArgs& args, |
134 base::trace_event::ProcessMemoryDump* memory_dump) { | 133 base::trace_event::ProcessMemoryDump* memory_dump) { |
135 using base::trace_event::MemoryDumpLevelOfDetail; | 134 using base::trace_event::MemoryDumpLevelOfDetail; |
136 | 135 |
137 MemoryDumpLevelOfDetail level_of_detail = args.level_of_detail; | 136 MemoryDumpLevelOfDetail level_of_detail = args.level_of_detail; |
138 if (is_heap_profiling_enabled_) { | 137 if (allocation_register_.is_enabled()) { |
139 // Overhead should always be reported, regardless of light vs. heavy. | 138 // Overhead should always be reported, regardless of light vs. heavy. |
140 base::trace_event::TraceEventMemoryOverhead overhead; | 139 base::trace_event::TraceEventMemoryOverhead overhead; |
141 std::unordered_map<base::trace_event::AllocationContext, | 140 std::unordered_map<base::trace_event::AllocationContext, |
142 base::trace_event::AllocationMetrics> | 141 base::trace_event::AllocationMetrics> |
143 metrics_by_context; | 142 metrics_by_context; |
144 { | 143 // Dump only the overhead estimation in non-detailed dumps. |
145 MutexLocker locker(allocation_register_mutex_); | 144 if (level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { |
146 // Dump only the overhead estimation in non-detailed dumps. | 145 allocation_register_.UpdateAndReturnsMetrics(metrics_by_context); |
147 if (level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { | |
148 for (const auto& alloc_size : *allocation_register_) { | |
149 base::trace_event::AllocationMetrics& metrics = | |
150 metrics_by_context[alloc_size.context]; | |
151 metrics.size += alloc_size.size; | |
152 metrics.count++; | |
153 } | |
154 } | |
155 allocation_register_->EstimateTraceMemoryOverhead(&overhead); | |
156 } | 146 } |
| 147 allocation_register_.EstimateTraceMemoryOverhead(&overhead); |
157 memory_dump->DumpHeapUsage(metrics_by_context, overhead, "partition_alloc"); | 148 memory_dump->DumpHeapUsage(metrics_by_context, overhead, "partition_alloc"); |
158 } | 149 } |
159 | 150 |
160 PartitionStatsDumperImpl partition_stats_dumper(memory_dump, level_of_detail); | 151 PartitionStatsDumperImpl partition_stats_dumper(memory_dump, level_of_detail); |
161 | 152 |
162 base::trace_event::MemoryAllocatorDump* partitions_dump = | 153 base::trace_event::MemoryAllocatorDump* partitions_dump = |
163 memory_dump->CreateAllocatorDump(base::StringPrintf( | 154 memory_dump->CreateAllocatorDump(base::StringPrintf( |
164 "%s/%s", kPartitionAllocDumpName, kPartitionsDumpName)); | 155 "%s/%s", kPartitionAllocDumpName, kPartitionsDumpName)); |
165 | 156 |
166 // This method calls memoryStats.partitionsDumpBucketStats with memory | 157 // This method calls memoryStats.partitionsDumpBucketStats with memory |
167 // statistics. | 158 // statistics. |
168 WTF::Partitions::DumpMemoryStats( | 159 WTF::Partitions::DumpMemoryStats( |
169 level_of_detail != MemoryDumpLevelOfDetail::DETAILED, | 160 level_of_detail != MemoryDumpLevelOfDetail::DETAILED, |
170 &partition_stats_dumper); | 161 &partition_stats_dumper); |
171 | 162 |
172 base::trace_event::MemoryAllocatorDump* allocated_objects_dump = | 163 base::trace_event::MemoryAllocatorDump* allocated_objects_dump = |
173 memory_dump->CreateAllocatorDump(Partitions::kAllocatedObjectPoolName); | 164 memory_dump->CreateAllocatorDump(Partitions::kAllocatedObjectPoolName); |
174 allocated_objects_dump->AddScalar("size", "bytes", | 165 allocated_objects_dump->AddScalar("size", "bytes", |
175 partition_stats_dumper.TotalActiveBytes()); | 166 partition_stats_dumper.TotalActiveBytes()); |
176 memory_dump->AddOwnershipEdge(allocated_objects_dump->guid(), | 167 memory_dump->AddOwnershipEdge(allocated_objects_dump->guid(), |
177 partitions_dump->guid()); | 168 partitions_dump->guid()); |
178 | 169 |
179 return true; | 170 return true; |
180 } | 171 } |
181 | 172 |
182 // |m_allocationRegister| should be initialized only when necessary to avoid | 173 // |m_allocationRegister| should be initialized only when necessary to avoid |
183 // waste of memory. | 174 // waste of memory. |
184 PartitionAllocMemoryDumpProvider::PartitionAllocMemoryDumpProvider() | 175 PartitionAllocMemoryDumpProvider::PartitionAllocMemoryDumpProvider() {} |
185 : allocation_register_(nullptr), is_heap_profiling_enabled_(false) {} | |
186 | 176 |
187 PartitionAllocMemoryDumpProvider::~PartitionAllocMemoryDumpProvider() {} | 177 PartitionAllocMemoryDumpProvider::~PartitionAllocMemoryDumpProvider() {} |
188 | 178 |
189 void PartitionAllocMemoryDumpProvider::OnHeapProfilingEnabled(bool enabled) { | 179 void PartitionAllocMemoryDumpProvider::OnHeapProfilingEnabled(bool enabled) { |
190 if (enabled) { | 180 if (enabled) { |
191 { | 181 allocation_register_.SetEnabled(); |
192 MutexLocker locker(allocation_register_mutex_); | |
193 if (!allocation_register_) | |
194 allocation_register_.reset(new base::trace_event::AllocationRegister()); | |
195 } | |
196 WTF::PartitionAllocHooks::SetAllocationHook(ReportAllocation); | 182 WTF::PartitionAllocHooks::SetAllocationHook(ReportAllocation); |
197 WTF::PartitionAllocHooks::SetFreeHook(ReportFree); | 183 WTF::PartitionAllocHooks::SetFreeHook(ReportFree); |
198 } else { | 184 } else { |
199 WTF::PartitionAllocHooks::SetAllocationHook(nullptr); | 185 WTF::PartitionAllocHooks::SetAllocationHook(nullptr); |
200 WTF::PartitionAllocHooks::SetFreeHook(nullptr); | 186 WTF::PartitionAllocHooks::SetFreeHook(nullptr); |
| 187 allocation_register_.SetDisabled(); |
201 } | 188 } |
202 is_heap_profiling_enabled_ = enabled; | |
203 } | 189 } |
204 | 190 |
205 void PartitionAllocMemoryDumpProvider::insert(void* address, | 191 void PartitionAllocMemoryDumpProvider::insert(void* address, |
206 size_t size, | 192 size_t size, |
207 const char* type_name) { | 193 const char* type_name) { |
208 base::trace_event::AllocationContext context; | 194 base::trace_event::AllocationContext context; |
209 if (!base::trace_event::AllocationContextTracker:: | 195 if (!base::trace_event::AllocationContextTracker:: |
210 GetInstanceForCurrentThread() | 196 GetInstanceForCurrentThread() |
211 ->GetContextSnapshot(&context)) | 197 ->GetContextSnapshot(&context)) |
212 return; | 198 return; |
213 | 199 |
214 context.type_name = type_name; | 200 context.type_name = type_name; |
215 MutexLocker locker(allocation_register_mutex_); | 201 if (!allocation_register_.is_enabled()) |
216 if (allocation_register_) | 202 return; |
217 allocation_register_->Insert(address, size, context); | 203 allocation_register_.Insert(address, size, context); |
218 } | 204 } |
219 | 205 |
220 void PartitionAllocMemoryDumpProvider::Remove(void* address) { | 206 void PartitionAllocMemoryDumpProvider::Remove(void* address) { |
221 MutexLocker locker(allocation_register_mutex_); | 207 if (!allocation_register_.is_enabled()) |
222 if (allocation_register_) | 208 return; |
223 allocation_register_->Remove(address); | 209 allocation_register_.Remove(address); |
224 } | 210 } |
225 | 211 |
226 } // namespace blink | 212 } // namespace blink |
OLD | NEW |