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" |
Scott Violet | 4416579 | 2018-02-22 02:08:08 | [diff] [blame] | 12 | #include "base/allocator/buildflags.h" |
siggi | ba33ec0 | 2016-08-26 16:13:07 | [diff] [blame] | 13 | #include "base/debug/profiler.h" |
avi | bd1ed05 | 2015-12-24 04:03:44 | [diff] [blame] | 14 | #include "base/trace_event/process_memory_dump.h" |
David 'Digit' Turner | 8e6ae28 | 2018-10-26 08:03:51 | [diff] [blame^] | 15 | #include "base/trace_event/traced_value.h" |
avi | bd1ed05 | 2015-12-24 04:03:44 | [diff] [blame] | 16 | #include "build/build_config.h" |
| 17 | |
ssid | 3aa02fe | 2015-11-07 16:15:07 | [diff] [blame] | 18 | #if defined(OS_MACOSX) |
| 19 | #include <malloc/malloc.h> |
| 20 | #else |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 21 | #include <malloc.h> |
ssid | 3aa02fe | 2015-11-07 16:15:07 | [diff] [blame] | 22 | #endif |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 23 | #if defined(OS_WIN) |
| 24 | #include <windows.h> |
| 25 | #endif |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 26 | |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 27 | namespace base { |
| 28 | namespace trace_event { |
| 29 | |
primiano | fd907216 | 2016-03-25 02:13:28 | [diff] [blame] | 30 | namespace { |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 31 | #if defined(OS_WIN) |
| 32 | // A structure containing some information about a given heap. |
| 33 | struct WinHeapInfo { |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 34 | size_t committed_size; |
| 35 | size_t uncommitted_size; |
| 36 | size_t allocated_size; |
| 37 | size_t block_count; |
| 38 | }; |
| 39 | |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 40 | // NOTE: crbug.com/665516 |
| 41 | // Unfortunately, there is no safe way to collect information from secondary |
| 42 | // heaps due to limitations and racy nature of this piece of WinAPI. |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 43 | void WinHeapMemoryDumpImpl(WinHeapInfo* crt_heap_info) { |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 44 | // Iterate through whichever heap our CRT is using. |
| 45 | HANDLE crt_heap = reinterpret_cast<HANDLE>(_get_heap_handle()); |
| 46 | ::HeapLock(crt_heap); |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 47 | PROCESS_HEAP_ENTRY heap_entry; |
| 48 | heap_entry.lpData = nullptr; |
| 49 | // Walk over all the entries in the main heap. |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 50 | while (::HeapWalk(crt_heap, &heap_entry) != FALSE) { |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 51 | if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) { |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 52 | crt_heap_info->allocated_size += heap_entry.cbData; |
| 53 | crt_heap_info->block_count++; |
kraynov | ad50729 | 2016-11-25 18:01:23 | [diff] [blame] | 54 | } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) { |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 55 | crt_heap_info->committed_size += heap_entry.Region.dwCommittedSize; |
| 56 | crt_heap_info->uncommitted_size += heap_entry.Region.dwUnCommittedSize; |
liamjm | c56e1ffa | 2016-10-15 01:04:46 | [diff] [blame] | 57 | } |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 58 | } |
siggi | 82535f6 | 2016-12-06 22:29:03 | [diff] [blame] | 59 | CHECK(::HeapUnlock(crt_heap) == TRUE); |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 60 | } |
| 61 | #endif // defined(OS_WIN) |
| 62 | } // namespace |
| 63 | |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 64 | // static |
primiano | fadec05e | 2015-06-03 16:57:32 | [diff] [blame] | 65 | const char MallocDumpProvider::kAllocatedObjects[] = "malloc/allocated_objects"; |
| 66 | |
| 67 | // static |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 68 | MallocDumpProvider* MallocDumpProvider::GetInstance() { |
| 69 | return Singleton<MallocDumpProvider, |
| 70 | LeakySingletonTraits<MallocDumpProvider>>::get(); |
| 71 | } |
| 72 | |
erikchen | 37097a9 | 2018-05-22 20:28:47 | [diff] [blame] | 73 | MallocDumpProvider::MallocDumpProvider() = default; |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 74 | MallocDumpProvider::~MallocDumpProvider() = default; |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 75 | |
| 76 | // Called at trace dump point time. Creates a snapshot the memory counters for |
| 77 | // the current process. |
ssid | 90694aeec | 2015-08-06 13:01:30 | [diff] [blame] | 78 | bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args, |
| 79 | ProcessMemoryDump* pmd) { |
Erik Chen | 4f64989c | 2017-11-10 18:15:53 | [diff] [blame] | 80 | { |
| 81 | base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_); |
| 82 | if (!emit_metrics_on_memory_dump_) |
| 83 | return true; |
| 84 | } |
| 85 | |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 86 | size_t total_virtual_size = 0; |
| 87 | size_t resident_size = 0; |
| 88 | size_t allocated_objects_size = 0; |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 89 | size_t allocated_objects_count = 0; |
ssid | 86f78c1 | 2015-12-21 11:45:32 | [diff] [blame] | 90 | #if defined(USE_TCMALLOC) |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 91 | bool res = |
| 92 | allocator::GetNumericProperty("generic.heap_size", &total_virtual_size); |
| 93 | DCHECK(res); |
| 94 | res = allocator::GetNumericProperty("generic.total_physical_bytes", |
| 95 | &resident_size); |
| 96 | DCHECK(res); |
| 97 | res = allocator::GetNumericProperty("generic.current_allocated_bytes", |
| 98 | &allocated_objects_size); |
| 99 | DCHECK(res); |
ssid | 86f78c1 | 2015-12-21 11:45:32 | [diff] [blame] | 100 | #elif defined(OS_MACOSX) || defined(OS_IOS) |
| 101 | malloc_statistics_t stats = {0}; |
| 102 | malloc_zone_statistics(nullptr, &stats); |
| 103 | total_virtual_size = stats.size_allocated; |
| 104 | allocated_objects_size = stats.size_in_use; |
| 105 | |
erikchen | 792525b | 2017-03-10 18:06:15 | [diff] [blame] | 106 | // Resident size is approximated pretty well by stats.max_size_in_use. |
| 107 | // However, on macOS, freed blocks are both resident and reusable, which is |
| 108 | // semantically equivalent to deallocated. The implementation of libmalloc |
| 109 | // will also only hold a fixed number of freed regions before actually |
| 110 | // starting to deallocate them, so stats.max_size_in_use is also not |
| 111 | // representative of the peak size. As a result, stats.max_size_in_use is |
| 112 | // typically somewhere between actually resident [non-reusable] pages, and |
| 113 | // peak size. This is not very useful, so we just use stats.size_in_use for |
| 114 | // resident_size, even though it's an underestimate and fails to account for |
| 115 | // fragmentation. See |
| 116 | // https://bugs.chromium.org/p/chromium/issues/detail?id=695263#c1. |
| 117 | resident_size = stats.size_in_use; |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 118 | #elif defined(OS_WIN) |
Erik Chen | 99e8167 | 2017-11-08 23:15:07 | [diff] [blame] | 119 | // This is too expensive on Windows, crbug.com/780735. |
| 120 | if (args.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { |
| 121 | WinHeapInfo main_heap_info = {}; |
| 122 | WinHeapMemoryDumpImpl(&main_heap_info); |
| 123 | total_virtual_size = |
| 124 | main_heap_info.committed_size + main_heap_info.uncommitted_size; |
| 125 | // Resident size is approximated with committed heap size. Note that it is |
| 126 | // possible to do this with better accuracy on windows by intersecting the |
| 127 | // working set with the virtual memory ranges occuipied by the heap. It's |
| 128 | // not clear that this is worth it, as it's fairly expensive to do. |
| 129 | resident_size = main_heap_info.committed_size; |
| 130 | allocated_objects_size = main_heap_info.allocated_size; |
| 131 | allocated_objects_count = main_heap_info.block_count; |
| 132 | } |
scottmg | 6ea9ff3e | 2017-05-19 00:08:16 | [diff] [blame] | 133 | #elif defined(OS_FUCHSIA) |
| 134 | // TODO(fuchsia): Port, see https://crbug.com/706592. |
ssid | 3aa02fe | 2015-11-07 16:15:07 | [diff] [blame] | 135 | #else |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 136 | struct mallinfo info = mallinfo(); |
| 137 | DCHECK_GE(info.arena + info.hblkhd, info.uordblks); |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 138 | |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 139 | // In case of Android's jemalloc |arena| is 0 and the outer pages size is |
| 140 | // reported by |hblkhd|. In case of dlmalloc the total is given by |
| 141 | // |arena| + |hblkhd|. For more details see link: http://goo.gl/fMR8lF. |
| 142 | total_virtual_size = info.arena + info.hblkhd; |
| 143 | resident_size = info.uordblks; |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 144 | |
| 145 | // Total allocated space is given by |uordblks|. |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 146 | allocated_objects_size = info.uordblks; |
ssid | 3aa02fe | 2015-11-07 16:15:07 | [diff] [blame] | 147 | #endif |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 148 | |
primiano | fadec05e | 2015-06-03 16:57:32 | [diff] [blame] | 149 | MemoryAllocatorDump* outer_dump = pmd->CreateAllocatorDump("malloc"); |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 150 | outer_dump->AddScalar("virtual_size", MemoryAllocatorDump::kUnitsBytes, |
| 151 | total_virtual_size); |
| 152 | outer_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
| 153 | MemoryAllocatorDump::kUnitsBytes, resident_size); |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 154 | |
primiano | fadec05e | 2015-06-03 16:57:32 | [diff] [blame] | 155 | MemoryAllocatorDump* inner_dump = pmd->CreateAllocatorDump(kAllocatedObjects); |
| 156 | inner_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 157 | MemoryAllocatorDump::kUnitsBytes, |
| 158 | allocated_objects_size); |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 159 | if (allocated_objects_count != 0) { |
siggi | 52114f27 | 2016-08-31 23:51:30 | [diff] [blame] | 160 | inner_dump->AddScalar(MemoryAllocatorDump::kNameObjectCount, |
siggi | 7bec59a | 2016-08-25 20:22:26 | [diff] [blame] | 161 | MemoryAllocatorDump::kUnitsObjects, |
| 162 | allocated_objects_count); |
| 163 | } |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 164 | |
siggi | 52114f27 | 2016-08-31 23:51:30 | [diff] [blame] | 165 | if (resident_size > allocated_objects_size) { |
ssid | 86f78c1 | 2015-12-21 11:45:32 | [diff] [blame] | 166 | // Explicitly specify why is extra memory resident. In tcmalloc it accounts |
| 167 | // for free lists and caches. In mac and ios it accounts for the |
| 168 | // fragmentation and metadata. |
| 169 | MemoryAllocatorDump* other_dump = |
| 170 | pmd->CreateAllocatorDump("malloc/metadata_fragmentation_caches"); |
| 171 | other_dump->AddScalar(MemoryAllocatorDump::kNameSize, |
| 172 | MemoryAllocatorDump::kUnitsBytes, |
| 173 | resident_size - allocated_objects_size); |
| 174 | } |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 175 | return true; |
| 176 | } |
| 177 | |
Erik Chen | 4f64989c | 2017-11-10 18:15:53 | [diff] [blame] | 178 | void MallocDumpProvider::EnableMetrics() { |
| 179 | base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_); |
| 180 | emit_metrics_on_memory_dump_ = true; |
| 181 | } |
| 182 | |
| 183 | void MallocDumpProvider::DisableMetrics() { |
| 184 | base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_); |
| 185 | emit_metrics_on_memory_dump_ = false; |
| 186 | } |
| 187 | |
ssid | 0738685 | 2015-04-14 15:32:37 | [diff] [blame] | 188 | } // namespace trace_event |
| 189 | } // namespace base |