| 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/malloc_dump_provider.h" | 5 #include "base/trace_event/malloc_dump_provider.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <unordered_map> | 9 #include <unordered_map> |
| 10 | 10 |
| 11 #include "base/allocator/allocator_extension.h" | 11 #include "base/allocator/allocator_extension.h" |
| 12 #include "base/allocator/allocator_shim.h" | 12 #include "base/allocator/allocator_shim.h" |
| 13 #include "base/allocator/features.h" | 13 #include "base/allocator/features.h" |
| 14 #include "base/bind.h" |
| 14 #include "base/debug/profiler.h" | 15 #include "base/debug/profiler.h" |
| 15 #include "base/trace_event/heap_profiler_allocation_context.h" | 16 #include "base/trace_event/heap_profiler_allocation_context.h" |
| 16 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" | 17 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" |
| 17 #include "base/trace_event/heap_profiler_heap_dump_writer.h" | 18 #include "base/trace_event/heap_profiler_allocation_register.h" |
| 19 #include "base/trace_event/heap_profiler_event_writer.h" |
| 18 #include "base/trace_event/process_memory_dump.h" | 20 #include "base/trace_event/process_memory_dump.h" |
| 19 #include "base/trace_event/trace_event_argument.h" | 21 #include "base/trace_event/trace_event_argument.h" |
| 20 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 21 | 23 |
| 22 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
| 23 #include <malloc/malloc.h> | 25 #include <malloc/malloc.h> |
| 24 #else | 26 #else |
| 25 #include <malloc.h> | 27 #include <malloc.h> |
| 26 #endif | 28 #endif |
| 27 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 // The dumps of the heap profiler should be created only when heap profiling | 293 // The dumps of the heap profiler should be created only when heap profiling |
| 292 // was enabled (--enable-heap-profiling) AND a DETAILED dump is requested. | 294 // was enabled (--enable-heap-profiling) AND a DETAILED dump is requested. |
| 293 // However, when enabled, the overhead of the heap profiler should be always | 295 // However, when enabled, the overhead of the heap profiler should be always |
| 294 // reported to avoid oscillations of the malloc total in LIGHT dumps. | 296 // reported to avoid oscillations of the malloc total in LIGHT dumps. |
| 295 | 297 |
| 296 tid_dumping_heap_ = PlatformThread::CurrentId(); | 298 tid_dumping_heap_ = PlatformThread::CurrentId(); |
| 297 // At this point the Insert/RemoveAllocation hooks will ignore this thread. | 299 // At this point the Insert/RemoveAllocation hooks will ignore this thread. |
| 298 // Enclosing all the temporary data structures in a scope, so that the heap | 300 // Enclosing all the temporary data structures in a scope, so that the heap |
| 299 // profiler does not see unbalanced malloc/free calls from these containers. | 301 // profiler does not see unbalanced malloc/free calls from these containers. |
| 300 { | 302 { |
| 301 TraceEventMemoryOverhead overhead; | |
| 302 std::unordered_map<AllocationContext, AllocationMetrics> metrics_by_context; | |
| 303 if (args.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { | 303 if (args.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { |
| 304 ShardedAllocationRegister::OutputMetrics shim_metrics = | 304 struct ShimMetrics { |
| 305 allocation_register_.UpdateAndReturnsMetrics(metrics_by_context); | 305 size_t size; |
| 306 size_t count; |
| 307 }; |
| 308 ShimMetrics shim_metrics = {0}; |
| 309 auto visit_allocation = [](ShimMetrics* metrics, |
| 310 const AllocationRegister::Allocation& alloc) { |
| 311 metrics->size += alloc.size; |
| 312 metrics->count += 1; |
| 313 }; |
| 314 allocation_register_.VisitAllocations(base::BindRepeating( |
| 315 visit_allocation, base::Unretained(&shim_metrics))); |
| 306 | 316 |
| 307 // Aggregate data for objects allocated through the shim. | 317 // Aggregate data for objects allocated through the shim. |
| 308 inner_dump->AddScalar("shim_allocated_objects_size", | 318 inner_dump->AddScalar("shim_allocated_objects_size", |
| 309 MemoryAllocatorDump::kUnitsBytes, | 319 MemoryAllocatorDump::kUnitsBytes, |
| 310 shim_metrics.size); | 320 shim_metrics.size); |
| 311 inner_dump->AddScalar("shim_allocator_object_count", | 321 inner_dump->AddScalar("shim_allocator_object_count", |
| 312 MemoryAllocatorDump::kUnitsObjects, | 322 MemoryAllocatorDump::kUnitsObjects, |
| 313 shim_metrics.count); | 323 shim_metrics.count); |
| 314 } | 324 } |
| 315 allocation_register_.EstimateTraceMemoryOverhead(&overhead); | |
| 316 | 325 |
| 317 pmd->DumpHeapUsage(metrics_by_context, overhead, "malloc"); | 326 pmd->DumpHeapUsage(allocation_register_, "malloc"); |
| 318 } | 327 } |
| 319 tid_dumping_heap_ = kInvalidThreadId; | 328 tid_dumping_heap_ = kInvalidThreadId; |
| 320 | 329 |
| 321 return true; | 330 return true; |
| 322 } | 331 } |
| 323 | 332 |
| 324 void MallocDumpProvider::OnHeapProfilingEnabled(bool enabled) { | 333 void MallocDumpProvider::OnHeapProfilingEnabled(bool enabled) { |
| 325 #if BUILDFLAG(USE_ALLOCATOR_SHIM) | 334 #if BUILDFLAG(USE_ALLOCATOR_SHIM) |
| 326 if (enabled) { | 335 if (enabled) { |
| 327 allocation_register_.SetEnabled(); | 336 allocation_register_.SetEnabled(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 if (tid_dumping_heap_ != kInvalidThreadId && | 372 if (tid_dumping_heap_ != kInvalidThreadId && |
| 364 tid_dumping_heap_ == PlatformThread::CurrentId()) | 373 tid_dumping_heap_ == PlatformThread::CurrentId()) |
| 365 return; | 374 return; |
| 366 if (!allocation_register_.is_enabled()) | 375 if (!allocation_register_.is_enabled()) |
| 367 return; | 376 return; |
| 368 allocation_register_.Remove(address); | 377 allocation_register_.Remove(address); |
| 369 } | 378 } |
| 370 | 379 |
| 371 } // namespace trace_event | 380 } // namespace trace_event |
| 372 } // namespace base | 381 } // namespace base |
| OLD | NEW |