blob: 22957a9991ba17ab801976f9860509d418e5852c [file] [log] [blame]
Primiano Tucciaf429f92017-12-19 01:51:50 +01001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SRC_TRACING_IPC_SERVICE_CONSUMER_IPC_SERVICE_H_
18#define SRC_TRACING_IPC_SERVICE_CONSUMER_IPC_SERVICE_H_
19
Primiano Tuccid52e6272018-04-06 19:06:53 +020020#include <list>
Primiano Tucciaf429f92017-12-19 01:51:50 +010021#include <map>
22#include <memory>
23#include <string>
24
Primiano Tucci2c5488f2019-06-01 03:27:28 +010025#include "perfetto/ext/base/weak_ptr.h"
26#include "perfetto/ext/ipc/basic_types.h"
27#include "perfetto/ext/tracing/core/consumer.h"
28#include "perfetto/ext/tracing/core/tracing_service.h"
Primiano Tucciaf429f92017-12-19 01:51:50 +010029
Primiano Tucci20b760c2018-01-19 12:36:12 +000030#include "perfetto/ipc/consumer_port.ipc.h"
Primiano Tucciaf429f92017-12-19 01:51:50 +010031
32namespace perfetto {
33
34namespace ipc {
35class Host;
36} // namespace ipc
37
38// Implements the Consumer port of the IPC service. This class proxies requests
39// and responses between the core service logic (|svc_|) and remote Consumer(s)
40// on the IPC socket, through the methods overriddden from ConsumerPort.
Primiano Tucci954d8ed2018-02-28 23:08:53 +000041class ConsumerIPCService : public protos::ConsumerPort {
Primiano Tucciaf429f92017-12-19 01:51:50 +010042 public:
Florian Mayer6a1a4d52018-06-08 16:47:07 +010043 explicit ConsumerIPCService(TracingService* core_service);
Primiano Tucciaf429f92017-12-19 01:51:50 +010044 ~ConsumerIPCService() override;
45
46 // ConsumerPort implementation (from .proto IPC definition).
Primiano Tucci954d8ed2018-02-28 23:08:53 +000047 void EnableTracing(const protos::EnableTracingRequest&,
Primiano Tucciaf429f92017-12-19 01:51:50 +010048 DeferredEnableTracingResponse) override;
Primiano Tucci674076d2018-10-01 10:41:09 +010049 void StartTracing(const protos::StartTracingRequest&,
50 DeferredStartTracingResponse) override;
Oystein Eftevaagcb6e4c82019-03-06 15:38:26 -080051 void ChangeTraceConfig(const protos::ChangeTraceConfigRequest&,
52 DeferredChangeTraceConfigResponse) override;
Primiano Tucci954d8ed2018-02-28 23:08:53 +000053 void DisableTracing(const protos::DisableTracingRequest&,
Primiano Tucciaf429f92017-12-19 01:51:50 +010054 DeferredDisableTracingResponse) override;
Primiano Tucci954d8ed2018-02-28 23:08:53 +000055 void ReadBuffers(const protos::ReadBuffersRequest&,
Primiano Tucciaf429f92017-12-19 01:51:50 +010056 DeferredReadBuffersResponse) override;
Primiano Tucci954d8ed2018-02-28 23:08:53 +000057 void FreeBuffers(const protos::FreeBuffersRequest&,
Primiano Tucciaf429f92017-12-19 01:51:50 +010058 DeferredFreeBuffersResponse) override;
Primiano Tuccid52e6272018-04-06 19:06:53 +020059 void Flush(const protos::FlushRequest&, DeferredFlushResponse) override;
Primiano Tucci9ba1d842018-12-20 17:31:04 +010060 void Detach(const protos::DetachRequest&, DeferredDetachResponse) override;
61 void Attach(const protos::AttachRequest&, DeferredAttachResponse) override;
Eric Secklereaf29ed2019-01-23 09:53:55 +000062 void GetTraceStats(const protos::GetTraceStatsRequest&,
63 DeferredGetTraceStatsResponse) override;
Eric Seckler7b0c9452019-03-18 13:14:36 +000064 void ObserveEvents(const protos::ObserveEventsRequest&,
65 DeferredObserveEventsResponse) override;
Primiano Tucciaf429f92017-12-19 01:51:50 +010066 void OnClientDisconnected() override;
67
68 private:
69 // Acts like a Consumer with the core Service business logic (which doesn't
70 // know anything about the remote transport), but all it does is proxying
71 // methods to the remote Consumer on the other side of the IPC channel.
72 class RemoteConsumer : public Consumer {
73 public:
74 RemoteConsumer();
75 ~RemoteConsumer() override;
76
77 // These methods are called by the |core_service_| business logic. There is
78 // no connection here, these methods are posted straight away.
79 void OnConnect() override;
80 void OnDisconnect() override;
Primiano Tuccidca727d2018-04-04 11:31:55 +020081 void OnTracingDisabled() override;
Primiano Tucci3324dfc2017-12-20 14:35:58 +010082 void OnTraceData(std::vector<TracePacket>, bool has_more) override;
Primiano Tucci9ba1d842018-12-20 17:31:04 +010083 void OnDetach(bool) override;
84 void OnAttach(bool, const TraceConfig&) override;
Eric Secklereaf29ed2019-01-23 09:53:55 +000085 void OnTraceStats(bool, const TraceStats&) override;
Eric Seckler7b0c9452019-03-18 13:14:36 +000086 void OnObservableEvents(const ObservableEvents&) override;
87
88 void CloseObserveEventsResponseStream();
Primiano Tucciaf429f92017-12-19 01:51:50 +010089
90 // The interface obtained from the core service business logic through
Florian Mayer6a1a4d52018-06-08 16:47:07 +010091 // TracingService::ConnectConsumer(this). This allows to invoke methods for
92 // a specific Consumer on the Service business logic.
93 std::unique_ptr<TracingService::ConsumerEndpoint> service_endpoint;
Primiano Tucciaf429f92017-12-19 01:51:50 +010094
Eric Seckler7b0c9452019-03-18 13:14:36 +000095 // After ReadBuffers() is invoked, this binds the async callback that
Primiano Tucciaf429f92017-12-19 01:51:50 +010096 // allows to stream trace packets back to the client.
97 DeferredReadBuffersResponse read_buffers_response;
Primiano Tucci2ffd1a52018-03-27 01:01:30 +010098
99 // After EnableTracing() is invoked, this binds the async callback that
Primiano Tuccidca727d2018-04-04 11:31:55 +0200100 // allows to send the OnTracingDisabled notification.
Primiano Tucci2ffd1a52018-03-27 01:01:30 +0100101 DeferredEnableTracingResponse enable_tracing_response;
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100102
103 // After Detach() is invoked, this binds the async callback that allows to
104 // send the session id to the consumer.
105 DeferredDetachResponse detach_response;
106
107 // As above, but for the Attach() case.
108 DeferredAttachResponse attach_response;
Eric Secklereaf29ed2019-01-23 09:53:55 +0000109
110 // As above, but for GetTraceStats().
111 DeferredGetTraceStatsResponse get_trace_stats_response;
Eric Seckler7b0c9452019-03-18 13:14:36 +0000112
113 // After ObserveEvents() is invoked, this binds the async callback that
114 // allows to stream ObservableEvents back to the client.
115 DeferredObserveEventsResponse observe_events_response;
Primiano Tucciaf429f92017-12-19 01:51:50 +0100116 };
117
Primiano Tuccid52e6272018-04-06 19:06:53 +0200118 // This has to be a container that doesn't invalidate iterators.
119 using PendingFlushResponses = std::list<DeferredFlushResponse>;
120
Primiano Tucciaf429f92017-12-19 01:51:50 +0100121 ConsumerIPCService(const ConsumerIPCService&) = delete;
122 ConsumerIPCService& operator=(const ConsumerIPCService&) = delete;
123
124 // Returns the ConsumerEndpoint in the core business logic that corresponds to
125 // the current IPC request.
126 RemoteConsumer* GetConsumerForCurrentRequest();
127
Primiano Tuccid52e6272018-04-06 19:06:53 +0200128 void OnFlushCallback(bool success, PendingFlushResponses::iterator);
129
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100130 TracingService* const core_service_;
Primiano Tucciaf429f92017-12-19 01:51:50 +0100131
132 // Maps IPC clients to ConsumerEndpoint instances registered on the
133 // |core_service_| business logic.
134 std::map<ipc::ClientID, std::unique_ptr<RemoteConsumer>> consumers_;
135
Primiano Tuccid52e6272018-04-06 19:06:53 +0200136 PendingFlushResponses pending_flush_responses_;
137
138 base::WeakPtrFactory<ConsumerIPCService> weak_ptr_factory_; // Keep last.
Primiano Tucciaf429f92017-12-19 01:51:50 +0100139};
140
141} // namespace perfetto
142
143#endif // SRC_TRACING_IPC_SERVICE_CONSUMER_IPC_SERVICE_H_