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
7 #include <stddef.h>
8
9 #include <unordered_map>
10
11 #include "base/allocator/allocator_extension.h"
12 #include "base/allocator/buildflags.h"
13 #include "base/debug/profiler.h"
14 #include "base/trace_event/process_memory_dump.h"
15 #include "base/trace_event/traced_value.h"
16 #include "build/build_config.h"
17
18 #if defined(OS_APPLE)
19 #include <malloc/malloc.h>
20 #elif defined(OS_BSD)
21 #include <stdlib.h>
22 #else
23 #include <malloc.h>
24 #endif
25 #if defined(OS_WIN)
26 #include <windows.h>
27 #endif
28
29 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
30 #include "base/allocator/allocator_shim_default_dispatch_to_partition_alloc.h"
31 #endif
32
33 namespace base {
34 namespace trace_event {
35
36 namespace {
37 #if defined(OS_WIN)
38 // A structure containing some information about a given heap.
39 struct WinHeapInfo {
40 size_t committed_size;
41 size_t uncommitted_size;
42 size_t allocated_size;
43 size_t block_count;
44 };
45
46 // NOTE: crbug.com/665516
47 // Unfortunately, there is no safe way to collect information from secondary
48 // heaps due to limitations and racy nature of this piece of WinAPI.
WinHeapMemoryDumpImpl(WinHeapInfo * crt_heap_info)49 void WinHeapMemoryDumpImpl(WinHeapInfo* crt_heap_info) {
50 // Iterate through whichever heap our CRT is using.
51 HANDLE crt_heap = reinterpret_cast<HANDLE>(_get_heap_handle());
52 ::HeapLock(crt_heap);
53 PROCESS_HEAP_ENTRY heap_entry;
54 heap_entry.lpData = nullptr;
55 // Walk over all the entries in the main heap.
56 while (::HeapWalk(crt_heap, &heap_entry) != FALSE) {
57 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
58 crt_heap_info->allocated_size += heap_entry.cbData;
59 crt_heap_info->block_count++;
60 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) {
61 crt_heap_info->committed_size += heap_entry.Region.dwCommittedSize;
62 crt_heap_info->uncommitted_size += heap_entry.Region.dwUnCommittedSize;
63 }
64 }
65 CHECK(::HeapUnlock(crt_heap) == TRUE);
66 }
67 #endif // defined(OS_WIN)
68
69 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
ReportDetailedPartitionAllocStats(ProcessMemoryDump * pmd)70 void ReportDetailedPartitionAllocStats(ProcessMemoryDump* pmd) {
71 SimplePartitionStatsDumper allocator_dumper;
72 internal::PartitionAllocMalloc::Allocator()->DumpStats("malloc", false,
73 &allocator_dumper);
74
75 if (allocator_dumper.stats().has_thread_cache) {
76 const auto& stats = allocator_dumper.stats().all_thread_caches_stats;
77 auto* thread_cache_dump = pmd->CreateAllocatorDump("malloc/thread_cache");
78 ReportPartitionAllocThreadCacheStats(thread_cache_dump, stats);
79 const auto& main_thread_stats =
80 allocator_dumper.stats().current_thread_cache_stats;
81 auto* main_thread_cache_dump =
82 pmd->CreateAllocatorDump("malloc/thread_cache/main_thread");
83 ReportPartitionAllocThreadCacheStats(main_thread_cache_dump,
84 main_thread_stats);
85 }
86 }
87 #endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
88
89 } // namespace
90
91 // static
92 const char MallocDumpProvider::kAllocatedObjects[] = "malloc/allocated_objects";
93
94 // static
GetInstance()95 MallocDumpProvider* MallocDumpProvider::GetInstance() {
96 return Singleton<MallocDumpProvider,
97 LeakySingletonTraits<MallocDumpProvider>>::get();
98 }
99
100 MallocDumpProvider::MallocDumpProvider() = default;
101 MallocDumpProvider::~MallocDumpProvider() = default;
102
103 // Called at trace dump point time. Creates a snapshot the memory counters for
104 // the current process.
OnMemoryDump(const MemoryDumpArgs & args,ProcessMemoryDump * pmd)105 bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args,
106 ProcessMemoryDump* pmd) {
107 {
108 base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_);
109 if (!emit_metrics_on_memory_dump_)
110 return true;
111 }
112
113 size_t total_virtual_size = 0;
114 size_t resident_size = 0;
115 size_t allocated_objects_size = 0;
116 size_t allocated_objects_count = 0;
117 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
118 if (args.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) {
119 ReportDetailedPartitionAllocStats(pmd);
120 }
121 #endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
122
123 #if BUILDFLAG(USE_TCMALLOC)
124 bool res =
125 allocator::GetNumericProperty("generic.heap_size", &total_virtual_size);
126 DCHECK(res);
127 res = allocator::GetNumericProperty("generic.total_physical_bytes",
128 &resident_size);
129 DCHECK(res);
130 res = allocator::GetNumericProperty("generic.current_allocated_bytes",
131 &allocated_objects_size);
132 DCHECK(res);
133 #elif defined(OS_APPLE)
134 malloc_statistics_t stats = {0};
135 malloc_zone_statistics(nullptr, &stats);
136 total_virtual_size = stats.size_allocated;
137 allocated_objects_size = stats.size_in_use;
138
139 // Resident size is approximated pretty well by stats.max_size_in_use.
140 // However, on macOS, freed blocks are both resident and reusable, which is
141 // semantically equivalent to deallocated. The implementation of libmalloc
142 // will also only hold a fixed number of freed regions before actually
143 // starting to deallocate them, so stats.max_size_in_use is also not
144 // representative of the peak size. As a result, stats.max_size_in_use is
145 // typically somewhere between actually resident [non-reusable] pages, and
146 // peak size. This is not very useful, so we just use stats.size_in_use for
147 // resident_size, even though it's an underestimate and fails to account for
148 // fragmentation. See
149 // https://bugs.chromium.org/p/chromium/issues/detail?id=695263#c1.
150 resident_size = stats.size_in_use;
151 #elif defined(OS_WIN)
152 // This is too expensive on Windows, crbug.com/780735.
153 if (args.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) {
154 WinHeapInfo main_heap_info = {};
155 WinHeapMemoryDumpImpl(&main_heap_info);
156 total_virtual_size =
157 main_heap_info.committed_size + main_heap_info.uncommitted_size;
158 // Resident size is approximated with committed heap size. Note that it is
159 // possible to do this with better accuracy on windows by intersecting the
160 // working set with the virtual memory ranges occuipied by the heap. It's
161 // not clear that this is worth it, as it's fairly expensive to do.
162 resident_size = main_heap_info.committed_size;
163 allocated_objects_size = main_heap_info.allocated_size;
164 allocated_objects_count = main_heap_info.block_count;
165 }
166 #elif defined(OS_FUCHSIA)
167 // TODO(fuchsia): Port, see https://crbug.com/706592.
168 #elif defined(OS_BSD)
169 total_virtual_size = 0;
170 allocated_objects_size = 0;
171 #else
172 struct mallinfo info = mallinfo();
173 // In case of Android's jemalloc |arena| is 0 and the outer pages size is
174 // reported by |hblkhd|. In case of dlmalloc the total is given by
175 // |arena| + |hblkhd|. For more details see link: http://goo.gl/fMR8lF.
176 total_virtual_size = info.arena + info.hblkhd;
177 resident_size = info.uordblks;
178
179 // Total allocated space is given by |uordblks|.
180 allocated_objects_size = info.uordblks;
181 #endif
182
183 MemoryAllocatorDump* outer_dump = pmd->CreateAllocatorDump("malloc");
184 outer_dump->AddScalar("virtual_size", MemoryAllocatorDump::kUnitsBytes,
185 total_virtual_size);
186 outer_dump->AddScalar(MemoryAllocatorDump::kNameSize,
187 MemoryAllocatorDump::kUnitsBytes, resident_size);
188
189 MemoryAllocatorDump* inner_dump = pmd->CreateAllocatorDump(kAllocatedObjects);
190 inner_dump->AddScalar(MemoryAllocatorDump::kNameSize,
191 MemoryAllocatorDump::kUnitsBytes,
192 allocated_objects_size);
193 if (allocated_objects_count != 0) {
194 inner_dump->AddScalar(MemoryAllocatorDump::kNameObjectCount,
195 MemoryAllocatorDump::kUnitsObjects,
196 allocated_objects_count);
197 }
198
199 if (resident_size > allocated_objects_size) {
200 // Explicitly specify why is extra memory resident. In tcmalloc it accounts
201 // for free lists and caches. In mac and ios it accounts for the
202 // fragmentation and metadata.
203 MemoryAllocatorDump* other_dump =
204 pmd->CreateAllocatorDump("malloc/metadata_fragmentation_caches");
205 other_dump->AddScalar(MemoryAllocatorDump::kNameSize,
206 MemoryAllocatorDump::kUnitsBytes,
207 resident_size - allocated_objects_size);
208 }
209 return true;
210 }
211
EnableMetrics()212 void MallocDumpProvider::EnableMetrics() {
213 base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_);
214 emit_metrics_on_memory_dump_ = true;
215 }
216
DisableMetrics()217 void MallocDumpProvider::DisableMetrics() {
218 base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_);
219 emit_metrics_on_memory_dump_ = false;
220 }
221
222 #if BUILDFLAG(USE_PARTITION_ALLOC)
ReportPartitionAllocThreadCacheStats(MemoryAllocatorDump * dump,const ThreadCacheStats & stats)223 void ReportPartitionAllocThreadCacheStats(MemoryAllocatorDump* dump,
224 const ThreadCacheStats& stats) {
225 dump->AddScalar("alloc_count", "scalar", stats.alloc_count);
226 dump->AddScalar("alloc_hits", "scalar", stats.alloc_hits);
227 dump->AddScalar("alloc_misses", "scalar", stats.alloc_misses);
228
229 dump->AddScalar("alloc_miss_empty", "scalar", stats.alloc_miss_empty);
230 dump->AddScalar("alloc_miss_too_large", "scalar", stats.alloc_miss_too_large);
231
232 dump->AddScalar("cache_fill_count", "scalar", stats.cache_fill_count);
233 dump->AddScalar("cache_fill_hits", "scalar", stats.cache_fill_hits);
234 dump->AddScalar("cache_fill_misses", "scalar", stats.cache_fill_misses);
235 dump->AddScalar("cache_fill_bucket_full", "scalar",
236 stats.cache_fill_bucket_full);
237 dump->AddScalar("cache_fill_too_large", "scalar", stats.cache_fill_too_large);
238
239 dump->AddScalar("size", "bytes", stats.bucket_total_memory);
240 dump->AddScalar("metadata_overhead", "bytes", stats.metadata_overhead);
241 }
242 #endif // BUILDFLAG(USE_PARTITION_ALLOC)
243
244 } // namespace trace_event
245 } // namespace base
246