| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "content/browser/tracing/tracing_controller_impl.h" | 4 #include "content/browser/tracing/tracing_controller_impl.h" |
| 5 | 5 |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/cpu.h" | 7 #include "base/cpu.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/guid.h" | 9 #include "base/guid.h" |
| 10 #include "base/json/string_escape.h" | 10 #include "base/json/string_escape.h" |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 TracingController* TracingController::GetInstance() { | 185 TracingController* TracingController::GetInstance() { |
| 186 return TracingControllerImpl::GetInstance(); | 186 return TracingControllerImpl::GetInstance(); |
| 187 } | 187 } |
| 188 | 188 |
| 189 TracingControllerImpl::TracingControllerImpl() | 189 TracingControllerImpl::TracingControllerImpl() |
| 190 : pending_start_tracing_ack_count_(0), | 190 : pending_start_tracing_ack_count_(0), |
| 191 pending_stop_tracing_ack_count_(0), | 191 pending_stop_tracing_ack_count_(0), |
| 192 pending_trace_log_status_ack_count_(0), | 192 pending_trace_log_status_ack_count_(0), |
| 193 maximum_trace_buffer_usage_(0), | 193 maximum_trace_buffer_usage_(0), |
| 194 approximate_event_count_(0), | 194 approximate_event_count_(0), |
| 195 pending_memory_dump_ack_count_(0), | |
| 196 failed_memory_dump_count_(0), | |
| 197 pending_clock_sync_ack_count_(0), | 195 pending_clock_sync_ack_count_(0), |
| 198 is_tracing_(false) { | 196 is_tracing_(false) { |
| 199 base::trace_event::MemoryDumpManager::GetInstance()->Initialize( | |
| 200 this /* delegate */, true /* is_coordinator */); | |
| 201 | |
| 202 // Deliberately leaked, like this class. | 197 // Deliberately leaked, like this class. |
| 203 base::FileTracing::SetProvider(new FileTracingProviderImpl); | 198 base::FileTracing::SetProvider(new FileTracingProviderImpl); |
| 204 } | 199 } |
| 205 | 200 |
| 206 TracingControllerImpl::~TracingControllerImpl() { | 201 TracingControllerImpl::~TracingControllerImpl() { |
| 207 // This is a Leaky instance. | 202 // This is a Leaky instance. |
| 208 NOTREACHED(); | 203 NOTREACHED(); |
| 209 } | 204 } |
| 210 | 205 |
| 211 TracingControllerImpl* TracingControllerImpl::GetInstance() { | 206 TracingControllerImpl* TracingControllerImpl::GetInstance() { |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 pending_trace_log_status_filters_.find(trace_message_filter); | 523 pending_trace_log_status_filters_.find(trace_message_filter); |
| 529 if (it != pending_trace_log_status_filters_.end()) { | 524 if (it != pending_trace_log_status_filters_.end()) { |
| 530 BrowserThread::PostTask( | 525 BrowserThread::PostTask( |
| 531 BrowserThread::UI, FROM_HERE, | 526 BrowserThread::UI, FROM_HERE, |
| 532 base::Bind(&TracingControllerImpl::OnTraceLogStatusReply, | 527 base::Bind(&TracingControllerImpl::OnTraceLogStatusReply, |
| 533 base::Unretained(this), | 528 base::Unretained(this), |
| 534 base::RetainedRef(trace_message_filter), | 529 base::RetainedRef(trace_message_filter), |
| 535 base::trace_event::TraceLogStatus())); | 530 base::trace_event::TraceLogStatus())); |
| 536 } | 531 } |
| 537 } | 532 } |
| 538 if (pending_memory_dump_ack_count_ > 0) { | |
| 539 DCHECK(!queued_memory_dump_requests_.empty()); | |
| 540 TraceMessageFilterSet::const_iterator it = | |
| 541 pending_memory_dump_filters_.find(trace_message_filter); | |
| 542 if (it != pending_memory_dump_filters_.end()) { | |
| 543 BrowserThread::PostTask( | |
| 544 BrowserThread::UI, FROM_HERE, | |
| 545 base::Bind(&TracingControllerImpl::OnProcessMemoryDumpResponse, | |
| 546 base::Unretained(this), | |
| 547 base::RetainedRef(trace_message_filter), | |
| 548 queued_memory_dump_requests_.front().args.dump_guid, | |
| 549 false /* success */)); | |
| 550 } | |
| 551 } | |
| 552 trace_message_filters_.erase(trace_message_filter); | 533 trace_message_filters_.erase(trace_message_filter); |
| 553 } | 534 } |
| 554 | 535 |
| 555 void TracingControllerImpl::AddTracingAgent(const std::string& agent_name) { | 536 void TracingControllerImpl::AddTracingAgent(const std::string& agent_name) { |
| 556 #if defined(OS_CHROMEOS) | 537 #if defined(OS_CHROMEOS) |
| 557 auto* debug_daemon = | 538 auto* debug_daemon = |
| 558 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | 539 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 559 if (agent_name == debug_daemon->GetTracingAgentName()) { | 540 if (agent_name == debug_daemon->GetTracingAgentName()) { |
| 560 additional_tracing_agents_.push_back(debug_daemon); | 541 additional_tracing_agents_.push_back(debug_daemon); |
| 561 debug_daemon->SetStopAgentTracingTaskRunner( | 542 debug_daemon->SetStopAgentTracingTaskRunner( |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 888 for (base::DictionaryValue::Iterator it(*metadata); !it.IsAtEnd(); | 869 for (base::DictionaryValue::Iterator it(*metadata); !it.IsAtEnd(); |
| 889 it.Advance()) { | 870 it.Advance()) { |
| 890 if (filter.Run(it.key())) | 871 if (filter.Run(it.key())) |
| 891 filtered_metadata->Set(it.key(), it.value().DeepCopy()); | 872 filtered_metadata->Set(it.key(), it.value().DeepCopy()); |
| 892 else | 873 else |
| 893 filtered_metadata->SetString(it.key(), "__stripped__"); | 874 filtered_metadata->SetString(it.key(), "__stripped__"); |
| 894 } | 875 } |
| 895 sink->AddMetadata(std::move(filtered_metadata)); | 876 sink->AddMetadata(std::move(filtered_metadata)); |
| 896 } | 877 } |
| 897 | 878 |
| 898 void TracingControllerImpl::RequestGlobalMemoryDump( | |
| 899 const base::trace_event::MemoryDumpRequestArgs& args, | |
| 900 const base::trace_event::MemoryDumpCallback& callback) { | |
| 901 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 902 BrowserThread::PostTask( | |
| 903 BrowserThread::UI, FROM_HERE, | |
| 904 base::Bind(&TracingControllerImpl::RequestGlobalMemoryDump, | |
| 905 base::Unretained(this), args, callback)); | |
| 906 return; | |
| 907 } | |
| 908 | |
| 909 bool another_dump_already_in_progress = !queued_memory_dump_requests_.empty(); | |
| 910 | |
| 911 // If this is a periodic memory dump request and there already is another | |
| 912 // request in the queue with the same level of detail, there's no point in | |
| 913 // enqueuing this request. | |
| 914 if (another_dump_already_in_progress && | |
| 915 args.dump_type == base::trace_event::MemoryDumpType::PERIODIC_INTERVAL) { | |
| 916 for (const auto& request : queued_memory_dump_requests_) { | |
| 917 if (request.args.level_of_detail == args.level_of_detail) { | |
| 918 VLOG(1) << base::trace_event::MemoryDumpManager::kLogPrefix << " (" | |
| 919 << base::trace_event::MemoryDumpTypeToString(args.dump_type) | |
| 920 << ") skipped because another dump request with the same " | |
| 921 "level of detail (" | |
| 922 << base::trace_event::MemoryDumpLevelOfDetailToString( | |
| 923 args.level_of_detail) | |
| 924 << ") is already in the queue"; | |
| 925 if (!callback.is_null()) | |
| 926 callback.Run(args.dump_guid, false /* success */); | |
| 927 return; | |
| 928 } | |
| 929 } | |
| 930 } | |
| 931 | |
| 932 queued_memory_dump_requests_.emplace_back(args, callback); | |
| 933 | |
| 934 // If another dump is already in progress, this dump will automatically be | |
| 935 // scheduled when the other dump finishes. | |
| 936 if (another_dump_already_in_progress) | |
| 937 return; | |
| 938 | |
| 939 PerformNextQueuedGlobalMemoryDump(); | |
| 940 } | |
| 941 | |
| 942 void TracingControllerImpl::PerformNextQueuedGlobalMemoryDump() { | |
| 943 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 944 DCHECK(!queued_memory_dump_requests_.empty()); | |
| 945 const base::trace_event::MemoryDumpRequestArgs& args = | |
| 946 queued_memory_dump_requests_.front().args; | |
| 947 | |
| 948 // Count myself (local trace) in pending_memory_dump_ack_count_, acked by | |
| 949 // OnBrowserProcessMemoryDumpDone(). | |
| 950 pending_memory_dump_ack_count_ = trace_message_filters_.size() + 1; | |
| 951 pending_memory_dump_filters_.clear(); | |
| 952 failed_memory_dump_count_ = 0; | |
| 953 | |
| 954 MemoryDumpManagerDelegate::CreateProcessDump( | |
| 955 args, base::Bind(&TracingControllerImpl::OnBrowserProcessMemoryDumpDone, | |
| 956 base::Unretained(this))); | |
| 957 | |
| 958 // If there are no child processes we are just done. | |
| 959 if (pending_memory_dump_ack_count_ == 1) | |
| 960 return; | |
| 961 | |
| 962 pending_memory_dump_filters_ = trace_message_filters_; | |
| 963 | |
| 964 for (const scoped_refptr<TraceMessageFilter>& tmf : trace_message_filters_) | |
| 965 tmf->SendProcessMemoryDumpRequest(args); | |
| 966 } | |
| 967 | |
| 968 TracingControllerImpl::QueuedMemoryDumpRequest::QueuedMemoryDumpRequest( | |
| 969 const base::trace_event::MemoryDumpRequestArgs& args, | |
| 970 const base::trace_event::MemoryDumpCallback& callback) | |
| 971 : args(args), callback(callback) {} | |
| 972 | |
| 973 TracingControllerImpl::QueuedMemoryDumpRequest::~QueuedMemoryDumpRequest() {} | |
| 974 | |
| 975 uint64_t TracingControllerImpl::GetTracingProcessId() const { | |
| 976 return ChildProcessHost::kBrowserTracingProcessId; | |
| 977 } | |
| 978 | |
| 979 void TracingControllerImpl::AddTraceMessageFilterObserver( | 879 void TracingControllerImpl::AddTraceMessageFilterObserver( |
| 980 TraceMessageFilterObserver* observer) { | 880 TraceMessageFilterObserver* observer) { |
| 981 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 881 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 982 trace_message_filter_observers_.AddObserver(observer); | 882 trace_message_filter_observers_.AddObserver(observer); |
| 983 | 883 |
| 984 for (auto& filter : trace_message_filters_) | 884 for (auto& filter : trace_message_filters_) |
| 985 observer->OnTraceMessageFilterAdded(filter.get()); | 885 observer->OnTraceMessageFilterAdded(filter.get()); |
| 986 } | 886 } |
| 987 | 887 |
| 988 void TracingControllerImpl::RemoveTraceMessageFilterObserver( | 888 void TracingControllerImpl::RemoveTraceMessageFilterObserver( |
| 989 TraceMessageFilterObserver* observer) { | 889 TraceMessageFilterObserver* observer) { |
| 990 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 890 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 991 trace_message_filter_observers_.RemoveObserver(observer); | 891 trace_message_filter_observers_.RemoveObserver(observer); |
| 992 | 892 |
| 993 for (auto& filter : trace_message_filters_) | 893 for (auto& filter : trace_message_filters_) |
| 994 observer->OnTraceMessageFilterRemoved(filter.get()); | 894 observer->OnTraceMessageFilterRemoved(filter.get()); |
| 995 } | 895 } |
| 996 | 896 |
| 997 void TracingControllerImpl::OnProcessMemoryDumpResponse( | |
| 998 TraceMessageFilter* trace_message_filter, | |
| 999 uint64_t dump_guid, | |
| 1000 bool success) { | |
| 1001 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 1002 BrowserThread::PostTask( | |
| 1003 BrowserThread::UI, FROM_HERE, | |
| 1004 base::Bind(&TracingControllerImpl::OnProcessMemoryDumpResponse, | |
| 1005 base::Unretained(this), | |
| 1006 base::RetainedRef(trace_message_filter), dump_guid, | |
| 1007 success)); | |
| 1008 return; | |
| 1009 } | |
| 1010 | |
| 1011 TraceMessageFilterSet::iterator it = | |
| 1012 pending_memory_dump_filters_.find(trace_message_filter); | |
| 1013 | |
| 1014 DCHECK(!queued_memory_dump_requests_.empty()); | |
| 1015 if (queued_memory_dump_requests_.front().args.dump_guid != dump_guid || | |
| 1016 it == pending_memory_dump_filters_.end()) { | |
| 1017 DLOG(WARNING) << "Received unexpected memory dump response: " << dump_guid; | |
| 1018 return; | |
| 1019 } | |
| 1020 | |
| 1021 DCHECK_GT(pending_memory_dump_ack_count_, 0); | |
| 1022 --pending_memory_dump_ack_count_; | |
| 1023 pending_memory_dump_filters_.erase(it); | |
| 1024 if (!success) { | |
| 1025 ++failed_memory_dump_count_; | |
| 1026 VLOG(1) << base::trace_event::MemoryDumpManager::kLogPrefix | |
| 1027 << " failed because of NACK from child " | |
| 1028 << trace_message_filter->peer_pid(); | |
| 1029 } | |
| 1030 FinalizeGlobalMemoryDumpIfAllProcessesReplied(); | |
| 1031 } | |
| 1032 | |
| 1033 void TracingControllerImpl::OnBrowserProcessMemoryDumpDone(uint64_t dump_guid, | |
| 1034 bool success) { | |
| 1035 DCHECK_GT(pending_memory_dump_ack_count_, 0); | |
| 1036 --pending_memory_dump_ack_count_; | |
| 1037 if (!success) { | |
| 1038 ++failed_memory_dump_count_; | |
| 1039 VLOG(1) << base::trace_event::MemoryDumpManager::kLogPrefix | |
| 1040 << " aborted on the current process"; | |
| 1041 } | |
| 1042 FinalizeGlobalMemoryDumpIfAllProcessesReplied(); | |
| 1043 } | |
| 1044 | |
| 1045 void TracingControllerImpl::FinalizeGlobalMemoryDumpIfAllProcessesReplied() { | |
| 1046 if (pending_memory_dump_ack_count_ > 0) | |
| 1047 return; | |
| 1048 | |
| 1049 DCHECK(!queued_memory_dump_requests_.empty()); | |
| 1050 { | |
| 1051 const auto& callback = queued_memory_dump_requests_.front().callback; | |
| 1052 if (!callback.is_null()) { | |
| 1053 const bool global_success = failed_memory_dump_count_ == 0; | |
| 1054 callback.Run(queued_memory_dump_requests_.front().args.dump_guid, | |
| 1055 global_success); | |
| 1056 } | |
| 1057 } | |
| 1058 queued_memory_dump_requests_.pop_front(); | |
| 1059 | |
| 1060 // Schedule the next queued dump (if applicable). | |
| 1061 if (!queued_memory_dump_requests_.empty()) { | |
| 1062 BrowserThread::PostTask( | |
| 1063 BrowserThread::UI, FROM_HERE, | |
| 1064 base::Bind(&TracingControllerImpl::PerformNextQueuedGlobalMemoryDump, | |
| 1065 base::Unretained(this))); | |
| 1066 } | |
| 1067 } | |
| 1068 | |
| 1069 } // namespace content | 897 } // namespace content |
| OLD | NEW |