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