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