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 // Calling |GetContextSnapshot| is only valid when |capture_enabled| is true. | |
Primiano Tucci (use gerrit)
2015/10/19 21:32:03
maybe add a comment saying that you do the GetEmpt
| |
25 AllocationContext context = | |
26 AllocationContextTracker::capture_enabled() | |
Primiano Tucci (use gerrit)
2015/10/19 21:32:03
We had a chat offline, I think that you can delete
| |
27 ? AllocationContextTracker::GetContextSnapshot() | |
28 : AllocationContext::GetEmpty(); | |
29 | |
30 AutoLock guard(g_allocation_register_lock.Get()); | |
31 | |
32 if (g_allocation_register) | |
33 g_allocation_register->Insert(address, size, context); | |
34 } | |
35 | |
36 void ReportFree(void* address) { | |
37 AutoLock guard(g_allocation_register_lock.Get()); | |
38 | |
39 if (g_allocation_register) | |
40 g_allocation_register->Remove(address); | |
41 } | |
42 | |
43 } // namespace | |
44 | |
10 namespace content { | 45 namespace content { |
11 | 46 |
12 WebMemoryDumpProviderAdapter::WebMemoryDumpProviderAdapter( | 47 WebMemoryDumpProviderAdapter::WebMemoryDumpProviderAdapter( |
13 blink::WebMemoryDumpProvider* wmdp) | 48 blink::WebMemoryDumpProvider* wmdp) |
14 : web_memory_dump_provider_(wmdp), is_registered_(false) { | 49 : web_memory_dump_provider_(wmdp), is_registered_(false) { |
15 } | 50 } |
16 | 51 |
17 WebMemoryDumpProviderAdapter::~WebMemoryDumpProviderAdapter() { | 52 WebMemoryDumpProviderAdapter::~WebMemoryDumpProviderAdapter() { |
18 DCHECK(!is_registered_); | 53 DCHECK(!is_registered_); |
19 } | 54 } |
20 | 55 |
21 bool WebMemoryDumpProviderAdapter::OnMemoryDump( | 56 bool WebMemoryDumpProviderAdapter::OnMemoryDump( |
22 const base::trace_event::MemoryDumpArgs& args, | 57 const base::trace_event::MemoryDumpArgs& args, |
23 base::trace_event::ProcessMemoryDump* pmd) { | 58 base::trace_event::ProcessMemoryDump* pmd) { |
24 blink::WebMemoryDumpLevelOfDetail level; | 59 blink::WebMemoryDumpLevelOfDetail level; |
25 switch (args.level_of_detail) { | 60 switch (args.level_of_detail) { |
26 case base::trace_event::MemoryDumpLevelOfDetail::LIGHT: | 61 case base::trace_event::MemoryDumpLevelOfDetail::LIGHT: |
27 level = blink::WebMemoryDumpLevelOfDetail::Light; | 62 level = blink::WebMemoryDumpLevelOfDetail::Light; |
28 break; | 63 break; |
29 case base::trace_event::MemoryDumpLevelOfDetail::DETAILED: | 64 case base::trace_event::MemoryDumpLevelOfDetail::DETAILED: |
30 level = blink::WebMemoryDumpLevelOfDetail::Detailed; | 65 level = blink::WebMemoryDumpLevelOfDetail::Detailed; |
31 break; | 66 break; |
32 default: | 67 default: |
33 NOTREACHED(); | 68 NOTREACHED(); |
34 return false; | 69 return false; |
35 } | 70 } |
36 WebProcessMemoryDumpImpl web_pmd_impl(args.level_of_detail, pmd); | 71 WebProcessMemoryDumpImpl web_pmd_impl(args.level_of_detail, pmd); |
37 | 72 |
73 if (web_memory_dump_provider_->supportsHeapProfiling()) { | |
74 // TODO(ruuda): Dump |g_allocation_register| into the |ProcessMemoryDump|. | |
75 } | |
76 | |
38 return web_memory_dump_provider_->onMemoryDump(level, &web_pmd_impl); | 77 return web_memory_dump_provider_->onMemoryDump(level, &web_pmd_impl); |
39 } | 78 } |
40 | 79 |
80 void WebMemoryDumpProviderAdapter::OnHeapProfilingEnabled(bool enabled) { | |
81 if (!web_memory_dump_provider_->supportsHeapProfiling()) | |
82 return; | |
83 | |
84 if (enabled) { | |
85 AutoLock guard(g_allocation_register_lock.Get()); | |
Primiano Tucci (use gerrit)
2015/10/19 21:32:03
either put brances around here or move the lock up
| |
86 | |
87 if (!g_allocation_register) { | |
88 g_allocation_register = new AllocationRegister(); | |
89 } | |
90 | |
91 // Make this dump provider call the global hooks on every allocation / free. | |
92 // TODO(ruuda): Because bookkeeping is done here in the adapter, and not in | |
93 // the dump providers themselves, all dump providers in Blink share the | |
94 // same global allocation register. At the moment this is not a problem, | |
95 // because the only dump provider that supports heap profiling is the | |
96 // PartitionAlloc dump provider. When Blink can depend on base and this | |
97 // glue layer is removed, dump providers can have their own instance of the | |
98 // allocation register. | |
99 web_memory_dump_provider_->onHeapProfilingEnabled(ReportAllocation, | |
100 ReportFree); | |
101 } else { | |
102 web_memory_dump_provider_->onHeapProfilingEnabled(nullptr, nullptr); | |
103 } | |
104 } | |
105 | |
41 } // namespace content | 106 } // namespace content |
OLD | NEW |