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"
9 #include "base/debug/profiler.h"
10 #include "base/strings/string_util.h"
11 #include "base/trace_event/process_memory_dump.h"
12 #include "base/win/windows_version.h"
15 namespace trace_event
{
17 #define DUMP_ROOT_NAME "winheap"
19 const char WinHeapDumpProvider::kAllocatedObjects
[] =
20 DUMP_ROOT_NAME
"/allocated_objects";
24 // Report a heap dump to a process memory dump. The |heap_info| structure
25 // contains the information about this heap, and |dump_absolute_name| will be
26 // used to represent it in the report.
27 void ReportHeapDump(ProcessMemoryDump
* pmd
, const WinHeapInfo
& heap_info
) {
28 MemoryAllocatorDump
* outer_dump
= pmd
->CreateAllocatorDump(DUMP_ROOT_NAME
);
29 outer_dump
->AddScalar(MemoryAllocatorDump::kNameSize
,
30 MemoryAllocatorDump::kUnitsBytes
,
31 heap_info
.committed_size
);
33 MemoryAllocatorDump
* inner_dump
=
34 pmd
->CreateAllocatorDump(WinHeapDumpProvider::kAllocatedObjects
);
35 inner_dump
->AddScalar(MemoryAllocatorDump::kNameSize
,
36 MemoryAllocatorDump::kUnitsBytes
,
37 heap_info
.allocated_size
);
38 inner_dump
->AddScalar(MemoryAllocatorDump::kNameObjectsCount
,
39 MemoryAllocatorDump::kUnitsObjects
,
40 heap_info
.block_count
);
45 WinHeapDumpProvider
* WinHeapDumpProvider::GetInstance() {
46 return Singleton
<WinHeapDumpProvider
,
47 LeakySingletonTraits
<WinHeapDumpProvider
>>::get();
50 bool WinHeapDumpProvider::OnMemoryDump(ProcessMemoryDump
* pmd
) {
51 // This method might be flaky for 2 reasons:
52 // - GetProcessHeaps is racy by design. It returns a snapshot of the
53 // available heaps, but there's no guarantee that that snapshot remains
54 // valid. If a heap disappears between GetProcessHeaps() and HeapWalk()
55 // then chaos should be assumed. This flakyness is acceptable for tracing.
56 // - The MSDN page for HeapLock says: "If the HeapLock function is called on
57 // a heap created with the HEAP_NO_SERIALIZATION flag, the results are
58 // undefined.". This is a problem on Windows XP where some system DLLs are
59 // known for creating heaps with this particular flag. For this reason
60 // this function should be disabled on XP.
62 // See https://crbug.com/487291 for more details about this.
63 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
66 // Disable this dump provider for the SyzyASan instrumented build
67 // because they don't support the heap walking functions yet.
69 if (base::debug::IsBinaryInstrumented())
73 // Retrieves the number of heaps in the current process.
74 DWORD number_of_heaps
= ::GetProcessHeaps(0, NULL
);
75 WinHeapInfo all_heap_info
= {0};
77 // Try to retrieve a handle to all the heaps owned by this process. Returns
78 // false if the number of heaps has changed.
80 // This is inherently racy as is, but it's not something that we observe a lot
81 // in Chrome, the heaps tend to be created at startup only.
82 scoped_ptr
<HANDLE
[]> all_heaps(new HANDLE
[number_of_heaps
]);
83 if (::GetProcessHeaps(number_of_heaps
, all_heaps
.get()) != number_of_heaps
)
86 // Skip the pointer to the heap array to avoid accounting the memory used by
87 // this dump provider.
88 std::set
<void*> block_to_skip
;
89 block_to_skip
.insert(all_heaps
.get());
91 // Retrieves some metrics about each heap.
92 for (size_t i
= 0; i
< number_of_heaps
; ++i
) {
93 WinHeapInfo heap_info
= {0};
94 heap_info
.heap_id
= all_heaps
[i
];
95 GetHeapInformation(&heap_info
, block_to_skip
);
97 all_heap_info
.allocated_size
+= heap_info
.allocated_size
;
98 all_heap_info
.committed_size
+= heap_info
.committed_size
;
99 all_heap_info
.block_count
+= heap_info
.block_count
;
101 // Report the heap dump.
102 ReportHeapDump(pmd
, all_heap_info
);
106 bool WinHeapDumpProvider::GetHeapInformation(
107 WinHeapInfo
* heap_info
,
108 const std::set
<void*>& block_to_skip
) {
109 CHECK(::HeapLock(heap_info
->heap_id
) == TRUE
);
110 PROCESS_HEAP_ENTRY heap_entry
;
111 heap_entry
.lpData
= nullptr;
112 // Walk over all the entries in this heap.
113 while (::HeapWalk(heap_info
->heap_id
, &heap_entry
) != FALSE
) {
114 if (block_to_skip
.count(heap_entry
.lpData
) == 1)
116 if ((heap_entry
.wFlags
& PROCESS_HEAP_ENTRY_BUSY
) != 0) {
117 heap_info
->allocated_size
+= heap_entry
.cbData
;
118 heap_info
->block_count
++;
119 } else if ((heap_entry
.wFlags
& PROCESS_HEAP_REGION
) != 0) {
120 heap_info
->committed_size
+= heap_entry
.Region
.dwCommittedSize
;
123 CHECK(::HeapUnlock(heap_info
->heap_id
) == TRUE
);
127 } // namespace trace_event