Revert of Revert of Roll src/third_party/skia 8bcc7a0:724ae28 (patchset #1 id:1 of...
[chromium-blink-merge.git] / base / trace_event / winheap_dump_provider_win.cc
blobc8921f445a6850b03fae822f33836600c5d670e4
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.
5 #include "base/trace_event/winheap_dump_provider_win.h"
7 #include <windows.h>
9 #include "base/debug/profiler.h"
10 #include "base/trace_event/process_memory_dump.h"
11 #include "base/win/windows_version.h"
13 namespace base {
14 namespace trace_event {
16 namespace {
18 // Report a heap dump to a process memory dump. The |heap_info| structure
19 // contains the information about this heap, and |dump_absolute_name| will be
20 // used to represent it in the report.
21 void ReportHeapDump(ProcessMemoryDump* pmd,
22 const WinHeapInfo& heap_info,
23 const std::string& dump_absolute_name) {
24 MemoryAllocatorDump* outer_dump =
25 pmd->CreateAllocatorDump(dump_absolute_name);
26 outer_dump->AddScalar(MemoryAllocatorDump::kNameSize,
27 MemoryAllocatorDump::kUnitsBytes,
28 heap_info.committed_size);
30 MemoryAllocatorDump* inner_dump =
31 pmd->CreateAllocatorDump(dump_absolute_name + "/allocated_objects");
32 inner_dump->AddScalar(MemoryAllocatorDump::kNameSize,
33 MemoryAllocatorDump::kUnitsBytes,
34 heap_info.allocated_size);
35 inner_dump->AddScalar(MemoryAllocatorDump::kNameObjectsCount,
36 MemoryAllocatorDump::kUnitsObjects,
37 heap_info.block_count);
40 } // namespace
42 WinHeapDumpProvider* WinHeapDumpProvider::GetInstance() {
43 return Singleton<WinHeapDumpProvider,
44 LeakySingletonTraits<WinHeapDumpProvider>>::get();
47 bool WinHeapDumpProvider::OnMemoryDump(ProcessMemoryDump* pmd) {
48 // This method might be flaky for 2 reasons:
49 // - GetProcessHeaps is racy by design. It returns a snapshot of the
50 // available heaps, but there's no guarantee that that snapshot remains
51 // valid. If a heap disappears between GetProcessHeaps() and HeapWalk()
52 // then chaos should be assumed. This flakyness is acceptable for tracing.
53 // - The MSDN page for HeapLock says: "If the HeapLock function is called on
54 // a heap created with the HEAP_NO_SERIALIZATION flag, the results are
55 // undefined.". This is a problem on Windows XP where some system DLLs are
56 // known for creating heaps with this particular flag. For this reason
57 // this function should be disabled on XP.
59 // See https://crbug.com/487291 for more details about this.
60 if (base::win::GetVersion() < base::win::VERSION_VISTA)
61 return false;
63 // Disable this dump provider for the SyzyASan instrumented build
64 // because they don't support the heap walking functions yet.
65 #if defined(SYZYASAN)
66 if (base::debug::IsBinaryInstrumented())
67 return false;
68 #endif
70 // Retrieves the number of heaps in the current process.
71 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL);
72 WinHeapInfo all_heap_info = {0};
74 // Try to retrieve a handle to all the heaps owned by this process. Returns
75 // false if the number of heaps has changed.
77 // This is inherently racy as is, but it's not something that we observe a lot
78 // in Chrome, the heaps tend to be created at startup only.
79 scoped_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]);
80 if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps)
81 return false;
83 // Skip the pointer to the heap array to avoid accounting the memory used by
84 // this dump provider.
85 std::set<void*> block_to_skip;
86 block_to_skip.insert(all_heaps.get());
88 // Retrieves some metrics about each heap.
89 for (size_t i = 0; i < number_of_heaps; ++i) {
90 WinHeapInfo heap_info = {0};
91 heap_info.heap_id = all_heaps[i];
92 GetHeapInformation(&heap_info, block_to_skip);
94 all_heap_info.allocated_size += heap_info.allocated_size;
95 all_heap_info.committed_size += heap_info.committed_size;
96 all_heap_info.block_count += heap_info.block_count;
98 // Report the heap dump.
99 ReportHeapDump(pmd, all_heap_info, "winheap");
100 return true;
103 bool WinHeapDumpProvider::GetHeapInformation(
104 WinHeapInfo* heap_info,
105 const std::set<void*>& block_to_skip) {
106 CHECK(::HeapLock(heap_info->heap_id) == TRUE);
107 PROCESS_HEAP_ENTRY heap_entry;
108 heap_entry.lpData = nullptr;
109 // Walk over all the entries in this heap.
110 while (::HeapWalk(heap_info->heap_id, &heap_entry) != FALSE) {
111 if (block_to_skip.count(heap_entry.lpData) == 1)
112 continue;
113 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
114 heap_info->allocated_size += heap_entry.cbData;
115 heap_info->block_count++;
116 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) {
117 heap_info->committed_size += heap_entry.Region.dwCommittedSize;
120 CHECK(::HeapUnlock(heap_info->heap_id) == TRUE);
121 return true;
124 } // namespace trace_event
125 } // namespace base