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