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 "mojo/services/tracing/tracing_app.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/message_loop/message_loop.h" | |
15 | |
16 namespace tracing { | |
17 | |
18 TracingApp::TracingApp() : collector_binding_(this), tracing_active_(false) { | |
19 } | |
20 | |
21 TracingApp::~TracingApp() { | |
22 } | |
23 | |
24 bool TracingApp::AcceptConnection(mojo::Connection* connection) { | |
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; | |
43 } | |
44 | |
45 bool TracingApp::ShellConnectionLost() { | |
46 // TODO(beng): This is only required because TracingApp isn't run by | |
47 // ApplicationRunner - instead it's launched automatically by the standalone | |
48 // shell. It shouldn't be. | |
49 base::MessageLoop::current()->QuitWhenIdle(); | |
50 return false; | |
51 } | |
52 | |
53 void TracingApp::Create(mojo::Connection* connection, | |
54 mojo::InterfaceRequest<TraceCollector> request) { | |
55 collector_binding_.Bind(std::move(request)); | |
56 } | |
57 | |
58 void TracingApp::Create( | |
59 mojo::Connection* connection, | |
60 mojo::InterfaceRequest<StartupPerformanceDataCollector> request) { | |
61 startup_performance_data_collector_bindings_.AddBinding(this, | |
62 std::move(request)); | |
63 } | |
64 | |
65 void TracingApp::Start(mojo::ScopedDataPipeProducerHandle stream, | |
66 const mojo::String& categories) { | |
67 tracing_categories_ = categories; | |
68 sink_.reset(new TraceDataSink(std::move(stream))); | |
69 provider_ptrs_.ForAllPtrs([categories, this](TraceProvider* controller) { | |
70 TraceRecorderPtr ptr; | |
71 recorder_impls_.push_back( | |
72 new TraceRecorderImpl(GetProxy(&ptr), sink_.get())); | |
73 controller->StartTracing(categories, std::move(ptr)); | |
74 }); | |
75 tracing_active_ = true; | |
76 } | |
77 | |
78 void TracingApp::StopAndFlush() { | |
79 // Remove any collectors that closed their message pipes before we called | |
80 // StopTracing(). | |
81 for (int i = static_cast<int>(recorder_impls_.size()) - 1; i >= 0; --i) { | |
82 if (!recorder_impls_[i]->TraceRecorderHandle().is_valid()) { | |
83 recorder_impls_.erase(recorder_impls_.begin() + i); | |
84 } | |
85 } | |
86 | |
87 tracing_active_ = false; | |
88 provider_ptrs_.ForAllPtrs( | |
89 [](TraceProvider* controller) { controller->StopTracing(); }); | |
90 | |
91 // Sending the StopTracing message to registered controllers will request that | |
92 // 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 | |
94 // 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 | |
96 // 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 | |
98 // elapsed. | |
99 static const MojoDeadline kTimeToWaitMicros = 1000 * 1000; | |
100 MojoTimeTicks end = MojoGetTimeTicksNow() + kTimeToWaitMicros; | |
101 | |
102 while (!recorder_impls_.empty()) { | |
103 MojoTimeTicks now = MojoGetTimeTicksNow(); | |
104 if (now >= end) // Timed out? | |
105 break; | |
106 | |
107 MojoDeadline mojo_deadline = end - now; | |
108 std::vector<mojo::Handle> handles; | |
109 std::vector<MojoHandleSignals> signals; | |
110 for (const auto& it : recorder_impls_) { | |
111 handles.push_back(it->TraceRecorderHandle()); | |
112 signals.push_back(MOJO_HANDLE_SIGNAL_READABLE | | |
113 MOJO_HANDLE_SIGNAL_PEER_CLOSED); | |
114 } | |
115 std::vector<MojoHandleSignalsState> signals_states(signals.size()); | |
116 const mojo::WaitManyResult wait_many_result = | |
117 mojo::WaitMany(handles, signals, mojo_deadline, &signals_states); | |
118 if (wait_many_result.result == MOJO_RESULT_DEADLINE_EXCEEDED) { | |
119 // Timed out waiting, nothing more to read. | |
120 LOG(WARNING) << "Timed out waiting for trace flush"; | |
121 break; | |
122 } | |
123 if (wait_many_result.IsIndexValid()) { | |
124 // Iterate backwards so we can remove closed pipes from |recorder_impls_| | |
125 // without invalidating subsequent offsets. | |
126 for (size_t i = signals_states.size(); i != 0; --i) { | |
127 size_t index = i - 1; | |
128 MojoHandleSignals satisfied = signals_states[index].satisfied_signals; | |
129 // To avoid dropping data, don't close unless there's no | |
130 // readable signal. | |
131 if (satisfied & MOJO_HANDLE_SIGNAL_READABLE) | |
132 recorder_impls_[index]->TryRead(); | |
133 else if (satisfied & MOJO_HANDLE_SIGNAL_PEER_CLOSED) | |
134 recorder_impls_.erase(recorder_impls_.begin() + index); | |
135 } | |
136 } | |
137 } | |
138 AllDataCollected(); | |
139 } | |
140 | |
141 void TracingApp::SetShellProcessCreationTime(int64_t time) { | |
142 if (startup_performance_times_.shell_process_creation_time == 0) | |
143 startup_performance_times_.shell_process_creation_time = time; | |
144 } | |
145 | |
146 void TracingApp::SetShellMainEntryPointTime(int64_t time) { | |
147 if (startup_performance_times_.shell_main_entry_point_time == 0) | |
148 startup_performance_times_.shell_main_entry_point_time = time; | |
149 } | |
150 | |
151 void TracingApp::SetBrowserMessageLoopStartTicks(int64_t ticks) { | |
152 if (startup_performance_times_.browser_message_loop_start_ticks == 0) | |
153 startup_performance_times_.browser_message_loop_start_ticks = ticks; | |
154 } | |
155 | |
156 void TracingApp::SetBrowserWindowDisplayTicks(int64_t ticks) { | |
157 if (startup_performance_times_.browser_window_display_ticks == 0) | |
158 startup_performance_times_.browser_window_display_ticks = ticks; | |
159 } | |
160 | |
161 void TracingApp::SetBrowserOpenTabsTimeDelta(int64_t delta) { | |
162 if (startup_performance_times_.browser_open_tabs_time_delta == 0) | |
163 startup_performance_times_.browser_open_tabs_time_delta = delta; | |
164 } | |
165 | |
166 void TracingApp::SetFirstWebContentsMainFrameLoadTicks(int64_t ticks) { | |
167 if (startup_performance_times_.first_web_contents_main_frame_load_ticks == 0) | |
168 startup_performance_times_.first_web_contents_main_frame_load_ticks = ticks; | |
169 } | |
170 | |
171 void TracingApp::SetFirstVisuallyNonEmptyLayoutTicks(int64_t ticks) { | |
172 if (startup_performance_times_.first_visually_non_empty_layout_ticks == 0) | |
173 startup_performance_times_.first_visually_non_empty_layout_ticks = ticks; | |
174 } | |
175 | |
176 void TracingApp::GetStartupPerformanceTimes( | |
177 const GetStartupPerformanceTimesCallback& callback) { | |
178 callback.Run(startup_performance_times_.Clone()); | |
179 } | |
180 | |
181 void TracingApp::AllDataCollected() { | |
182 recorder_impls_.clear(); | |
183 sink_.reset(); | |
184 } | |
185 | |
186 } // namespace tracing | |
OLD | NEW |