Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(245)

Side by Side Diff: services/tracing/tracing_app.cc

Issue 1055703002: Gather trace data by waiting on handles with a fixed timeout (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 TracingApp::~TracingApp() {
viettrungluu 2015/04/02 19:54:13 nit: add blank line above
16 }
17
18 bool TracingApp::ConfigureIncomingConnection(
19 mojo::ApplicationConnection* connection) {
20 connection->AddService<TraceCoordinator>(this);
21
22 // If someone connects to us they may want to use the TraceCoordinator
23 // interface and/or they may want to expose themselves to be traced. Attempt
24 // to connect to the TraceController interface to see if the application
25 // connecting to us wants to be traced. They can refuse the connection or
26 // close the pipe if not.
27 TraceControllerPtr controller_ptr;
28 connection->ConnectToService(&controller_ptr);
29 if (tracing_active_) {
30 TraceDataCollectorPtr collector_ptr;
31 collector_impls_.push_back(
32 new CollectorImpl(GetProxy(&collector_ptr), sink_.get()));
33 controller_ptr->StartTracing(tracing_categories_, collector_ptr.Pass());
34 }
35 controller_ptrs_.AddInterfacePtr(controller_ptr.Pass());
36 return true;
37 }
38
39 // mojo::InterfaceFactory<TraceCoordinator> implementation.
40 void TracingApp::Create(mojo::ApplicationConnection* connection,
41 mojo::InterfaceRequest<TraceCoordinator> request) {
42 coordinator_binding_.Bind(request.Pass());
43 }
44
45 // tracing::TraceCoordinator implementation.
46 void TracingApp::Start(mojo::ScopedDataPipeProducerHandle stream,
47 const mojo::String& categories) {
48 tracing_categories_ = categories;
49 sink_.reset(new TraceDataSink(stream.Pass()));
50 controller_ptrs_.ForAllPtrs([categories, this](TraceController* controller) {
51 TraceDataCollectorPtr ptr;
52 collector_impls_.push_back(new CollectorImpl(GetProxy(&ptr), sink_.get()));
53 controller->StartTracing(categories, ptr.Pass());
54 });
55 tracing_active_ = true;
56 }
57
58 void TracingApp::StopAndFlush() {
59 tracing_active_ = false;
60 controller_ptrs_.ForAllPtrs(
61 [](TraceController* controller) { controller->StopTracing(); });
62
63 // Sending the StopTracing message to registered controllers will request that
64 // they send trace data back via the collector interface and, when they are
65 // done, close the collector pipe. We don't know how long they will take. We
66 // want to read all data that any collector might send until all collectors or
67 // closed or an (arbitrary) deadline has passed. Since the bindings don't
68 // support this directly we do our own MojoWaitMany over the handles and read
69 // individual messages until all are closed or our absolute deadline has
70 // elapsed.
71 static const MojoDeadline kTimeToWaitMicros = 100 * 1000;
72 MojoTimeTicks end = MojoGetTimeTicksNow() + kTimeToWaitMicros;
73
74 while (!collector_impls_.empty()) {
75 MojoTimeTicks now = MojoGetTimeTicksNow();
76 if (now >= end) // Timed out?
77 break;
78
79 MojoDeadline mojo_deadline = end - now;
80 std::vector<mojo::Handle> handles;
81 std::vector<MojoHandleSignals> signals;
82 for (const auto& it : collector_impls_) {
83 handles.push_back(it->RawHandleValue());
84 signals.push_back(MOJO_HANDLE_SIGNAL_READABLE |
85 MOJO_HANDLE_SIGNAL_PEER_CLOSED);
86 }
87 std::vector<MojoHandleSignalsState> signals_states(signals.size());
88 const mojo::WaitManyResult wait_many_result =
89 mojo::WaitMany(handles, signals, mojo_deadline, &signals_states);
90 if (wait_many_result.result == MOJO_RESULT_DEADLINE_EXCEEDED) {
91 // Timed out waiting, nothing more to read.
92 break;
93 } else if (wait_many_result.IsIndexValid()) {
viettrungluu 2015/04/02 19:54:13 nit: no "else" needed?
94 // Iterate backwards so we can remove closed pipes from |collector_impls_|
95 // without invalidating subsequent offsets.
96 for (size_t i = signals_states.size(); i != 0; --i) {
97 size_t index = i - 1;
98 MojoHandleSignals satisfied = signals_states[index].satisfied_signals;
99 if (satisfied & MOJO_HANDLE_SIGNAL_READABLE)
100 collector_impls_[index]->Read();
101 if (satisfied & MOJO_HANDLE_SIGNAL_PEER_CLOSED)
102 collector_impls_.erase(collector_impls_.begin() + index);
103 }
104 }
105 }
106 AllDataCollected();
107 }
108
109 void TracingApp::AllDataCollected() {
110 collector_impls_.clear();
111 sink_->Flush();
112 }
113
114 } // namespace tracing
OLDNEW
« services/tracing/tracing_app.h ('K') | « services/tracing/tracing_app.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698