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 "base/trace_event/memory_dump_manager.h" | 5 #include "base/trace_event/memory_dump_manager.h" |
6 | 6 |
7 #include <inttypes.h> | 7 #include <inttypes.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 // static | 150 // static |
151 MemoryDumpManager* MemoryDumpManager::GetInstance() { | 151 MemoryDumpManager* MemoryDumpManager::GetInstance() { |
152 if (g_instance_for_testing) | 152 if (g_instance_for_testing) |
153 return g_instance_for_testing; | 153 return g_instance_for_testing; |
154 | 154 |
155 return Singleton<MemoryDumpManager, | 155 return Singleton<MemoryDumpManager, |
156 LeakySingletonTraits<MemoryDumpManager>>::get(); | 156 LeakySingletonTraits<MemoryDumpManager>>::get(); |
157 } | 157 } |
158 | 158 |
159 // static | 159 // static |
160 void MemoryDumpManager::SetInstanceForTesting(MemoryDumpManager* instance) { | 160 std::unique_ptr<MemoryDumpManager> |
161 g_instance_for_testing = instance; | 161 MemoryDumpManager::CreateInstanceForTesting() { |
| 162 std::unique_ptr<MemoryDumpManager> instance(new MemoryDumpManager()); |
| 163 g_instance_for_testing = instance.get(); |
| 164 return instance; |
162 } | 165 } |
163 | 166 |
164 MemoryDumpManager::MemoryDumpManager() | 167 MemoryDumpManager::MemoryDumpManager() |
165 : is_coordinator_(false), | 168 : is_coordinator_(false), |
166 tracing_process_id_(kInvalidTracingProcessId), | 169 tracing_process_id_(kInvalidTracingProcessId), |
167 dumper_registrations_ignored_for_testing_(false), | 170 dumper_registrations_ignored_for_testing_(false), |
168 heap_profiling_enabled_(false) { | 171 heap_profiling_enabled_(false) { |
169 g_next_guid.GetNext(); // Make sure that first guid is not zero. | 172 g_next_guid.GetNext(); // Make sure that first guid is not zero. |
170 | 173 |
171 // At this point the command line may not be initialized but we try to | 174 // At this point the command line may not be initialized but we try to |
172 // enable the heap profiler to capture allocations as soon as possible. | 175 // enable the heap profiler to capture allocations as soon as possible. |
173 EnableHeapProfilingIfNeeded(); | 176 EnableHeapProfilingIfNeeded(); |
174 } | 177 } |
175 | 178 |
176 MemoryDumpManager::~MemoryDumpManager() { | 179 MemoryDumpManager::~MemoryDumpManager() { |
177 Thread* dump_thread = nullptr; | 180 Thread* dump_thread = nullptr; |
178 { | 181 { |
179 AutoLock lock(lock_); | 182 AutoLock lock(lock_); |
180 if (dump_thread_) { | 183 if (dump_thread_) { |
181 dump_thread = dump_thread_.get(); | 184 dump_thread = dump_thread_.get(); |
182 } | 185 } |
183 } | 186 } |
184 if (dump_thread) { | 187 if (dump_thread) { |
185 dump_thread->Stop(); | 188 dump_thread->Stop(); |
186 } | 189 } |
187 AutoLock lock(lock_); | 190 AutoLock lock(lock_); |
188 dump_thread_.reset(); | 191 dump_thread_.reset(); |
| 192 g_instance_for_testing = nullptr; |
189 } | 193 } |
190 | 194 |
191 void MemoryDumpManager::EnableHeapProfilingIfNeeded() { | 195 void MemoryDumpManager::EnableHeapProfilingIfNeeded() { |
192 if (heap_profiling_enabled_) | 196 if (heap_profiling_enabled_) |
193 return; | 197 return; |
194 | 198 |
195 if (!CommandLine::InitializedForCurrentProcess() || | 199 if (!CommandLine::InitializedForCurrentProcess() || |
196 !CommandLine::ForCurrentProcess()->HasSwitch( | 200 !CommandLine::ForCurrentProcess()->HasSwitch( |
197 switches::kEnableHeapProfiling)) | 201 switches::kEnableHeapProfiling)) |
198 return; | 202 return; |
(...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
904 if (iter == process_dumps.end()) { | 908 if (iter == process_dumps.end()) { |
905 std::unique_ptr<ProcessMemoryDump> new_pmd( | 909 std::unique_ptr<ProcessMemoryDump> new_pmd( |
906 new ProcessMemoryDump(heap_profiler_serialization_state, dump_args)); | 910 new ProcessMemoryDump(heap_profiler_serialization_state, dump_args)); |
907 iter = process_dumps.insert(std::make_pair(pid, std::move(new_pmd))).first; | 911 iter = process_dumps.insert(std::make_pair(pid, std::move(new_pmd))).first; |
908 } | 912 } |
909 return iter->second.get(); | 913 return iter->second.get(); |
910 } | 914 } |
911 | 915 |
912 } // namespace trace_event | 916 } // namespace trace_event |
913 } // namespace base | 917 } // namespace base |
OLD | NEW |