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/trace_event_argument.h"
16 #include "build/build_config.h"
17
18 #if defined(OS_MACOSX)
19 #include <malloc/malloc.h>
20 #else
21 #include <malloc.h>
22 #endif
23 #if defined(OS_WIN)
24 #include <windows.h>
25 #endif
26
27 namespace base {
28 namespace trace_event {
29
30 namespace {
31 #if defined(OS_WIN)
32 // A structure containing some information about a given heap.
33 struct WinHeapInfo {
34 size_t committed_size;
35 size_t uncommitted_size;
36 size_t allocated_size;
37 size_t block_count;
38 };
39
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.
WinHeapMemoryDumpImpl(WinHeapInfo * crt_heap_info)43 void WinHeapMemoryDumpImpl(WinHeapInfo* crt_heap_info) {
44 // Iterate through whichever heap our CRT is using.
45 HANDLE crt_heap = reinterpret_cast<HANDLE>(_get_heap_handle());
46 ::HeapLock(crt_heap);
47 PROCESS_HEAP_ENTRY heap_entry;
48 heap_entry.lpData = nullptr;
49 // Walk over all the entries in the main heap.
50 while (::HeapWalk(crt_heap, &heap_entry) != FALSE) {
51 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
52 crt_heap_info->allocated_size += heap_entry.cbData;
53 crt_heap_info->block_count++;
54 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) {
55 crt_heap_info->committed_size += heap_entry.Region.dwCommittedSize;
56 crt_heap_info->uncommitted_size += heap_entry.Region.dwUnCommittedSize;
57 }
58 }
59 CHECK(::HeapUnlock(crt_heap) == TRUE);
60 }
61 #endif // defined(OS_WIN)
62 } // namespace
63
64 // static
65 const char MallocDumpProvider::kAllocatedObjects[] = "malloc/allocated_objects";
66
67 // static
GetInstance()68 MallocDumpProvider* MallocDumpProvider::GetInstance() {
69 return Singleton<MallocDumpProvider,
70 LeakySingletonTraits<MallocDumpProvider>>::get();
71 }
72
73 MallocDumpProvider::MallocDumpProvider() = default;
74 MallocDumpProvider::~MallocDumpProvider() = default;
75
76 // Called at trace dump point time. Creates a snapshot the memory counters for
77 // the current process.
OnMemoryDump(const MemoryDumpArgs & args,ProcessMemoryDump * pmd)78 bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args,
79 ProcessMemoryDump* pmd) {
80 {
81 base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_);
82 if (!emit_metrics_on_memory_dump_)
83 return true;
84 }
85
86 size_t total_virtual_size = 0;
87 size_t resident_size = 0;
88 size_t allocated_objects_size = 0;
89 size_t allocated_objects_count = 0;
90 #if defined(USE_TCMALLOC)
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);
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
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;
118 #elif defined(OS_WIN)
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 }
133 #elif defined(OS_FUCHSIA)
134 // TODO(fuchsia): Port, see https://crbug.com/706592.
135 #else
136 struct mallinfo info = mallinfo();
137 DCHECK_GE(info.arena + info.hblkhd, info.uordblks);
138
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;
144
145 // Total allocated space is given by |uordblks|.
146 allocated_objects_size = info.uordblks;
147 #endif
148
149 MemoryAllocatorDump* outer_dump = pmd->CreateAllocatorDump("malloc");
150 outer_dump->AddScalar("virtual_size", MemoryAllocatorDump::kUnitsBytes,
151 total_virtual_size);
152 outer_dump->AddScalar(MemoryAllocatorDump::kNameSize,
153 MemoryAllocatorDump::kUnitsBytes, resident_size);
154
155 MemoryAllocatorDump* inner_dump = pmd->CreateAllocatorDump(kAllocatedObjects);
156 inner_dump->AddScalar(MemoryAllocatorDump::kNameSize,
157 MemoryAllocatorDump::kUnitsBytes,
158 allocated_objects_size);
159 if (allocated_objects_count != 0) {
160 inner_dump->AddScalar(MemoryAllocatorDump::kNameObjectCount,
161 MemoryAllocatorDump::kUnitsObjects,
162 allocated_objects_count);
163 }
164
165 if (resident_size > allocated_objects_size) {
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 }
175 return true;
176 }
177
EnableMetrics()178 void MallocDumpProvider::EnableMetrics() {
179 base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_);
180 emit_metrics_on_memory_dump_ = true;
181 }
182
DisableMetrics()183 void MallocDumpProvider::DisableMetrics() {
184 base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_);
185 emit_metrics_on_memory_dump_ = false;
186 }
187
188 } // namespace trace_event
189 } // namespace base
190