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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/containers/hash_tables.h" | |
10 #include "base/lazy_instance.h" | |
11 #include "base/synchronization/lock.h" | |
12 #include "base/trace_event/heap_profiler_allocation_context.h" | |
13 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" | |
14 #include "base/trace_event/heap_profiler_allocation_register.h" | |
15 #include "base/trace_event/heap_profiler_heap_dump_writer.h" | |
16 #include "base/trace_event/process_memory_dump.h" | |
17 #include "base/trace_event/trace_event_argument.h" | 9 #include "base/trace_event/trace_event_argument.h" |
18 #include "base/trace_event/trace_event_memory_overhead.h" | |
19 #include "content/child/web_process_memory_dump_impl.h" | 10 #include "content/child/web_process_memory_dump_impl.h" |
20 #include "third_party/WebKit/public/platform/WebMemoryDumpProvider.h" | 11 #include "third_party/WebKit/public/platform/WebMemoryDumpProvider.h" |
21 | 12 |
22 using namespace base; | |
23 using namespace base::trace_event; | |
24 | |
25 namespace { | |
26 | |
27 // TODO(ruuda): Move these into the dump providers once Blink can depend on | |
28 // base. See https://crbug.com/548254. | |
29 AllocationRegister* g_allocation_register = nullptr; | |
30 LazyInstance<Lock>::Leaky g_allocation_register_lock = | |
31 LAZY_INSTANCE_INITIALIZER; | |
32 bool g_heap_profiling_enabled = false; | |
33 | |
34 void ReportAllocation(void* address, size_t size, const char* type_name) { | |
35 AllocationContext context = AllocationContextTracker::GetContextSnapshot(); | |
36 context.type_name = type_name; | |
37 AutoLock lock(g_allocation_register_lock.Get()); | |
38 | |
39 if (g_allocation_register) | |
40 g_allocation_register->Insert(address, size, context); | |
41 } | |
42 | |
43 void ReportFree(void* address) { | |
44 AutoLock lock(g_allocation_register_lock.Get()); | |
45 | |
46 if (g_allocation_register) | |
47 g_allocation_register->Remove(address); | |
48 } | |
49 | |
50 } // namespace | |
51 | |
52 namespace content { | 13 namespace content { |
53 | 14 |
54 WebMemoryDumpProviderAdapter::WebMemoryDumpProviderAdapter( | 15 WebMemoryDumpProviderAdapter::WebMemoryDumpProviderAdapter( |
55 blink::WebMemoryDumpProvider* wmdp) | 16 blink::WebMemoryDumpProvider* wmdp) |
56 : web_memory_dump_provider_(wmdp), is_registered_(false) { | 17 : web_memory_dump_provider_(wmdp), is_registered_(false) { |
57 } | 18 } |
58 | 19 |
59 WebMemoryDumpProviderAdapter::~WebMemoryDumpProviderAdapter() { | 20 WebMemoryDumpProviderAdapter::~WebMemoryDumpProviderAdapter() { |
60 DCHECK(!is_registered_); | 21 DCHECK(!is_registered_); |
61 } | 22 } |
62 | 23 |
63 bool WebMemoryDumpProviderAdapter::OnMemoryDump( | 24 bool WebMemoryDumpProviderAdapter::OnMemoryDump( |
64 const base::trace_event::MemoryDumpArgs& args, | 25 const base::trace_event::MemoryDumpArgs& args, |
65 base::trace_event::ProcessMemoryDump* pmd) { | 26 base::trace_event::ProcessMemoryDump* pmd) { |
66 blink::WebMemoryDumpLevelOfDetail level; | 27 blink::WebMemoryDumpLevelOfDetail level; |
67 switch (args.level_of_detail) { | 28 switch (args.level_of_detail) { |
68 case base::trace_event::MemoryDumpLevelOfDetail::LIGHT: | 29 case base::trace_event::MemoryDumpLevelOfDetail::LIGHT: |
69 level = blink::WebMemoryDumpLevelOfDetail::Light; | 30 level = blink::WebMemoryDumpLevelOfDetail::Light; |
70 break; | 31 break; |
71 case base::trace_event::MemoryDumpLevelOfDetail::DETAILED: | 32 case base::trace_event::MemoryDumpLevelOfDetail::DETAILED: |
72 level = blink::WebMemoryDumpLevelOfDetail::Detailed; | 33 level = blink::WebMemoryDumpLevelOfDetail::Detailed; |
73 break; | 34 break; |
74 default: | 35 default: |
75 NOTREACHED(); | 36 NOTREACHED(); |
76 return false; | 37 return false; |
77 } | 38 } |
78 WebProcessMemoryDumpImpl web_pmd_impl(args.level_of_detail, pmd); | 39 WebProcessMemoryDumpImpl web_pmd_impl(args.level_of_detail, pmd); |
79 | |
80 if (args.level_of_detail == MemoryDumpLevelOfDetail::DETAILED && | |
81 web_memory_dump_provider_->supportsHeapProfiling() && | |
82 g_heap_profiling_enabled) { | |
83 TraceEventMemoryOverhead overhead; | |
84 hash_map<AllocationContext, size_t> bytes_by_context; | |
85 { | |
86 AutoLock lock(g_allocation_register_lock.Get()); | |
87 for (const auto& alloc_size : *g_allocation_register) | |
88 bytes_by_context[alloc_size.context] += alloc_size.size; | |
89 | |
90 g_allocation_register->EstimateTraceMemoryOverhead(&overhead); | |
91 } | |
92 | |
93 scoped_refptr<TracedValue> heap_dump = ExportHeapDump( | |
94 bytes_by_context, pmd->session_state()->stack_frame_deduplicator(), | |
95 pmd->session_state()->type_name_deduplicator()); | |
96 pmd->AddHeapDump("partition_alloc", heap_dump); | |
97 overhead.DumpInto("tracing/heap_profiler", pmd); | |
98 } | |
99 | |
100 return web_memory_dump_provider_->onMemoryDump(level, &web_pmd_impl); | 40 return web_memory_dump_provider_->onMemoryDump(level, &web_pmd_impl); |
101 } | 41 } |
102 | 42 |
103 void WebMemoryDumpProviderAdapter::OnHeapProfilingEnabled(bool enabled) { | 43 void WebMemoryDumpProviderAdapter::OnHeapProfilingEnabled(bool enabled) { |
104 if (!web_memory_dump_provider_->supportsHeapProfiling()) | 44 if (!web_memory_dump_provider_->supportsHeapProfiling()) |
105 return; | 45 return; |
106 | 46 web_memory_dump_provider_->onHeapProfilingEnabled(enabled); |
107 if (enabled) { | |
108 { | |
109 AutoLock lock(g_allocation_register_lock.Get()); | |
110 if (!g_allocation_register) | |
111 g_allocation_register = new AllocationRegister(); | |
112 } | |
113 | |
114 // Make this dump provider call the global hooks on every allocation / free. | |
115 // TODO(ruuda): Because bookkeeping is done here in the adapter, and not in | |
116 // the dump providers themselves, all dump providers in Blink share the | |
117 // same global allocation register. At the moment this is not a problem, | |
118 // because the only dump provider that supports heap profiling is the | |
119 // PartitionAlloc dump provider. When Blink can depend on base and this | |
120 // glue layer is removed, dump providers can have their own instance of the | |
121 // allocation register. Tracking bug: https://crbug.com/548254. | |
122 web_memory_dump_provider_->onHeapProfilingEnabled(ReportAllocation, | |
123 ReportFree); | |
124 } else { | |
125 web_memory_dump_provider_->onHeapProfilingEnabled(nullptr, nullptr); | |
126 } | |
127 | |
128 g_heap_profiling_enabled = enabled; | |
129 } | 47 } |
130 | 48 |
131 } // namespace content | 49 } // namespace content |
OLD | NEW |