Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: base/trace_event/winheap_dump_provider_win.cc

Issue 1180693002: Update from https://crrev.com/333737 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/winheap_dump_provider_win.h" 5 #include "base/trace_event/winheap_dump_provider_win.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 8
9 #include "base/trace_event/process_memory_dump.h" 9 #include "base/trace_event/process_memory_dump.h"
10 #include "base/win/windows_version.h"
10 11
11 namespace base { 12 namespace base {
12 namespace trace_event { 13 namespace trace_event {
13 14
14 namespace { 15 namespace {
15 16
16 // Report a heap dump to a process memory dump. The |heap_info| structure 17 // Report a heap dump to a process memory dump. The |heap_info| structure
17 // contains the information about this heap, and |dump_absolute_name| will be 18 // contains the information about this heap, and |dump_absolute_name| will be
18 // used to represent it in the report. 19 // used to represent it in the report.
19 bool ReportHeapDump(ProcessMemoryDump* pmd, 20 void ReportHeapDump(ProcessMemoryDump* pmd,
20 const WinHeapInfo& heap_info, 21 const WinHeapInfo& heap_info,
21 const std::string& dump_absolute_name) { 22 const std::string& dump_absolute_name) {
22 MemoryAllocatorDump* dump = pmd->CreateAllocatorDump(dump_absolute_name); 23 MemoryAllocatorDump* outer_dump =
23 if (!dump) 24 pmd->CreateAllocatorDump(dump_absolute_name);
24 return false; 25 outer_dump->AddScalar(MemoryAllocatorDump::kNameSize,
25 dump->AddScalar(MemoryAllocatorDump::kNameOuterSize, 26 MemoryAllocatorDump::kUnitsBytes,
26 MemoryAllocatorDump::kUnitsBytes, heap_info.committed_size); 27 heap_info.committed_size);
27 dump->AddScalar(MemoryAllocatorDump::kNameInnerSize, 28
28 MemoryAllocatorDump::kUnitsBytes, heap_info.allocated_size); 29 MemoryAllocatorDump* inner_dump =
29 dump->AddScalar(MemoryAllocatorDump::kNameObjectsCount, 30 pmd->CreateAllocatorDump(dump_absolute_name + "/allocated_objects");
30 MemoryAllocatorDump::kUnitsObjects, heap_info.block_count); 31 inner_dump->AddScalar(MemoryAllocatorDump::kNameSize,
31 return true; 32 MemoryAllocatorDump::kUnitsBytes,
33 heap_info.allocated_size);
34 inner_dump->AddScalar(MemoryAllocatorDump::kNameObjectsCount,
35 MemoryAllocatorDump::kUnitsObjects,
36 heap_info.block_count);
32 } 37 }
33 38
34 } // namespace 39 } // namespace
35 40
36 WinHeapDumpProvider* WinHeapDumpProvider::GetInstance() { 41 WinHeapDumpProvider* WinHeapDumpProvider::GetInstance() {
37 return Singleton<WinHeapDumpProvider, 42 return Singleton<WinHeapDumpProvider,
38 LeakySingletonTraits<WinHeapDumpProvider>>::get(); 43 LeakySingletonTraits<WinHeapDumpProvider>>::get();
39 } 44 }
40 45
41 bool WinHeapDumpProvider::OnMemoryDump(ProcessMemoryDump* pmd) { 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
42 // Retrieves the number of heaps in the current process. 62 // Retrieves the number of heaps in the current process.
43 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL); 63 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL);
44 WinHeapInfo all_heap_info = {0}; 64 WinHeapInfo all_heap_info = {0};
45 65
46 // Try to retrieve a handle to all the heaps owned by this process. Returns 66 // Try to retrieve a handle to all the heaps owned by this process. Returns
47 // false if the number of heaps has changed. 67 // false if the number of heaps has changed.
48 // 68 //
49 // This is inherently racy as is, but it's not something that we observe a lot 69 // This is inherently racy as is, but it's not something that we observe a lot
50 // in Chrome, the heaps tend to be created at startup only. 70 // in Chrome, the heaps tend to be created at startup only.
51 scoped_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]); 71 scoped_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]);
52 if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps) 72 if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps)
53 return false; 73 return false;
54 74
55 // Skip the pointer to the heap array to avoid accounting the memory used by 75 // Skip the pointer to the heap array to avoid accounting the memory used by
56 // this dump provider. 76 // this dump provider.
57 std::set<void*> block_to_skip; 77 std::set<void*> block_to_skip;
58 block_to_skip.insert(all_heaps.get()); 78 block_to_skip.insert(all_heaps.get());
59 79
60 // Retrieves some metrics about each heap. 80 // Retrieves some metrics about each heap.
61 for (size_t i = 0; i < number_of_heaps; ++i) { 81 for (size_t i = 0; i < number_of_heaps; ++i) {
62 WinHeapInfo heap_info = {0}; 82 WinHeapInfo heap_info = {0};
63 heap_info.heap_id = all_heaps[i]; 83 heap_info.heap_id = all_heaps[i];
64 GetHeapInformation(&heap_info, block_to_skip); 84 GetHeapInformation(&heap_info, block_to_skip);
65 85
66 all_heap_info.allocated_size += heap_info.allocated_size; 86 all_heap_info.allocated_size += heap_info.allocated_size;
67 all_heap_info.committed_size += heap_info.committed_size; 87 all_heap_info.committed_size += heap_info.committed_size;
68 all_heap_info.block_count += heap_info.block_count; 88 all_heap_info.block_count += heap_info.block_count;
69 } 89 }
70 // Report the heap dump. 90 // Report the heap dump.
71 if (!ReportHeapDump(pmd, all_heap_info, "winheap")) 91 ReportHeapDump(pmd, all_heap_info, "winheap");
72 return false;
73
74 return true; 92 return true;
75 } 93 }
76 94
77 bool WinHeapDumpProvider::GetHeapInformation( 95 bool WinHeapDumpProvider::GetHeapInformation(
78 WinHeapInfo* heap_info, 96 WinHeapInfo* heap_info,
79 const std::set<void*>& block_to_skip) { 97 const std::set<void*>& block_to_skip) {
80 CHECK(::HeapLock(heap_info->heap_id) == TRUE); 98 CHECK(::HeapLock(heap_info->heap_id) == TRUE);
81 PROCESS_HEAP_ENTRY heap_entry; 99 PROCESS_HEAP_ENTRY heap_entry;
82 heap_entry.lpData = nullptr; 100 heap_entry.lpData = nullptr;
83 // Walk over all the entries in this heap. 101 // Walk over all the entries in this heap.
84 while (::HeapWalk(heap_info->heap_id, &heap_entry) != FALSE) { 102 while (::HeapWalk(heap_info->heap_id, &heap_entry) != FALSE) {
85 if (block_to_skip.count(heap_entry.lpData) == 1) 103 if (block_to_skip.count(heap_entry.lpData) == 1)
86 continue; 104 continue;
87 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) { 105 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
88 heap_info->allocated_size += heap_entry.cbData; 106 heap_info->allocated_size += heap_entry.cbData;
89 heap_info->block_count++; 107 heap_info->block_count++;
90 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) { 108 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) {
91 heap_info->committed_size += heap_entry.Region.dwCommittedSize; 109 heap_info->committed_size += heap_entry.Region.dwCommittedSize;
92 } 110 }
93 } 111 }
94 CHECK(::HeapUnlock(heap_info->heap_id) == TRUE); 112 CHECK(::HeapUnlock(heap_info->heap_id) == TRUE);
95 return true; 113 return true;
96 } 114 }
97 115
98 } // namespace trace_event 116 } // namespace trace_event
99 } // namespace base 117 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/trace_event_unittest.cc ('k') | base/trace_event/winheap_dump_provider_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698