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" |
primiano | fd907216 | 2016-03-25 02:13:28 | [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 { |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 35 | #if BUILDFLAG(USE_EXPERIMENTAL_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 | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 142 | #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 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) { |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 200 | size_t total_virtual_size = 0; |
| 201 | size_t resident_size = 0; |
| 202 | size_t allocated_objects_size = 0; |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 203 | size_t allocated_objects_count = 0; |
ssid | 86f78c1 | 2015-12-21 11:45:32 | [diff] [blame] | 204 | #if defined(USE_TCMALLOC) |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 205 | bool res = |
| 206 | allocator::GetNumericProperty("generic.heap_size", &total_virtual_size); |
| 207 | DCHECK(res); |
| 208 | res = allocator::GetNumericProperty("generic.total_physical_bytes", |
| 209 | &resident_size); |
| 210 | DCHECK(res); |
| 211 | res = allocator::GetNumericProperty("generic.current_allocated_bytes", |
| 212 | &allocated_objects_size); |
| 213 | DCHECK(res); |
ssid | 86f78c1 | 2015-12-21 11:45:32 | [diff] [blame] | 214 | #elif defined(OS_MACOSX) || defined(OS_IOS) |
| 215 | malloc_statistics_t stats = {0}; |
| 216 | malloc_zone_statistics(nullptr, &stats); |
| 217 | total_virtual_size = stats.size_allocated; |
| 218 | allocated_objects_size = stats.size_in_use; |
| 219 | |
erikchen | 792525b | 2017-03-10 18:06:15 | [diff] [blame] | 220 | // Resident size is approximated pretty well by stats.max_size_in_use. |
| 221 | // However, on macOS, freed blocks are both resident and reusable, which is |
| 222 | // semantically equivalent to deallocated. The implementation of libmalloc |
| 223 | // will also only hold a fixed number of freed regions before actually |
| 224 | // starting to deallocate them, so stats.max_size_in_use is also not |
| 225 | // representative of the peak size. As a result, stats.max_size_in_use is |
| 226 | // typically somewhere between actually resident [non-reusable] pages, and |
| 227 | // peak size. This is not very useful, so we just use stats.size_in_use for |
| 228 | // resident_size, even though it's an underestimate and fails to account for |
| 229 | // fragmentation. See |
| 230 | // https://bugs.chromium.org/p/chromium/issues/detail?id=695263#c1. |
| 231 | resident_size = stats.size_in_use; |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 232 | #elif defined(OS_WIN) |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 233 | WinHeapInfo main_heap_info = {}; |
| 234 | WinHeapMemoryDumpImpl(&main_heap_info); |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 235 | total_virtual_size = |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 236 | main_heap_info.committed_size + main_heap_info.uncommitted_size; |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 237 | // Resident size is approximated with committed heap size. Note that it is |
| 238 | // possible to do this with better accuracy on windows by intersecting the |
| 239 | // working set with the virtual memory ranges occuipied by the heap. It's not |
| 240 | // clear that this is worth it, as it's fairly expensive to do. |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 241 | resident_size = main_heap_info.committed_size; |
| 242 | allocated_objects_size = main_heap_info.allocated_size; |
| 243 | allocated_objects_count = main_heap_info.block_count; |
scottmg | 6ea9ff3e | 2017-05-19 00:08:16 | [diff] [blame] | 244 | #elif defined(OS_FUCHSIA) |
| 245 | // TODO(fuchsia): Port, see https://crbug.com/706592. |
ssid | 3aa02fe | 2015-11-07 16:15:07 | [diff] [blame] | 246 | #else |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 247 | struct mallinfo info = mallinfo(); |
| 248 | DCHECK_GE(info.arena + info.hblkhd, info.uordblks); |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 249 | |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 250 | // In case of Android's jemalloc |arena| is 0 and the outer pages size is |
| 251 | // reported by |hblkhd|. In case of dlmalloc the total is given by |
| 252 | // |arena| + |hblkhd|. For more details see link: http://goo.gl/fMR8lF. |
| 253 | total_virtual_size = info.arena + info.hblkhd; |
| 254 | resident_size = info.uordblks; |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 255 | |
| 256 | // Total allocated space is given by |uordblks|. |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 257 | allocated_objects_size = info.uordblks; |
ssid | 3aa02fe | 2015-11-07 16:15:07 | [diff] [blame] | 258 | #endif |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 259 | |
primiano | fadec05e | 2015-06-03 16:57:32 | [diff] [blame] | 260 | MemoryAllocatorDump* outer_dump = pmd->CreateAllocatorDump("malloc"); |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 261 | outer_dump->AddScalar("virtual_size", MemoryAllocatorDump::kUnitsBytes, |
| 262 | total_virtual_size); |
| 263 | outer_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
| 264 | MemoryAllocatorDump::kUnitsBytes, resident_size); |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 265 | |
primiano | fadec05e | 2015-06-03 16:57:32 | [diff] [blame] | 266 | MemoryAllocatorDump* inner_dump = pmd->CreateAllocatorDump(kAllocatedObjects); |
| 267 | inner_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 268 | MemoryAllocatorDump::kUnitsBytes, |
| 269 | allocated_objects_size); |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 270 | if (allocated_objects_count != 0) { |
siggi | 52114f27 | 2016-08-31 23:51:30 | [diff] [blame] | 271 | inner_dump->AddScalar(MemoryAllocatorDump::kNameObjectCount, |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 272 | MemoryAllocatorDump::kUnitsObjects, |
| 273 | allocated_objects_count); |
| 274 | } |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 275 | |
siggi | 52114f27 | 2016-08-31 23:51:30 | [diff] [blame] | 276 | if (resident_size > allocated_objects_size) { |
ssid | 86f78c1 | 2015-12-21 11:45:32 | [diff] [blame] | 277 | // Explicitly specify why is extra memory resident. In tcmalloc it accounts |
| 278 | // for free lists and caches. In mac and ios it accounts for the |
| 279 | // fragmentation and metadata. |
| 280 | MemoryAllocatorDump* other_dump = |
| 281 | pmd->CreateAllocatorDump("malloc/metadata_fragmentation_caches"); |
| 282 | other_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
| 283 | MemoryAllocatorDump::kUnitsBytes, |
| 284 | resident_size - allocated_objects_size); |
| 285 | } |
| 286 | |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 287 | // Heap profiler dumps. |
erikchen | bd599af5 | 2017-05-22 21:15:07 | [diff] [blame^] | 288 | if (!allocation_register_.is_enabled()) |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 289 | return true; |
| 290 | |
| 291 | // The dumps of the heap profiler should be created only when heap profiling |
| 292 | // was enabled (--enable-heap-profiling) AND a DETAILED dump is requested. |
| 293 | // However, when enabled, the overhead of the heap profiler should be always |
| 294 | // reported to avoid oscillations of the malloc total in LIGHT dumps. |
| 295 | |
| 296 | tid_dumping_heap_ = PlatformThread::CurrentId(); |
| 297 | // At this point the Insert/RemoveAllocation hooks will ignore this thread. |
etienneb | c907941 | 2017-05-10 17:58:48 | [diff] [blame] | 298 | // Enclosing all the temporary data structures in a scope, so that the heap |
| 299 | // profiler does not see unbalanced malloc/free calls from these containers. |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 300 | { |
etienneb | c907941 | 2017-05-10 17:58:48 | [diff] [blame] | 301 | size_t shim_allocated_objects_size = 0; |
| 302 | size_t shim_allocated_objects_count = 0; |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 303 | TraceEventMemoryOverhead overhead; |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame] | 304 | std::unordered_map<AllocationContext, AllocationMetrics> metrics_by_context; |
erikchen | bd599af5 | 2017-05-22 21:15:07 | [diff] [blame^] | 305 | if (args.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { |
| 306 | ShardedAllocationRegister::OutputMetrics metrics = |
| 307 | allocation_register_.UpdateAndReturnsMetrics(metrics_by_context); |
etienneb | c907941 | 2017-05-10 17:58:48 | [diff] [blame] | 308 | |
erikchen | bd599af5 | 2017-05-22 21:15:07 | [diff] [blame^] | 309 | // Aggregate data for objects allocated through the shim. |
| 310 | shim_allocated_objects_size += metrics.size; |
| 311 | shim_allocated_objects_count += metrics.count; |
| 312 | } |
| 313 | allocation_register_.EstimateTraceMemoryOverhead(&overhead); |
etienneb | c907941 | 2017-05-10 17:58:48 | [diff] [blame] | 314 | |
erikchen | bd599af5 | 2017-05-22 21:15:07 | [diff] [blame^] | 315 | inner_dump->AddScalar("shim_allocated_objects_size", |
| 316 | MemoryAllocatorDump::kUnitsBytes, |
| 317 | shim_allocated_objects_size); |
| 318 | inner_dump->AddScalar("shim_allocator_object_count", |
| 319 | MemoryAllocatorDump::kUnitsObjects, |
| 320 | shim_allocated_objects_count); |
bashi | b873c0d4 | 2016-05-12 05:41:04 | [diff] [blame] | 321 | pmd->DumpHeapUsage(metrics_by_context, overhead, "malloc"); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 322 | } |
| 323 | tid_dumping_heap_ = kInvalidThreadId; |
| 324 | |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 325 | return true; |
| 326 | } |
| 327 | |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 328 | void MallocDumpProvider::OnHeapProfilingEnabled(bool enabled) { |
| 329 | #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 330 | if (enabled) { |
erikchen | bd599af5 | 2017-05-22 21:15:07 | [diff] [blame^] | 331 | allocation_register_.SetEnabled(); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 332 | allocator::InsertAllocatorDispatch(&g_allocator_hooks); |
| 333 | } else { |
erikchen | bd599af5 | 2017-05-22 21:15:07 | [diff] [blame^] | 334 | allocation_register_.SetDisabled(); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 335 | } |
| 336 | #endif |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void MallocDumpProvider::InsertAllocation(void* address, size_t size) { |
| 340 | // CurrentId() can be a slow operation (crbug.com/497226). This apparently |
| 341 | // redundant condition short circuits the CurrentID() calls when unnecessary. |
| 342 | if (tid_dumping_heap_ != kInvalidThreadId && |
| 343 | tid_dumping_heap_ == PlatformThread::CurrentId()) |
| 344 | return; |
| 345 | |
| 346 | // AllocationContextTracker will return nullptr when called re-reentrantly. |
| 347 | // This is the case of GetInstanceForCurrentThread() being called for the |
| 348 | // first time, which causes a new() inside the tracker which re-enters the |
| 349 | // heap profiler, in which case we just want to early out. |
vmpstr | 5170bf9 | 2016-06-29 02:15:58 | [diff] [blame] | 350 | auto* tracker = AllocationContextTracker::GetInstanceForCurrentThread(); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 351 | if (!tracker) |
| 352 | return; |
dskiba | 9ab14b2 | 2017-01-18 21:53:42 | [diff] [blame] | 353 | |
| 354 | AllocationContext context; |
| 355 | if (!tracker->GetContextSnapshot(&context)) |
| 356 | return; |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 357 | |
erikchen | bd599af5 | 2017-05-22 21:15:07 | [diff] [blame^] | 358 | if (!allocation_register_.is_enabled()) |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 359 | return; |
| 360 | |
erikchen | bd599af5 | 2017-05-22 21:15:07 | [diff] [blame^] | 361 | allocation_register_.Insert(address, size, context); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | void MallocDumpProvider::RemoveAllocation(void* address) { |
| 365 | // No re-entrancy is expected here as none of the calls below should |
| 366 | // cause a free()-s (|allocation_register_| does its own heap management). |
| 367 | if (tid_dumping_heap_ != kInvalidThreadId && |
| 368 | tid_dumping_heap_ == PlatformThread::CurrentId()) |
| 369 | return; |
erikchen | bd599af5 | 2017-05-22 21:15:07 | [diff] [blame^] | 370 | if (!allocation_register_.is_enabled()) |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 371 | return; |
erikchen | bd599af5 | 2017-05-22 21:15:07 | [diff] [blame^] | 372 | allocation_register_.Remove(address); |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 373 | } |
| 374 | |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 375 | } // namespace trace_event |
| 376 | } // namespace base |