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/debug/profiler.h" | 14 #include "base/debug/profiler.h" |
15 #include "base/trace_event/heap_profiler_allocation_context.h" | 15 #include "base/trace_event/heap_profiler_allocation_context.h" |
16 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" | 16 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" |
17 #include "base/trace_event/heap_profiler_allocation_register.h" | |
18 #include "base/trace_event/heap_profiler_heap_dump_writer.h" | 17 #include "base/trace_event/heap_profiler_heap_dump_writer.h" |
19 #include "base/trace_event/process_memory_dump.h" | 18 #include "base/trace_event/process_memory_dump.h" |
20 #include "base/trace_event/trace_event_argument.h" | 19 #include "base/trace_event/trace_event_argument.h" |
21 #include "build/build_config.h" | 20 #include "build/build_config.h" |
22 | 21 |
23 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
24 #include <malloc/malloc.h> | 23 #include <malloc/malloc.h> |
25 #else | 24 #else |
26 #include <malloc.h> | 25 #include <malloc.h> |
27 #endif | 26 #endif |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 // static | 182 // static |
184 const char MallocDumpProvider::kAllocatedObjects[] = "malloc/allocated_objects"; | 183 const char MallocDumpProvider::kAllocatedObjects[] = "malloc/allocated_objects"; |
185 | 184 |
186 // static | 185 // static |
187 MallocDumpProvider* MallocDumpProvider::GetInstance() { | 186 MallocDumpProvider* MallocDumpProvider::GetInstance() { |
188 return Singleton<MallocDumpProvider, | 187 return Singleton<MallocDumpProvider, |
189 LeakySingletonTraits<MallocDumpProvider>>::get(); | 188 LeakySingletonTraits<MallocDumpProvider>>::get(); |
190 } | 189 } |
191 | 190 |
192 MallocDumpProvider::MallocDumpProvider() | 191 MallocDumpProvider::MallocDumpProvider() |
193 : heap_profiler_enabled_(false), tid_dumping_heap_(kInvalidThreadId) {} | 192 : tid_dumping_heap_(kInvalidThreadId) {} |
194 | 193 |
195 MallocDumpProvider::~MallocDumpProvider() {} | 194 MallocDumpProvider::~MallocDumpProvider() {} |
196 | 195 |
197 // Called at trace dump point time. Creates a snapshot the memory counters for | 196 // Called at trace dump point time. Creates a snapshot the memory counters for |
198 // the current process. | 197 // the current process. |
199 bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, | 198 bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, |
200 ProcessMemoryDump* pmd) { | 199 ProcessMemoryDump* pmd) { |
201 size_t total_virtual_size = 0; | 200 size_t total_virtual_size = 0; |
202 size_t resident_size = 0; | 201 size_t resident_size = 0; |
203 size_t allocated_objects_size = 0; | 202 size_t allocated_objects_size = 0; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 // for free lists and caches. In mac and ios it accounts for the | 278 // for free lists and caches. In mac and ios it accounts for the |
280 // fragmentation and metadata. | 279 // fragmentation and metadata. |
281 MemoryAllocatorDump* other_dump = | 280 MemoryAllocatorDump* other_dump = |
282 pmd->CreateAllocatorDump("malloc/metadata_fragmentation_caches"); | 281 pmd->CreateAllocatorDump("malloc/metadata_fragmentation_caches"); |
283 other_dump->AddScalar(MemoryAllocatorDump::kNameSize, | 282 other_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
284 MemoryAllocatorDump::kUnitsBytes, | 283 MemoryAllocatorDump::kUnitsBytes, |
285 resident_size - allocated_objects_size); | 284 resident_size - allocated_objects_size); |
286 } | 285 } |
287 | 286 |
288 // Heap profiler dumps. | 287 // Heap profiler dumps. |
289 if (!heap_profiler_enabled_) | 288 if (!allocation_register_.is_enabled()) |
290 return true; | 289 return true; |
291 | 290 |
292 // The dumps of the heap profiler should be created only when heap profiling | 291 // The dumps of the heap profiler should be created only when heap profiling |
293 // was enabled (--enable-heap-profiling) AND a DETAILED dump is requested. | 292 // was enabled (--enable-heap-profiling) AND a DETAILED dump is requested. |
294 // However, when enabled, the overhead of the heap profiler should be always | 293 // However, when enabled, the overhead of the heap profiler should be always |
295 // reported to avoid oscillations of the malloc total in LIGHT dumps. | 294 // reported to avoid oscillations of the malloc total in LIGHT dumps. |
296 | 295 |
297 tid_dumping_heap_ = PlatformThread::CurrentId(); | 296 tid_dumping_heap_ = PlatformThread::CurrentId(); |
298 // At this point the Insert/RemoveAllocation hooks will ignore this thread. | 297 // At this point the Insert/RemoveAllocation hooks will ignore this thread. |
299 // Enclosing all the temporary data structures in a scope, so that the heap | 298 // Enclosing all the temporary data structures in a scope, so that the heap |
300 // profiler does not see unbalanced malloc/free calls from these containers. | 299 // profiler does not see unbalanced malloc/free calls from these containers. |
301 { | 300 { |
302 size_t shim_allocated_objects_size = 0; | 301 size_t shim_allocated_objects_size = 0; |
303 size_t shim_allocated_objects_count = 0; | 302 size_t shim_allocated_objects_count = 0; |
304 TraceEventMemoryOverhead overhead; | 303 TraceEventMemoryOverhead overhead; |
305 std::unordered_map<AllocationContext, AllocationMetrics> metrics_by_context; | 304 std::unordered_map<AllocationContext, AllocationMetrics> metrics_by_context; |
306 { | 305 if (args.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { |
307 AutoLock lock(allocation_register_lock_); | 306 ShardedAllocationRegister::OutputMetrics metrics = |
308 if (allocation_register_) { | 307 allocation_register_.UpdateAndReturnsMetrics(metrics_by_context); |
309 if (args.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { | |
310 for (const auto& alloc_size : *allocation_register_) { | |
311 AllocationMetrics& metrics = metrics_by_context[alloc_size.context]; | |
312 metrics.size += alloc_size.size; | |
313 metrics.count++; | |
314 | 308 |
315 // Aggregate data for objects allocated through the shim. | 309 // Aggregate data for objects allocated through the shim. |
316 shim_allocated_objects_size += alloc_size.size; | 310 shim_allocated_objects_size += metrics.size; |
317 shim_allocated_objects_count++; | 311 shim_allocated_objects_count += metrics.count; |
318 } | 312 } |
319 } | 313 allocation_register_.EstimateTraceMemoryOverhead(&overhead); |
320 allocation_register_->EstimateTraceMemoryOverhead(&overhead); | |
321 } | |
322 | 314 |
323 inner_dump->AddScalar("shim_allocated_objects_size", | 315 inner_dump->AddScalar("shim_allocated_objects_size", |
324 MemoryAllocatorDump::kUnitsBytes, | 316 MemoryAllocatorDump::kUnitsBytes, |
325 shim_allocated_objects_size); | 317 shim_allocated_objects_size); |
326 inner_dump->AddScalar("shim_allocator_object_count", | 318 inner_dump->AddScalar("shim_allocator_object_count", |
327 MemoryAllocatorDump::kUnitsObjects, | 319 MemoryAllocatorDump::kUnitsObjects, |
328 shim_allocated_objects_count); | 320 shim_allocated_objects_count); |
329 } // lock(allocation_register_lock_) | |
330 pmd->DumpHeapUsage(metrics_by_context, overhead, "malloc"); | 321 pmd->DumpHeapUsage(metrics_by_context, overhead, "malloc"); |
331 } | 322 } |
332 tid_dumping_heap_ = kInvalidThreadId; | 323 tid_dumping_heap_ = kInvalidThreadId; |
333 | 324 |
334 return true; | 325 return true; |
335 } | 326 } |
336 | 327 |
337 void MallocDumpProvider::OnHeapProfilingEnabled(bool enabled) { | 328 void MallocDumpProvider::OnHeapProfilingEnabled(bool enabled) { |
338 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | 329 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
339 if (enabled) { | 330 if (enabled) { |
340 { | 331 allocation_register_.SetEnabled(); |
341 AutoLock lock(allocation_register_lock_); | |
342 allocation_register_.reset(new AllocationRegister()); | |
343 } | |
344 allocator::InsertAllocatorDispatch(&g_allocator_hooks); | 332 allocator::InsertAllocatorDispatch(&g_allocator_hooks); |
345 } else { | 333 } else { |
346 AutoLock lock(allocation_register_lock_); | 334 allocation_register_.SetDisabled(); |
347 allocation_register_.reset(); | |
348 // Insert/RemoveAllocation below will no-op if the register is torn down. | |
349 // Once disabled, heap profiling will not re-enabled anymore for the | |
350 // lifetime of the process. | |
351 } | 335 } |
352 #endif | 336 #endif |
353 heap_profiler_enabled_ = enabled; | |
354 } | 337 } |
355 | 338 |
356 void MallocDumpProvider::InsertAllocation(void* address, size_t size) { | 339 void MallocDumpProvider::InsertAllocation(void* address, size_t size) { |
357 // CurrentId() can be a slow operation (crbug.com/497226). This apparently | 340 // CurrentId() can be a slow operation (crbug.com/497226). This apparently |
358 // redundant condition short circuits the CurrentID() calls when unnecessary. | 341 // redundant condition short circuits the CurrentID() calls when unnecessary. |
359 if (tid_dumping_heap_ != kInvalidThreadId && | 342 if (tid_dumping_heap_ != kInvalidThreadId && |
360 tid_dumping_heap_ == PlatformThread::CurrentId()) | 343 tid_dumping_heap_ == PlatformThread::CurrentId()) |
361 return; | 344 return; |
362 | 345 |
363 // AllocationContextTracker will return nullptr when called re-reentrantly. | 346 // AllocationContextTracker will return nullptr when called re-reentrantly. |
364 // This is the case of GetInstanceForCurrentThread() being called for the | 347 // This is the case of GetInstanceForCurrentThread() being called for the |
365 // first time, which causes a new() inside the tracker which re-enters the | 348 // first time, which causes a new() inside the tracker which re-enters the |
366 // heap profiler, in which case we just want to early out. | 349 // heap profiler, in which case we just want to early out. |
367 auto* tracker = AllocationContextTracker::GetInstanceForCurrentThread(); | 350 auto* tracker = AllocationContextTracker::GetInstanceForCurrentThread(); |
368 if (!tracker) | 351 if (!tracker) |
369 return; | 352 return; |
370 | 353 |
371 AllocationContext context; | 354 AllocationContext context; |
372 if (!tracker->GetContextSnapshot(&context)) | 355 if (!tracker->GetContextSnapshot(&context)) |
373 return; | 356 return; |
374 | 357 |
375 AutoLock lock(allocation_register_lock_); | 358 if (!allocation_register_.is_enabled()) |
376 if (!allocation_register_) | |
377 return; | 359 return; |
378 | 360 |
379 allocation_register_->Insert(address, size, context); | 361 allocation_register_.Insert(address, size, context); |
380 } | 362 } |
381 | 363 |
382 void MallocDumpProvider::RemoveAllocation(void* address) { | 364 void MallocDumpProvider::RemoveAllocation(void* address) { |
383 // No re-entrancy is expected here as none of the calls below should | 365 // No re-entrancy is expected here as none of the calls below should |
384 // cause a free()-s (|allocation_register_| does its own heap management). | 366 // cause a free()-s (|allocation_register_| does its own heap management). |
385 if (tid_dumping_heap_ != kInvalidThreadId && | 367 if (tid_dumping_heap_ != kInvalidThreadId && |
386 tid_dumping_heap_ == PlatformThread::CurrentId()) | 368 tid_dumping_heap_ == PlatformThread::CurrentId()) |
387 return; | 369 return; |
388 AutoLock lock(allocation_register_lock_); | 370 if (!allocation_register_.is_enabled()) |
389 if (!allocation_register_) | |
390 return; | 371 return; |
391 allocation_register_->Remove(address); | 372 allocation_register_.Remove(address); |
392 } | 373 } |
393 | 374 |
394 } // namespace trace_event | 375 } // namespace trace_event |
395 } // namespace base | 376 } // namespace base |
OLD | NEW |