OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "services/tracing/service.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <utility> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/logging.h" | |
14 #include "base/memory/ptr_util.h" | |
15 #include "base/message_loop/message_loop.h" | |
16 #include "mojo/public/cpp/system/wait.h" | |
17 #include "services/service_manager/public/cpp/service_info.h" | |
18 | |
19 namespace tracing { | |
20 | |
21 Service::Service() : collector_binding_(this), tracing_active_(false) { | |
22 registry_.AddInterface<mojom::Factory>(this); | |
23 registry_.AddInterface<mojom::Collector>(this); | |
24 registry_.AddInterface<mojom::StartupPerformanceDataCollector>(this); | |
25 } | |
26 Service::~Service() {} | |
27 | |
28 void Service::OnBindInterface(const service_manager::ServiceInfo& source_info, | |
29 const std::string& interface_name, | |
30 mojo::ScopedMessagePipeHandle interface_pipe) { | |
31 registry_.BindInterface(source_info.identity, interface_name, | |
32 std::move(interface_pipe)); | |
33 } | |
34 | |
35 bool Service::OnServiceManagerConnectionLost() { | |
36 // TODO(beng): This is only required because Service isn't run by | |
37 // ServiceRunner - instead it's launched automatically by the standalone | |
38 // service manager. It shouldn't be. | |
39 base::MessageLoop::current()->QuitWhenIdle(); | |
40 return false; | |
41 } | |
42 | |
43 void Service::Create(const service_manager::Identity& remote_identity, | |
44 mojom::FactoryRequest request) { | |
45 bindings_.AddBinding(this, std::move(request)); | |
46 } | |
47 | |
48 void Service::Create(const service_manager::Identity& remote_identity, | |
49 mojom::CollectorRequest request) { | |
50 collector_binding_.Bind(std::move(request)); | |
51 } | |
52 | |
53 void Service::Create(const service_manager::Identity& remote_identity, | |
54 mojom::StartupPerformanceDataCollectorRequest request) { | |
55 startup_performance_data_collector_bindings_.AddBinding(this, | |
56 std::move(request)); | |
57 } | |
58 | |
59 void Service::CreateRecorder(mojom::ProviderPtr provider) { | |
60 if (tracing_active_) { | |
61 mojom::RecorderPtr recorder_ptr; | |
62 recorder_impls_.push_back( | |
63 base::MakeUnique<Recorder>(MakeRequest(&recorder_ptr), sink_.get())); | |
64 provider->StartTracing(tracing_categories_, std::move(recorder_ptr)); | |
65 } | |
66 provider_ptrs_.AddPtr(std::move(provider)); | |
67 } | |
68 | |
69 void Service::Start(mojo::ScopedDataPipeProducerHandle stream, | |
70 const std::string& categories) { | |
71 tracing_categories_ = categories; | |
72 sink_.reset(new DataSink(std::move(stream))); | |
73 provider_ptrs_.ForAllPtrs( | |
74 [categories, this](mojom::Provider* controller) { | |
75 mojom::RecorderPtr ptr; | |
76 recorder_impls_.push_back( | |
77 base::MakeUnique<Recorder>(MakeRequest(&ptr), sink_.get())); | |
78 controller->StartTracing(categories, std::move(ptr)); | |
79 }); | |
80 tracing_active_ = true; | |
81 } | |
82 | |
83 void Service::StopAndFlush() { | |
84 // Remove any collectors that closed their message pipes before we called | |
85 // StopTracing(). | |
86 for (int i = static_cast<int>(recorder_impls_.size()) - 1; i >= 0; --i) { | |
87 if (!recorder_impls_[i]->RecorderHandle().is_valid()) { | |
88 recorder_impls_.erase(recorder_impls_.begin() + i); | |
89 } | |
90 } | |
91 | |
92 tracing_active_ = false; | |
93 provider_ptrs_.ForAllPtrs( | |
94 [](mojom::Provider* controller) { controller->StopTracing(); }); | |
95 | |
96 // Sending the StopTracing message to registered controllers will request that | |
97 // they send trace data back via the collector interface and, when they are | |
98 // done, close the collector pipe. We don't know how long they will take. We | |
99 // want to read all data that any collector might send until all collectors or | |
100 // closed or an (arbitrary) deadline has passed. Since the bindings don't | |
101 // support this directly we do our own WaitMany over the handles and read | |
102 // individual messages until all are closed or our absolute deadline has | |
103 // elapsed. | |
104 static const MojoDeadline kTimeToWaitMicros = 1000 * 1000; | |
105 MojoTimeTicks end = MojoGetTimeTicksNow() + kTimeToWaitMicros; | |
106 | |
107 while (!recorder_impls_.empty()) { | |
108 MojoTimeTicks now = MojoGetTimeTicksNow(); | |
109 if (now >= end) // Timed out? | |
110 break; | |
111 | |
112 std::vector<mojo::Handle> handles; | |
113 std::vector<MojoHandleSignals> signals; | |
114 for (const auto& it : recorder_impls_) { | |
115 handles.push_back(it->RecorderHandle()); | |
116 signals.push_back(MOJO_HANDLE_SIGNAL_READABLE | | |
117 MOJO_HANDLE_SIGNAL_PEER_CLOSED); | |
118 } | |
119 std::vector<MojoHandleSignalsState> signals_states(signals.size()); | |
120 size_t result_index; | |
121 | |
122 // TODO(rockot): Use a timed wait here to avoid hanging forever in the case | |
123 // of a misbehaving or unresponsive collector. | |
124 MojoResult rv = | |
125 mojo::WaitMany(handles.data(), signals.data(), handles.size(), | |
126 &result_index, signals_states.data()); | |
127 if (rv == MOJO_RESULT_OK || rv == MOJO_RESULT_FAILED_PRECONDITION) { | |
128 // Iterate backwards so we can remove closed pipes from |recorder_impls_| | |
129 // without invalidating subsequent offsets. | |
130 for (size_t i = signals_states.size(); i != 0; --i) { | |
131 size_t index = i - 1; | |
132 MojoHandleSignals satisfied = signals_states[index].satisfied_signals; | |
133 // To avoid dropping data, don't close unless there's no | |
134 // readable signal. | |
135 if (satisfied & MOJO_HANDLE_SIGNAL_READABLE) | |
136 recorder_impls_[index]->TryRead(); | |
137 else if (satisfied & MOJO_HANDLE_SIGNAL_PEER_CLOSED) | |
138 recorder_impls_.erase(recorder_impls_.begin() + index); | |
139 } | |
140 } | |
141 } | |
142 AllDataCollected(); | |
143 } | |
144 | |
145 void Service::SetServiceManagerProcessCreationTime(int64_t time) { | |
146 if (startup_performance_times_.service_manager_process_creation_time == 0) | |
147 startup_performance_times_.service_manager_process_creation_time = time; | |
148 } | |
149 | |
150 void Service::SetServiceManagerMainEntryPointTime(int64_t time) { | |
151 if (startup_performance_times_.service_manager_main_entry_point_time == 0) | |
152 startup_performance_times_.service_manager_main_entry_point_time = time; | |
153 } | |
154 | |
155 void Service::SetBrowserMessageLoopStartTicks(int64_t ticks) { | |
156 if (startup_performance_times_.browser_message_loop_start_ticks == 0) | |
157 startup_performance_times_.browser_message_loop_start_ticks = ticks; | |
158 } | |
159 | |
160 void Service::SetBrowserWindowDisplayTicks(int64_t ticks) { | |
161 if (startup_performance_times_.browser_window_display_ticks == 0) | |
162 startup_performance_times_.browser_window_display_ticks = ticks; | |
163 } | |
164 | |
165 void Service::SetBrowserOpenTabsTimeDelta(int64_t delta) { | |
166 if (startup_performance_times_.browser_open_tabs_time_delta == 0) | |
167 startup_performance_times_.browser_open_tabs_time_delta = delta; | |
168 } | |
169 | |
170 void Service::SetFirstWebContentsMainFrameLoadTicks(int64_t ticks) { | |
171 if (startup_performance_times_.first_web_contents_main_frame_load_ticks == 0) | |
172 startup_performance_times_.first_web_contents_main_frame_load_ticks = ticks; | |
173 } | |
174 | |
175 void Service::SetFirstVisuallyNonEmptyLayoutTicks(int64_t ticks) { | |
176 if (startup_performance_times_.first_visually_non_empty_layout_ticks == 0) | |
177 startup_performance_times_.first_visually_non_empty_layout_ticks = ticks; | |
178 } | |
179 | |
180 void Service::GetStartupPerformanceTimes( | |
181 const GetStartupPerformanceTimesCallback& callback) { | |
182 callback.Run(startup_performance_times_.Clone()); | |
183 } | |
184 | |
185 void Service::AllDataCollected() { | |
186 recorder_impls_.clear(); | |
187 sink_.reset(); | |
188 } | |
189 | |
190 } // namespace tracing | |
OLD | NEW |