Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / base / trace_event / winheap_dump_provider_win.cc
blobe1e9bcf4a7c454bbdfb1daf72b1c266c0eea7132
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.
5 #include "base/trace_event/winheap_dump_provider_win.h"
7 #include <windows.h>
9 #include "base/trace_event/process_memory_dump.h"
11 namespace base {
12 namespace trace_event {
14 namespace {
16 // 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 // used to represent it in the report.
19 bool ReportHeapDump(ProcessMemoryDump* pmd,
20 const WinHeapInfo& heap_info,
21 const std::string& dump_absolute_name) {
22 MemoryAllocatorDump* dump = pmd->CreateAllocatorDump(dump_absolute_name);
23 if (!dump)
24 return false;
25 dump->AddScalar(MemoryAllocatorDump::kNameOuterSize,
26 MemoryAllocatorDump::kUnitsBytes, heap_info.committed_size);
27 dump->AddScalar(MemoryAllocatorDump::kNameInnerSize,
28 MemoryAllocatorDump::kUnitsBytes, heap_info.allocated_size);
29 dump->AddScalar(MemoryAllocatorDump::kNameObjectsCount,
30 MemoryAllocatorDump::kUnitsObjects, heap_info.block_count);
31 return true;
34 } // namespace
36 WinHeapDumpProvider* WinHeapDumpProvider::GetInstance() {
37 return Singleton<WinHeapDumpProvider,
38 LeakySingletonTraits<WinHeapDumpProvider>>::get();
41 bool WinHeapDumpProvider::OnMemoryDump(ProcessMemoryDump* pmd) {
42 // Retrieves the number of heaps in the current process.
43 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL);
44 WinHeapInfo all_heap_info = {0};
46 // Try to retrieve a handle to all the heaps owned by this process. Returns
47 // false if the number of heaps has changed.
49 // 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.
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;
55 // Skip the pointer to the heap array to avoid accounting the memory used by
56 // this dump provider.
57 std::set<void*> block_to_skip;
58 block_to_skip.insert(all_heaps.get());
60 // Retrieves some metrics about each heap.
61 for (size_t i = 0; i < number_of_heaps; ++i) {
62 WinHeapInfo heap_info = {0};
63 heap_info.heap_id = all_heaps[i];
64 GetHeapInformation(&heap_info, block_to_skip);
66 all_heap_info.allocated_size += heap_info.allocated_size;
67 all_heap_info.committed_size += heap_info.committed_size;
68 all_heap_info.block_count += heap_info.block_count;
70 // Report the heap dump.
71 if (!ReportHeapDump(pmd, all_heap_info, "winheap"))
72 return false;
74 return true;
77 bool WinHeapDumpProvider::GetHeapInformation(
78 WinHeapInfo* heap_info,
79 const std::set<void*>& block_to_skip) {
80 CHECK(::HeapLock(heap_info->heap_id) == TRUE);
81 PROCESS_HEAP_ENTRY heap_entry;
82 heap_entry.lpData = nullptr;
83 // Walk over all the entries in this heap.
84 while (::HeapWalk(heap_info->heap_id, &heap_entry) != FALSE) {
85 if (block_to_skip.count(heap_entry.lpData) == 1)
86 continue;
87 if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
88 heap_info->allocated_size += heap_entry.cbData;
89 heap_info->block_count++;
90 } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) {
91 heap_info->committed_size += heap_entry.Region.dwCommittedSize;
94 CHECK(::HeapUnlock(heap_info->heap_id) == TRUE);
95 return true;
98 } // namespace trace_event
99 } // namespace base