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 "platform/heap/BlinkGCMemoryDumpProvider.h" | 5 #include "platform/heap/BlinkGCMemoryDumpProvider.h" |
6 | 6 |
7 #include <unordered_map> | 7 #include <unordered_map> |
8 | 8 |
9 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" | 9 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" |
10 #include "base/trace_event/heap_profiler_allocation_register.h" | |
11 #include "base/trace_event/memory_allocator_dump.h" | 10 #include "base/trace_event/memory_allocator_dump.h" |
12 #include "base/trace_event/process_memory_dump.h" | 11 #include "base/trace_event/process_memory_dump.h" |
| 12 #include "base/trace_event/sharded_allocation_register.h" |
13 #include "base/trace_event/trace_event_memory_overhead.h" | 13 #include "base/trace_event/trace_event_memory_overhead.h" |
14 #include "platform/heap/Handle.h" | 14 #include "platform/heap/Handle.h" |
15 #include "platform/instrumentation/tracing/web_memory_allocator_dump.h" | 15 #include "platform/instrumentation/tracing/web_memory_allocator_dump.h" |
16 #include "platform/wtf/StdLibExtras.h" | 16 #include "platform/wtf/StdLibExtras.h" |
17 #include "platform/wtf/Threading.h" | 17 #include "platform/wtf/Threading.h" |
18 #include "public/platform/Platform.h" | 18 #include "public/platform/Platform.h" |
19 | 19 |
20 namespace blink { | 20 namespace blink { |
21 namespace { | 21 namespace { |
22 | 22 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 using base::trace_event::MemoryDumpLevelOfDetail; | 59 using base::trace_event::MemoryDumpLevelOfDetail; |
60 MemoryDumpLevelOfDetail level_of_detail = args.level_of_detail; | 60 MemoryDumpLevelOfDetail level_of_detail = args.level_of_detail; |
61 // In the case of a detailed dump perform a mark-only GC pass to collect | 61 // In the case of a detailed dump perform a mark-only GC pass to collect |
62 // more detailed stats. | 62 // more detailed stats. |
63 if (level_of_detail == MemoryDumpLevelOfDetail::DETAILED) | 63 if (level_of_detail == MemoryDumpLevelOfDetail::DETAILED) |
64 ThreadState::Current()->CollectGarbage(BlinkGC::kNoHeapPointersOnStack, | 64 ThreadState::Current()->CollectGarbage(BlinkGC::kNoHeapPointersOnStack, |
65 BlinkGC::kTakeSnapshot, | 65 BlinkGC::kTakeSnapshot, |
66 BlinkGC::kForcedGC); | 66 BlinkGC::kForcedGC); |
67 DumpMemoryTotals(memory_dump); | 67 DumpMemoryTotals(memory_dump); |
68 | 68 |
69 if (is_heap_profiling_enabled_) { | 69 if (allocation_register_->IsEnabled()) { |
70 // Overhead should always be reported, regardless of light vs. heavy. | 70 // Overhead should always be reported, regardless of light vs. heavy. |
71 base::trace_event::TraceEventMemoryOverhead overhead; | 71 base::trace_event::TraceEventMemoryOverhead overhead; |
72 std::unordered_map<base::trace_event::AllocationContext, | 72 std::unordered_map<base::trace_event::AllocationContext, |
73 base::trace_event::AllocationMetrics> | 73 base::trace_event::AllocationMetrics> |
74 metrics_by_context; | 74 metrics_by_context; |
75 { | 75 if (level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { |
76 MutexLocker locker(allocation_register_mutex_); | 76 allocation_register_->UpdateAndReturnsMetrics(metrics_by_context); |
77 if (level_of_detail == MemoryDumpLevelOfDetail::DETAILED) { | |
78 for (const auto& alloc_size : *allocation_register_) { | |
79 base::trace_event::AllocationMetrics& metrics = | |
80 metrics_by_context[alloc_size.context]; | |
81 metrics.size += alloc_size.size; | |
82 metrics.count++; | |
83 } | |
84 } | |
85 allocation_register_->EstimateTraceMemoryOverhead(&overhead); | |
86 } | 77 } |
| 78 allocation_register_->EstimateTraceMemoryOverhead(&overhead); |
87 memory_dump->DumpHeapUsage(metrics_by_context, overhead, "blink_gc"); | 79 memory_dump->DumpHeapUsage(metrics_by_context, overhead, "blink_gc"); |
88 } | 80 } |
89 | 81 |
90 // Merge all dumps collected by ThreadHeap::collectGarbage. | 82 // Merge all dumps collected by ThreadHeap::collectGarbage. |
91 if (level_of_detail == MemoryDumpLevelOfDetail::DETAILED) | 83 if (level_of_detail == MemoryDumpLevelOfDetail::DETAILED) |
92 memory_dump->TakeAllDumpsFrom(current_process_memory_dump_.get()); | 84 memory_dump->TakeAllDumpsFrom(current_process_memory_dump_.get()); |
93 return true; | 85 return true; |
94 } | 86 } |
95 | 87 |
96 void BlinkGCMemoryDumpProvider::OnHeapProfilingEnabled(bool enabled) { | 88 void BlinkGCMemoryDumpProvider::OnHeapProfilingEnabled(bool enabled) { |
97 if (enabled) { | 89 if (enabled) { |
98 { | 90 if (!allocation_register_->IsInitialized()) |
99 MutexLocker locker(allocation_register_mutex_); | 91 allocation_register_->Initialize(); |
100 if (!allocation_register_) | |
101 allocation_register_.reset(new base::trace_event::AllocationRegister()); | |
102 } | |
103 HeapAllocHooks::SetAllocationHook(ReportAllocation); | 92 HeapAllocHooks::SetAllocationHook(ReportAllocation); |
104 HeapAllocHooks::SetFreeHook(ReportFree); | 93 HeapAllocHooks::SetFreeHook(ReportFree); |
105 } else { | 94 } else { |
106 HeapAllocHooks::SetAllocationHook(nullptr); | 95 HeapAllocHooks::SetAllocationHook(nullptr); |
107 HeapAllocHooks::SetFreeHook(nullptr); | 96 HeapAllocHooks::SetFreeHook(nullptr); |
108 } | 97 } |
109 is_heap_profiling_enabled_ = enabled; | 98 allocation_register_->SetEnabled(enabled); |
110 } | 99 } |
111 | 100 |
112 base::trace_event::MemoryAllocatorDump* | 101 base::trace_event::MemoryAllocatorDump* |
113 BlinkGCMemoryDumpProvider::CreateMemoryAllocatorDumpForCurrentGC( | 102 BlinkGCMemoryDumpProvider::CreateMemoryAllocatorDumpForCurrentGC( |
114 const String& absolute_name) { | 103 const String& absolute_name) { |
115 // TODO(bashi): Change type name of |absoluteName|. | 104 // TODO(bashi): Change type name of |absoluteName|. |
116 return current_process_memory_dump_->CreateAllocatorDump( | 105 return current_process_memory_dump_->CreateAllocatorDump( |
117 absolute_name.Utf8().data()); | 106 absolute_name.Utf8().data()); |
118 } | 107 } |
119 | 108 |
120 void BlinkGCMemoryDumpProvider::ClearProcessDumpForCurrentGC() { | 109 void BlinkGCMemoryDumpProvider::ClearProcessDumpForCurrentGC() { |
121 current_process_memory_dump_->Clear(); | 110 current_process_memory_dump_->Clear(); |
122 } | 111 } |
123 | 112 |
124 BlinkGCMemoryDumpProvider::BlinkGCMemoryDumpProvider() | 113 BlinkGCMemoryDumpProvider::BlinkGCMemoryDumpProvider() |
125 : current_process_memory_dump_(new base::trace_event::ProcessMemoryDump( | 114 : allocation_register_(new base::trace_event::ShardedAllocationRegister), |
| 115 current_process_memory_dump_(new base::trace_event::ProcessMemoryDump( |
126 nullptr, | 116 nullptr, |
127 {base::trace_event::MemoryDumpLevelOfDetail::DETAILED})), | 117 {base::trace_event::MemoryDumpLevelOfDetail::DETAILED})) {} |
128 is_heap_profiling_enabled_(false) {} | |
129 | 118 |
130 void BlinkGCMemoryDumpProvider::insert(Address address, | 119 void BlinkGCMemoryDumpProvider::insert(Address address, |
131 size_t size, | 120 size_t size, |
132 const char* type_name) { | 121 const char* type_name) { |
133 base::trace_event::AllocationContext context; | 122 base::trace_event::AllocationContext context; |
134 if (!base::trace_event::AllocationContextTracker:: | 123 if (!base::trace_event::AllocationContextTracker:: |
135 GetInstanceForCurrentThread() | 124 GetInstanceForCurrentThread() |
136 ->GetContextSnapshot(&context)) | 125 ->GetContextSnapshot(&context)) |
137 return; | 126 return; |
138 context.type_name = type_name; | 127 context.type_name = type_name; |
139 MutexLocker locker(allocation_register_mutex_); | 128 if (!allocation_register_->IsEnabled()) |
140 if (allocation_register_) | 129 return; |
141 allocation_register_->Insert(address, size, context); | 130 allocation_register_->Insert(address, size, context); |
142 } | 131 } |
143 | 132 |
144 void BlinkGCMemoryDumpProvider::Remove(Address address) { | 133 void BlinkGCMemoryDumpProvider::Remove(Address address) { |
145 MutexLocker locker(allocation_register_mutex_); | 134 if (!allocation_register_->IsEnabled()) |
146 if (allocation_register_) | 135 return; |
147 allocation_register_->Remove(address); | 136 allocation_register_->Remove(address); |
148 } | 137 } |
149 | 138 |
150 } // namespace blink | 139 } // namespace blink |
OLD | NEW |