Add ICU message format support
[chromium-blink-merge.git] / base / trace_event / winheap_dump_provider_win.cc
blob8f910b3ce15d69bc797e992ffc148b453b9d3561
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/strings/string_util.h"
11 #include "base/trace_event/process_memory_dump.h"
12 #include "base/win/windows_version.h"
14 namespace base {
15 namespace trace_event {
17 #define DUMP_ROOT_NAME "winheap"
18 // static
19 const char WinHeapDumpProvider::kAllocatedObjects[] =
20 DUMP_ROOT_NAME "/allocated_objects";
22 namespace {
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);
43 } // namespace
45 WinHeapDumpProvider* WinHeapDumpProvider::GetInstance() {
46 return Singleton<WinHeapDumpProvider,
47 LeakySingletonTraits<WinHeapDumpProvider>>::get();
50 bool WinHeapDumpProvider::OnMemoryDump(const MemoryDumpArgs& args,
51 ProcessMemoryDump* pmd) {
52 // This method might be flaky for 2 reasons:
53 // - GetProcessHeaps is racy by design. It returns a snapshot of the
54 // available heaps, but there's no guarantee that that snapshot remains
55 // valid. If a heap disappears between GetProcessHeaps() and HeapWalk()
56 // then chaos should be assumed. This flakyness is acceptable for tracing.
57 // - The MSDN page for HeapLock says: "If the HeapLock function is called on
58 // a heap created with the HEAP_NO_SERIALIZATION flag, the results are
59 // undefined.". This is a problem on Windows XP where some system DLLs are
60 // known for creating heaps with this particular flag. For this reason
61 // this function should be disabled on XP.
63 // See https://crbug.com/487291 for more details about this.
64 if (base::win::GetVersion() < base::win::VERSION_VISTA)
65 return false;
67 // Disable this dump provider for the SyzyASan instrumented build
68 // because they don't support the heap walking functions yet.
69 #if defined(SYZYASAN)
70 if (base::debug::IsBinaryInstrumented())
71 return false;
72 #endif
74 // Retrieves the number of heaps in the current process.
75 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL);
76 WinHeapInfo all_heap_info = {0};
78 // Try to retrieve a handle to all the heaps owned by this process. Returns
79 // false if the number of heaps has changed.
81 // This is inherently racy as is, but it's not something that we observe a lot
82 // in Chrome, the heaps tend to be created at startup only.
83 scoped_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]);
84 if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps)
85 return false;
87 // Skip the pointer to the heap array to avoid accounting the memory used by
88 // this dump provider.
89 std::set<void*> block_to_skip;
90 block_to_skip.insert(all_heaps.get());
92 // Retrieves some metrics about each heap.
93 for (size_t i = 0; i < number_of_heaps; ++i) {
94 WinHeapInfo heap_info = {0};
95 heap_info.heap_id = all_heaps[i];
96 GetHeapInformation(&heap_info, block_to_skip);
98 all_heap_info.allocated_size += heap_info.allocated_size;
99 all_heap_info.committed_size += heap_info.committed_size;
100 all_heap_info.block_count += heap_info.block_count;
102 // Report the heap dump.
103 ReportHeapDump(pmd, all_heap_info);
104 return true;
107 bool WinHeapDumpProvider::GetHeapInformation(
108 WinHeapInfo* heap_info,
109 const std::set<void*>& block_to_skip) {
110 CHECK(::HeapLock(heap_info->heap_id) == TRUE);
111 PROCESS_HEAP_ENTRY heap_entry;
112 heap_entry.lpData = nullptr;
113 // Walk over all the entries in this heap.
114 while (::HeapWalk(heap_info->heap_id, &heap_entry) != FALSE) {
115 if (block_to_skip.count(heap_entry.lpData) == 1)
116 continue;
117 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
118 heap_info->allocated_size += heap_entry.cbData;
119 heap_info->block_count++;
120 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) {
121 heap_info->committed_size += heap_entry.Region.dwCommittedSize;
124 CHECK(::HeapUnlock(heap_info->heap_id) == TRUE);
125 return true;
128 } // namespace trace_event
129 } // namespace base