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