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