blob: e73f0ab16a638009e4e4d03f60f75a3979eb90c8 [file] [log] [blame]
ergb9210582015-05-09 03:52:16 +09001// 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 "base/bind.h"
erge5f066b2015-10-22 03:24:05 +09008#include "base/logging.h"
ergb9210582015-05-09 03:52:16 +09009#include "base/message_loop/message_loop.h"
jam8bb27ff2015-05-15 07:56:10 +090010#include "mojo/application/public/cpp/application_connection.h"
ergb9210582015-05-09 03:52:16 +090011
12namespace tracing {
13
erge5f066b2015-10-22 03:24:05 +090014TracingApp::TracingApp() : collector_binding_(this), tracing_active_(false) {
15}
ergb9210582015-05-09 03:52:16 +090016
erge5f066b2015-10-22 03:24:05 +090017TracingApp::~TracingApp() {
18}
ergb9210582015-05-09 03:52:16 +090019
20bool TracingApp::ConfigureIncomingConnection(
21 mojo::ApplicationConnection* connection) {
erge5f066b2015-10-22 03:24:05 +090022 connection->AddService<TraceCollector>(this);
msw339a3c42015-08-19 03:55:31 +090023 connection->AddService<StartupPerformanceDataCollector>(this);
ergb9210582015-05-09 03:52:16 +090024
erge5f066b2015-10-22 03:24:05 +090025 // If someone connects to us they may want to use the TraceCollector
ergb9210582015-05-09 03:52:16 +090026 // interface and/or they may want to expose themselves to be traced. Attempt
erge5f066b2015-10-22 03:24:05 +090027 // to connect to the TraceProvider interface to see if the application
ergb9210582015-05-09 03:52:16 +090028 // connecting to us wants to be traced. They can refuse the connection or
29 // close the pipe if not.
erge5f066b2015-10-22 03:24:05 +090030 TraceProviderPtr provider_ptr;
31 connection->ConnectToService(&provider_ptr);
32 if (tracing_active_) {
33 TraceRecorderPtr recorder_ptr;
34 recorder_impls_.push_back(
35 new TraceRecorderImpl(GetProxy(&recorder_ptr), sink_.get()));
36 provider_ptr->StartTracing(tracing_categories_, recorder_ptr.Pass());
37 }
38 provider_ptrs_.AddInterfacePtr(provider_ptr.Pass());
ergb9210582015-05-09 03:52:16 +090039 return true;
40}
41
erge5f066b2015-10-22 03:24:05 +090042void TracingApp::Create(mojo::ApplicationConnection* connection,
43 mojo::InterfaceRequest<TraceCollector> request) {
44 collector_binding_.Bind(request.Pass());
ergb9210582015-05-09 03:52:16 +090045}
46
msw339a3c42015-08-19 03:55:31 +090047void TracingApp::Create(
48 mojo::ApplicationConnection* connection,
49 mojo::InterfaceRequest<StartupPerformanceDataCollector> request) {
50 startup_performance_data_collector_bindings_.AddBinding(this, request.Pass());
51}
52
ergb9210582015-05-09 03:52:16 +090053void TracingApp::Start(mojo::ScopedDataPipeProducerHandle stream,
54 const mojo::String& categories) {
erge5f066b2015-10-22 03:24:05 +090055 tracing_categories_ = categories;
ergb9210582015-05-09 03:52:16 +090056 sink_.reset(new TraceDataSink(stream.Pass()));
erge5f066b2015-10-22 03:24:05 +090057 provider_ptrs_.ForAllPtrs([categories, this](TraceProvider* controller) {
58 TraceRecorderPtr ptr;
59 recorder_impls_.push_back(
60 new TraceRecorderImpl(GetProxy(&ptr), sink_.get()));
61 controller->StartTracing(categories, ptr.Pass());
62 });
63 tracing_active_ = true;
ergb9210582015-05-09 03:52:16 +090064}
65
66void TracingApp::StopAndFlush() {
erge5f066b2015-10-22 03:24:05 +090067 // Remove any collectors that closed their message pipes before we called
68 // StopTracing().
69 for (int i = static_cast<int>(recorder_impls_.size()) - 1; i >= 0; --i) {
70 if (!recorder_impls_[i]->TraceRecorderHandle().is_valid()) {
71 recorder_impls_.erase(recorder_impls_.begin() + i);
72 }
73 }
ergb9210582015-05-09 03:52:16 +090074
erge5f066b2015-10-22 03:24:05 +090075 tracing_active_ = false;
76 provider_ptrs_.ForAllPtrs(
77 [](TraceProvider* controller) { controller->StopTracing(); });
78
79 // Sending the StopTracing message to registered controllers will request that
80 // they send trace data back via the collector interface and, when they are
81 // done, close the collector pipe. We don't know how long they will take. We
82 // want to read all data that any collector might send until all collectors or
83 // closed or an (arbitrary) deadline has passed. Since the bindings don't
84 // support this directly we do our own MojoWaitMany over the handles and read
85 // individual messages until all are closed or our absolute deadline has
86 // elapsed.
87 static const MojoDeadline kTimeToWaitMicros = 1000 * 1000;
88 MojoTimeTicks end = MojoGetTimeTicksNow() + kTimeToWaitMicros;
89
90 while (!recorder_impls_.empty()) {
91 MojoTimeTicks now = MojoGetTimeTicksNow();
92 if (now >= end) // Timed out?
93 break;
94
95 MojoDeadline mojo_deadline = end - now;
96 std::vector<mojo::Handle> handles;
97 std::vector<MojoHandleSignals> signals;
98 for (const auto& it : recorder_impls_) {
99 handles.push_back(it->TraceRecorderHandle());
100 signals.push_back(MOJO_HANDLE_SIGNAL_READABLE |
101 MOJO_HANDLE_SIGNAL_PEER_CLOSED);
102 }
103 std::vector<MojoHandleSignalsState> signals_states(signals.size());
104 const mojo::WaitManyResult wait_many_result =
105 mojo::WaitMany(handles, signals, mojo_deadline, &signals_states);
106 if (wait_many_result.result == MOJO_RESULT_DEADLINE_EXCEEDED) {
107 // Timed out waiting, nothing more to read.
108 LOG(WARNING) << "Timed out waiting for trace flush";
109 break;
110 }
111 if (wait_many_result.IsIndexValid()) {
112 // Iterate backwards so we can remove closed pipes from |recorder_impls_|
113 // without invalidating subsequent offsets.
114 for (size_t i = signals_states.size(); i != 0; --i) {
115 size_t index = i - 1;
116 MojoHandleSignals satisfied = signals_states[index].satisfied_signals;
117 // To avoid dropping data, don't close unless there's no
118 // readable signal.
119 if (satisfied & MOJO_HANDLE_SIGNAL_READABLE)
120 recorder_impls_[index]->TryRead();
121 else if (satisfied & MOJO_HANDLE_SIGNAL_PEER_CLOSED)
122 recorder_impls_.erase(recorder_impls_.begin() + index);
123 }
124 }
125 }
126 AllDataCollected();
ergb9210582015-05-09 03:52:16 +0900127}
128
msw339a3c42015-08-19 03:55:31 +0900129void TracingApp::SetShellProcessCreationTime(int64 time) {
130 if (startup_performance_times_.shell_process_creation_time == 0)
131 startup_performance_times_.shell_process_creation_time = time;
132}
133
mswde4a5112015-08-26 12:28:35 +0900134void TracingApp::SetShellMainEntryPointTime(int64 time) {
135 if (startup_performance_times_.shell_main_entry_point_time == 0)
136 startup_performance_times_.shell_main_entry_point_time = time;
137}
138
gabdf51c132015-11-12 08:37:19 +0900139void TracingApp::SetBrowserMessageLoopStartTicks(int64 ticks) {
140 if (startup_performance_times_.browser_message_loop_start_ticks == 0)
141 startup_performance_times_.browser_message_loop_start_ticks = ticks;
msw339a3c42015-08-19 03:55:31 +0900142}
143
gabdf51c132015-11-12 08:37:19 +0900144void TracingApp::SetBrowserWindowDisplayTicks(int64 ticks) {
145 if (startup_performance_times_.browser_window_display_ticks == 0)
146 startup_performance_times_.browser_window_display_ticks = ticks;
msw339a3c42015-08-19 03:55:31 +0900147}
148
msw565fba82015-08-28 08:06:12 +0900149void TracingApp::SetBrowserOpenTabsTimeDelta(int64 delta) {
150 if (startup_performance_times_.browser_open_tabs_time_delta == 0)
151 startup_performance_times_.browser_open_tabs_time_delta = delta;
msw339a3c42015-08-19 03:55:31 +0900152}
153
gabdf51c132015-11-12 08:37:19 +0900154void TracingApp::SetFirstWebContentsMainFrameLoadTicks(int64 ticks) {
155 if (startup_performance_times_.first_web_contents_main_frame_load_ticks == 0)
156 startup_performance_times_.first_web_contents_main_frame_load_ticks = ticks;
msw339a3c42015-08-19 03:55:31 +0900157}
158
gabdf51c132015-11-12 08:37:19 +0900159void TracingApp::SetFirstVisuallyNonEmptyLayoutTicks(int64 ticks) {
160 if (startup_performance_times_.first_visually_non_empty_layout_ticks == 0)
161 startup_performance_times_.first_visually_non_empty_layout_ticks = ticks;
msw339a3c42015-08-19 03:55:31 +0900162}
163
164void TracingApp::GetStartupPerformanceTimes(
165 const GetStartupPerformanceTimesCallback& callback) {
166 callback.Run(startup_performance_times_.Clone());
167}
168
ergb9210582015-05-09 03:52:16 +0900169void TracingApp::AllDataCollected() {
erge5f066b2015-10-22 03:24:05 +0900170 recorder_impls_.clear();
171 sink_.reset();
ergb9210582015-05-09 03:52:16 +0900172}
173
174} // namespace tracing