ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/trace_event/malloc_dump_provider.h" |
| 6 | |
avi | bd1ed05 | 2015-12-24 04:03:44 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame^] | 9 | #include <unordered_map> |
| 10 | |
avi | bd1ed05 | 2015-12-24 04:03:44 | [diff] [blame] | 11 | #include "base/allocator/allocator_extension.h" |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 12 | #include "base/allocator/allocator_shim.h" |
| 13 | #include "base/allocator/features.h" |
siggi | ba33ec0 | 2016-08-26 16:13:07 | [diff] [blame] | 14 | #include "base/debug/profiler.h" |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 15 | #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_register.h" |
| 18 | #include "base/trace_event/heap_profiler_heap_dump_writer.h" |
avi | bd1ed05 | 2015-12-24 04:03:44 | [diff] [blame] | 19 | #include "base/trace_event/process_memory_dump.h" |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 20 | #include "base/trace_event/trace_event_argument.h" |
avi | bd1ed05 | 2015-12-24 04:03:44 | [diff] [blame] | 21 | #include "build/build_config.h" |
| 22 | |
ssid | 3aa02fe | 2015-11-07 16:15:07 | [diff] [blame] | 23 | #if defined(OS_MACOSX) |
| 24 | #include <malloc/malloc.h> |
| 25 | #else |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 26 | #include <malloc.h> |
ssid | 3aa02fe | 2015-11-07 16:15:07 | [diff] [blame] | 27 | #endif |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 28 | #if defined(OS_WIN) |
| 29 | #include <windows.h> |
| 30 | #endif |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 31 | |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 32 | namespace base { |
| 33 | namespace trace_event { |
| 34 | |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 35 | namespace { |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 36 | #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 37 | |
| 38 | using allocator::AllocatorDispatch; |
| 39 | |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 40 | void* HookAlloc(const AllocatorDispatch* self, size_t size, void* context) { |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 41 | const AllocatorDispatch* const next = self->next; |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 42 | void* ptr = next->alloc_function(next, size, context); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 43 | if (ptr) |
| 44 | MallocDumpProvider::GetInstance()->InsertAllocation(ptr, size); |
| 45 | return ptr; |
| 46 | } |
| 47 | |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 48 | void* HookZeroInitAlloc(const AllocatorDispatch* self, |
| 49 | size_t n, |
| 50 | size_t size, |
| 51 | void* context) { |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 52 | const AllocatorDispatch* const next = self->next; |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 53 | void* ptr = next->alloc_zero_initialized_function(next, n, size, context); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 54 | if (ptr) |
| 55 | MallocDumpProvider::GetInstance()->InsertAllocation(ptr, n * size); |
| 56 | return ptr; |
| 57 | } |
| 58 | |
etienneb | dc2b22eb | 2017-03-21 17:11:43 | [diff] [blame] | 59 | void* HookAllocAligned(const AllocatorDispatch* self, |
| 60 | size_t alignment, |
| 61 | size_t size, |
| 62 | void* context) { |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 63 | const AllocatorDispatch* const next = self->next; |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 64 | void* ptr = next->alloc_aligned_function(next, alignment, size, context); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 65 | if (ptr) |
| 66 | MallocDumpProvider::GetInstance()->InsertAllocation(ptr, size); |
| 67 | return ptr; |
| 68 | } |
| 69 | |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 70 | void* HookRealloc(const AllocatorDispatch* self, |
| 71 | void* address, |
| 72 | size_t size, |
| 73 | void* context) { |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 74 | const AllocatorDispatch* const next = self->next; |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 75 | void* ptr = next->realloc_function(next, address, size, context); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 76 | MallocDumpProvider::GetInstance()->RemoveAllocation(address); |
| 77 | if (size > 0) // realloc(size == 0) means free(). |
| 78 | MallocDumpProvider::GetInstance()->InsertAllocation(ptr, size); |
| 79 | return ptr; |
| 80 | } |
| 81 | |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 82 | void HookFree(const AllocatorDispatch* self, void* address, void* context) { |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 83 | if (address) |
| 84 | MallocDumpProvider::GetInstance()->RemoveAllocation(address); |
| 85 | const AllocatorDispatch* const next = self->next; |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 86 | next->free_function(next, address, context); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 87 | } |
| 88 | |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 89 | size_t HookGetSizeEstimate(const AllocatorDispatch* self, |
| 90 | void* address, |
| 91 | void* context) { |
siggi | 46e1b07 | 2016-09-09 16:43:31 | [diff] [blame] | 92 | const AllocatorDispatch* const next = self->next; |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 93 | return next->get_size_estimate_function(next, address, context); |
siggi | 46e1b07 | 2016-09-09 16:43:31 | [diff] [blame] | 94 | } |
| 95 | |
erikchen | 0d0395a | 2017-02-02 06:16:29 | [diff] [blame] | 96 | unsigned HookBatchMalloc(const AllocatorDispatch* self, |
| 97 | size_t size, |
| 98 | void** results, |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 99 | unsigned num_requested, |
| 100 | void* context) { |
erikchen | 0d0395a | 2017-02-02 06:16:29 | [diff] [blame] | 101 | const AllocatorDispatch* const next = self->next; |
| 102 | unsigned count = |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 103 | next->batch_malloc_function(next, size, results, num_requested, context); |
erikchen | 0d0395a | 2017-02-02 06:16:29 | [diff] [blame] | 104 | for (unsigned i = 0; i < count; ++i) { |
| 105 | MallocDumpProvider::GetInstance()->InsertAllocation(results[i], size); |
| 106 | } |
| 107 | return count; |
| 108 | } |
| 109 | |
| 110 | void HookBatchFree(const AllocatorDispatch* self, |
| 111 | void** to_be_freed, |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 112 | unsigned num_to_be_freed, |
| 113 | void* context) { |
erikchen | 0d0395a | 2017-02-02 06:16:29 | [diff] [blame] | 114 | const AllocatorDispatch* const next = self->next; |
| 115 | for (unsigned i = 0; i < num_to_be_freed; ++i) { |
| 116 | MallocDumpProvider::GetInstance()->RemoveAllocation(to_be_freed[i]); |
| 117 | } |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 118 | next->batch_free_function(next, to_be_freed, num_to_be_freed, context); |
erikchen | 0d0395a | 2017-02-02 06:16:29 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void HookFreeDefiniteSize(const AllocatorDispatch* self, |
| 122 | void* ptr, |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 123 | size_t size, |
| 124 | void* context) { |
erikchen | 0d0395a | 2017-02-02 06:16:29 | [diff] [blame] | 125 | if (ptr) |
| 126 | MallocDumpProvider::GetInstance()->RemoveAllocation(ptr); |
| 127 | const AllocatorDispatch* const next = self->next; |
erikchen | eff0ecb | 2017-02-20 13:04:50 | [diff] [blame] | 128 | next->free_definite_size_function(next, ptr, size, context); |
erikchen | 0d0395a | 2017-02-02 06:16:29 | [diff] [blame] | 129 | } |
| 130 | |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 131 | AllocatorDispatch g_allocator_hooks = { |
erikchen | 0d0395a | 2017-02-02 06:16:29 | [diff] [blame] | 132 | &HookAlloc, /* alloc_function */ |
| 133 | &HookZeroInitAlloc, /* alloc_zero_initialized_function */ |
etienneb | dc2b22eb | 2017-03-21 17:11:43 | [diff] [blame] | 134 | &HookAllocAligned, /* alloc_aligned_function */ |
erikchen | 0d0395a | 2017-02-02 06:16:29 | [diff] [blame] | 135 | &HookRealloc, /* realloc_function */ |
| 136 | &HookFree, /* free_function */ |
| 137 | &HookGetSizeEstimate, /* get_size_estimate_function */ |
| 138 | &HookBatchMalloc, /* batch_malloc_function */ |
| 139 | &HookBatchFree, /* batch_free_function */ |
| 140 | &HookFreeDefiniteSize, /* free_definite_size_function */ |
| 141 | nullptr, /* next */ |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 142 | }; |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 143 | #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 144 | |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 145 | #if defined(OS_WIN) |
| 146 | // A structure containing some information about a given heap. |
| 147 | struct WinHeapInfo { |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 148 | size_t committed_size; |
| 149 | size_t uncommitted_size; |
| 150 | size_t allocated_size; |
| 151 | size_t block_count; |
| 152 | }; |
| 153 | |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 154 | // NOTE: crbug.com/665516 |
| 155 | // Unfortunately, there is no safe way to collect information from secondary |
| 156 | // heaps due to limitations and racy nature of this piece of WinAPI. |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 157 | void WinHeapMemoryDumpImpl(WinHeapInfo* crt_heap_info) { |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 158 | #if defined(SYZYASAN) |
| 159 | if (base::debug::IsBinaryInstrumented()) |
| 160 | return; |
| 161 | #endif |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 162 | |
| 163 | // Iterate through whichever heap our CRT is using. |
| 164 | HANDLE crt_heap = reinterpret_cast<HANDLE>(_get_heap_handle()); |
| 165 | ::HeapLock(crt_heap); |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 166 | PROCESS_HEAP_ENTRY heap_entry; |
| 167 | heap_entry.lpData = nullptr; |
| 168 | // Walk over all the entries in the main heap. |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 169 | while (::HeapWalk(crt_heap, &heap_entry) != FALSE) { |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 170 | if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) { |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 171 | crt_heap_info->allocated_size += heap_entry.cbData; |
| 172 | crt_heap_info->block_count++; |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 173 | } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) { |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 174 | crt_heap_info->committed_size += heap_entry.Region.dwCommittedSize; |
| 175 | crt_heap_info->uncommitted_size += heap_entry.Region.dwUnCommittedSize; |
liamjm | c56e1ffa | 2016-10-15 01:04:46 | [diff] [blame] | 176 | } |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 177 | } |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 178 | CHECK(::HeapUnlock(crt_heap) == TRUE); |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 179 | } |
| 180 | #endif // defined(OS_WIN) |
| 181 | } // namespace |
| 182 | |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 183 | // static |
primiano | fadec05e | 2015-06-03 16:57:32 | [diff] [blame] | 184 | const char MallocDumpProvider::kAllocatedObjects[] = "malloc/allocated_objects"; |
| 185 | |
| 186 | // static |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 187 | MallocDumpProvider* MallocDumpProvider::GetInstance() { |
| 188 | return Singleton<MallocDumpProvider, |
| 189 | LeakySingletonTraits<MallocDumpProvider>>::get(); |
| 190 | } |
| 191 | |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 192 | MallocDumpProvider::MallocDumpProvider() |
| 193 | : heap_profiler_enabled_(false), tid_dumping_heap_(kInvalidThreadId) {} |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 194 | |
ssid | 3aa02fe | 2015-11-07 16:15:07 | [diff] [blame] | 195 | MallocDumpProvider::~MallocDumpProvider() {} |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 196 | |
| 197 | // Called at trace dump point time. Creates a snapshot the memory counters for |
| 198 | // the current process. |
ssid | 90694aeec | 2015-08-06 13:01:30 | [diff] [blame] | 199 | bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, |
| 200 | ProcessMemoryDump* pmd) { |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 201 | size_t total_virtual_size = 0; |
| 202 | size_t resident_size = 0; |
| 203 | size_t allocated_objects_size = 0; |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 204 | size_t allocated_objects_count = 0; |
ssid | 86f78c1 | 2015-12-21 11:45:32 | [diff] [blame] | 205 | #if defined(USE_TCMALLOC) |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 206 | bool res = |
| 207 | allocator::GetNumericProperty("generic.heap_size", &total_virtual_size); |
| 208 | DCHECK(res); |
| 209 | res = allocator::GetNumericProperty("generic.total_physical_bytes", |
| 210 | &resident_size); |
| 211 | DCHECK(res); |
| 212 | res = allocator::GetNumericProperty("generic.current_allocated_bytes", |
| 213 | &allocated_objects_size); |
| 214 | DCHECK(res); |
ssid | 86f78c1 | 2015-12-21 11:45:32 | [diff] [blame] | 215 | #elif defined(OS_MACOSX) || defined(OS_IOS) |
| 216 | malloc_statistics_t stats = {0}; |
| 217 | malloc_zone_statistics(nullptr, &stats); |
| 218 | total_virtual_size = stats.size_allocated; |
| 219 | allocated_objects_size = stats.size_in_use; |
| 220 | |
erikchen | 792525b | 2017-03-10 18:06:15 | [diff] [blame] | 221 | // Resident size is approximated pretty well by stats.max_size_in_use. |
| 222 | // However, on macOS, freed blocks are both resident and reusable, which is |
| 223 | // semantically equivalent to deallocated. The implementation of libmalloc |
| 224 | // will also only hold a fixed number of freed regions before actually |
| 225 | // starting to deallocate them, so stats.max_size_in_use is also not |
| 226 | // representative of the peak size. As a result, stats.max_size_in_use is |
| 227 | // typically somewhere between actually resident [non-reusable] pages, and |
| 228 | // peak size. This is not very useful, so we just use stats.size_in_use for |
| 229 | // resident_size, even though it's an underestimate and fails to account for |
| 230 | // fragmentation. See |
| 231 | // https://bugs.chromium.org/p/chromium/issues/detail?id=695263#c1. |
| 232 | resident_size = stats.size_in_use; |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 233 | #elif defined(OS_WIN) |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 234 | WinHeapInfo main_heap_info = {}; |
| 235 | WinHeapMemoryDumpImpl(&main_heap_info); |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 236 | total_virtual_size = |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 237 | main_heap_info.committed_size + main_heap_info.uncommitted_size; |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 238 | // Resident size is approximated with committed heap size. Note that it is |
| 239 | // possible to do this with better accuracy on windows by intersecting the |
| 240 | // working set with the virtual memory ranges occuipied by the heap. It's not |
| 241 | // clear that this is worth it, as it's fairly expensive to do. |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 242 | resident_size = main_heap_info.committed_size; |
| 243 | allocated_objects_size = main_heap_info.allocated_size; |
| 244 | allocated_objects_count = main_heap_info.block_count; |
ssid | 3aa02fe | 2015-11-07 16:15:07 | [diff] [blame] | 245 | #else |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 246 | struct mallinfo info = mallinfo(); |
| 247 | DCHECK_GE(info.arena + info.hblkhd, info.uordblks); |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 248 | |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 249 | // In case of Android's jemalloc |arena| is 0 and the outer pages size is |
| 250 | // reported by |hblkhd|. In case of dlmalloc the total is given by |
| 251 | // |arena| + |hblkhd|. For more details see link: http://goo.gl/fMR8lF. |
| 252 | total_virtual_size = info.arena + info.hblkhd; |
| 253 | resident_size = info.uordblks; |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 254 | |
| 255 | // Total allocated space is given by |uordblks|. |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 256 | allocated_objects_size = info.uordblks; |
ssid | 3aa02fe | 2015-11-07 16:15:07 | [diff] [blame] | 257 | #endif |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 258 | |
primiano | fadec05e | 2015-06-03 16:57:32 | [diff] [blame] | 259 | MemoryAllocatorDump* outer_dump = pmd->CreateAllocatorDump("malloc"); |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 260 | outer_dump->AddScalar("virtual_size", MemoryAllocatorDump::kUnitsBytes, |
| 261 | total_virtual_size); |
| 262 | outer_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
| 263 | MemoryAllocatorDump::kUnitsBytes, resident_size); |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 264 | |
primiano | fadec05e | 2015-06-03 16:57:32 | [diff] [blame] | 265 | MemoryAllocatorDump* inner_dump = pmd->CreateAllocatorDump(kAllocatedObjects); |
| 266 | inner_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 267 | MemoryAllocatorDump::kUnitsBytes, |
| 268 | allocated_objects_size); |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 269 | if (allocated_objects_count != 0) { |
siggi | 52114f27 | 2016-08-31 23:51:30 | [diff] [blame] | 270 | inner_dump->AddScalar(MemoryAllocatorDump::kNameObjectCount, |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 271 | MemoryAllocatorDump::kUnitsObjects, |
| 272 | allocated_objects_count); |
| 273 | } |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 274 | |
siggi | 52114f27 | 2016-08-31 23:51:30 | [diff] [blame] | 275 | if (resident_size > allocated_objects_size) { |
ssid | 86f78c1 | 2015-12-21 11:45:32 | [diff] [blame] | 276 | // Explicitly specify why is extra memory resident. In tcmalloc it accounts |
| 277 | // for free lists and caches. In mac and ios it accounts for the |
| 278 | // fragmentation and metadata. |
| 279 | MemoryAllocatorDump* other_dump = |
| 280 | pmd->CreateAllocatorDump("malloc/metadata_fragmentation_caches"); |
| 281 | other_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
| 282 | MemoryAllocatorDump::kUnitsBytes, |
| 283 | resident_size - allocated_objects_size); |
| 284 | } |
| 285 | |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 286 | // Heap profiler dumps. |
| 287 | if (!heap_profiler_enabled_) |
| 288 | return true; |
| 289 | |
| 290 | // The dumps of the heap profiler should be created only when heap profiling |
| 291 | // was enabled (--enable-heap-profiling) AND a DETAILED dump is requested. |
| 292 | // However, when enabled, the overhead of the heap profiler should be always |
| 293 | // reported to avoid oscillations of the malloc total in LIGHT dumps. |
| 294 | |
| 295 | tid_dumping_heap_ = PlatformThread::CurrentId(); |
| 296 | // At this point the Insert/RemoveAllocation hooks will ignore this thread. |
| 297 | // Enclosing all the temporariy data structures in a scope, so that the heap |
| 298 | // profiler does not see unabalanced malloc/free calls from these containers. |
| 299 | { |
| 300 | TraceEventMemoryOverhead overhead; |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame^] | 301 | std::unordered_map<AllocationContext, AllocationMetrics> metrics_by_context; |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 302 | { |
| 303 | AutoLock lock(allocation_register_lock_); |
| 304 | if (allocation_register_) { |
| 305 | if (args.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { |
ssid | 1eedc59 | 2016-04-15 01:56:44 | [diff] [blame] | 306 | for (const auto& alloc_size : *allocation_register_) { |
| 307 | AllocationMetrics& metrics = metrics_by_context[alloc_size.context]; |
| 308 | metrics.size += alloc_size.size; |
| 309 | metrics.count++; |
| 310 | } |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 311 | } |
| 312 | allocation_register_->EstimateTraceMemoryOverhead(&overhead); |
| 313 | } |
| 314 | } // lock(allocation_register_lock_) |
bashi | b873c0d4 | 2016-05-12 05:41:04 | [diff] [blame] | 315 | pmd->DumpHeapUsage(metrics_by_context, overhead, "malloc"); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 316 | } |
| 317 | tid_dumping_heap_ = kInvalidThreadId; |
| 318 | |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 319 | return true; |
| 320 | } |
| 321 | |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 322 | void MallocDumpProvider::OnHeapProfilingEnabled(bool enabled) { |
| 323 | #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 324 | if (enabled) { |
| 325 | { |
| 326 | AutoLock lock(allocation_register_lock_); |
| 327 | allocation_register_.reset(new AllocationRegister()); |
| 328 | } |
| 329 | allocator::InsertAllocatorDispatch(&g_allocator_hooks); |
| 330 | } else { |
| 331 | AutoLock lock(allocation_register_lock_); |
| 332 | allocation_register_.reset(); |
| 333 | // Insert/RemoveAllocation below will no-op if the register is torn down. |
| 334 | // Once disabled, heap profiling will not re-enabled anymore for the |
| 335 | // lifetime of the process. |
| 336 | } |
| 337 | #endif |
| 338 | heap_profiler_enabled_ = enabled; |
| 339 | } |
| 340 | |
| 341 | void MallocDumpProvider::InsertAllocation(void* address, size_t size) { |
| 342 | // CurrentId() can be a slow operation (crbug.com/497226). This apparently |
| 343 | // redundant condition short circuits the CurrentID() calls when unnecessary. |
| 344 | if (tid_dumping_heap_ != kInvalidThreadId && |
| 345 | tid_dumping_heap_ == PlatformThread::CurrentId()) |
| 346 | return; |
| 347 | |
| 348 | // AllocationContextTracker will return nullptr when called re-reentrantly. |
| 349 | // This is the case of GetInstanceForCurrentThread() being called for the |
| 350 | // first time, which causes a new() inside the tracker which re-enters the |
| 351 | // heap profiler, in which case we just want to early out. |
vmpstr | 5170bf9 | 2016-06-29 02:15:58 | [diff] [blame] | 352 | auto* tracker = AllocationContextTracker::GetInstanceForCurrentThread(); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 353 | if (!tracker) |
| 354 | return; |
dskiba | 9ab14b2 | 2017-01-18 21:53:42 | [diff] [blame] | 355 | |
| 356 | AllocationContext context; |
| 357 | if (!tracker->GetContextSnapshot(&context)) |
| 358 | return; |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 359 | |
| 360 | AutoLock lock(allocation_register_lock_); |
| 361 | if (!allocation_register_) |
| 362 | return; |
| 363 | |
| 364 | allocation_register_->Insert(address, size, context); |
| 365 | } |
| 366 | |
| 367 | void MallocDumpProvider::RemoveAllocation(void* address) { |
| 368 | // No re-entrancy is expected here as none of the calls below should |
| 369 | // cause a free()-s (|allocation_register_| does its own heap management). |
| 370 | if (tid_dumping_heap_ != kInvalidThreadId && |
| 371 | tid_dumping_heap_ == PlatformThread::CurrentId()) |
| 372 | return; |
| 373 | AutoLock lock(allocation_register_lock_); |
| 374 | if (!allocation_register_) |
| 375 | return; |
| 376 | allocation_register_->Remove(address); |
| 377 | } |
| 378 | |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 379 | } // namespace trace_event |
| 380 | } // namespace base |