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