Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: content/browser/tracing/background_tracing_manager_impl.cc

Issue 1089253003: Re-land first pass BackgroundTracingManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Split into multiple files in content/public/browser. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "content/browser/tracing/background_tracing_manager_impl.h"
6
7 #include "base/macros.h"
8 #include "content/public/browser/browser_thread.h"
9
10 namespace content {
11
12 namespace {
13
14 base::LazyInstance<BackgroundTracingManagerImpl>::Leaky g_controller =
15 LAZY_INSTANCE_INITIALIZER;
16
17 } // namespace
18
19 BackgroundTracingManagerImpl::TraceDataEndpointWrapper::
20 TraceDataEndpointWrapper(base::Callback<void()> done_callback)
21 : done_callback_(done_callback) {
22 }
23
24 BackgroundTracingManagerImpl::TraceDataEndpointWrapper::
25 ~TraceDataEndpointWrapper() {
26 }
27
28 void BackgroundTracingManagerImpl::TraceDataEndpointWrapper::
29 ReceiveTraceFinalContents(const std::string& file_contents) {
30 upload_sink_->Upload(file_contents, done_callback_);
31 }
32
33 void BackgroundTracingManagerImpl::TraceDataEndpointWrapper::SetUploadSink(
34 scoped_refptr<content::BackgroundTracingUploadSink> upload_sink) {
35 upload_sink_ = upload_sink;
36 }
37
38 BackgroundTracingManager* BackgroundTracingManager::GetInstance() {
39 return BackgroundTracingManagerImpl::GetInstance();
40 }
41
42 BackgroundTracingManagerImpl* BackgroundTracingManagerImpl::GetInstance() {
43 return g_controller.Pointer();
44 }
45
46 BackgroundTracingManagerImpl::BackgroundTracingManagerImpl()
47 : is_gathering_(false), trigger_handle_ids_(0) {
48 data_endpoint_wrapper_ = new TraceDataEndpointWrapper(
49 base::Bind(&BackgroundTracingManagerImpl::OnFinalizeComplete,
50 base::Unretained(this)));
51 }
52
53 BackgroundTracingManagerImpl::~BackgroundTracingManagerImpl() {
54 NOTREACHED();
55 }
56
57 void BackgroundTracingManagerImpl::WhenIdle(
58 base::Callback<void()> idle_callback) {
59 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
60 idle_callback_ = idle_callback;
61 }
62
63 bool BackgroundTracingManagerImpl::IsSupportedConfig(
64 scoped_refptr<BackgroundTracingConfig> config) {
65 // No config is just fine, we just don't do anything.
66 if (!config)
67 return true;
68
69 // TODO(simonhatch): Implement reactive tracing path.
70 if (config->mode() != BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE)
71 return false;
72
73 // TODO(fmeawad): Implement uma triggers.
74 BackgroundTracingPreemptiveConfig* preemptive_config =
75 static_cast<BackgroundTracingPreemptiveConfig*>(config.get());
76 const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>&
77 configs = preemptive_config->configs();
78 for (size_t i = 0; i < configs.size(); ++i) {
79 if (configs[i].type !=
80 BackgroundTracingPreemptiveConfig::MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED)
81 return false;
82 ;
83 }
84
85 return true;
86 }
87
88 bool BackgroundTracingManagerImpl::SetActiveScenario(
89 scoped_refptr<BackgroundTracingConfig> config,
90 scoped_refptr<BackgroundTracingUploadSink> upload_sink) {
91 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
92 if (!IsSupportedConfig(config))
93 return false;
94
95 // No point in tracing if there's nowhere to send it.
96 if (config && !upload_sink)
97 return false;
98
99 config_ = config;
100
101 data_endpoint_wrapper_->SetUploadSink(upload_sink);
102
103 EnableRecordingIfConfigNeedsIt();
104
105 return true;
106 }
107
108 void BackgroundTracingManagerImpl::EnableRecordingIfConfigNeedsIt() {
109 if (!config_)
110 return;
111
112 if (config_->mode() == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) {
113 EnableRecording(GetCategoryFilterForCategoryPreset(
114 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get())
115 ->category_preset()));
116 } else {
117 // TODO(simonhatch): Implement reactive tracing path.
118 NOTREACHED();
119 }
120 }
121
122 bool BackgroundTracingManagerImpl::IsAbleToTriggerTracing(
123 TriggerHandle handle) const {
124 if (!config_)
125 return false;
126
127 // If the last trace is still uploading, we don't allow a new one to trigger.
128 if (is_gathering_)
129 return false;
130
131 if (!IsTriggerHandleValid(handle)) {
132 return false;
133 }
134
135 std::string trigger_name = GetTriggerNameFromHandle(handle);
136
137 if (config_->mode() == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) {
138 BackgroundTracingPreemptiveConfig* preemptive_config =
139 static_cast<BackgroundTracingPreemptiveConfig*>(config_.get());
140
141 const std::vector<BackgroundTracingPreemptiveConfig::MonitoringRule>&
142 configs = preemptive_config->configs();
143
144 for (size_t i = 0; i < configs.size(); ++i) {
145 if (configs[i].type != BackgroundTracingPreemptiveConfig::
146 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED)
147 continue;
148
149 if (trigger_name == configs[i].named_trigger_info.trigger_name) {
150 return true;
151 }
152 }
153 } else {
154 // TODO(simonhatch): Implement reactive path.
155 NOTREACHED();
156 }
157
158 return false;
159 }
160
161 void BackgroundTracingManagerImpl::DidTriggerHappen(
162 BackgroundTracingManagerImpl::TriggerHandle handle,
163 StartedFinalizingCallback callback) {
164 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
165 content::BrowserThread::PostTask(
166 content::BrowserThread::UI, FROM_HERE,
167 base::Bind(&BackgroundTracingManagerImpl::DidTriggerHappen,
168 base::Unretained(this), handle, callback));
169 return;
170 }
171
172 if (!IsAbleToTriggerTracing(handle)) {
173 if (!callback.is_null())
174 callback.Run(false);
175 return;
176 }
177
178 if (config_->mode() == BackgroundTracingConfig::PREEMPTIVE_TRACING_MODE) {
179 BeginFinalizing(callback);
180 } else {
181 // TODO(simonhatch): Implement reactive tracing path.
182 NOTREACHED();
183 }
184 }
185
186 BackgroundTracingManagerImpl::TriggerHandle
187 BackgroundTracingManagerImpl::RegisterTriggerType(const char* trigger_name) {
188 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
189
190 trigger_handle_ids_ += 1;
191
192 trigger_handles_.insert(
193 std::pair<TriggerHandle, std::string>(trigger_handle_ids_, trigger_name));
194
195 return static_cast<TriggerHandle>(trigger_handle_ids_);
196 }
197
198 bool BackgroundTracingManagerImpl::IsTriggerHandleValid(
199 BackgroundTracingManager::TriggerHandle handle) const {
200 return trigger_handles_.find(handle) != trigger_handles_.end();
201 }
202
203 std::string BackgroundTracingManagerImpl::GetTriggerNameFromHandle(
204 BackgroundTracingManager::TriggerHandle handle) const {
205 CHECK(IsTriggerHandleValid(handle));
206 return trigger_handles_.find(handle)->second;
207 }
208
209 void BackgroundTracingManagerImpl::GetTriggerNameList(
210 std::vector<std::string>& trigger_names) {
211 for (std::map<TriggerHandle, std::string>::iterator it =
212 trigger_handles_.begin();
213 it != trigger_handles_.end(); ++it)
214 trigger_names.push_back(it->second);
215 }
216
217 void BackgroundTracingManagerImpl::InvalidateTriggerHandlesForTesting() {
218 trigger_handles_.clear();
219 }
220
221 void BackgroundTracingManagerImpl::EnableRecording(
222 base::trace_event::CategoryFilter category_filter) {
223 TracingController::GetInstance()->EnableRecording(
224 category_filter,
225 base::trace_event::TraceOptions(base::trace_event::RECORD_CONTINUOUSLY),
226 TracingController::EnableRecordingDoneCallback());
227 }
228
229 void BackgroundTracingManagerImpl::OnFinalizeComplete() {
230 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
231 BrowserThread::PostTask(
232 BrowserThread::UI, FROM_HERE,
233 base::Bind(&BackgroundTracingManagerImpl::OnFinalizeComplete,
234 base::Unretained(this)));
235 return;
236 }
237
238 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
239
240 is_gathering_ = false;
241
242 if (!idle_callback_.is_null())
243 idle_callback_.Run();
244
245 // Now that a trace has completed, we may need to enable recording again.
246 EnableRecordingIfConfigNeedsIt();
247 }
248
249 void BackgroundTracingManagerImpl::BeginFinalizing(
250 StartedFinalizingCallback callback) {
251 is_gathering_ = true;
252
253 content::TracingController::GetInstance()->DisableRecording(
254 content::TracingController::CreateCompressedStringSink(
255 data_endpoint_wrapper_));
256
257 if (!callback.is_null())
258 callback.Run(true);
259 }
260
261 base::trace_event::CategoryFilter
262 BackgroundTracingManagerImpl::GetCategoryFilterForCategoryPreset(
263 BackgroundTracingConfig::CategoryPreset preset) const {
264 switch (preset) {
265 case BackgroundTracingConfig::CategoryPreset::BENCHMARK:
266 return base::trace_event::CategoryFilter(
267 "benchmark,"
268 "disabled-by-default-toplevel.flow,"
269 "disabled-by-default-ipc.flow");
270 case BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP:
271 return base::trace_event::CategoryFilter(
272 "*,disabled-by-default-blink.debug.layout");
273 }
274 NOTREACHED();
275 return base::trace_event::CategoryFilter();
276 }
277
278 } // namspace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698