| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/child/web_memory_dump_provider_adapter.h" | 5 #include "content/child/web_memory_dump_provider_adapter.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/synchronization/lock.h" |
| 9 #include "base/trace_event/memory_profiler_allocation_context.h" |
| 10 #include "base/trace_event/memory_profiler_allocation_register.h" |
| 7 #include "content/child/web_process_memory_dump_impl.h" | 11 #include "content/child/web_process_memory_dump_impl.h" |
| 8 #include "third_party/WebKit/public/platform/WebMemoryDumpProvider.h" | 12 #include "third_party/WebKit/public/platform/WebMemoryDumpProvider.h" |
| 9 | 13 |
| 14 using namespace base; |
| 15 using namespace base::trace_event; |
| 16 |
| 17 namespace { |
| 18 |
| 19 AllocationRegister* g_allocation_register = nullptr; |
| 20 LazyInstance<Lock>::Leaky g_allocation_register_lock = |
| 21 LAZY_INSTANCE_INITIALIZER; |
| 22 |
| 23 void ReportAllocation(void* address, size_t size) { |
| 24 AllocationContext context = AllocationContextTracker::GetContextSnapshot(); |
| 25 AutoLock lock(g_allocation_register_lock.Get()); |
| 26 |
| 27 if (g_allocation_register) |
| 28 g_allocation_register->Insert(address, size, context); |
| 29 } |
| 30 |
| 31 void ReportFree(void* address) { |
| 32 AutoLock lock(g_allocation_register_lock.Get()); |
| 33 |
| 34 if (g_allocation_register) |
| 35 g_allocation_register->Remove(address); |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 10 namespace content { | 40 namespace content { |
| 11 | 41 |
| 12 WebMemoryDumpProviderAdapter::WebMemoryDumpProviderAdapter( | 42 WebMemoryDumpProviderAdapter::WebMemoryDumpProviderAdapter( |
| 13 blink::WebMemoryDumpProvider* wmdp) | 43 blink::WebMemoryDumpProvider* wmdp) |
| 14 : web_memory_dump_provider_(wmdp), is_registered_(false) { | 44 : web_memory_dump_provider_(wmdp), is_registered_(false) { |
| 15 } | 45 } |
| 16 | 46 |
| 17 WebMemoryDumpProviderAdapter::~WebMemoryDumpProviderAdapter() { | 47 WebMemoryDumpProviderAdapter::~WebMemoryDumpProviderAdapter() { |
| 18 DCHECK(!is_registered_); | 48 DCHECK(!is_registered_); |
| 19 } | 49 } |
| 20 | 50 |
| 21 bool WebMemoryDumpProviderAdapter::OnMemoryDump( | 51 bool WebMemoryDumpProviderAdapter::OnMemoryDump( |
| 22 const base::trace_event::MemoryDumpArgs& args, | 52 const base::trace_event::MemoryDumpArgs& args, |
| 23 base::trace_event::ProcessMemoryDump* pmd) { | 53 base::trace_event::ProcessMemoryDump* pmd) { |
| 24 blink::WebMemoryDumpLevelOfDetail level; | 54 blink::WebMemoryDumpLevelOfDetail level; |
| 25 switch (args.level_of_detail) { | 55 switch (args.level_of_detail) { |
| 26 case base::trace_event::MemoryDumpLevelOfDetail::LIGHT: | 56 case base::trace_event::MemoryDumpLevelOfDetail::LIGHT: |
| 27 level = blink::WebMemoryDumpLevelOfDetail::Light; | 57 level = blink::WebMemoryDumpLevelOfDetail::Light; |
| 28 break; | 58 break; |
| 29 case base::trace_event::MemoryDumpLevelOfDetail::DETAILED: | 59 case base::trace_event::MemoryDumpLevelOfDetail::DETAILED: |
| 30 level = blink::WebMemoryDumpLevelOfDetail::Detailed; | 60 level = blink::WebMemoryDumpLevelOfDetail::Detailed; |
| 31 break; | 61 break; |
| 32 default: | 62 default: |
| 33 NOTREACHED(); | 63 NOTREACHED(); |
| 34 return false; | 64 return false; |
| 35 } | 65 } |
| 36 WebProcessMemoryDumpImpl web_pmd_impl(args.level_of_detail, pmd); | 66 WebProcessMemoryDumpImpl web_pmd_impl(args.level_of_detail, pmd); |
| 37 | 67 |
| 68 if (web_memory_dump_provider_->supportsHeapProfiling()) { |
| 69 // TODO(ruuda): Dump |g_allocation_register| into the |ProcessMemoryDump|. |
| 70 } |
| 71 |
| 38 return web_memory_dump_provider_->onMemoryDump(level, &web_pmd_impl); | 72 return web_memory_dump_provider_->onMemoryDump(level, &web_pmd_impl); |
| 39 } | 73 } |
| 40 | 74 |
| 75 void WebMemoryDumpProviderAdapter::OnHeapProfilingEnabled(bool enabled) { |
| 76 if (!web_memory_dump_provider_->supportsHeapProfiling()) |
| 77 return; |
| 78 |
| 79 if (enabled) { |
| 80 { |
| 81 AutoLock lock(g_allocation_register_lock.Get()); |
| 82 if (!g_allocation_register) |
| 83 g_allocation_register = new AllocationRegister(); |
| 84 } |
| 85 |
| 86 // Make this dump provider call the global hooks on every allocation / free. |
| 87 // TODO(ruuda): Because bookkeeping is done here in the adapter, and not in |
| 88 // the dump providers themselves, all dump providers in Blink share the |
| 89 // same global allocation register. At the moment this is not a problem, |
| 90 // because the only dump provider that supports heap profiling is the |
| 91 // PartitionAlloc dump provider. When Blink can depend on base and this |
| 92 // glue layer is removed, dump providers can have their own instance of the |
| 93 // allocation register. |
| 94 web_memory_dump_provider_->onHeapProfilingEnabled(ReportAllocation, |
| 95 ReportFree); |
| 96 } else { |
| 97 web_memory_dump_provider_->onHeapProfilingEnabled(nullptr, nullptr); |
| 98 } |
| 99 } |
| 100 |
| 41 } // namespace content | 101 } // namespace content |
| OLD | NEW |