Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 1 | /* |
| 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 Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 20 | #include <list> |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 21 | #include <map> |
| 22 | #include <memory> |
| 23 | #include <string> |
| 24 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame^] | 25 | #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 Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 29 | |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 30 | #include "perfetto/ipc/consumer_port.ipc.h" |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 31 | |
| 32 | namespace perfetto { |
| 33 | |
| 34 | namespace ipc { |
| 35 | class 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 Tucci | 954d8ed | 2018-02-28 23:08:53 +0000 | [diff] [blame] | 41 | class ConsumerIPCService : public protos::ConsumerPort { |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 42 | public: |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 43 | explicit ConsumerIPCService(TracingService* core_service); |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 44 | ~ConsumerIPCService() override; |
| 45 | |
| 46 | // ConsumerPort implementation (from .proto IPC definition). |
Primiano Tucci | 954d8ed | 2018-02-28 23:08:53 +0000 | [diff] [blame] | 47 | void EnableTracing(const protos::EnableTracingRequest&, |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 48 | DeferredEnableTracingResponse) override; |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 49 | void StartTracing(const protos::StartTracingRequest&, |
| 50 | DeferredStartTracingResponse) override; |
Oystein Eftevaag | cb6e4c8 | 2019-03-06 15:38:26 -0800 | [diff] [blame] | 51 | void ChangeTraceConfig(const protos::ChangeTraceConfigRequest&, |
| 52 | DeferredChangeTraceConfigResponse) override; |
Primiano Tucci | 954d8ed | 2018-02-28 23:08:53 +0000 | [diff] [blame] | 53 | void DisableTracing(const protos::DisableTracingRequest&, |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 54 | DeferredDisableTracingResponse) override; |
Primiano Tucci | 954d8ed | 2018-02-28 23:08:53 +0000 | [diff] [blame] | 55 | void ReadBuffers(const protos::ReadBuffersRequest&, |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 56 | DeferredReadBuffersResponse) override; |
Primiano Tucci | 954d8ed | 2018-02-28 23:08:53 +0000 | [diff] [blame] | 57 | void FreeBuffers(const protos::FreeBuffersRequest&, |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 58 | DeferredFreeBuffersResponse) override; |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 59 | void Flush(const protos::FlushRequest&, DeferredFlushResponse) override; |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 60 | void Detach(const protos::DetachRequest&, DeferredDetachResponse) override; |
| 61 | void Attach(const protos::AttachRequest&, DeferredAttachResponse) override; |
Eric Seckler | eaf29ed | 2019-01-23 09:53:55 +0000 | [diff] [blame] | 62 | void GetTraceStats(const protos::GetTraceStatsRequest&, |
| 63 | DeferredGetTraceStatsResponse) override; |
Eric Seckler | 7b0c945 | 2019-03-18 13:14:36 +0000 | [diff] [blame] | 64 | void ObserveEvents(const protos::ObserveEventsRequest&, |
| 65 | DeferredObserveEventsResponse) override; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 66 | 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 Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 81 | void OnTracingDisabled() override; |
Primiano Tucci | 3324dfc | 2017-12-20 14:35:58 +0100 | [diff] [blame] | 82 | void OnTraceData(std::vector<TracePacket>, bool has_more) override; |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 83 | void OnDetach(bool) override; |
| 84 | void OnAttach(bool, const TraceConfig&) override; |
Eric Seckler | eaf29ed | 2019-01-23 09:53:55 +0000 | [diff] [blame] | 85 | void OnTraceStats(bool, const TraceStats&) override; |
Eric Seckler | 7b0c945 | 2019-03-18 13:14:36 +0000 | [diff] [blame] | 86 | void OnObservableEvents(const ObservableEvents&) override; |
| 87 | |
| 88 | void CloseObserveEventsResponseStream(); |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 89 | |
| 90 | // The interface obtained from the core service business logic through |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 91 | // 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 Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 94 | |
Eric Seckler | 7b0c945 | 2019-03-18 13:14:36 +0000 | [diff] [blame] | 95 | // After ReadBuffers() is invoked, this binds the async callback that |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 96 | // allows to stream trace packets back to the client. |
| 97 | DeferredReadBuffersResponse read_buffers_response; |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 98 | |
| 99 | // After EnableTracing() is invoked, this binds the async callback that |
Primiano Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 100 | // allows to send the OnTracingDisabled notification. |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 101 | DeferredEnableTracingResponse enable_tracing_response; |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 102 | |
| 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 Seckler | eaf29ed | 2019-01-23 09:53:55 +0000 | [diff] [blame] | 109 | |
| 110 | // As above, but for GetTraceStats(). |
| 111 | DeferredGetTraceStatsResponse get_trace_stats_response; |
Eric Seckler | 7b0c945 | 2019-03-18 13:14:36 +0000 | [diff] [blame] | 112 | |
| 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 Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 116 | }; |
| 117 | |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 118 | // This has to be a container that doesn't invalidate iterators. |
| 119 | using PendingFlushResponses = std::list<DeferredFlushResponse>; |
| 120 | |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 121 | 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 Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 128 | void OnFlushCallback(bool success, PendingFlushResponses::iterator); |
| 129 | |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 130 | TracingService* const core_service_; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 131 | |
| 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 Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 136 | PendingFlushResponses pending_flush_responses_; |
| 137 | |
| 138 | base::WeakPtrFactory<ConsumerIPCService> weak_ptr_factory_; // Keep last. |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | } // namespace perfetto |
| 142 | |
| 143 | #endif // SRC_TRACING_IPC_SERVICE_CONSUMER_IPC_SERVICE_H_ |