OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "services/tracing/tracing_app.h" | 5 #include "services/tracing/service.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 | 15 |
16 namespace tracing { | 16 namespace tracing { |
17 | 17 |
18 TracingApp::TracingApp() : collector_binding_(this), tracing_active_(false) { | 18 Service::Service() : collector_binding_(this), tracing_active_(false) {} |
19 } | 19 Service::~Service() {} |
20 | 20 |
21 TracingApp::~TracingApp() { | 21 bool Service::OnConnect(shell::Connection* connection) { |
22 } | 22 connection->AddInterface<mojom::Factory>(this); |
23 | 23 connection->AddInterface<mojom::Collector>(this); |
24 bool TracingApp::OnConnect(shell::Connection* connection) { | 24 connection->AddInterface<mojom::StartupPerformanceDataCollector>(this); |
25 connection->AddInterface<TraceCollector>(this); | |
26 connection->AddInterface<StartupPerformanceDataCollector>(this); | |
27 | |
28 // If someone connects to us they may want to use the TraceCollector | |
29 // interface and/or they may want to expose themselves to be traced. Attempt | |
30 // to connect to the TraceProvider interface to see if the application | |
31 // connecting to us wants to be traced. They can refuse the connection or | |
32 // close the pipe if not. | |
33 TraceProviderPtr provider_ptr; | |
34 connection->GetInterface(&provider_ptr); | |
35 if (tracing_active_) { | |
36 TraceRecorderPtr recorder_ptr; | |
37 recorder_impls_.push_back( | |
38 new TraceRecorderImpl(GetProxy(&recorder_ptr), sink_.get())); | |
39 provider_ptr->StartTracing(tracing_categories_, std::move(recorder_ptr)); | |
40 } | |
41 provider_ptrs_.AddPtr(std::move(provider_ptr)); | |
42 return true; | 25 return true; |
43 } | 26 } |
44 | 27 |
45 bool TracingApp::OnStop() { | 28 bool Service::OnStop() { |
46 // TODO(beng): This is only required because TracingApp isn't run by | 29 // TODO(beng): This is only required because Service isn't run by |
47 // ServiceRunner - instead it's launched automatically by the standalone | 30 // ServiceRunner - instead it's launched automatically by the standalone |
48 // shell. It shouldn't be. | 31 // shell. It shouldn't be. |
49 base::MessageLoop::current()->QuitWhenIdle(); | 32 base::MessageLoop::current()->QuitWhenIdle(); |
50 return false; | 33 return false; |
51 } | 34 } |
52 | 35 |
53 void TracingApp::Create(const shell::Identity& remote_identity, | 36 void Service::Create(const shell::Identity& remote_identity, |
54 mojo::InterfaceRequest<TraceCollector> request) { | 37 mojom::FactoryRequest request) { |
| 38 bindings_.AddBinding(this, std::move(request)); |
| 39 } |
| 40 |
| 41 void Service::Create(const shell::Identity& remote_identity, |
| 42 mojom::CollectorRequest request) { |
55 collector_binding_.Bind(std::move(request)); | 43 collector_binding_.Bind(std::move(request)); |
56 } | 44 } |
57 | 45 |
58 void TracingApp::Create( | 46 void Service::Create( |
59 const shell::Identity& remote_identity, | 47 const shell::Identity& remote_identity, |
60 mojo::InterfaceRequest<StartupPerformanceDataCollector> request) { | 48 mojom::StartupPerformanceDataCollectorRequest request) { |
61 startup_performance_data_collector_bindings_.AddBinding(this, | 49 startup_performance_data_collector_bindings_.AddBinding(this, |
62 std::move(request)); | 50 std::move(request)); |
63 } | 51 } |
64 | 52 |
65 void TracingApp::Start(mojo::ScopedDataPipeProducerHandle stream, | 53 void Service::CreateRecorder(mojom::ProviderPtr provider) { |
66 const mojo::String& categories) { | 54 if (tracing_active_) { |
| 55 mojom::RecorderPtr recorder_ptr; |
| 56 recorder_impls_.push_back( |
| 57 new Recorder(GetProxy(&recorder_ptr), sink_.get())); |
| 58 provider->StartTracing(tracing_categories_, std::move(recorder_ptr)); |
| 59 } |
| 60 provider_ptrs_.AddPtr(std::move(provider)); |
| 61 } |
| 62 |
| 63 void Service::Start(mojo::ScopedDataPipeProducerHandle stream, |
| 64 const std::string& categories) { |
67 tracing_categories_ = categories; | 65 tracing_categories_ = categories; |
68 sink_.reset(new TraceDataSink(std::move(stream))); | 66 sink_.reset(new DataSink(std::move(stream))); |
69 provider_ptrs_.ForAllPtrs([categories, this](TraceProvider* controller) { | 67 provider_ptrs_.ForAllPtrs( |
70 TraceRecorderPtr ptr; | 68 [categories, this](mojom::Provider* controller) { |
71 recorder_impls_.push_back( | 69 mojom::RecorderPtr ptr; |
72 new TraceRecorderImpl(GetProxy(&ptr), sink_.get())); | 70 recorder_impls_.push_back( |
73 controller->StartTracing(categories, std::move(ptr)); | 71 new Recorder(GetProxy(&ptr), sink_.get())); |
74 }); | 72 controller->StartTracing(categories, std::move(ptr)); |
| 73 }); |
75 tracing_active_ = true; | 74 tracing_active_ = true; |
76 } | 75 } |
77 | 76 |
78 void TracingApp::StopAndFlush() { | 77 void Service::StopAndFlush() { |
79 // Remove any collectors that closed their message pipes before we called | 78 // Remove any collectors that closed their message pipes before we called |
80 // StopTracing(). | 79 // StopTracing(). |
81 for (int i = static_cast<int>(recorder_impls_.size()) - 1; i >= 0; --i) { | 80 for (int i = static_cast<int>(recorder_impls_.size()) - 1; i >= 0; --i) { |
82 if (!recorder_impls_[i]->TraceRecorderHandle().is_valid()) { | 81 if (!recorder_impls_[i]->RecorderHandle().is_valid()) { |
83 recorder_impls_.erase(recorder_impls_.begin() + i); | 82 recorder_impls_.erase(recorder_impls_.begin() + i); |
84 } | 83 } |
85 } | 84 } |
86 | 85 |
87 tracing_active_ = false; | 86 tracing_active_ = false; |
88 provider_ptrs_.ForAllPtrs( | 87 provider_ptrs_.ForAllPtrs( |
89 [](TraceProvider* controller) { controller->StopTracing(); }); | 88 [](mojom::Provider* controller) { controller->StopTracing(); }); |
90 | 89 |
91 // Sending the StopTracing message to registered controllers will request that | 90 // Sending the StopTracing message to registered controllers will request that |
92 // they send trace data back via the collector interface and, when they are | 91 // they send trace data back via the collector interface and, when they are |
93 // done, close the collector pipe. We don't know how long they will take. We | 92 // done, close the collector pipe. We don't know how long they will take. We |
94 // want to read all data that any collector might send until all collectors or | 93 // want to read all data that any collector might send until all collectors or |
95 // closed or an (arbitrary) deadline has passed. Since the bindings don't | 94 // closed or an (arbitrary) deadline has passed. Since the bindings don't |
96 // support this directly we do our own MojoWaitMany over the handles and read | 95 // support this directly we do our own MojoWaitMany over the handles and read |
97 // individual messages until all are closed or our absolute deadline has | 96 // individual messages until all are closed or our absolute deadline has |
98 // elapsed. | 97 // elapsed. |
99 static const MojoDeadline kTimeToWaitMicros = 1000 * 1000; | 98 static const MojoDeadline kTimeToWaitMicros = 1000 * 1000; |
100 MojoTimeTicks end = MojoGetTimeTicksNow() + kTimeToWaitMicros; | 99 MojoTimeTicks end = MojoGetTimeTicksNow() + kTimeToWaitMicros; |
101 | 100 |
102 while (!recorder_impls_.empty()) { | 101 while (!recorder_impls_.empty()) { |
103 MojoTimeTicks now = MojoGetTimeTicksNow(); | 102 MojoTimeTicks now = MojoGetTimeTicksNow(); |
104 if (now >= end) // Timed out? | 103 if (now >= end) // Timed out? |
105 break; | 104 break; |
106 | 105 |
107 MojoDeadline mojo_deadline = end - now; | 106 MojoDeadline mojo_deadline = end - now; |
108 std::vector<mojo::Handle> handles; | 107 std::vector<mojo::Handle> handles; |
109 std::vector<MojoHandleSignals> signals; | 108 std::vector<MojoHandleSignals> signals; |
110 for (auto* it : recorder_impls_) { | 109 for (auto* it : recorder_impls_) { |
111 handles.push_back(it->TraceRecorderHandle()); | 110 handles.push_back(it->RecorderHandle()); |
112 signals.push_back(MOJO_HANDLE_SIGNAL_READABLE | | 111 signals.push_back(MOJO_HANDLE_SIGNAL_READABLE | |
113 MOJO_HANDLE_SIGNAL_PEER_CLOSED); | 112 MOJO_HANDLE_SIGNAL_PEER_CLOSED); |
114 } | 113 } |
115 std::vector<MojoHandleSignalsState> signals_states(signals.size()); | 114 std::vector<MojoHandleSignalsState> signals_states(signals.size()); |
116 const mojo::WaitManyResult wait_many_result = | 115 const mojo::WaitManyResult wait_many_result = |
117 mojo::WaitMany(handles, signals, mojo_deadline, &signals_states); | 116 mojo::WaitMany(handles, signals, mojo_deadline, &signals_states); |
118 if (wait_many_result.result == MOJO_RESULT_DEADLINE_EXCEEDED) { | 117 if (wait_many_result.result == MOJO_RESULT_DEADLINE_EXCEEDED) { |
119 // Timed out waiting, nothing more to read. | 118 // Timed out waiting, nothing more to read. |
120 LOG(WARNING) << "Timed out waiting for trace flush"; | 119 LOG(WARNING) << "Timed out waiting for trace flush"; |
121 break; | 120 break; |
122 } | 121 } |
123 if (wait_many_result.IsIndexValid()) { | 122 if (wait_many_result.IsIndexValid()) { |
124 // Iterate backwards so we can remove closed pipes from |recorder_impls_| | 123 // Iterate backwards so we can remove closed pipes from |recorder_impls_| |
125 // without invalidating subsequent offsets. | 124 // without invalidating subsequent offsets. |
126 for (size_t i = signals_states.size(); i != 0; --i) { | 125 for (size_t i = signals_states.size(); i != 0; --i) { |
127 size_t index = i - 1; | 126 size_t index = i - 1; |
128 MojoHandleSignals satisfied = signals_states[index].satisfied_signals; | 127 MojoHandleSignals satisfied = signals_states[index].satisfied_signals; |
129 // To avoid dropping data, don't close unless there's no | 128 // To avoid dropping data, don't close unless there's no |
130 // readable signal. | 129 // readable signal. |
131 if (satisfied & MOJO_HANDLE_SIGNAL_READABLE) | 130 if (satisfied & MOJO_HANDLE_SIGNAL_READABLE) |
132 recorder_impls_[index]->TryRead(); | 131 recorder_impls_[index]->TryRead(); |
133 else if (satisfied & MOJO_HANDLE_SIGNAL_PEER_CLOSED) | 132 else if (satisfied & MOJO_HANDLE_SIGNAL_PEER_CLOSED) |
134 recorder_impls_.erase(recorder_impls_.begin() + index); | 133 recorder_impls_.erase(recorder_impls_.begin() + index); |
135 } | 134 } |
136 } | 135 } |
137 } | 136 } |
138 AllDataCollected(); | 137 AllDataCollected(); |
139 } | 138 } |
140 | 139 |
141 void TracingApp::SetShellProcessCreationTime(int64_t time) { | 140 void Service::SetShellProcessCreationTime(int64_t time) { |
142 if (startup_performance_times_.shell_process_creation_time == 0) | 141 if (startup_performance_times_.shell_process_creation_time == 0) |
143 startup_performance_times_.shell_process_creation_time = time; | 142 startup_performance_times_.shell_process_creation_time = time; |
144 } | 143 } |
145 | 144 |
146 void TracingApp::SetShellMainEntryPointTime(int64_t time) { | 145 void Service::SetShellMainEntryPointTime(int64_t time) { |
147 if (startup_performance_times_.shell_main_entry_point_time == 0) | 146 if (startup_performance_times_.shell_main_entry_point_time == 0) |
148 startup_performance_times_.shell_main_entry_point_time = time; | 147 startup_performance_times_.shell_main_entry_point_time = time; |
149 } | 148 } |
150 | 149 |
151 void TracingApp::SetBrowserMessageLoopStartTicks(int64_t ticks) { | 150 void Service::SetBrowserMessageLoopStartTicks(int64_t ticks) { |
152 if (startup_performance_times_.browser_message_loop_start_ticks == 0) | 151 if (startup_performance_times_.browser_message_loop_start_ticks == 0) |
153 startup_performance_times_.browser_message_loop_start_ticks = ticks; | 152 startup_performance_times_.browser_message_loop_start_ticks = ticks; |
154 } | 153 } |
155 | 154 |
156 void TracingApp::SetBrowserWindowDisplayTicks(int64_t ticks) { | 155 void Service::SetBrowserWindowDisplayTicks(int64_t ticks) { |
157 if (startup_performance_times_.browser_window_display_ticks == 0) | 156 if (startup_performance_times_.browser_window_display_ticks == 0) |
158 startup_performance_times_.browser_window_display_ticks = ticks; | 157 startup_performance_times_.browser_window_display_ticks = ticks; |
159 } | 158 } |
160 | 159 |
161 void TracingApp::SetBrowserOpenTabsTimeDelta(int64_t delta) { | 160 void Service::SetBrowserOpenTabsTimeDelta(int64_t delta) { |
162 if (startup_performance_times_.browser_open_tabs_time_delta == 0) | 161 if (startup_performance_times_.browser_open_tabs_time_delta == 0) |
163 startup_performance_times_.browser_open_tabs_time_delta = delta; | 162 startup_performance_times_.browser_open_tabs_time_delta = delta; |
164 } | 163 } |
165 | 164 |
166 void TracingApp::SetFirstWebContentsMainFrameLoadTicks(int64_t ticks) { | 165 void Service::SetFirstWebContentsMainFrameLoadTicks(int64_t ticks) { |
167 if (startup_performance_times_.first_web_contents_main_frame_load_ticks == 0) | 166 if (startup_performance_times_.first_web_contents_main_frame_load_ticks == 0) |
168 startup_performance_times_.first_web_contents_main_frame_load_ticks = ticks; | 167 startup_performance_times_.first_web_contents_main_frame_load_ticks = ticks; |
169 } | 168 } |
170 | 169 |
171 void TracingApp::SetFirstVisuallyNonEmptyLayoutTicks(int64_t ticks) { | 170 void Service::SetFirstVisuallyNonEmptyLayoutTicks(int64_t ticks) { |
172 if (startup_performance_times_.first_visually_non_empty_layout_ticks == 0) | 171 if (startup_performance_times_.first_visually_non_empty_layout_ticks == 0) |
173 startup_performance_times_.first_visually_non_empty_layout_ticks = ticks; | 172 startup_performance_times_.first_visually_non_empty_layout_ticks = ticks; |
174 } | 173 } |
175 | 174 |
176 void TracingApp::GetStartupPerformanceTimes( | 175 void Service::GetStartupPerformanceTimes( |
177 const GetStartupPerformanceTimesCallback& callback) { | 176 const GetStartupPerformanceTimesCallback& callback) { |
178 callback.Run(startup_performance_times_.Clone()); | 177 callback.Run(startup_performance_times_.Clone()); |
179 } | 178 } |
180 | 179 |
181 void TracingApp::AllDataCollected() { | 180 void Service::AllDataCollected() { |
182 recorder_impls_.clear(); | 181 recorder_impls_.clear(); |
183 sink_.reset(); | 182 sink_.reset(); |
184 } | 183 } |
185 | 184 |
186 } // namespace tracing | 185 } // namespace tracing |
OLD | NEW |