| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "services/tracing/public/cpp/provider.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/callback.h" | |
| 10 #include "base/lazy_instance.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/single_thread_task_runner.h" | |
| 14 #include "base/synchronization/lock.h" | |
| 15 #include "base/threading/platform_thread.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "base/time/time.h" | |
| 18 #include "base/trace_event/trace_config.h" | |
| 19 #include "base/trace_event/trace_event.h" | |
| 20 #include "services/service_manager/public/cpp/connector.h" | |
| 21 #include "services/tracing/public/cpp/switches.h" | |
| 22 #include "services/tracing/public/interfaces/constants.mojom.h" | |
| 23 | |
| 24 namespace tracing { | |
| 25 namespace { | |
| 26 | |
| 27 // Controls access to |g_tracing_singleton_created|, which can be accessed from | |
| 28 // different threads. | |
| 29 base::LazyInstance<base::Lock>::Leaky g_singleton_lock = | |
| 30 LAZY_INSTANCE_INITIALIZER; | |
| 31 | |
| 32 // Whether we are the first TracingImpl to be created in this service. The first | |
| 33 // TracingImpl in a physical service connects to the tracing service. | |
| 34 bool g_tracing_singleton_created = false; | |
| 35 | |
| 36 } | |
| 37 | |
| 38 Provider::Provider() | |
| 39 : binding_(this), tracing_forced_(false), weak_factory_(this) {} | |
| 40 | |
| 41 Provider::~Provider() { | |
| 42 StopTracing(); | |
| 43 } | |
| 44 | |
| 45 void Provider::InitializeWithFactoryInternal(mojom::FactoryPtr* factory) { | |
| 46 mojom::ProviderPtr provider; | |
| 47 Bind(MakeRequest(&provider)); | |
| 48 (*factory)->CreateRecorder(std::move(provider)); | |
| 49 #ifdef NDEBUG | |
| 50 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 51 tracing::kEarlyTracing)) { | |
| 52 ForceEnableTracing(); | |
| 53 } | |
| 54 #else | |
| 55 ForceEnableTracing(); | |
| 56 #endif | |
| 57 } | |
| 58 | |
| 59 void Provider::InitializeWithFactory(mojom::FactoryPtr* factory) { | |
| 60 { | |
| 61 base::AutoLock lock(g_singleton_lock.Get()); | |
| 62 if (g_tracing_singleton_created) | |
| 63 return; | |
| 64 g_tracing_singleton_created = true; | |
| 65 } | |
| 66 InitializeWithFactoryInternal(factory); | |
| 67 } | |
| 68 | |
| 69 void Provider::Initialize(service_manager::Connector* connector, | |
| 70 const std::string& url) { | |
| 71 { | |
| 72 base::AutoLock lock(g_singleton_lock.Get()); | |
| 73 if (g_tracing_singleton_created) | |
| 74 return; | |
| 75 g_tracing_singleton_created = true; | |
| 76 } | |
| 77 mojom::FactoryPtr factory; | |
| 78 connector->BindInterface(tracing::mojom::kServiceName, &factory); | |
| 79 InitializeWithFactoryInternal(&factory); | |
| 80 // This will only set the name for the first app in a loaded mojo file. It's | |
| 81 // up to something like CoreServices to name its own child threads. | |
| 82 base::PlatformThread::SetName(url); | |
| 83 } | |
| 84 | |
| 85 void Provider::Bind(mojom::ProviderRequest request) { | |
| 86 if (!binding_.is_bound()) { | |
| 87 binding_.Bind(std::move(request)); | |
| 88 } else { | |
| 89 LOG(ERROR) << "Cannot accept two connections to TraceProvider."; | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void Provider::StartTracing(const std::string& categories, | |
| 94 mojom::RecorderPtr recorder) { | |
| 95 DCHECK(!recorder_); | |
| 96 recorder_ = std::move(recorder); | |
| 97 tracing_forced_ = false; | |
| 98 if (!base::trace_event::TraceLog::GetInstance()->IsEnabled()) { | |
| 99 base::trace_event::TraceLog::GetInstance()->SetEnabled( | |
| 100 base::trace_event::TraceConfig(categories, | |
| 101 base::trace_event::RECORD_UNTIL_FULL), | |
| 102 base::trace_event::TraceLog::RECORDING_MODE); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void Provider::StopTracing() { | |
| 107 if (recorder_) { | |
| 108 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
| 109 | |
| 110 base::trace_event::TraceLog::GetInstance()->Flush( | |
| 111 base::Bind(&Provider::SendChunk, base::Unretained(this))); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void Provider::ForceEnableTracing() { | |
| 116 base::trace_event::TraceLog::GetInstance()->SetEnabled( | |
| 117 base::trace_event::TraceConfig("*", base::trace_event::RECORD_UNTIL_FULL), | |
| 118 base::trace_event::TraceLog::RECORDING_MODE); | |
| 119 tracing_forced_ = true; | |
| 120 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 121 FROM_HERE, | |
| 122 base::Bind(&Provider::DelayedStop, weak_factory_.GetWeakPtr())); | |
| 123 } | |
| 124 | |
| 125 void Provider::DelayedStop() { | |
| 126 // We use this indirection to account for cases where the Initialize method | |
| 127 // takes more than one second to finish; thus we start the countdown only when | |
| 128 // the current thread is unblocked. | |
| 129 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 130 FROM_HERE, | |
| 131 base::Bind(&Provider::StopIfForced, weak_factory_.GetWeakPtr()), | |
| 132 base::TimeDelta::FromSeconds(1)); | |
| 133 } | |
| 134 | |
| 135 void Provider::StopIfForced() { | |
| 136 if (!tracing_forced_) { | |
| 137 return; | |
| 138 } | |
| 139 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
| 140 base::trace_event::TraceLog::GetInstance()->Flush( | |
| 141 base::Callback<void(const scoped_refptr<base::RefCountedString>&, | |
| 142 bool)>()); | |
| 143 } | |
| 144 | |
| 145 void Provider::SendChunk( | |
| 146 const scoped_refptr<base::RefCountedString>& events_str, | |
| 147 bool has_more_events) { | |
| 148 DCHECK(recorder_); | |
| 149 // The string will be empty if an error eccured or there were no trace | |
| 150 // events. Empty string is not a valid chunk to record so skip in this case. | |
| 151 if (!events_str->data().empty()) { | |
| 152 recorder_->Record(events_str->data()); | |
| 153 } | |
| 154 if (!has_more_events) { | |
| 155 recorder_.reset(); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 } // namespace tracing | |
| OLD | NEW |