OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 | 4 |
5 #include "content/browser/tracing/tracing_controller_impl.h" | 5 #include "content/browser/tracing/tracing_controller_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 file_util::CloseFile(file_); | 106 file_util::CloseFile(file_); |
107 file_ = NULL; | 107 file_ = NULL; |
108 | 108 |
109 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); | 109 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
110 } | 110 } |
111 | 111 |
112 | 112 |
113 TracingControllerImpl::TracingControllerImpl() : | 113 TracingControllerImpl::TracingControllerImpl() : |
114 pending_disable_recording_ack_count_(0), | 114 pending_disable_recording_ack_count_(0), |
115 pending_capture_monitoring_snapshot_ack_count_(0), | 115 pending_capture_monitoring_snapshot_ack_count_(0), |
116 is_recording_(false), | 116 // Tracing may have been enabled by ContentMainRunner if kTraceStartup |
| 117 // is specified in command line. |
| 118 is_recording_(TraceLog::GetInstance()->IsEnabled()), |
117 is_monitoring_(false), | 119 is_monitoring_(false), |
118 trace_options_(TraceLog::RECORD_UNTIL_FULL), | |
119 category_filter_( | 120 category_filter_( |
120 base::debug::CategoryFilter::kDefaultCategoryFilterString) { | 121 base::debug::CategoryFilter::kDefaultCategoryFilterString) { |
121 } | 122 } |
122 | 123 |
123 TracingControllerImpl::~TracingControllerImpl() { | 124 TracingControllerImpl::~TracingControllerImpl() { |
124 // This is a Leaky instance. | 125 // This is a Leaky instance. |
125 NOTREACHED(); | 126 NOTREACHED(); |
126 } | 127 } |
127 | 128 |
128 TracingControllerImpl* TracingControllerImpl::GetInstance() { | 129 TracingControllerImpl* TracingControllerImpl::GetInstance() { |
(...skipping 21 matching lines...) Expand all Loading... |
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
151 | 152 |
152 if (!can_enable_recording()) | 153 if (!can_enable_recording()) |
153 return false; | 154 return false; |
154 | 155 |
155 #if defined(OS_ANDROID) | 156 #if defined(OS_ANDROID) |
156 if (pending_get_categories_done_callback_.is_null()) | 157 if (pending_get_categories_done_callback_.is_null()) |
157 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); | 158 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); |
158 #endif | 159 #endif |
159 | 160 |
160 trace_options_ = TraceLog::GetInstance()->trace_options(); | 161 TraceLog::Options trace_options = TraceLog::GetInstance()->trace_options(); |
161 TraceLog::GetInstance()->SetEnabled(filter, trace_options_); | 162 TraceLog::GetInstance()->SetEnabled(filter, trace_options); |
162 | 163 |
163 is_recording_ = true; | 164 is_recording_ = true; |
164 category_filter_ = TraceLog::GetInstance()->GetCurrentCategoryFilter(); | 165 category_filter_ = TraceLog::GetInstance()->GetCurrentCategoryFilter(); |
165 | 166 |
166 // Notify all child processes. | 167 // Notify all child processes. |
167 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | 168 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { |
168 it->get()->SendBeginTracing( | 169 it->get()->SendBeginTracing(category_filter_.ToString(), trace_options); |
169 category_filter_.ToString(), trace_options_, false); | |
170 } | 170 } |
171 | 171 |
172 if (!callback.is_null()) | 172 if (!callback.is_null()) |
173 callback.Run(); | 173 callback.Run(); |
174 return true; | 174 return true; |
175 } | 175 } |
176 | 176 |
177 bool TracingControllerImpl::DisableRecording( | 177 bool TracingControllerImpl::DisableRecording( |
178 const base::FilePath& result_file_path, | 178 const base::FilePath& result_file_path, |
179 const TracingFileResultCallback& callback) { | 179 const TracingFileResultCallback& callback) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 is_monitoring_ = true; | 232 is_monitoring_ = true; |
233 | 233 |
234 #if defined(OS_ANDROID) | 234 #if defined(OS_ANDROID) |
235 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); | 235 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); |
236 #endif | 236 #endif |
237 | 237 |
238 int monitoring_tracing_options = 0; | 238 int monitoring_tracing_options = 0; |
239 if (options & ENABLE_SAMPLING) | 239 if (options & ENABLE_SAMPLING) |
240 monitoring_tracing_options |= base::debug::TraceLog::MONITOR_SAMPLING; | 240 monitoring_tracing_options |= base::debug::TraceLog::MONITOR_SAMPLING; |
241 | 241 |
| 242 TraceLog::GetInstance()->SetEnabled( |
| 243 filter, base::debug::TraceLog::Options(monitoring_tracing_options)); |
| 244 |
242 // Notify all child processes. | 245 // Notify all child processes. |
243 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | 246 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { |
244 it->get()->SendEnableMonitoring(filter.ToString(), | 247 it->get()->SendEnableMonitoring(filter.ToString(), |
245 base::debug::TraceLog::Options(monitoring_tracing_options)); | 248 base::debug::TraceLog::Options(monitoring_tracing_options)); |
246 } | 249 } |
247 | 250 |
248 if (!callback.is_null()) | 251 if (!callback.is_null()) |
249 callback.Run(); | 252 callback.Run(); |
250 return true; | 253 return true; |
251 } | 254 } |
252 | 255 |
253 bool TracingControllerImpl::DisableMonitoring( | 256 bool TracingControllerImpl::DisableMonitoring( |
254 const DisableMonitoringDoneCallback& callback) { | 257 const DisableMonitoringDoneCallback& callback) { |
255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
256 | 259 |
257 if (!can_disable_monitoring()) | 260 if (!can_disable_monitoring()) |
258 return false; | 261 return false; |
259 is_monitoring_ = false; | 262 is_monitoring_ = false; |
260 | 263 |
| 264 TraceLog::GetInstance()->SetDisabled(); |
| 265 |
261 // Notify all child processes. | 266 // Notify all child processes. |
262 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | 267 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { |
263 it->get()->SendDisableMonitoring(); | 268 it->get()->SendDisableMonitoring(); |
264 } | 269 } |
265 | 270 |
266 if (!callback.is_null()) | 271 if (!callback.is_null()) |
267 callback.Run(); | 272 callback.Run(); |
268 return true; | 273 return true; |
269 } | 274 } |
270 | 275 |
271 void TracingControllerImpl::GetMonitoringStatus( | 276 void TracingControllerImpl::GetMonitoringStatus( |
272 bool* out_enabled, | 277 bool* out_enabled, |
273 base::debug::CategoryFilter* out_filter, | 278 base::debug::CategoryFilter* out_filter, |
274 TracingController::Options* out_options) { | 279 TracingController::Options* out_options) { |
275 NOTIMPLEMENTED(); | 280 NOTIMPLEMENTED(); |
276 } | 281 } |
277 | 282 |
278 void TracingControllerImpl::CaptureMonitoringSnapshot( | 283 void TracingControllerImpl::CaptureMonitoringSnapshot( |
279 const base::FilePath& result_file_path, | 284 const base::FilePath& result_file_path, |
280 const TracingFileResultCallback& callback) { | 285 const TracingFileResultCallback& callback) { |
281 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
282 | 287 |
283 if (!can_disable_monitoring()) | 288 if (!can_disable_monitoring()) |
284 return; | 289 return; |
285 | 290 |
| 291 if (callback.is_null() && result_file_path.empty()) |
| 292 return; |
| 293 |
286 pending_capture_monitoring_snapshot_done_callback_ = callback; | 294 pending_capture_monitoring_snapshot_done_callback_ = callback; |
287 | 295 monitoring_snapshot_file_.reset(new ResultFile(result_file_path)); |
288 if (!callback.is_null() || !result_file_path.empty()) | |
289 monitoring_snapshot_file_.reset(new ResultFile(result_file_path)); | |
290 | 296 |
291 // There could be a case where there are no child processes and filters_ | 297 // There could be a case where there are no child processes and filters_ |
292 // is empty. In that case we can immediately tell the subscriber that tracing | 298 // is empty. In that case we can immediately tell the subscriber that tracing |
293 // has ended. To avoid recursive calls back to the subscriber, we will just | 299 // has ended. To avoid recursive calls back to the subscriber, we will just |
294 // use the existing asynchronous OnCaptureMonitoringSnapshotAcked code. | 300 // use the existing asynchronous OnCaptureMonitoringSnapshotAcked code. |
295 // Count myself in pending_capture_monitoring_snapshot_ack_count_, | 301 // Count myself in pending_capture_monitoring_snapshot_ack_count_, |
296 // acked below. | 302 // acked below. |
297 pending_capture_monitoring_snapshot_ack_count_ = filters_.size() + 1; | 303 pending_capture_monitoring_snapshot_ack_count_ = filters_.size() + 1; |
298 | 304 |
299 // Handle special case of zero child processes. | 305 // Handle special case of zero child processes. |
(...skipping 18 matching lines...) Expand all Loading... |
318 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 324 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
319 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 325 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
320 base::Bind(&TracingControllerImpl::AddFilter, base::Unretained(this), | 326 base::Bind(&TracingControllerImpl::AddFilter, base::Unretained(this), |
321 make_scoped_refptr(filter))); | 327 make_scoped_refptr(filter))); |
322 return; | 328 return; |
323 } | 329 } |
324 | 330 |
325 filters_.insert(filter); | 331 filters_.insert(filter); |
326 if (can_disable_recording()) { | 332 if (can_disable_recording()) { |
327 std::string cf_str = category_filter_.ToString(); | 333 std::string cf_str = category_filter_.ToString(); |
328 filter->SendBeginTracing(cf_str, trace_options_, false); | 334 filter->SendBeginTracing(cf_str, TraceLog::GetInstance()->trace_options()); |
329 } | 335 } |
330 } | 336 } |
331 | 337 |
332 void TracingControllerImpl::RemoveFilter(TraceMessageFilter* filter) { | 338 void TracingControllerImpl::RemoveFilter(TraceMessageFilter* filter) { |
333 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 339 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
334 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 340 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
335 base::Bind(&TracingControllerImpl::RemoveFilter, base::Unretained(this), | 341 base::Bind(&TracingControllerImpl::RemoveFilter, base::Unretained(this), |
336 make_scoped_refptr(filter))); | 342 make_scoped_refptr(filter))); |
337 return; | 343 return; |
338 } | 344 } |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 | 463 |
458 void TracingControllerImpl::OnMonitoringTraceDataCollected( | 464 void TracingControllerImpl::OnMonitoringTraceDataCollected( |
459 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | 465 const scoped_refptr<base::RefCountedString>& events_str_ptr) { |
460 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 466 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
461 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 467 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
462 base::Bind(&TracingControllerImpl::OnMonitoringTraceDataCollected, | 468 base::Bind(&TracingControllerImpl::OnMonitoringTraceDataCollected, |
463 base::Unretained(this), events_str_ptr)); | 469 base::Unretained(this), events_str_ptr)); |
464 return; | 470 return; |
465 } | 471 } |
466 | 472 |
467 if (!monitoring_snapshot_file_) | 473 if (monitoring_snapshot_file_) |
468 monitoring_snapshot_file_->Write(events_str_ptr); | 474 monitoring_snapshot_file_->Write(events_str_ptr); |
469 } | 475 } |
470 | 476 |
471 void TracingControllerImpl::OnLocalTraceDataCollected( | 477 void TracingControllerImpl::OnLocalTraceDataCollected( |
472 const scoped_refptr<base::RefCountedString>& events_str_ptr, | 478 const scoped_refptr<base::RefCountedString>& events_str_ptr, |
473 bool has_more_events) { | 479 bool has_more_events) { |
474 if (events_str_ptr->data().size()) | 480 if (events_str_ptr->data().size()) |
475 OnTraceDataCollected(events_str_ptr); | 481 OnTraceDataCollected(events_str_ptr); |
476 | 482 |
477 if (has_more_events) | 483 if (has_more_events) |
(...skipping 12 matching lines...) Expand all Loading... |
490 OnMonitoringTraceDataCollected(events_str_ptr); | 496 OnMonitoringTraceDataCollected(events_str_ptr); |
491 | 497 |
492 if (has_more_events) | 498 if (has_more_events) |
493 return; | 499 return; |
494 | 500 |
495 // Simulate an CaptureMonitoringSnapshotAcked for the local trace. | 501 // Simulate an CaptureMonitoringSnapshotAcked for the local trace. |
496 OnCaptureMonitoringSnapshotAcked(); | 502 OnCaptureMonitoringSnapshotAcked(); |
497 } | 503 } |
498 | 504 |
499 } // namespace content | 505 } // namespace content |
OLD | NEW |