| 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 <algorithm> | |
| 7 #include <memory> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/bind.h" | 6 #include "base/bind.h" |
| 11 #include "base/cpu.h" | 7 #include "base/cpu.h" |
| 12 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 13 #include "base/guid.h" | 9 #include "base/guid.h" |
| 14 #include "base/json/string_escape.h" | 10 #include "base/json/string_escape.h" |
| 15 #include "base/macros.h" | 11 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 17 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
| 19 #include "base/threading/sequenced_worker_pool.h" | 15 #include "base/threading/sequenced_worker_pool.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 42 #define ENABLE_POWER_TRACING | 38 #define ENABLE_POWER_TRACING |
| 43 #endif | 39 #endif |
| 44 | 40 |
| 45 #if defined(ENABLE_POWER_TRACING) | 41 #if defined(ENABLE_POWER_TRACING) |
| 46 #include "content/browser/tracing/power_tracing_agent.h" | 42 #include "content/browser/tracing/power_tracing_agent.h" |
| 47 #endif | 43 #endif |
| 48 | 44 |
| 49 #if defined(OS_CHROMEOS) | 45 #if defined(OS_CHROMEOS) |
| 50 #include "chromeos/dbus/dbus_thread_manager.h" | 46 #include "chromeos/dbus/dbus_thread_manager.h" |
| 51 #include "chromeos/dbus/debug_daemon_client.h" | 47 #include "chromeos/dbus/debug_daemon_client.h" |
| 52 #include "content/browser/tracing/arc_tracing_agent.h" | |
| 53 #endif | 48 #endif |
| 54 | 49 |
| 55 #if defined(OS_WIN) | 50 #if defined(OS_WIN) |
| 56 #include "content/browser/tracing/etw_tracing_agent_win.h" | 51 #include "content/browser/tracing/etw_tracing_agent_win.h" |
| 57 #endif | 52 #endif |
| 58 | 53 |
| 59 using base::trace_event::TraceLog; | 54 using base::trace_event::TraceLog; |
| 60 using base::trace_event::TraceConfig; | 55 using base::trace_event::TraceConfig; |
| 61 | 56 |
| 62 namespace content { | 57 namespace content { |
| 63 | 58 |
| 64 namespace { | 59 namespace { |
| 65 | 60 |
| 66 base::LazyInstance<TracingControllerImpl>::Leaky g_controller = | 61 base::LazyInstance<TracingControllerImpl>::Leaky g_controller = |
| 67 LAZY_INSTANCE_INITIALIZER; | 62 LAZY_INSTANCE_INITIALIZER; |
| 68 | 63 |
| 69 const char kChromeTracingAgentName[] = "chrome"; | 64 const char kChromeTracingAgentName[] = "chrome"; |
| 70 const char kETWTracingAgentName[] = "etw"; | 65 const char kETWTracingAgentName[] = "etw"; |
| 71 const char kArcTracingAgentName[] = "arc"; | |
| 72 const char kChromeTraceLabel[] = "traceEvents"; | 66 const char kChromeTraceLabel[] = "traceEvents"; |
| 73 | 67 |
| 74 const int kStartTracingTimeoutSeconds = 30; | 68 const int kStartTracingTimeoutSeconds = 30; |
| 75 const int kIssueClockSyncTimeoutSeconds = 30; | 69 const int kIssueClockSyncTimeoutSeconds = 30; |
| 76 const int kStopTracingRetryTimeMilliseconds = 100; | 70 const int kStopTracingRetryTimeMilliseconds = 100; |
| 77 | 71 |
| 78 std::string GetNetworkTypeString() { | 72 std::string GetNetworkTypeString() { |
| 79 switch (net::NetworkChangeNotifier::GetConnectionType()) { | 73 switch (net::NetworkChangeNotifier::GetConnectionType()) { |
| 80 case net::NetworkChangeNotifier::CONNECTION_ETHERNET: | 74 case net::NetworkChangeNotifier::CONNECTION_ETHERNET: |
| 81 return "Ethernet"; | 75 return "Ethernet"; |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 #if defined(OS_CHROMEOS) | 282 #if defined(OS_CHROMEOS) |
| 289 chromeos::DebugDaemonClient* debug_daemon = | 283 chromeos::DebugDaemonClient* debug_daemon = |
| 290 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | 284 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 291 if (debug_daemon) { | 285 if (debug_daemon) { |
| 292 debug_daemon->StartAgentTracing( | 286 debug_daemon->StartAgentTracing( |
| 293 trace_config, | 287 trace_config, |
| 294 base::Bind(&TracingControllerImpl::OnStartAgentTracingAcked, | 288 base::Bind(&TracingControllerImpl::OnStartAgentTracingAcked, |
| 295 base::Unretained(this))); | 289 base::Unretained(this))); |
| 296 ++pending_start_tracing_ack_count_; | 290 ++pending_start_tracing_ack_count_; |
| 297 } | 291 } |
| 298 | |
| 299 ArcTracingAgent::GetInstance()->StartAgentTracing( | |
| 300 trace_config, | |
| 301 base::Bind(&TracingControllerImpl::OnStartAgentTracingAcked, | |
| 302 base::Unretained(this))); | |
| 303 ++pending_start_tracing_ack_count_; | |
| 304 #elif defined(OS_WIN) | 292 #elif defined(OS_WIN) |
| 305 EtwTracingAgent::GetInstance()->StartAgentTracing( | 293 EtwTracingAgent::GetInstance()->StartAgentTracing( |
| 306 trace_config, | 294 trace_config, |
| 307 base::Bind(&TracingControllerImpl::OnStartAgentTracingAcked, | 295 base::Bind(&TracingControllerImpl::OnStartAgentTracingAcked, |
| 308 base::Unretained(this))); | 296 base::Unretained(this))); |
| 309 ++pending_start_tracing_ack_count_; | 297 ++pending_start_tracing_ack_count_; |
| 310 #endif | 298 #endif |
| 311 } | 299 } |
| 312 | 300 |
| 313 // TraceLog may have been enabled in startup tracing before threads are ready. | 301 // TraceLog may have been enabled in startup tracing before threads are ready. |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 void TracingControllerImpl::AddTracingAgent(const std::string& agent_name) { | 556 void TracingControllerImpl::AddTracingAgent(const std::string& agent_name) { |
| 569 #if defined(OS_CHROMEOS) | 557 #if defined(OS_CHROMEOS) |
| 570 auto* debug_daemon = | 558 auto* debug_daemon = |
| 571 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | 559 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 572 if (agent_name == debug_daemon->GetTracingAgentName()) { | 560 if (agent_name == debug_daemon->GetTracingAgentName()) { |
| 573 additional_tracing_agents_.push_back(debug_daemon); | 561 additional_tracing_agents_.push_back(debug_daemon); |
| 574 debug_daemon->SetStopAgentTracingTaskRunner( | 562 debug_daemon->SetStopAgentTracingTaskRunner( |
| 575 BrowserThread::GetBlockingPool()); | 563 BrowserThread::GetBlockingPool()); |
| 576 return; | 564 return; |
| 577 } | 565 } |
| 578 | |
| 579 if (agent_name == kArcTracingAgentName) { | |
| 580 additional_tracing_agents_.push_back(ArcTracingAgent::GetInstance()); | |
| 581 return; | |
| 582 } | |
| 583 #elif defined(OS_WIN) | 566 #elif defined(OS_WIN) |
| 584 auto* etw_agent = EtwTracingAgent::GetInstance(); | 567 auto* etw_agent = EtwTracingAgent::GetInstance(); |
| 585 if (agent_name == etw_agent->GetTracingAgentName()) { | 568 if (agent_name == etw_agent->GetTracingAgentName()) { |
| 586 additional_tracing_agents_.push_back(etw_agent); | 569 additional_tracing_agents_.push_back(etw_agent); |
| 587 return; | 570 return; |
| 588 } | 571 } |
| 589 #endif | 572 #endif |
| 590 | 573 |
| 591 #if defined(ENABLE_POWER_TRACING) | 574 #if defined(ENABLE_POWER_TRACING) |
| 592 auto* power_agent = PowerTracingAgent::GetInstance(); | 575 auto* power_agent = PowerTracingAgent::GetInstance(); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 } | 660 } |
| 678 | 661 |
| 679 void TracingControllerImpl::OnEndAgentTracingAcked( | 662 void TracingControllerImpl::OnEndAgentTracingAcked( |
| 680 const std::string& agent_name, | 663 const std::string& agent_name, |
| 681 const std::string& events_label, | 664 const std::string& events_label, |
| 682 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | 665 const scoped_refptr<base::RefCountedString>& events_str_ptr) { |
| 683 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 666 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 684 | 667 |
| 685 if (trace_data_sink_.get() && events_str_ptr && | 668 if (trace_data_sink_.get() && events_str_ptr && |
| 686 !events_str_ptr->data().empty()) { | 669 !events_str_ptr->data().empty()) { |
| 670 std::string json_string; |
| 687 if (agent_name == kETWTracingAgentName) { | 671 if (agent_name == kETWTracingAgentName) { |
| 688 // The Windows kernel events are kept into a JSON format stored as string | 672 // The Windows kernel events are kept into a JSON format stored as string |
| 689 // and must not be escaped. | 673 // and must not be escaped. |
| 690 trace_data_sink_->AddAgentTrace(events_label, events_str_ptr->data()); | 674 json_string = events_str_ptr->data(); |
| 691 } else if (agent_name != kArcTracingAgentName) { | 675 } else { |
| 692 // ARC trace data is obtained via systrace. Ignore the empty data. | 676 json_string = base::GetQuotedJSONString(events_str_ptr->data()); |
| 693 // Quote other trace data as JSON strings and merge them into | |
| 694 // |trace_data_sink_|. | |
| 695 trace_data_sink_->AddAgentTrace( | |
| 696 events_label, base::GetQuotedJSONString(events_str_ptr->data())); | |
| 697 } | 677 } |
| 678 trace_data_sink_->AddAgentTrace(events_label, json_string); |
| 698 } | 679 } |
| 699 std::vector<std::string> category_groups; | 680 std::vector<std::string> category_groups; |
| 700 OnStopTracingAcked(NULL, category_groups); | 681 OnStopTracingAcked(NULL, category_groups); |
| 701 } | 682 } |
| 702 | 683 |
| 703 void TracingControllerImpl::OnTraceDataCollected( | 684 void TracingControllerImpl::OnTraceDataCollected( |
| 704 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | 685 const scoped_refptr<base::RefCountedString>& events_str_ptr) { |
| 705 // OnTraceDataCollected may be called from any browser thread, either by the | 686 // OnTraceDataCollected may be called from any browser thread, either by the |
| 706 // local event trace system or from child processes via TraceMessageFilter. | 687 // local event trace system or from child processes via TraceMessageFilter. |
| 707 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 688 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 842 void TracingControllerImpl::RecordClockSyncMarker( | 823 void TracingControllerImpl::RecordClockSyncMarker( |
| 843 const std::string& sync_id, | 824 const std::string& sync_id, |
| 844 const RecordClockSyncMarkerCallback& callback) { | 825 const RecordClockSyncMarkerCallback& callback) { |
| 845 DCHECK(SupportsExplicitClockSync()); | 826 DCHECK(SupportsExplicitClockSync()); |
| 846 | 827 |
| 847 TRACE_EVENT_CLOCK_SYNC_RECEIVER(sync_id); | 828 TRACE_EVENT_CLOCK_SYNC_RECEIVER(sync_id); |
| 848 } | 829 } |
| 849 | 830 |
| 850 void TracingControllerImpl::IssueClockSyncMarker() { | 831 void TracingControllerImpl::IssueClockSyncMarker() { |
| 851 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 832 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 852 DCHECK_EQ(0, pending_clock_sync_ack_count_); | 833 DCHECK(pending_clock_sync_ack_count_ == 0); |
| 853 | 834 |
| 854 for (auto* it : additional_tracing_agents_) { | 835 for (auto* it : additional_tracing_agents_) { |
| 855 if (it->SupportsExplicitClockSync()) { | 836 if (it->SupportsExplicitClockSync()) { |
| 856 it->RecordClockSyncMarker( | 837 it->RecordClockSyncMarker( |
| 857 base::GenerateGUID(), | 838 base::GenerateGUID(), |
| 858 base::Bind(&TracingControllerImpl::OnClockSyncMarkerRecordedByAgent, | 839 base::Bind(&TracingControllerImpl::OnClockSyncMarkerRecordedByAgent, |
| 859 base::Unretained(this))); | 840 base::Unretained(this))); |
| 860 pending_clock_sync_ack_count_++; | 841 pending_clock_sync_ack_count_++; |
| 861 } | 842 } |
| 862 } | 843 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 882 // success indicator instead of having to rely on sentinel issue_ts and | 863 // success indicator instead of having to rely on sentinel issue_ts and |
| 883 // issue_end_ts values to signal failure. | 864 // issue_end_ts values to signal failure. |
| 884 if (!(issue_ts == base::TimeTicks() || issue_end_ts == base::TimeTicks())) | 865 if (!(issue_ts == base::TimeTicks() || issue_end_ts == base::TimeTicks())) |
| 885 TRACE_EVENT_CLOCK_SYNC_ISSUER(sync_id, issue_ts, issue_end_ts); | 866 TRACE_EVENT_CLOCK_SYNC_ISSUER(sync_id, issue_ts, issue_end_ts); |
| 886 | 867 |
| 887 // Timer is not running means that clock sync already timed out. | 868 // Timer is not running means that clock sync already timed out. |
| 888 if (!clock_sync_timer_.IsRunning()) | 869 if (!clock_sync_timer_.IsRunning()) |
| 889 return; | 870 return; |
| 890 | 871 |
| 891 // Stop tracing only if all agents report back. | 872 // Stop tracing only if all agents report back. |
| 892 if (--pending_clock_sync_ack_count_ == 0) { | 873 if(--pending_clock_sync_ack_count_ == 0) { |
| 893 clock_sync_timer_.Stop(); | 874 clock_sync_timer_.Stop(); |
| 894 StopTracingAfterClockSync(); | 875 StopTracingAfterClockSync(); |
| 895 } | 876 } |
| 896 } | 877 } |
| 897 | 878 |
| 898 void TracingControllerImpl::AddFilteredMetadata( | 879 void TracingControllerImpl::AddFilteredMetadata( |
| 899 TracingController::TraceDataSink* sink, | 880 TracingController::TraceDataSink* sink, |
| 900 std::unique_ptr<base::DictionaryValue> metadata, | 881 std::unique_ptr<base::DictionaryValue> metadata, |
| 901 const MetadataFilterPredicate& filter) { | 882 const MetadataFilterPredicate& filter) { |
| 902 if (filter.is_null()) { | 883 if (filter.is_null()) { |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1080 // Schedule the next queued dump (if applicable). | 1061 // Schedule the next queued dump (if applicable). |
| 1081 if (!queued_memory_dump_requests_.empty()) { | 1062 if (!queued_memory_dump_requests_.empty()) { |
| 1082 BrowserThread::PostTask( | 1063 BrowserThread::PostTask( |
| 1083 BrowserThread::UI, FROM_HERE, | 1064 BrowserThread::UI, FROM_HERE, |
| 1084 base::Bind(&TracingControllerImpl::PerformNextQueuedGlobalMemoryDump, | 1065 base::Bind(&TracingControllerImpl::PerformNextQueuedGlobalMemoryDump, |
| 1085 base::Unretained(this))); | 1066 base::Unretained(this))); |
| 1086 } | 1067 } |
| 1087 } | 1068 } |
| 1088 | 1069 |
| 1089 } // namespace content | 1070 } // namespace content |
| OLD | NEW |