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

Unified Diff: base/trace_event/winheap_dump_provider_win.cc

Issue 1100173002: Add a WinHeap dump provider to the memory profiler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: base/trace_event/winheap_dump_provider_win.cc
diff --git a/base/trace_event/winheap_dump_provider_win.cc b/base/trace_event/winheap_dump_provider_win.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ab0f62a0539ee62fcd867804dedd02f18b47ce58
--- /dev/null
+++ b/base/trace_event/winheap_dump_provider_win.cc
@@ -0,0 +1,102 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/trace_event/winheap_dump_provider.h"
+
+#include <windows.h>
+
+#include "base/trace_event/process_memory_dump.h"
+
+namespace base {
+namespace trace_event {
+
+namespace {
+
+const char kDumperFriendlyName[] = "winheap";
+
+// Report a heap dump to a process memory dump. The |heap_info| structure
+// contains the information about this heap, and |heap_name| will be used to
+// represent it in the report.
+bool ReportHeapDump(ProcessMemoryDump* pmd, const WinHeapInfo& heap_info,
+ const std::string& heap_name) {
+ 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
+ MemoryAllocatorDump* dump =
+ pmd->CreateAllocatorDump(kDumperFriendlyName, heap_name);
+ if (!dump)
+ return false;
+ dump->set_physical_size_in_bytes(heap_info.committed_size);
+ dump->set_allocated_objects_count(heap_info.block_count);
+ dump->set_allocated_objects_size_in_bytes(heap_info.allocated_size);
+ return true;
+}
+
+} // namespace
+
chrisha 2015/04/23 09:23:28 Remove extra blank line.
Sébastien Marchand 2015/04/23 10:52:03 Done.
+
+WinHeapDumpProvider* WinHeapDumpProvider::GetInstance() {
+ return Singleton<WinHeapDumpProvider,
+ LeakySingletonTraits<WinHeapDumpProvider>>::get();
+}
+
+bool WinHeapDumpProvider::DumpInto(ProcessMemoryDump* pmd) {
+ 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.
+
+ // Retrieves the number of heaps in the current process.
+ DWORD number_of_heaps = ::GetProcessHeaps(0, NULL);
+ WinHeapInfo all_heap_info = { 0 };
+
+ // Try to retrieve a handle to all the heaps owned by this process. Returns
+ // false if the number of heaps has changed.
+ scoped_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]);
+ if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps)
+ 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
+
+ // Skip the pointer to the heap array.
+ std::set<void*> block_to_skip;
+ 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.
+
+ // 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.
+ for (size_t i = 0; i < number_of_heaps; ++i) {
+ WinHeapInfo heap_info = { 0 };
+ heap_info.heap_id = all_heaps[i];
+ GetHeapInformation(&heap_info, block_to_skip);
+
+ 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
+ all_heap_info.committed_size += heap_info.committed_size;
+ all_heap_info.block_count += heap_info.block_count;
+ }
+ // Report the heap dump.
+ if (!ReportHeapDump(pmd, all_heap_info, MemoryAllocatorDump::kRootHeap))
+ return false;
+
+ return true;
+}
+
+const char* WinHeapDumpProvider::GetFriendlyName() const {
+ return kDumperFriendlyName;
+}
+
+bool WinHeapDumpProvider::GetHeapInformation(WinHeapInfo* heap_info,
+ const std::set<void*>& block_to_skip) {
+ DCHECK_NE(reinterpret_cast<WinHeapInfo*>(nullptr), heap_info);
+ CHECK(::HeapLock(heap_info->heap_id) == TRUE);
+ PROCESS_HEAP_ENTRY heap_entry;
+ heap_entry.lpData = nullptr;
+ // Walk over all the entries in this heap.
+ while (::HeapWalk(heap_info->heap_id, &heap_entry) != FALSE) {
+ 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.
+ continue;
+ if ((heap_entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
+ heap_info->allocated_size += heap_entry.cbData;
+ heap_info->block_count++;
+ } else if ((heap_entry.wFlags & PROCESS_HEAP_REGION) != 0) {
+ heap_info->committed_size += heap_entry.Region.dwCommittedSize;
+ }
+ }
+ CHECK(::HeapUnlock(heap_info->heap_id) == TRUE);
+ return true;
+}
+
+} // namespace trace_event
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698