| 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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 DCHECK(written == 1); | 87 DCHECK(written == 1); |
| 88 file_util::CloseFile(file_); | 88 file_util::CloseFile(file_); |
| 89 file_ = NULL; | 89 file_ = NULL; |
| 90 base::ThreadRestrictions::SetIOAllowed(old_io_allowed); | 90 base::ThreadRestrictions::SetIOAllowed(old_io_allowed); |
| 91 } | 91 } |
| 92 | 92 |
| 93 | 93 |
| 94 TracingControllerImpl::TracingControllerImpl() : | 94 TracingControllerImpl::TracingControllerImpl() : |
| 95 pending_disable_recording_ack_count_(0), | 95 pending_disable_recording_ack_count_(0), |
| 96 pending_capture_monitoring_snapshot_ack_count_(0), | 96 pending_capture_monitoring_snapshot_ack_count_(0), |
| 97 pending_trace_buffer_percent_full_ack_count_(0), |
| 98 maximum_trace_buffer_percent_full_(0), |
| 97 // Tracing may have been enabled by ContentMainRunner if kTraceStartup | 99 // Tracing may have been enabled by ContentMainRunner if kTraceStartup |
| 98 // is specified in command line. | 100 // is specified in command line. |
| 99 is_recording_(TraceLog::GetInstance()->IsEnabled()), | 101 is_recording_(TraceLog::GetInstance()->IsEnabled()), |
| 100 is_monitoring_(false), | 102 is_monitoring_(false), |
| 101 category_filter_( | 103 category_filter_( |
| 102 base::debug::CategoryFilter::kDefaultCategoryFilterString) { | 104 base::debug::CategoryFilter::kDefaultCategoryFilterString) { |
| 103 } | 105 } |
| 104 | 106 |
| 105 TracingControllerImpl::~TracingControllerImpl() { | 107 TracingControllerImpl::~TracingControllerImpl() { |
| 106 // This is a Leaky instance. | 108 // This is a Leaky instance. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 133 | 135 |
| 134 if (!can_enable_recording()) | 136 if (!can_enable_recording()) |
| 135 return false; | 137 return false; |
| 136 | 138 |
| 137 #if defined(OS_ANDROID) | 139 #if defined(OS_ANDROID) |
| 138 if (pending_get_categories_done_callback_.is_null()) | 140 if (pending_get_categories_done_callback_.is_null()) |
| 139 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); | 141 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); |
| 140 #endif | 142 #endif |
| 141 | 143 |
| 142 TraceLog::Options trace_options = TraceLog::GetInstance()->trace_options(); | 144 TraceLog::Options trace_options = (options & RECORD_CONTINUOUSLY) ? |
| 145 TraceLog::RECORD_CONTINUOUSLY : TraceLog::RECORD_UNTIL_FULL; |
| 146 if (options & ENABLE_SAMPLING) { |
| 147 trace_options = static_cast<TraceLog::Options>( |
| 148 trace_options | TraceLog::ENABLE_SAMPLING); |
| 149 } |
| 150 // TODO(haraken): How to handle ENABLE_SYSTRACE? |
| 151 |
| 143 TraceLog::GetInstance()->SetEnabled(filter, trace_options); | 152 TraceLog::GetInstance()->SetEnabled(filter, trace_options); |
| 144 | |
| 145 is_recording_ = true; | 153 is_recording_ = true; |
| 146 category_filter_ = TraceLog::GetInstance()->GetCurrentCategoryFilter(); | 154 category_filter_ = TraceLog::GetInstance()->GetCurrentCategoryFilter(); |
| 147 | 155 |
| 148 // Notify all child processes. | 156 // Notify all child processes. |
| 149 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | 157 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { |
| 150 it->get()->SendBeginTracing(category_filter_.ToString(), trace_options); | 158 it->get()->SendBeginTracing(category_filter_.ToString(), trace_options); |
| 151 } | 159 } |
| 152 | 160 |
| 153 if (!callback.is_null()) | 161 if (!callback.is_null()) |
| 154 callback.Run(); | 162 callback.Run(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 // Notify all child processes. | 296 // Notify all child processes. |
| 289 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { | 297 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { |
| 290 it->get()->SendCaptureMonitoringSnapshot(); | 298 it->get()->SendCaptureMonitoringSnapshot(); |
| 291 } | 299 } |
| 292 | 300 |
| 293 #if defined(OS_ANDROID) | 301 #if defined(OS_ANDROID) |
| 294 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); | 302 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); |
| 295 #endif | 303 #endif |
| 296 } | 304 } |
| 297 | 305 |
| 306 bool TracingControllerImpl::GetTraceBufferPercentFull( |
| 307 const GetTraceBufferPercentFullCallback& callback) { |
| 308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 309 |
| 310 if (!can_get_trace_buffer_percent_full() || callback.is_null()) |
| 311 return false; |
| 312 |
| 313 pending_trace_buffer_percent_full_callback_ = callback; |
| 314 |
| 315 // Count myself in pending_trace_buffer_percent_full_ack_count_, acked below. |
| 316 pending_trace_buffer_percent_full_ack_count_ = filters_.size() + 1; |
| 317 |
| 318 // Handle special case of zero child processes. |
| 319 if (pending_trace_buffer_percent_full_ack_count_ == 1) { |
| 320 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 321 base::Bind(&TracingControllerImpl::OnTraceBufferPercentFullReply, |
| 322 base::Unretained(this), |
| 323 TraceLog::GetInstance()->GetBufferPercentFull())); |
| 324 } |
| 325 |
| 326 // Notify all child processes. |
| 327 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) { |
| 328 it->get()->SendGetTraceBufferPercentFull(); |
| 329 } |
| 330 return true; |
| 331 } |
| 332 |
| 298 void TracingControllerImpl::AddFilter(TraceMessageFilter* filter) { | 333 void TracingControllerImpl::AddFilter(TraceMessageFilter* filter) { |
| 299 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 334 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 300 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 335 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 301 base::Bind(&TracingControllerImpl::AddFilter, base::Unretained(this), | 336 base::Bind(&TracingControllerImpl::AddFilter, base::Unretained(this), |
| 302 make_scoped_refptr(filter))); | 337 make_scoped_refptr(filter))); |
| 303 return; | 338 return; |
| 304 } | 339 } |
| 305 | 340 |
| 306 filters_.insert(filter); | 341 filters_.insert(filter); |
| 307 if (can_disable_recording()) { | 342 if (can_disable_recording()) { |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 if (events_str_ptr->data().size()) | 485 if (events_str_ptr->data().size()) |
| 451 OnMonitoringTraceDataCollected(events_str_ptr); | 486 OnMonitoringTraceDataCollected(events_str_ptr); |
| 452 | 487 |
| 453 if (has_more_events) | 488 if (has_more_events) |
| 454 return; | 489 return; |
| 455 | 490 |
| 456 // Simulate an CaptureMonitoringSnapshotAcked for the local trace. | 491 // Simulate an CaptureMonitoringSnapshotAcked for the local trace. |
| 457 OnCaptureMonitoringSnapshotAcked(); | 492 OnCaptureMonitoringSnapshotAcked(); |
| 458 } | 493 } |
| 459 | 494 |
| 495 void TracingControllerImpl::OnTraceBufferPercentFullReply(float percent_full) { |
| 496 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 497 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 498 base::Bind(&TracingControllerImpl::OnTraceBufferPercentFullReply, |
| 499 base::Unretained(this), percent_full)); |
| 500 return; |
| 501 } |
| 502 |
| 503 if (pending_trace_buffer_percent_full_ack_count_ == 0) |
| 504 return; |
| 505 |
| 506 maximum_trace_buffer_percent_full_ = |
| 507 std::max(maximum_trace_buffer_percent_full_, percent_full); |
| 508 |
| 509 if (--pending_trace_buffer_percent_full_ack_count_ == 0) { |
| 510 // Trigger callback if one is set. |
| 511 pending_trace_buffer_percent_full_callback_.Run( |
| 512 maximum_trace_buffer_percent_full_); |
| 513 pending_trace_buffer_percent_full_callback_.Reset(); |
| 514 } |
| 515 |
| 516 if (pending_trace_buffer_percent_full_ack_count_ == 1) { |
| 517 // The last ack represents local trace, so we need to ack it now. Note that |
| 518 // this code only executes if there were child processes. |
| 519 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 520 base::Bind(&TracingControllerImpl::OnTraceBufferPercentFullReply, |
| 521 base::Unretained(this), |
| 522 TraceLog::GetInstance()->GetBufferPercentFull())); |
| 523 } |
| 524 } |
| 525 |
| 460 } // namespace content | 526 } // namespace content |
| OLD | NEW |