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/tracing_app.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 | 10 |
11 namespace tracing { | 11 namespace tracing { |
12 | 12 |
13 TracingApp::TracingApp() : coordinator_binding_(this), tracing_active_(false) { | 13 TracingApp::TracingApp() : collector_binding_(this), tracing_active_(false) { |
14 } | 14 } |
15 | 15 |
16 TracingApp::~TracingApp() { | 16 TracingApp::~TracingApp() { |
17 } | 17 } |
18 | 18 |
19 bool TracingApp::ConfigureIncomingConnection( | 19 bool TracingApp::ConfigureIncomingConnection( |
20 mojo::ApplicationConnection* connection) { | 20 mojo::ApplicationConnection* connection) { |
21 connection->AddService<TraceCoordinator>(this); | 21 connection->AddService<TraceCollector>(this); |
22 | 22 |
23 // If someone connects to us they may want to use the TraceCoordinator | 23 // If someone connects to us they may want to use the TraceCollector |
24 // interface and/or they may want to expose themselves to be traced. Attempt | 24 // interface and/or they may want to expose themselves to be traced. Attempt |
25 // to connect to the TraceController interface to see if the application | 25 // to connect to the TraceProvider interface to see if the application |
26 // connecting to us wants to be traced. They can refuse the connection or | 26 // connecting to us wants to be traced. They can refuse the connection or |
27 // close the pipe if not. | 27 // close the pipe if not. |
28 TraceControllerPtr controller_ptr; | 28 TraceProviderPtr provider_ptr; |
29 connection->ConnectToService(&controller_ptr); | 29 connection->ConnectToService(&provider_ptr); |
30 if (tracing_active_) { | 30 if (tracing_active_) { |
31 TraceDataCollectorPtr collector_ptr; | 31 TraceRecorderPtr recorder_ptr; |
32 collector_impls_.push_back( | 32 recorder_impls_.push_back( |
33 new CollectorImpl(GetProxy(&collector_ptr), sink_.get())); | 33 new TraceRecorderImpl(GetProxy(&recorder_ptr), sink_.get())); |
34 controller_ptr->StartTracing(tracing_categories_, collector_ptr.Pass()); | 34 provider_ptr->StartTracing(tracing_categories_, recorder_ptr.Pass()); |
35 } | 35 } |
36 controller_ptrs_.AddInterfacePtr(controller_ptr.Pass()); | 36 provider_ptrs_.AddInterfacePtr(provider_ptr.Pass()); |
37 return true; | 37 return true; |
38 } | 38 } |
39 | 39 |
40 // mojo::InterfaceFactory<TraceCoordinator> implementation. | 40 // mojo::InterfaceFactory<TraceCollector> implementation. |
41 void TracingApp::Create(mojo::ApplicationConnection* connection, | 41 void TracingApp::Create(mojo::ApplicationConnection* connection, |
42 mojo::InterfaceRequest<TraceCoordinator> request) { | 42 mojo::InterfaceRequest<TraceCollector> request) { |
43 coordinator_binding_.Bind(request.Pass()); | 43 collector_binding_.Bind(request.Pass()); |
44 } | 44 } |
45 | 45 |
46 // tracing::TraceCoordinator implementation. | 46 // tracing::TraceCollector implementation. |
47 void TracingApp::Start(mojo::ScopedDataPipeProducerHandle stream, | 47 void TracingApp::Start(mojo::ScopedDataPipeProducerHandle stream, |
48 const mojo::String& categories) { | 48 const mojo::String& categories) { |
49 tracing_categories_ = categories; | 49 tracing_categories_ = categories; |
50 sink_.reset(new TraceDataSink(stream.Pass())); | 50 sink_.reset(new TraceDataSink(stream.Pass())); |
51 controller_ptrs_.ForAllPtrs([categories, this](TraceController* controller) { | 51 provider_ptrs_.ForAllPtrs([categories, this](TraceProvider* controller) { |
52 TraceDataCollectorPtr ptr; | 52 TraceRecorderPtr ptr; |
53 collector_impls_.push_back(new CollectorImpl(GetProxy(&ptr), sink_.get())); | 53 recorder_impls_.push_back( |
| 54 new TraceRecorderImpl(GetProxy(&ptr), sink_.get())); |
54 controller->StartTracing(categories, ptr.Pass()); | 55 controller->StartTracing(categories, ptr.Pass()); |
55 }); | 56 }); |
56 tracing_active_ = true; | 57 tracing_active_ = true; |
57 } | 58 } |
58 | 59 |
59 void TracingApp::StopAndFlush() { | 60 void TracingApp::StopAndFlush() { |
60 // Remove any collectors that closed their message pipes before we called | 61 // Remove any collectors that closed their message pipes before we called |
61 // StopTracing(). See https://github.com/domokit/mojo/issues/225. | 62 // StopTracing(). See https://github.com/domokit/mojo/issues/225. |
62 for (int i = collector_impls_.size() - 1; i >= 0; --i) { | 63 for (int i = recorder_impls_.size() - 1; i >= 0; --i) { |
63 if (!collector_impls_[i]->TraceDataCollectorHandle().is_valid()) { | 64 if (!recorder_impls_[i]->TraceRecorderHandle().is_valid()) { |
64 collector_impls_.erase(collector_impls_.begin() + i); | 65 recorder_impls_.erase(recorder_impls_.begin() + i); |
65 } | 66 } |
66 } | 67 } |
67 | 68 |
68 tracing_active_ = false; | 69 tracing_active_ = false; |
69 controller_ptrs_.ForAllPtrs( | 70 provider_ptrs_.ForAllPtrs( |
70 [](TraceController* controller) { controller->StopTracing(); }); | 71 [](TraceProvider* controller) { controller->StopTracing(); }); |
71 | 72 |
72 // Sending the StopTracing message to registered controllers will request that | 73 // Sending the StopTracing message to registered controllers will request that |
73 // they send trace data back via the collector interface and, when they are | 74 // they send trace data back via the collector interface and, when they are |
74 // done, close the collector pipe. We don't know how long they will take. We | 75 // done, close the collector pipe. We don't know how long they will take. We |
75 // want to read all data that any collector might send until all collectors or | 76 // want to read all data that any collector might send until all collectors or |
76 // closed or an (arbitrary) deadline has passed. Since the bindings don't | 77 // closed or an (arbitrary) deadline has passed. Since the bindings don't |
77 // support this directly we do our own MojoWaitMany over the handles and read | 78 // support this directly we do our own MojoWaitMany over the handles and read |
78 // individual messages until all are closed or our absolute deadline has | 79 // individual messages until all are closed or our absolute deadline has |
79 // elapsed. | 80 // elapsed. |
80 static const MojoDeadline kTimeToWaitMicros = 1000 * 1000; | 81 static const MojoDeadline kTimeToWaitMicros = 1000 * 1000; |
81 MojoTimeTicks end = MojoGetTimeTicksNow() + kTimeToWaitMicros; | 82 MojoTimeTicks end = MojoGetTimeTicksNow() + kTimeToWaitMicros; |
82 | 83 |
83 while (!collector_impls_.empty()) { | 84 while (!recorder_impls_.empty()) { |
84 MojoTimeTicks now = MojoGetTimeTicksNow(); | 85 MojoTimeTicks now = MojoGetTimeTicksNow(); |
85 if (now >= end) // Timed out? | 86 if (now >= end) // Timed out? |
86 break; | 87 break; |
87 | 88 |
88 MojoDeadline mojo_deadline = end - now; | 89 MojoDeadline mojo_deadline = end - now; |
89 std::vector<mojo::Handle> handles; | 90 std::vector<mojo::Handle> handles; |
90 std::vector<MojoHandleSignals> signals; | 91 std::vector<MojoHandleSignals> signals; |
91 for (const auto& it : collector_impls_) { | 92 for (const auto& it : recorder_impls_) { |
92 handles.push_back(it->TraceDataCollectorHandle()); | 93 handles.push_back(it->TraceRecorderHandle()); |
93 signals.push_back(MOJO_HANDLE_SIGNAL_READABLE | | 94 signals.push_back(MOJO_HANDLE_SIGNAL_READABLE | |
94 MOJO_HANDLE_SIGNAL_PEER_CLOSED); | 95 MOJO_HANDLE_SIGNAL_PEER_CLOSED); |
95 } | 96 } |
96 std::vector<MojoHandleSignalsState> signals_states(signals.size()); | 97 std::vector<MojoHandleSignalsState> signals_states(signals.size()); |
97 const mojo::WaitManyResult wait_many_result = | 98 const mojo::WaitManyResult wait_many_result = |
98 mojo::WaitMany(handles, signals, mojo_deadline, &signals_states); | 99 mojo::WaitMany(handles, signals, mojo_deadline, &signals_states); |
99 if (wait_many_result.result == MOJO_RESULT_DEADLINE_EXCEEDED) { | 100 if (wait_many_result.result == MOJO_RESULT_DEADLINE_EXCEEDED) { |
100 // Timed out waiting, nothing more to read. | 101 // Timed out waiting, nothing more to read. |
101 LOG(WARNING) << "Timed out waiting for trace flush"; | 102 LOG(WARNING) << "Timed out waiting for trace flush"; |
102 break; | 103 break; |
103 } | 104 } |
104 if (wait_many_result.IsIndexValid()) { | 105 if (wait_many_result.IsIndexValid()) { |
105 // Iterate backwards so we can remove closed pipes from |collector_impls_| | 106 // Iterate backwards so we can remove closed pipes from |recorder_impls_| |
106 // without invalidating subsequent offsets. | 107 // without invalidating subsequent offsets. |
107 for (size_t i = signals_states.size(); i != 0; --i) { | 108 for (size_t i = signals_states.size(); i != 0; --i) { |
108 size_t index = i - 1; | 109 size_t index = i - 1; |
109 MojoHandleSignals satisfied = signals_states[index].satisfied_signals; | 110 MojoHandleSignals satisfied = signals_states[index].satisfied_signals; |
110 // To avoid dropping data, don't close unless there's no | 111 // To avoid dropping data, don't close unless there's no |
111 // readable signal. | 112 // readable signal. |
112 if (satisfied & MOJO_HANDLE_SIGNAL_READABLE) | 113 if (satisfied & MOJO_HANDLE_SIGNAL_READABLE) |
113 collector_impls_[index]->TryRead(); | 114 recorder_impls_[index]->TryRead(); |
114 else if (satisfied & MOJO_HANDLE_SIGNAL_PEER_CLOSED) | 115 else if (satisfied & MOJO_HANDLE_SIGNAL_PEER_CLOSED) |
115 collector_impls_.erase(collector_impls_.begin() + index); | 116 recorder_impls_.erase(recorder_impls_.begin() + index); |
116 } | 117 } |
117 } | 118 } |
118 } | 119 } |
119 AllDataCollected(); | 120 AllDataCollected(); |
120 } | 121 } |
121 | 122 |
122 void TracingApp::AllDataCollected() { | 123 void TracingApp::AllDataCollected() { |
123 collector_impls_.clear(); | 124 recorder_impls_.clear(); |
124 sink_.reset(); | 125 sink_.reset(); |
125 } | 126 } |
126 | 127 |
127 } // namespace tracing | 128 } // namespace tracing |
OLD | NEW |