| 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 "base/allocator/allocator_extension.h" | 9 #include "base/allocator/allocator_extension.h" |
| 10 #include "base/allocator/allocator_shim.h" | 10 #include "base/allocator/allocator_shim.h" |
| 11 #include "base/allocator/features.h" | 11 #include "base/allocator/features.h" |
| 12 #include "base/debug/profiler.h" | 12 #include "base/debug/profiler.h" |
| 13 #include "base/memory/malloc_statistics.h" |
| 13 #include "base/trace_event/heap_profiler_allocation_context.h" | 14 #include "base/trace_event/heap_profiler_allocation_context.h" |
| 14 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" | 15 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" |
| 15 #include "base/trace_event/heap_profiler_allocation_register.h" | 16 #include "base/trace_event/heap_profiler_allocation_register.h" |
| 16 #include "base/trace_event/heap_profiler_heap_dump_writer.h" | 17 #include "base/trace_event/heap_profiler_heap_dump_writer.h" |
| 17 #include "base/trace_event/process_memory_dump.h" | 18 #include "base/trace_event/process_memory_dump.h" |
| 18 #include "base/trace_event/trace_event_argument.h" | 19 #include "base/trace_event/trace_event_argument.h" |
| 19 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 20 | 21 |
| 21 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
| 22 #include <malloc/malloc.h> | 23 #include <malloc/malloc.h> |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 187 |
| 187 MallocDumpProvider::MallocDumpProvider() | 188 MallocDumpProvider::MallocDumpProvider() |
| 188 : heap_profiler_enabled_(false), tid_dumping_heap_(kInvalidThreadId) {} | 189 : heap_profiler_enabled_(false), tid_dumping_heap_(kInvalidThreadId) {} |
| 189 | 190 |
| 190 MallocDumpProvider::~MallocDumpProvider() {} | 191 MallocDumpProvider::~MallocDumpProvider() {} |
| 191 | 192 |
| 192 // Called at trace dump point time. Creates a snapshot the memory counters for | 193 // Called at trace dump point time. Creates a snapshot the memory counters for |
| 193 // the current process. | 194 // the current process. |
| 194 bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, | 195 bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, |
| 195 ProcessMemoryDump* pmd) { | 196 ProcessMemoryDump* pmd) { |
| 196 size_t total_virtual_size = 0; | 197 base::memory::MallocStatistics stats = |
| 197 size_t resident_size = 0; | 198 base::memory::MallocStatistics::GetStatistics(); |
| 198 size_t allocated_objects_size = 0; | |
| 199 size_t allocated_objects_count = 0; | |
| 200 #if defined(USE_TCMALLOC) | |
| 201 bool res = | |
| 202 allocator::GetNumericProperty("generic.heap_size", &total_virtual_size); | |
| 203 DCHECK(res); | |
| 204 res = allocator::GetNumericProperty("generic.total_physical_bytes", | |
| 205 &resident_size); | |
| 206 DCHECK(res); | |
| 207 res = allocator::GetNumericProperty("generic.current_allocated_bytes", | |
| 208 &allocated_objects_size); | |
| 209 DCHECK(res); | |
| 210 #elif defined(OS_MACOSX) || defined(OS_IOS) | |
| 211 malloc_statistics_t stats = {0}; | |
| 212 malloc_zone_statistics(nullptr, &stats); | |
| 213 total_virtual_size = stats.size_allocated; | |
| 214 allocated_objects_size = stats.size_in_use; | |
| 215 | |
| 216 // The resident size is approximated to the max size in use, which would count | |
| 217 // the total size of all regions other than the free bytes at the end of each | |
| 218 // region. In each allocation region the allocations are rounded off to a | |
| 219 // fixed quantum, so the excess region will not be resident. | |
| 220 // See crrev.com/1531463004 for detailed explanation. | |
| 221 resident_size = stats.max_size_in_use; | |
| 222 #elif defined(OS_WIN) | |
| 223 WinHeapInfo all_heap_info = {}; | |
| 224 WinHeapMemoryDumpImpl(&all_heap_info); | |
| 225 total_virtual_size = | |
| 226 all_heap_info.committed_size + all_heap_info.uncommitted_size; | |
| 227 // Resident size is approximated with committed heap size. Note that it is | |
| 228 // possible to do this with better accuracy on windows by intersecting the | |
| 229 // working set with the virtual memory ranges occuipied by the heap. It's not | |
| 230 // clear that this is worth it, as it's fairly expensive to do. | |
| 231 resident_size = all_heap_info.committed_size; | |
| 232 allocated_objects_size = all_heap_info.allocated_size; | |
| 233 allocated_objects_count = all_heap_info.block_count; | |
| 234 #else | |
| 235 struct mallinfo info = mallinfo(); | |
| 236 DCHECK_GE(info.arena + info.hblkhd, info.uordblks); | |
| 237 | |
| 238 // In case of Android's jemalloc |arena| is 0 and the outer pages size is | |
| 239 // reported by |hblkhd|. In case of dlmalloc the total is given by | |
| 240 // |arena| + |hblkhd|. For more details see link: http://goo.gl/fMR8lF. | |
| 241 total_virtual_size = info.arena + info.hblkhd; | |
| 242 resident_size = info.uordblks; | |
| 243 | |
| 244 // Total allocated space is given by |uordblks|. | |
| 245 allocated_objects_size = info.uordblks; | |
| 246 #endif | |
| 247 | 199 |
| 248 MemoryAllocatorDump* outer_dump = pmd->CreateAllocatorDump("malloc"); | 200 MemoryAllocatorDump* outer_dump = pmd->CreateAllocatorDump("malloc"); |
| 249 outer_dump->AddScalar("virtual_size", MemoryAllocatorDump::kUnitsBytes, | 201 outer_dump->AddScalar("virtual_size", MemoryAllocatorDump::kUnitsBytes, |
| 250 total_virtual_size); | 202 stats.total_virtual_size); |
| 251 outer_dump->AddScalar(MemoryAllocatorDump::kNameSize, | 203 outer_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
| 252 MemoryAllocatorDump::kUnitsBytes, resident_size); | 204 MemoryAllocatorDump::kUnitsBytes, stats.resident_size); |
| 253 | 205 |
| 254 MemoryAllocatorDump* inner_dump = pmd->CreateAllocatorDump(kAllocatedObjects); | 206 MemoryAllocatorDump* inner_dump = pmd->CreateAllocatorDump(kAllocatedObjects); |
| 255 inner_dump->AddScalar(MemoryAllocatorDump::kNameSize, | 207 inner_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
| 256 MemoryAllocatorDump::kUnitsBytes, | 208 MemoryAllocatorDump::kUnitsBytes, |
| 257 allocated_objects_size); | 209 stats.allocated_objects_size); |
| 258 if (allocated_objects_count != 0) { | 210 if (stats.allocated_objects_count != 0) { |
| 259 inner_dump->AddScalar(MemoryAllocatorDump::kNameObjectCount, | 211 inner_dump->AddScalar(MemoryAllocatorDump::kNameObjectCount, |
| 260 MemoryAllocatorDump::kUnitsObjects, | 212 MemoryAllocatorDump::kUnitsObjects, |
| 261 allocated_objects_count); | 213 stats.allocated_objects_count); |
| 262 } | 214 } |
| 263 | 215 |
| 264 if (resident_size > allocated_objects_size) { | 216 if (stats.metadata_fragmentation_caches > 0) { |
| 265 // Explicitly specify why is extra memory resident. In tcmalloc it accounts | 217 // Explicitly specify why is extra memory resident. In tcmalloc it accounts |
| 266 // for free lists and caches. In mac and ios it accounts for the | 218 // for free lists and caches. In mac and ios it accounts for the |
| 267 // fragmentation and metadata. | 219 // fragmentation and metadata. |
| 268 MemoryAllocatorDump* other_dump = | 220 MemoryAllocatorDump* other_dump = |
| 269 pmd->CreateAllocatorDump("malloc/metadata_fragmentation_caches"); | 221 pmd->CreateAllocatorDump("malloc/metadata_fragmentation_caches"); |
| 270 other_dump->AddScalar(MemoryAllocatorDump::kNameSize, | 222 other_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
| 271 MemoryAllocatorDump::kUnitsBytes, | 223 MemoryAllocatorDump::kUnitsBytes, |
| 272 resident_size - allocated_objects_size); | 224 stats.metadata_fragmentation_caches); |
| 273 } | 225 } |
| 274 | 226 |
| 275 // Heap profiler dumps. | 227 // Heap profiler dumps. |
| 276 if (!heap_profiler_enabled_) | 228 if (!heap_profiler_enabled_) |
| 277 return true; | 229 return true; |
| 278 | 230 |
| 279 // The dumps of the heap profiler should be created only when heap profiling | 231 // The dumps of the heap profiler should be created only when heap profiling |
| 280 // was enabled (--enable-heap-profiling) AND a DETAILED dump is requested. | 232 // was enabled (--enable-heap-profiling) AND a DETAILED dump is requested. |
| 281 // However, when enabled, the overhead of the heap profiler should be always | 233 // However, when enabled, the overhead of the heap profiler should be always |
| 282 // reported to avoid oscillations of the malloc total in LIGHT dumps. | 234 // reported to avoid oscillations of the malloc total in LIGHT dumps. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 tid_dumping_heap_ == PlatformThread::CurrentId()) | 309 tid_dumping_heap_ == PlatformThread::CurrentId()) |
| 358 return; | 310 return; |
| 359 AutoLock lock(allocation_register_lock_); | 311 AutoLock lock(allocation_register_lock_); |
| 360 if (!allocation_register_) | 312 if (!allocation_register_) |
| 361 return; | 313 return; |
| 362 allocation_register_->Remove(address); | 314 allocation_register_->Remove(address); |
| 363 } | 315 } |
| 364 | 316 |
| 365 } // namespace trace_event | 317 } // namespace trace_event |
| 366 } // namespace base | 318 } // namespace base |
| OLD | NEW |