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 | |
11 namespace base { | |
12 namespace trace_event { | |
13 | |
14 namespace { | |
15 | |
16 const char kDumperFriendlyName[] = "winheap"; | |
17 | |
18 // Report a heap dump to a process memory dump. The |heap_info| structure | |
19 // contains the information about this heap, and |heap_name| will be used to | |
20 // represent it in the report. | |
21 bool ReportHeapDump(ProcessMemoryDump* pmd, | |
22 const WinHeapInfo& heap_info, | |
23 const std::string& heap_name) { | |
24 MemoryAllocatorDump* dump = | |
25 pmd->CreateAllocatorDump(kDumperFriendlyName, heap_name); | |
26 if (!dump) | |
27 return false; | |
28 dump->set_physical_size_in_bytes(heap_info.committed_size); | |
29 dump->set_allocated_objects_count(heap_info.block_count); | |
30 dump->set_allocated_objects_size_in_bytes(heap_info.allocated_size); | |
31 return true; | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 WinHeapDumpProvider* WinHeapDumpProvider::GetInstance() { | |
37 return Singleton<WinHeapDumpProvider, | |
38 LeakySingletonTraits<WinHeapDumpProvider>>::get(); | |
39 } | |
40 | |
41 bool WinHeapDumpProvider::DumpInto(ProcessMemoryDump* pmd) { | |
42 DCHECK_NE(reinterpret_cast<ProcessMemoryDump*>(nullptr), pmd); | |
Reid Kleckner
2015/04/23 18:27:37
Clang rejects this line with this error:
..\..\bas
Sébastien Marchand
2015/04/24 09:03:50
Done.
| |
43 | |
44 // Retrieves the number of heaps in the current process. | |
45 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL); | |
46 WinHeapInfo all_heap_info = {0}; | |
47 | |
48 // Try to retrieve a handle to all the heaps owned by this process. Returns | |
49 // false if the number of heaps has changed. | |
50 // | |
51 // This is inherently racy as is, but it's not something that we observe a lot | |
52 // in Chrome, the heaps tend to be created at startup only. | |
53 scoped_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]); | |
54 if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps) | |
55 return false; | |
56 | |
57 // Skip the pointer to the heap array to avoid accounting the memory used by | |
58 // this dump provider. | |
59 std::set<void*> block_to_skip; | |
60 block_to_skip.insert(all_heaps.get()); | |
61 | |
62 // Retrieves some metrics about each heap. | |
63 for (size_t i = 0; i < number_of_heaps; ++i) { | |
64 WinHeapInfo heap_info = {0}; | |
65 heap_info.heap_id = all_heaps[i]; | |
66 GetHeapInformation(&heap_info, block_to_skip); | |
67 | |
68 all_heap_info.allocated_size += heap_info.allocated_size; | |
69 all_heap_info.committed_size += heap_info.committed_size; | |
70 all_heap_info.block_count += heap_info.block_count; | |
71 } | |
72 // Report the heap dump. | |
73 if (!ReportHeapDump(pmd, all_heap_info, MemoryAllocatorDump::kRootHeap)) | |
74 return false; | |
75 | |
76 return true; | |
77 } | |
78 | |
79 const char* WinHeapDumpProvider::GetFriendlyName() const { | |
80 return kDumperFriendlyName; | |
81 } | |
82 | |
83 bool WinHeapDumpProvider::GetHeapInformation( | |
84 WinHeapInfo* heap_info, | |
85 const std::set<void*>& block_to_skip) { | |
86 CHECK(::HeapLock(heap_info->heap_id) == TRUE); | |
87 PROCESS_HEAP_ENTRY heap_entry; | |
88 heap_entry.lpData = nullptr; | |
89 // Walk over all the entries in this heap. | |
90 while (::HeapWalk(heap_info->heap_id, &heap_entry) != FALSE) { | |
91 if (block_to_skip.count(heap_entry.lpData) == 1) | |
92 continue; | |
93 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) { | |
94 heap_info->allocated_size += heap_entry.cbData; | |
95 heap_info->block_count++; | |
96 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) { | |
97 heap_info->committed_size += heap_entry.Region.dwCommittedSize; | |
98 } | |
99 } | |
100 CHECK(::HeapUnlock(heap_info->heap_id) == TRUE); | |
101 return true; | |
102 } | |
103 | |
104 } // namespace trace_event | |
105 } // namespace base | |
OLD | NEW |