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 <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/allocator/features.h" | 10 #include "base/allocator/features.h" |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 LeakySingletonTraits<MemoryDumpManager>>::get(); | 149 LeakySingletonTraits<MemoryDumpManager>>::get(); |
150 } | 150 } |
151 | 151 |
152 // static | 152 // static |
153 void MemoryDumpManager::SetInstanceForTesting(MemoryDumpManager* instance) { | 153 void MemoryDumpManager::SetInstanceForTesting(MemoryDumpManager* instance) { |
154 g_instance_for_testing = instance; | 154 g_instance_for_testing = instance; |
155 } | 155 } |
156 | 156 |
157 MemoryDumpManager::MemoryDumpManager() | 157 MemoryDumpManager::MemoryDumpManager() |
158 : delegate_(nullptr), | 158 : delegate_(nullptr), |
| 159 is_coordinator_(false), |
159 memory_tracing_enabled_(0), | 160 memory_tracing_enabled_(0), |
160 tracing_process_id_(kInvalidTracingProcessId), | 161 tracing_process_id_(kInvalidTracingProcessId), |
161 dumper_registrations_ignored_for_testing_(false), | 162 dumper_registrations_ignored_for_testing_(false), |
162 heap_profiling_enabled_(false) { | 163 heap_profiling_enabled_(false) { |
163 g_next_guid.GetNext(); // Make sure that first guid is not zero. | 164 g_next_guid.GetNext(); // Make sure that first guid is not zero. |
164 | 165 |
165 // At this point the command line may not be initialized but we try to | 166 // At this point the command line may not be initialized but we try to |
166 // enable the heap profiler to capture allocations as soon as possible. | 167 // enable the heap profiler to capture allocations as soon as possible. |
167 EnableHeapProfilingIfNeeded(); | 168 EnableHeapProfilingIfNeeded(); |
168 | 169 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 } else { | 207 } else { |
207 CHECK(false) << "Invalid mode '" << profiling_mode << "' for " | 208 CHECK(false) << "Invalid mode '" << profiling_mode << "' for " |
208 << switches::kEnableHeapProfiling << " flag."; | 209 << switches::kEnableHeapProfiling << " flag."; |
209 } | 210 } |
210 | 211 |
211 for (auto mdp : dump_providers_) | 212 for (auto mdp : dump_providers_) |
212 mdp->dump_provider->OnHeapProfilingEnabled(true); | 213 mdp->dump_provider->OnHeapProfilingEnabled(true); |
213 heap_profiling_enabled_ = true; | 214 heap_profiling_enabled_ = true; |
214 } | 215 } |
215 | 216 |
216 void MemoryDumpManager::Initialize(MemoryDumpManagerDelegate* delegate) { | 217 void MemoryDumpManager::Initialize(MemoryDumpManagerDelegate* delegate, |
| 218 bool is_coordinator) { |
217 { | 219 { |
218 AutoLock lock(lock_); | 220 AutoLock lock(lock_); |
219 DCHECK(delegate); | 221 DCHECK(delegate); |
220 DCHECK(!delegate_); | 222 DCHECK(!delegate_); |
221 delegate_ = delegate; | 223 delegate_ = delegate; |
| 224 is_coordinator_ = is_coordinator; |
222 EnableHeapProfilingIfNeeded(); | 225 EnableHeapProfilingIfNeeded(); |
223 } | 226 } |
224 | 227 |
225 // Enable the core dump providers. | 228 // Enable the core dump providers. |
226 #if defined(MALLOC_MEMORY_TRACING_SUPPORTED) | 229 #if defined(MALLOC_MEMORY_TRACING_SUPPORTED) |
227 RegisterDumpProvider(MallocDumpProvider::GetInstance(), "Malloc", nullptr); | 230 RegisterDumpProvider(MallocDumpProvider::GetInstance(), "Malloc", nullptr); |
228 #endif | 231 #endif |
229 | 232 |
230 #if defined(OS_ANDROID) | 233 #if defined(OS_ANDROID) |
231 RegisterDumpProvider(JavaHeapDumpProvider::GetInstance(), "JavaHeap", | 234 RegisterDumpProvider(JavaHeapDumpProvider::GetInstance(), "JavaHeap", |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 const uint64_t guid = | 453 const uint64_t guid = |
451 TraceLog::GetInstance()->MangleEventId(g_next_guid.GetNext()); | 454 TraceLog::GetInstance()->MangleEventId(g_next_guid.GetNext()); |
452 | 455 |
453 // Creates an async event to keep track of the global dump evolution. | 456 // Creates an async event to keep track of the global dump evolution. |
454 // The |wrapped_callback| will generate the ASYNC_END event and then invoke | 457 // The |wrapped_callback| will generate the ASYNC_END event and then invoke |
455 // the real |callback| provided by the caller. | 458 // the real |callback| provided by the caller. |
456 TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(kTraceCategory, "GlobalMemoryDump", | 459 TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(kTraceCategory, "GlobalMemoryDump", |
457 TRACE_ID_MANGLE(guid)); | 460 TRACE_ID_MANGLE(guid)); |
458 MemoryDumpCallback wrapped_callback = Bind(&OnGlobalDumpDone, callback); | 461 MemoryDumpCallback wrapped_callback = Bind(&OnGlobalDumpDone, callback); |
459 | 462 |
| 463 // Technically there is no need to grab the |lock_| here as the delegate is |
| 464 // long-lived and can only be set by Initialize(), which is locked and |
| 465 // necessarily happens before memory_tracing_enabled_ == true. |
| 466 // Not taking the |lock_|, though, is lakely make TSan barf and, at this point |
| 467 // (memory-infra is enabled) we're not in the fast-path anymore. |
| 468 MemoryDumpManagerDelegate* delegate; |
| 469 { |
| 470 AutoLock lock(lock_); |
| 471 delegate = delegate_; |
| 472 } |
| 473 |
460 // The delegate will coordinate the IPC broadcast and at some point invoke | 474 // The delegate will coordinate the IPC broadcast and at some point invoke |
461 // CreateProcessDump() to get a dump for the current process. | 475 // CreateProcessDump() to get a dump for the current process. |
462 MemoryDumpRequestArgs args = {guid, dump_type, level_of_detail}; | 476 MemoryDumpRequestArgs args = {guid, dump_type, level_of_detail}; |
463 delegate_->RequestGlobalMemoryDump(args, wrapped_callback); | 477 delegate->RequestGlobalMemoryDump(args, wrapped_callback); |
464 } | 478 } |
465 | 479 |
466 void MemoryDumpManager::RequestGlobalDump( | 480 void MemoryDumpManager::RequestGlobalDump( |
467 MemoryDumpType dump_type, | 481 MemoryDumpType dump_type, |
468 MemoryDumpLevelOfDetail level_of_detail) { | 482 MemoryDumpLevelOfDetail level_of_detail) { |
469 RequestGlobalDump(dump_type, level_of_detail, MemoryDumpCallback()); | 483 RequestGlobalDump(dump_type, level_of_detail, MemoryDumpCallback()); |
470 } | 484 } |
471 | 485 |
472 void MemoryDumpManager::CreateProcessDump(const MemoryDumpRequestArgs& args, | 486 void MemoryDumpManager::CreateProcessDump(const MemoryDumpRequestArgs& args, |
473 const MemoryDumpCallback& callback) { | 487 const MemoryDumpCallback& callback) { |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
841 if (mdpinfo->options.is_fast_polling_supported) | 855 if (mdpinfo->options.is_fast_polling_supported) |
842 dump_providers_for_polling_.insert(mdpinfo); | 856 dump_providers_for_polling_.insert(mdpinfo); |
843 } | 857 } |
844 // Notify polling supported only if some polling supported provider was | 858 // Notify polling supported only if some polling supported provider was |
845 // registered, else RegisterPollingMDPOnDumpThread() will notify when first | 859 // registered, else RegisterPollingMDPOnDumpThread() will notify when first |
846 // polling MDP registers. | 860 // polling MDP registers. |
847 if (!dump_providers_for_polling_.empty()) | 861 if (!dump_providers_for_polling_.empty()) |
848 dump_scheduler_->NotifyPollingSupported(); | 862 dump_scheduler_->NotifyPollingSupported(); |
849 | 863 |
850 // Only coordinator process triggers periodic global memory dumps. | 864 // Only coordinator process triggers periodic global memory dumps. |
851 if (delegate_->IsCoordinator()) | 865 if (is_coordinator_) |
852 dump_scheduler_->NotifyPeriodicTriggerSupported(); | 866 dump_scheduler_->NotifyPeriodicTriggerSupported(); |
853 } | 867 } |
854 | 868 |
855 } | 869 } |
856 | 870 |
857 void MemoryDumpManager::OnTraceLogDisabled() { | 871 void MemoryDumpManager::OnTraceLogDisabled() { |
858 // There might be a memory dump in progress while this happens. Therefore, | 872 // There might be a memory dump in progress while this happens. Therefore, |
859 // ensure that the MDM state which depends on the tracing enabled / disabled | 873 // ensure that the MDM state which depends on the tracing enabled / disabled |
860 // state is always accessed by the dumping methods holding the |lock_|. | 874 // state is always accessed by the dumping methods holding the |lock_|. |
861 if (!subtle::NoBarrier_Load(&memory_tracing_enabled_)) | 875 if (!subtle::NoBarrier_Load(&memory_tracing_enabled_)) |
(...skipping 24 matching lines...) Expand all Loading... |
886 } | 900 } |
887 } | 901 } |
888 | 902 |
889 bool MemoryDumpManager::IsDumpModeAllowed(MemoryDumpLevelOfDetail dump_mode) { | 903 bool MemoryDumpManager::IsDumpModeAllowed(MemoryDumpLevelOfDetail dump_mode) { |
890 AutoLock lock(lock_); | 904 AutoLock lock(lock_); |
891 if (!session_state_) | 905 if (!session_state_) |
892 return false; | 906 return false; |
893 return session_state_->IsDumpModeAllowed(dump_mode); | 907 return session_state_->IsDumpModeAllowed(dump_mode); |
894 } | 908 } |
895 | 909 |
| 910 uint64_t MemoryDumpManager::GetTracingProcessId() const { |
| 911 return delegate_->GetTracingProcessId(); |
| 912 } |
| 913 |
896 MemoryDumpManager::MemoryDumpProviderInfo::MemoryDumpProviderInfo( | 914 MemoryDumpManager::MemoryDumpProviderInfo::MemoryDumpProviderInfo( |
897 MemoryDumpProvider* dump_provider, | 915 MemoryDumpProvider* dump_provider, |
898 const char* name, | 916 const char* name, |
899 scoped_refptr<SequencedTaskRunner> task_runner, | 917 scoped_refptr<SequencedTaskRunner> task_runner, |
900 const MemoryDumpProvider::Options& options, | 918 const MemoryDumpProvider::Options& options, |
901 bool whitelisted_for_background_mode) | 919 bool whitelisted_for_background_mode) |
902 : dump_provider(dump_provider), | 920 : dump_provider(dump_provider), |
903 name(name), | 921 name(name), |
904 task_runner(std::move(task_runner)), | 922 task_runner(std::move(task_runner)), |
905 options(options), | 923 options(options), |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
947 if (iter == process_dumps.end()) { | 965 if (iter == process_dumps.end()) { |
948 std::unique_ptr<ProcessMemoryDump> new_pmd( | 966 std::unique_ptr<ProcessMemoryDump> new_pmd( |
949 new ProcessMemoryDump(session_state, dump_args)); | 967 new ProcessMemoryDump(session_state, dump_args)); |
950 iter = process_dumps.insert(std::make_pair(pid, std::move(new_pmd))).first; | 968 iter = process_dumps.insert(std::make_pair(pid, std::move(new_pmd))).first; |
951 } | 969 } |
952 return iter->second.get(); | 970 return iter->second.get(); |
953 } | 971 } |
954 | 972 |
955 } // namespace trace_event | 973 } // namespace trace_event |
956 } // namespace base | 974 } // namespace base |
OLD | NEW |