blob: 0077d8b0aecb5bb185a8cd945fb0c62a53914d07 [file] [log] [blame]
ssid07386852015-04-14 15:32:371// 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
avibd1ed052015-12-24 04:03:447#include <stddef.h>
8
brettw1ce49f62017-04-27 19:42:329#include <unordered_map>
10
avibd1ed052015-12-24 04:03:4411#include "base/allocator/allocator_extension.h"
Scott Violet44165792018-02-22 02:08:0812#include "base/allocator/buildflags.h"
siggiba33ec02016-08-26 16:13:0713#include "base/debug/profiler.h"
avibd1ed052015-12-24 04:03:4414#include "base/trace_event/process_memory_dump.h"
David 'Digit' Turner8e6ae282018-10-26 08:03:5115#include "base/trace_event/traced_value.h"
avibd1ed052015-12-24 04:03:4416#include "build/build_config.h"
17
ssid3aa02fe2015-11-07 16:15:0718#if defined(OS_MACOSX)
19#include <malloc/malloc.h>
20#else
ssid07386852015-04-14 15:32:3721#include <malloc.h>
ssid3aa02fe2015-11-07 16:15:0722#endif
siggi7bec59a2016-08-25 20:22:2623#if defined(OS_WIN)
24#include <windows.h>
25#endif
ssid07386852015-04-14 15:32:3726
ssid07386852015-04-14 15:32:3727namespace base {
28namespace trace_event {
29
primianofd9072162016-03-25 02:13:2830namespace {
siggi7bec59a2016-08-25 20:22:2631#if defined(OS_WIN)
32// A structure containing some information about a given heap.
33struct WinHeapInfo {
siggi7bec59a2016-08-25 20:22:2634 size_t committed_size;
35 size_t uncommitted_size;
36 size_t allocated_size;
37 size_t block_count;
38};
39
kraynovad507292016-11-25 18:01:2340// 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.
siggi82535f62016-12-06 22:29:0343void WinHeapMemoryDumpImpl(WinHeapInfo* crt_heap_info) {
siggi82535f62016-12-06 22:29:0344 // Iterate through whichever heap our CRT is using.
45 HANDLE crt_heap = reinterpret_cast<HANDLE>(_get_heap_handle());
46 ::HeapLock(crt_heap);
kraynovad507292016-11-25 18:01:2347 PROCESS_HEAP_ENTRY heap_entry;
48 heap_entry.lpData = nullptr;
49 // Walk over all the entries in the main heap.
siggi82535f62016-12-06 22:29:0350 while (::HeapWalk(crt_heap, &heap_entry) != FALSE) {
kraynovad507292016-11-25 18:01:2351 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
siggi82535f62016-12-06 22:29:0352 crt_heap_info->allocated_size += heap_entry.cbData;
53 crt_heap_info->block_count++;
kraynovad507292016-11-25 18:01:2354 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) {
siggi82535f62016-12-06 22:29:0355 crt_heap_info->committed_size += heap_entry.Region.dwCommittedSize;
56 crt_heap_info->uncommitted_size += heap_entry.Region.dwUnCommittedSize;
liamjmc56e1ffa2016-10-15 01:04:4657 }
siggi7bec59a2016-08-25 20:22:2658 }
siggi82535f62016-12-06 22:29:0359 CHECK(::HeapUnlock(crt_heap) == TRUE);
siggi7bec59a2016-08-25 20:22:2660}
61#endif // defined(OS_WIN)
62} // namespace
63
ssid07386852015-04-14 15:32:3764// static
primianofadec05e2015-06-03 16:57:3265const char MallocDumpProvider::kAllocatedObjects[] = "malloc/allocated_objects";
66
67// static
ssid07386852015-04-14 15:32:3768MallocDumpProvider* MallocDumpProvider::GetInstance() {
69 return Singleton<MallocDumpProvider,
70 LeakySingletonTraits<MallocDumpProvider>>::get();
71}
72
erikchen37097a92018-05-22 20:28:4773MallocDumpProvider::MallocDumpProvider() = default;
Chris Watkinsbb7211c2017-11-29 07:16:3874MallocDumpProvider::~MallocDumpProvider() = default;
ssid07386852015-04-14 15:32:3775
76// Called at trace dump point time. Creates a snapshot the memory counters for
77// the current process.
ssid90694aeec2015-08-06 13:01:3078bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args,
79 ProcessMemoryDump* pmd) {
Erik Chen4f64989c2017-11-10 18:15:5380 {
81 base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_);
82 if (!emit_metrics_on_memory_dump_)
83 return true;
84 }
85
ssid09434092015-10-26 23:05:0486 size_t total_virtual_size = 0;
87 size_t resident_size = 0;
88 size_t allocated_objects_size = 0;
siggi7bec59a2016-08-25 20:22:2689 size_t allocated_objects_count = 0;
ssid86f78c12015-12-21 11:45:3290#if defined(USE_TCMALLOC)
primianodda6c272015-12-07 16:51:0491 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);
ssid86f78c12015-12-21 11:45:32100#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
erikchen792525b2017-03-10 18:06:15106 // 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;
siggi7bec59a2016-08-25 20:22:26118#elif defined(OS_WIN)
Erik Chen99e81672017-11-08 23:15:07119 // 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 }
scottmg6ea9ff3e2017-05-19 00:08:16133#elif defined(OS_FUCHSIA)
134// TODO(fuchsia): Port, see https://crbug.com/706592.
ssid3aa02fe2015-11-07 16:15:07135#else
primianodda6c272015-12-07 16:51:04136 struct mallinfo info = mallinfo();
137 DCHECK_GE(info.arena + info.hblkhd, info.uordblks);
ssid09434092015-10-26 23:05:04138
primianodda6c272015-12-07 16:51:04139 // 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;
siggi7bec59a2016-08-25 20:22:26144
145 // Total allocated space is given by |uordblks|.
primianodda6c272015-12-07 16:51:04146 allocated_objects_size = info.uordblks;
ssid3aa02fe2015-11-07 16:15:07147#endif
ssid09434092015-10-26 23:05:04148
primianofadec05e2015-06-03 16:57:32149 MemoryAllocatorDump* outer_dump = pmd->CreateAllocatorDump("malloc");
ssid09434092015-10-26 23:05:04150 outer_dump->AddScalar("virtual_size", MemoryAllocatorDump::kUnitsBytes,
151 total_virtual_size);
152 outer_dump->AddScalar(MemoryAllocatorDump::kNameSize,
153 MemoryAllocatorDump::kUnitsBytes, resident_size);
ssid07386852015-04-14 15:32:37154
primianofadec05e2015-06-03 16:57:32155 MemoryAllocatorDump* inner_dump = pmd->CreateAllocatorDump(kAllocatedObjects);
156 inner_dump->AddScalar(MemoryAllocatorDump::kNameSize,
ssid09434092015-10-26 23:05:04157 MemoryAllocatorDump::kUnitsBytes,
158 allocated_objects_size);
siggi7bec59a2016-08-25 20:22:26159 if (allocated_objects_count != 0) {
siggi52114f272016-08-31 23:51:30160 inner_dump->AddScalar(MemoryAllocatorDump::kNameObjectCount,
siggi7bec59a2016-08-25 20:22:26161 MemoryAllocatorDump::kUnitsObjects,
162 allocated_objects_count);
163 }
ssid07386852015-04-14 15:32:37164
siggi52114f272016-08-31 23:51:30165 if (resident_size > allocated_objects_size) {
ssid86f78c12015-12-21 11:45:32166 // 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 }
ssid07386852015-04-14 15:32:37175 return true;
176}
177
Erik Chen4f64989c2017-11-10 18:15:53178void MallocDumpProvider::EnableMetrics() {
179 base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_);
180 emit_metrics_on_memory_dump_ = true;
181}
182
183void MallocDumpProvider::DisableMetrics() {
184 base::AutoLock auto_lock(emit_metrics_on_memory_dump_lock_);
185 emit_metrics_on_memory_dump_ = false;
186}
187
ssid07386852015-04-14 15:32:37188} // namespace trace_event
189} // namespace base