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