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 20 #include <list> 21 #include <map> 22 #include <memory> 23 #include <string> 24 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" 29 #include "perfetto/tracing/core/forward_decls.h" 30 #include "protos/perfetto/ipc/consumer_port.ipc.h" 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. 41 class ConsumerIPCService : public protos::gen::ConsumerPort { 42 public: 43 explicit ConsumerIPCService(TracingService* core_service); 44 ~ConsumerIPCService() override; 45 46 // ConsumerPort implementation (from .proto IPC definition). 47 void EnableTracing(const protos::gen::EnableTracingRequest&, 48 DeferredEnableTracingResponse) override; 49 void StartTracing(const protos::gen::StartTracingRequest&, 50 DeferredStartTracingResponse) override; 51 void ChangeTraceConfig(const protos::gen::ChangeTraceConfigRequest&, 52 DeferredChangeTraceConfigResponse) override; 53 void DisableTracing(const protos::gen::DisableTracingRequest&, 54 DeferredDisableTracingResponse) override; 55 void ReadBuffers(const protos::gen::ReadBuffersRequest&, 56 DeferredReadBuffersResponse) override; 57 void FreeBuffers(const protos::gen::FreeBuffersRequest&, 58 DeferredFreeBuffersResponse) override; 59 void Flush(const protos::gen::FlushRequest&, DeferredFlushResponse) override; 60 void Detach(const protos::gen::DetachRequest&, 61 DeferredDetachResponse) override; 62 void Attach(const protos::gen::AttachRequest&, 63 DeferredAttachResponse) override; 64 void GetTraceStats(const protos::gen::GetTraceStatsRequest&, 65 DeferredGetTraceStatsResponse) override; 66 void ObserveEvents(const protos::gen::ObserveEventsRequest&, 67 DeferredObserveEventsResponse) override; 68 void QueryServiceState(const protos::gen::QueryServiceStateRequest&, 69 DeferredQueryServiceStateResponse) override; 70 void QueryCapabilities(const protos::gen::QueryCapabilitiesRequest&, 71 DeferredQueryCapabilitiesResponse) override; 72 void SaveTraceForBugreport(const protos::gen::SaveTraceForBugreportRequest&, 73 DeferredSaveTraceForBugreportResponse) override; 74 void OnClientDisconnected() override; 75 76 private: 77 // Acts like a Consumer with the core Service business logic (which doesn't 78 // know anything about the remote transport), but all it does is proxying 79 // methods to the remote Consumer on the other side of the IPC channel. 80 class RemoteConsumer : public Consumer { 81 public: 82 RemoteConsumer(); 83 ~RemoteConsumer() override; 84 85 // These methods are called by the |core_service_| business logic. There is 86 // no connection here, these methods are posted straight away. 87 void OnConnect() override; 88 void OnDisconnect() override; 89 void OnTracingDisabled(const std::string& error) override; 90 void OnTraceData(std::vector<TracePacket>, bool has_more) override; 91 void OnDetach(bool) override; 92 void OnAttach(bool, const TraceConfig&) override; 93 void OnTraceStats(bool, const TraceStats&) override; 94 void OnObservableEvents(const ObservableEvents&) override; 95 96 void CloseObserveEventsResponseStream(); 97 98 // The interface obtained from the core service business logic through 99 // TracingService::ConnectConsumer(this). This allows to invoke methods for 100 // a specific Consumer on the Service business logic. 101 std::unique_ptr<TracingService::ConsumerEndpoint> service_endpoint; 102 103 // After ReadBuffers() is invoked, this binds the async callback that 104 // allows to stream trace packets back to the client. 105 DeferredReadBuffersResponse read_buffers_response; 106 107 // After EnableTracing() is invoked, this binds the async callback that 108 // allows to send the OnTracingDisabled notification. 109 DeferredEnableTracingResponse enable_tracing_response; 110 111 // After Detach() is invoked, this binds the async callback that allows to 112 // send the session id to the consumer. 113 DeferredDetachResponse detach_response; 114 115 // As above, but for the Attach() case. 116 DeferredAttachResponse attach_response; 117 118 // As above, but for GetTraceStats(). 119 DeferredGetTraceStatsResponse get_trace_stats_response; 120 121 // After ObserveEvents() is invoked, this binds the async callback that 122 // allows to stream ObservableEvents back to the client. 123 DeferredObserveEventsResponse observe_events_response; 124 }; 125 126 // This has to be a container that doesn't invalidate iterators. 127 using PendingFlushResponses = std::list<DeferredFlushResponse>; 128 using PendingQuerySvcResponses = std::list<DeferredQueryServiceStateResponse>; 129 using PendingQueryCapabilitiesResponses = 130 std::list<DeferredQueryCapabilitiesResponse>; 131 using PendingSaveTraceForBugreportResponses = 132 std::list<DeferredSaveTraceForBugreportResponse>; 133 134 ConsumerIPCService(const ConsumerIPCService&) = delete; 135 ConsumerIPCService& operator=(const ConsumerIPCService&) = delete; 136 137 // Returns the ConsumerEndpoint in the core business logic that corresponds to 138 // the current IPC request. 139 RemoteConsumer* GetConsumerForCurrentRequest(); 140 141 void OnFlushCallback(bool success, PendingFlushResponses::iterator); 142 void OnQueryServiceCallback(bool success, 143 const TracingServiceState&, 144 PendingQuerySvcResponses::iterator); 145 void OnQueryCapabilitiesCallback(const TracingServiceCapabilities&, 146 PendingQueryCapabilitiesResponses::iterator); 147 void OnSaveTraceForBugreportCallback( 148 bool success, 149 const std::string& msg, 150 PendingSaveTraceForBugreportResponses::iterator); 151 152 TracingService* const core_service_; 153 154 // Maps IPC clients to ConsumerEndpoint instances registered on the 155 // |core_service_| business logic. 156 std::map<ipc::ClientID, std::unique_ptr<RemoteConsumer>> consumers_; 157 158 PendingFlushResponses pending_flush_responses_; 159 PendingQuerySvcResponses pending_query_service_responses_; 160 PendingQueryCapabilitiesResponses pending_query_capabilities_responses_; 161 PendingSaveTraceForBugreportResponses pending_bugreport_responses_; 162 163 base::WeakPtrFactory<ConsumerIPCService> weak_ptr_factory_; // Keep last. 164 }; 165 166 } // namespace perfetto 167 168 #endif // SRC_TRACING_IPC_SERVICE_CONSUMER_IPC_SERVICE_H_ 169