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