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