Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: base/trace_event/malloc_dump_provider.cc

Issue 2528053002: Account only main WinHeap in MemoryDumpProvider on Windows. (Closed)
Patch Set: CHECK--; Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/malloc_dump_provider.h" 5 #include "base/trace_event/malloc_dump_provider.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/allocator/allocator_extension.h" 9 #include "base/allocator/allocator_extension.h"
10 #include "base/allocator/allocator_shim.h" 10 #include "base/allocator/allocator_shim.h"
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 &HookRealloc, /* realloc_function */ 89 &HookRealloc, /* realloc_function */
90 &HookFree, /* free_function */ 90 &HookFree, /* free_function */
91 &HookGetSizeEstimate, /* get_size_estimate_function */ 91 &HookGetSizeEstimate, /* get_size_estimate_function */
92 nullptr, /* next */ 92 nullptr, /* next */
93 }; 93 };
94 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) 94 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
95 95
96 #if defined(OS_WIN) 96 #if defined(OS_WIN)
97 // A structure containing some information about a given heap. 97 // A structure containing some information about a given heap.
98 struct WinHeapInfo { 98 struct WinHeapInfo {
99 HANDLE heap_id;
100 size_t committed_size; 99 size_t committed_size;
101 size_t uncommitted_size; 100 size_t uncommitted_size;
102 size_t allocated_size; 101 size_t allocated_size;
103 size_t block_count; 102 size_t block_count;
104 }; 103 };
105 104
106 bool GetHeapInformation(WinHeapInfo* heap_info, 105 // NOTE: crbug.com/665516
107 const std::set<void*>& block_to_skip) { 106 // Unfortunately, there is no safe way to collect information from secondary
108 // NOTE: crbug.com/464430 107 // heaps due to limitations and racy nature of this piece of WinAPI.
109 // As a part of the Client/Server Runtine Subsystem (CSRSS) lockdown in the 108 void WinHeapMemoryDumpImpl(WinHeapInfo* main_heap_info) {
110 // referenced bug, it will invalidate the heap used by CSRSS. The author has
111 // not found a way to clean up an invalid heap handle, so it will be left in
112 // the process's heap list. Therefore we need to support when there is this
113 // invalid heap handle in the heap list.
114 // HeapLock implicitly checks certain aspects of the HEAP structure, such as
115 // the signature. If this passes, we assume that this heap is valid and is
116 // not the one owned by CSRSS.
117 if (!::HeapLock(heap_info->heap_id)) {
118 return false;
119 }
120 PROCESS_HEAP_ENTRY heap_entry;
121 heap_entry.lpData = nullptr;
122 // Walk over all the entries in this heap.
123 while (::HeapWalk(heap_info->heap_id, &heap_entry) != FALSE) {
124 if (block_to_skip.count(heap_entry.lpData) == 1)
125 continue;
126 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
127 heap_info->allocated_size += heap_entry.cbData;
128 heap_info->block_count++;
129 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) {
130 heap_info->committed_size += heap_entry.Region.dwCommittedSize;
131 heap_info->uncommitted_size += heap_entry.Region.dwUnCommittedSize;
132 }
133 }
134 CHECK(::HeapUnlock(heap_info->heap_id) == TRUE);
135 return true;
136 }
137
138 void WinHeapMemoryDumpImpl(WinHeapInfo* all_heap_info) {
139 // This method might be flaky for 2 reasons:
140 // - GetProcessHeaps is racy by design. It returns a snapshot of the
141 // available heaps, but there's no guarantee that that snapshot remains
142 // valid. If a heap disappears between GetProcessHeaps() and HeapWalk()
143 // then chaos should be assumed. This flakyness is acceptable for tracing.
144 // - The MSDN page for HeapLock says: "If the HeapLock function is called on
145 // a heap created with the HEAP_NO_SERIALIZATION flag, the results are
146 // undefined."
147 // - Note that multiple heaps occur on Windows primarily because system and
148 // 3rd party DLLs will each create their own private heap. It's possible to
149 // retrieve the heap the CRT allocates from and report specifically on that
150 // heap. It's interesting to report all heaps, as e.g. loading or invoking
151 // on a Windows API may consume memory from a private heap.
152 #if defined(SYZYASAN) 109 #if defined(SYZYASAN)
153 if (base::debug::IsBinaryInstrumented()) 110 if (base::debug::IsBinaryInstrumented())
154 return; 111 return;
155 #endif 112 #endif
156 113 HANDLE main_heap = ::GetProcessHeap();
Sigurður Ásgeirsson 2016/11/30 13:49:04 This relies on an implementation detail of the CRT
157 // Retrieves the number of heaps in the current process. 114 ::HeapLock(main_heap);
158 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL); 115 PROCESS_HEAP_ENTRY heap_entry;
159 116 heap_entry.lpData = nullptr;
160 // Try to retrieve a handle to all the heaps owned by this process. Returns 117 // Walk over all the entries in the main heap.
161 // false if the number of heaps has changed. 118 while (::HeapWalk(main_heap, &heap_entry) != FALSE) {
162 // 119 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
163 // This is inherently racy as is, but it's not something that we observe a lot 120 main_heap_info->allocated_size += heap_entry.cbData;
164 // in Chrome, the heaps tend to be created at startup only. 121 main_heap_info->block_count++;
165 std::unique_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]); 122 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) {
166 if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps) 123 main_heap_info->committed_size += heap_entry.Region.dwCommittedSize;
167 return; 124 main_heap_info->uncommitted_size += heap_entry.Region.dwUnCommittedSize;
168
169 // Skip the pointer to the heap array to avoid accounting the memory used by
170 // this dump provider.
171 std::set<void*> block_to_skip;
172 block_to_skip.insert(all_heaps.get());
173
174 // Retrieves some metrics about each heap.
175 size_t heap_info_errors = 0;
176 for (size_t i = 0; i < number_of_heaps; ++i) {
177 WinHeapInfo heap_info = {0};
178 heap_info.heap_id = all_heaps[i];
179 if (GetHeapInformation(&heap_info, block_to_skip)) {
180 all_heap_info->allocated_size += heap_info.allocated_size;
181 all_heap_info->committed_size += heap_info.committed_size;
182 all_heap_info->uncommitted_size += heap_info.uncommitted_size;
183 all_heap_info->block_count += heap_info.block_count;
184 } else {
185 ++heap_info_errors;
186 // See notes in GetHeapInformation() but we only expect 1 heap to not be
187 // able to be read.
188 CHECK_EQ(1u, heap_info_errors);
189 } 125 }
190 } 126 }
127 CHECK(::HeapUnlock(main_heap) == TRUE);
191 } 128 }
192 #endif // defined(OS_WIN) 129 #endif // defined(OS_WIN)
193 } // namespace 130 } // namespace
194 131
195 // static 132 // static
196 const char MallocDumpProvider::kAllocatedObjects[] = "malloc/allocated_objects"; 133 const char MallocDumpProvider::kAllocatedObjects[] = "malloc/allocated_objects";
197 134
198 // static 135 // static
199 MallocDumpProvider* MallocDumpProvider::GetInstance() { 136 MallocDumpProvider* MallocDumpProvider::GetInstance() {
200 return Singleton<MallocDumpProvider, 137 return Singleton<MallocDumpProvider,
(...skipping 29 matching lines...) Expand all
230 total_virtual_size = stats.size_allocated; 167 total_virtual_size = stats.size_allocated;
231 allocated_objects_size = stats.size_in_use; 168 allocated_objects_size = stats.size_in_use;
232 169
233 // The resident size is approximated to the max size in use, which would count 170 // The resident size is approximated to the max size in use, which would count
234 // the total size of all regions other than the free bytes at the end of each 171 // the total size of all regions other than the free bytes at the end of each
235 // region. In each allocation region the allocations are rounded off to a 172 // region. In each allocation region the allocations are rounded off to a
236 // fixed quantum, so the excess region will not be resident. 173 // fixed quantum, so the excess region will not be resident.
237 // See crrev.com/1531463004 for detailed explanation. 174 // See crrev.com/1531463004 for detailed explanation.
238 resident_size = stats.max_size_in_use; 175 resident_size = stats.max_size_in_use;
239 #elif defined(OS_WIN) 176 #elif defined(OS_WIN)
240 WinHeapInfo all_heap_info = {}; 177 WinHeapInfo main_heap_info = {};
241 WinHeapMemoryDumpImpl(&all_heap_info); 178 WinHeapMemoryDumpImpl(&main_heap_info);
242 total_virtual_size = 179 total_virtual_size =
243 all_heap_info.committed_size + all_heap_info.uncommitted_size; 180 main_heap_info.committed_size + main_heap_info.uncommitted_size;
244 // Resident size is approximated with committed heap size. Note that it is 181 // Resident size is approximated with committed heap size. Note that it is
245 // possible to do this with better accuracy on windows by intersecting the 182 // possible to do this with better accuracy on windows by intersecting the
246 // working set with the virtual memory ranges occuipied by the heap. It's not 183 // working set with the virtual memory ranges occuipied by the heap. It's not
247 // clear that this is worth it, as it's fairly expensive to do. 184 // clear that this is worth it, as it's fairly expensive to do.
248 resident_size = all_heap_info.committed_size; 185 resident_size = main_heap_info.committed_size;
249 allocated_objects_size = all_heap_info.allocated_size; 186 allocated_objects_size = main_heap_info.allocated_size;
250 allocated_objects_count = all_heap_info.block_count; 187 allocated_objects_count = main_heap_info.block_count;
251 #else 188 #else
252 struct mallinfo info = mallinfo(); 189 struct mallinfo info = mallinfo();
253 DCHECK_GE(info.arena + info.hblkhd, info.uordblks); 190 DCHECK_GE(info.arena + info.hblkhd, info.uordblks);
254 191
255 // In case of Android's jemalloc |arena| is 0 and the outer pages size is 192 // In case of Android's jemalloc |arena| is 0 and the outer pages size is
256 // reported by |hblkhd|. In case of dlmalloc the total is given by 193 // reported by |hblkhd|. In case of dlmalloc the total is given by
257 // |arena| + |hblkhd|. For more details see link: http://goo.gl/fMR8lF. 194 // |arena| + |hblkhd|. For more details see link: http://goo.gl/fMR8lF.
258 total_virtual_size = info.arena + info.hblkhd; 195 total_virtual_size = info.arena + info.hblkhd;
259 resident_size = info.uordblks; 196 resident_size = info.uordblks;
260 197
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 tid_dumping_heap_ == PlatformThread::CurrentId()) 311 tid_dumping_heap_ == PlatformThread::CurrentId())
375 return; 312 return;
376 AutoLock lock(allocation_register_lock_); 313 AutoLock lock(allocation_register_lock_);
377 if (!allocation_register_) 314 if (!allocation_register_)
378 return; 315 return;
379 allocation_register_->Remove(address); 316 allocation_register_->Remove(address);
380 } 317 }
381 318
382 } // namespace trace_event 319 } // namespace trace_event
383 } // namespace base 320 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698