Chromium Code Reviews| 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.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, const WinHeapInfo& heap_info, | |
| 22 const std::string& heap_name) { | |
| 23 DCHECK_NE(reinterpret_cast<ProcessMemoryDump*>(nullptr), pmd); | |
|
Primiano Tucci (use gerrit)
2015/04/22 23:48:32
This would never happen, and even if it happens, A
Sébastien Marchand
2015/04/23 10:52:03
One more difference between my codebase (Syzygy) a
| |
| 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 | |
|
chrisha
2015/04/23 09:23:28
Remove extra blank line.
Sébastien Marchand
2015/04/23 10:52:03
Done.
| |
| 36 | |
| 37 WinHeapDumpProvider* WinHeapDumpProvider::GetInstance() { | |
| 38 return Singleton<WinHeapDumpProvider, | |
| 39 LeakySingletonTraits<WinHeapDumpProvider>>::get(); | |
| 40 } | |
| 41 | |
| 42 bool WinHeapDumpProvider::DumpInto(ProcessMemoryDump* pmd) { | |
| 43 DCHECK_NE(reinterpret_cast<ProcessMemoryDump*>(nullptr), pmd); | |
|
Primiano Tucci (use gerrit)
2015/04/22 23:48:32
ditto
Sébastien Marchand
2015/04/23 10:52:04
Done.
| |
| 44 | |
| 45 // Retrieves the number of heaps in the current process. | |
| 46 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL); | |
| 47 WinHeapInfo all_heap_info = { 0 }; | |
| 48 | |
| 49 // Try to retrieve a handle to all the heaps owned by this process. Returns | |
| 50 // false if the number of heaps has changed. | |
| 51 scoped_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]); | |
| 52 if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps) | |
| 53 return false; | |
|
chrisha
2015/04/23 09:23:28
A very minor thing, but this kind of racy 'get cou
Sébastien Marchand
2015/04/23 10:52:03
Yeah, in Chrome all the heaps get created at start
| |
| 54 | |
| 55 // Skip the pointer to the heap array. | |
| 56 std::set<void*> block_to_skip; | |
| 57 block_to_skip.insert(all_heaps.get()); | |
|
Primiano Tucci (use gerrit)
2015/04/22 23:48:32
Can you add just a comment to explain why you are
chrisha
2015/04/23 09:23:28
From my reading it looks like Seb is trying not to
Sébastien Marchand
2015/04/23 10:52:04
Yeah, my goal is to doesn't include the memory use
Sébastien Marchand
2015/04/23 10:52:04
Acknowledged.
| |
| 58 | |
| 59 // Retrieves some metrics about each heaps. | |
|
chrisha
2015/04/23 09:23:28
each heap*.
Sébastien Marchand
2015/04/23 10:52:04
Done.
| |
| 60 for (size_t i = 0; i < number_of_heaps; ++i) { | |
| 61 WinHeapInfo heap_info = { 0 }; | |
| 62 heap_info.heap_id = all_heaps[i]; | |
| 63 GetHeapInformation(&heap_info, block_to_skip); | |
| 64 | |
| 65 all_heap_info.allocated_size += heap_info.allocated_size; | |
|
Primiano Tucci (use gerrit)
2015/04/22 23:48:32
If you want you can ReportHeapDump the heaps invid
chrisha
2015/04/23 09:23:28
Yeah, it would be good to report them using names
Sébastien Marchand
2015/04/23 10:52:03
If it's not there yet then I'd prefer to stick wit
Sébastien Marchand
2015/04/23 10:52:04
I'm planning to report all the heaps individually,
Primiano Tucci (use gerrit)
2015/04/23 11:10:02
Yeah, makes sense.
Maybe add a todo or file a bug
| |
| 66 all_heap_info.committed_size += heap_info.committed_size; | |
| 67 all_heap_info.block_count += heap_info.block_count; | |
| 68 } | |
| 69 // Report the heap dump. | |
| 70 if (!ReportHeapDump(pmd, all_heap_info, MemoryAllocatorDump::kRootHeap)) | |
| 71 return false; | |
| 72 | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 const char* WinHeapDumpProvider::GetFriendlyName() const { | |
| 77 return kDumperFriendlyName; | |
| 78 } | |
| 79 | |
| 80 bool WinHeapDumpProvider::GetHeapInformation(WinHeapInfo* heap_info, | |
| 81 const std::set<void*>& block_to_skip) { | |
| 82 DCHECK_NE(reinterpret_cast<WinHeapInfo*>(nullptr), heap_info); | |
| 83 CHECK(::HeapLock(heap_info->heap_id) == TRUE); | |
| 84 PROCESS_HEAP_ENTRY heap_entry; | |
| 85 heap_entry.lpData = nullptr; | |
| 86 // Walk over all the entries in this heap. | |
| 87 while (::HeapWalk(heap_info->heap_id, &heap_entry) != FALSE) { | |
| 88 if (block_to_skip.find(heap_entry.lpData) != block_to_skip.end()) | |
|
Primiano Tucci (use gerrit)
2015/04/22 23:48:32
You can do if (block_to_skip.count(heap_entry.lpDa
Sébastien Marchand
2015/04/23 10:52:03
Done.
| |
| 89 continue; | |
| 90 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) { | |
| 91 heap_info->allocated_size += heap_entry.cbData; | |
| 92 heap_info->block_count++; | |
| 93 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) { | |
| 94 heap_info->committed_size += heap_entry.Region.dwCommittedSize; | |
| 95 } | |
| 96 } | |
| 97 CHECK(::HeapUnlock(heap_info->heap_id) == TRUE); | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 } // namespace trace_event | |
| 102 } // namespace base | |
| OLD | NEW |