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/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 | 155 |
156 DISALLOW_COPY_AND_ASSIGN(StringTraceDataSink); | 156 DISALLOW_COPY_AND_ASSIGN(StringTraceDataSink); |
157 }; | 157 }; |
158 | 158 |
159 } // namespace | 159 } // namespace |
160 | 160 |
161 TracingController* TracingController::GetInstance() { | 161 TracingController* TracingController::GetInstance() { |
162 return TracingControllerImpl::GetInstance(); | 162 return TracingControllerImpl::GetInstance(); |
163 } | 163 } |
164 | 164 |
165 TracingControllerImpl::TracingControllerImpl() : | 165 TracingControllerImpl::TracingControllerImpl() |
166 pending_disable_recording_ack_count_(0), | 166 : pending_disable_recording_ack_count_(0), |
167 pending_capture_monitoring_snapshot_ack_count_(0), | 167 pending_capture_monitoring_snapshot_ack_count_(0), |
168 pending_trace_buffer_percent_full_ack_count_(0), | 168 pending_trace_buffer_usage_ack_count_(0), |
169 maximum_trace_buffer_percent_full_(0), | 169 maximum_trace_buffer_usage_(0), |
| 170 approximate_event_count_(0), |
170 // Tracing may have been enabled by ContentMainRunner if kTraceStartup | 171 // Tracing may have been enabled by ContentMainRunner if kTraceStartup |
171 // is specified in command line. | 172 // is specified in command line. |
172 #if defined(OS_CHROMEOS) || defined(OS_WIN) | 173 #if defined(OS_CHROMEOS) || defined(OS_WIN) |
173 is_system_tracing_(false), | 174 is_system_tracing_(false), |
174 #endif | 175 #endif |
175 is_recording_(TraceLog::GetInstance()->IsEnabled()), | 176 is_recording_(TraceLog::GetInstance()->IsEnabled()), |
176 is_monitoring_(false) { | 177 is_monitoring_(false) { |
177 } | 178 } |
178 | 179 |
179 TracingControllerImpl::~TracingControllerImpl() { | 180 TracingControllerImpl::~TracingControllerImpl() { |
180 // This is a Leaky instance. | 181 // This is a Leaky instance. |
181 NOTREACHED(); | 182 NOTREACHED(); |
182 } | 183 } |
183 | 184 |
184 TracingControllerImpl* TracingControllerImpl::GetInstance() { | 185 TracingControllerImpl* TracingControllerImpl::GetInstance() { |
185 return g_controller.Pointer(); | 186 return g_controller.Pointer(); |
186 } | 187 } |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 it->get()->SendCaptureMonitoringSnapshot(); | 493 it->get()->SendCaptureMonitoringSnapshot(); |
493 } | 494 } |
494 | 495 |
495 #if defined(OS_ANDROID) | 496 #if defined(OS_ANDROID) |
496 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); | 497 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); |
497 #endif | 498 #endif |
498 | 499 |
499 return true; | 500 return true; |
500 } | 501 } |
501 | 502 |
502 bool TracingControllerImpl::GetTraceBufferPercentFull( | 503 bool TracingControllerImpl::GetTraceBufferUsage( |
503 const GetTraceBufferPercentFullCallback& callback) { | 504 const GetTraceBufferUsageCallback& callback) { |
504 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 505 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
505 | 506 |
506 if (!can_get_trace_buffer_percent_full() || callback.is_null()) | 507 if (!can_get_trace_buffer_usage() || callback.is_null()) |
507 return false; | 508 return false; |
508 | 509 |
509 pending_trace_buffer_percent_full_callback_ = callback; | 510 pending_trace_buffer_usage_callback_ = callback; |
510 | 511 |
511 // Count myself in pending_trace_buffer_percent_full_ack_count_, acked below. | 512 // Count myself in pending_trace_buffer_usage_ack_count_, acked below. |
512 pending_trace_buffer_percent_full_ack_count_ = | 513 pending_trace_buffer_usage_ack_count_ = trace_message_filters_.size() + 1; |
513 trace_message_filters_.size() + 1; | 514 pending_trace_buffer_usage_filters_ = trace_message_filters_; |
514 pending_trace_buffer_percent_full_filters_ = trace_message_filters_; | 515 maximum_trace_buffer_usage_ = 0; |
515 maximum_trace_buffer_percent_full_ = 0; | 516 approximate_event_count_ = 0; |
516 | 517 |
517 // Call OnTraceBufferPercentFullReply unconditionally for the browser process. | 518 TraceLog::BufferUsage buffer_usage = |
| 519 TraceLog::GetInstance()->GetBufferUsage(); |
| 520 // Call OnTraceBufferUsageReply unconditionally for the browser process. |
518 // This will result in immediate execution of the callback if there are no | 521 // This will result in immediate execution of the callback if there are no |
519 // child processes. | 522 // child processes. |
520 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 523 BrowserThread::PostTask( |
521 base::Bind(&TracingControllerImpl::OnTraceBufferPercentFullReply, | 524 BrowserThread::UI, FROM_HERE, |
522 base::Unretained(this), | 525 base::Bind(&TracingControllerImpl::OnTraceBufferUsageReply, |
523 scoped_refptr<TraceMessageFilter>(), | 526 base::Unretained(this), scoped_refptr<TraceMessageFilter>(), |
524 TraceLog::GetInstance()->GetBufferPercentFull())); | 527 buffer_usage.event_capacity, buffer_usage.event_count)); |
525 | 528 |
526 // Notify all child processes. | 529 // Notify all child processes. |
527 for (TraceMessageFilterSet::iterator it = trace_message_filters_.begin(); | 530 for (TraceMessageFilterSet::iterator it = trace_message_filters_.begin(); |
528 it != trace_message_filters_.end(); ++it) { | 531 it != trace_message_filters_.end(); ++it) { |
529 it->get()->SendGetTraceBufferPercentFull(); | 532 it->get()->SendGetTraceBufferUsage(); |
530 } | 533 } |
531 return true; | 534 return true; |
532 } | 535 } |
533 | 536 |
534 bool TracingControllerImpl::SetWatchEvent( | 537 bool TracingControllerImpl::SetWatchEvent( |
535 const std::string& category_name, | 538 const std::string& category_name, |
536 const std::string& event_name, | 539 const std::string& event_name, |
537 const WatchEventCallback& callback) { | 540 const WatchEventCallback& callback) { |
538 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 541 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
539 | 542 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 if (pending_capture_monitoring_snapshot_ack_count_ > 0) { | 628 if (pending_capture_monitoring_snapshot_ack_count_ > 0) { |
626 TraceMessageFilterSet::const_iterator it = | 629 TraceMessageFilterSet::const_iterator it = |
627 pending_capture_monitoring_filters_.find(trace_message_filter); | 630 pending_capture_monitoring_filters_.find(trace_message_filter); |
628 if (it != pending_capture_monitoring_filters_.end()) { | 631 if (it != pending_capture_monitoring_filters_.end()) { |
629 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 632 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
630 base::Bind(&TracingControllerImpl::OnCaptureMonitoringSnapshotAcked, | 633 base::Bind(&TracingControllerImpl::OnCaptureMonitoringSnapshotAcked, |
631 base::Unretained(this), | 634 base::Unretained(this), |
632 make_scoped_refptr(trace_message_filter))); | 635 make_scoped_refptr(trace_message_filter))); |
633 } | 636 } |
634 } | 637 } |
635 if (pending_trace_buffer_percent_full_ack_count_ > 0) { | 638 if (pending_trace_buffer_usage_ack_count_ > 0) { |
636 TraceMessageFilterSet::const_iterator it = | 639 TraceMessageFilterSet::const_iterator it = |
637 pending_trace_buffer_percent_full_filters_.find(trace_message_filter); | 640 pending_trace_buffer_usage_filters_.find(trace_message_filter); |
638 if (it != pending_trace_buffer_percent_full_filters_.end()) { | 641 if (it != pending_trace_buffer_usage_filters_.end()) { |
639 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 642 BrowserThread::PostTask( |
640 base::Bind(&TracingControllerImpl::OnTraceBufferPercentFullReply, | 643 BrowserThread::UI, FROM_HERE, |
| 644 base::Bind(&TracingControllerImpl::OnTraceBufferUsageReply, |
641 base::Unretained(this), | 645 base::Unretained(this), |
642 make_scoped_refptr(trace_message_filter), | 646 make_scoped_refptr(trace_message_filter), 0, 0)); |
643 0)); | |
644 } | 647 } |
645 } | 648 } |
646 | 649 |
647 trace_message_filters_.erase(trace_message_filter); | 650 trace_message_filters_.erase(trace_message_filter); |
648 } | 651 } |
649 | 652 |
650 void TracingControllerImpl::OnDisableRecordingAcked( | 653 void TracingControllerImpl::OnDisableRecordingAcked( |
651 TraceMessageFilter* trace_message_filter, | 654 TraceMessageFilter* trace_message_filter, |
652 const std::vector<std::string>& known_category_groups) { | 655 const std::vector<std::string>& known_category_groups) { |
653 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 656 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 if (events_str_ptr->data().size()) | 811 if (events_str_ptr->data().size()) |
809 OnMonitoringTraceDataCollected(events_str_ptr); | 812 OnMonitoringTraceDataCollected(events_str_ptr); |
810 | 813 |
811 if (has_more_events) | 814 if (has_more_events) |
812 return; | 815 return; |
813 | 816 |
814 // Simulate an CaptureMonitoringSnapshotAcked for the local trace. | 817 // Simulate an CaptureMonitoringSnapshotAcked for the local trace. |
815 OnCaptureMonitoringSnapshotAcked(NULL); | 818 OnCaptureMonitoringSnapshotAcked(NULL); |
816 } | 819 } |
817 | 820 |
818 void TracingControllerImpl::OnTraceBufferPercentFullReply( | 821 void TracingControllerImpl::OnTraceBufferUsageReply( |
819 TraceMessageFilter* trace_message_filter, | 822 TraceMessageFilter* trace_message_filter, |
820 float percent_full) { | 823 size_t event_capacity, |
| 824 size_t event_count) { |
821 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 825 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
822 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 826 BrowserThread::PostTask( |
823 base::Bind(&TracingControllerImpl::OnTraceBufferPercentFullReply, | 827 BrowserThread::UI, FROM_HERE, |
| 828 base::Bind(&TracingControllerImpl::OnTraceBufferUsageReply, |
824 base::Unretained(this), | 829 base::Unretained(this), |
825 make_scoped_refptr(trace_message_filter), | 830 make_scoped_refptr(trace_message_filter), event_capacity, |
826 percent_full)); | 831 event_count)); |
827 return; | 832 return; |
828 } | 833 } |
829 | 834 |
830 if (pending_trace_buffer_percent_full_ack_count_ == 0) | 835 if (pending_trace_buffer_usage_ack_count_ == 0) |
831 return; | 836 return; |
832 | 837 |
833 if (trace_message_filter && | 838 if (trace_message_filter && |
834 !pending_trace_buffer_percent_full_filters_.erase(trace_message_filter)) { | 839 !pending_trace_buffer_usage_filters_.erase(trace_message_filter)) { |
835 // The response from the specified message filter has already been received. | 840 // The response from the specified message filter has already been received. |
836 return; | 841 return; |
837 } | 842 } |
838 | 843 |
839 maximum_trace_buffer_percent_full_ = | 844 float percent_full = |
840 std::max(maximum_trace_buffer_percent_full_, percent_full); | 845 static_cast<float>(static_cast<double>(event_count) / event_capacity); |
| 846 maximum_trace_buffer_usage_ = |
| 847 std::max(maximum_trace_buffer_usage_, percent_full); |
| 848 approximate_event_count_ += event_count; |
841 | 849 |
842 if (--pending_trace_buffer_percent_full_ack_count_ == 0) { | 850 if (--pending_trace_buffer_usage_ack_count_ == 0) { |
843 // Trigger callback if one is set. | 851 // Trigger callback if one is set. |
844 pending_trace_buffer_percent_full_callback_.Run( | 852 pending_trace_buffer_usage_callback_.Run(maximum_trace_buffer_usage_, |
845 maximum_trace_buffer_percent_full_); | 853 approximate_event_count_); |
846 pending_trace_buffer_percent_full_callback_.Reset(); | 854 pending_trace_buffer_usage_callback_.Reset(); |
847 } | 855 } |
848 } | 856 } |
849 | 857 |
850 void TracingControllerImpl::OnWatchEventMatched() { | 858 void TracingControllerImpl::OnWatchEventMatched() { |
851 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 859 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
852 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 860 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
853 base::Bind(&TracingControllerImpl::OnWatchEventMatched, | 861 base::Bind(&TracingControllerImpl::OnWatchEventMatched, |
854 base::Unretained(this))); | 862 base::Unretained(this))); |
855 return; | 863 return; |
856 } | 864 } |
(...skipping 20 matching lines...) Expand all Loading... |
877 is_monitoring_ = is_monitoring; | 885 is_monitoring_ = is_monitoring; |
878 #if !defined(OS_ANDROID) | 886 #if !defined(OS_ANDROID) |
879 for (std::set<TracingUI*>::iterator it = tracing_uis_.begin(); | 887 for (std::set<TracingUI*>::iterator it = tracing_uis_.begin(); |
880 it != tracing_uis_.end(); it++) { | 888 it != tracing_uis_.end(); it++) { |
881 (*it)->OnMonitoringStateChanged(is_monitoring); | 889 (*it)->OnMonitoringStateChanged(is_monitoring); |
882 } | 890 } |
883 #endif | 891 #endif |
884 } | 892 } |
885 | 893 |
886 } // namespace content | 894 } // namespace content |
OLD | NEW |