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_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_ 18 #define SRC_TRACING_IPC_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_ 19 20 #include <stdint.h> 21 22 #include <list> 23 #include <vector> 24 25 #include "perfetto/ext/base/scoped_file.h" 26 #include "perfetto/ext/base/weak_ptr.h" 27 #include "perfetto/ext/ipc/service_proxy.h" 28 #include "perfetto/ext/tracing/core/basic_types.h" 29 #include "perfetto/ext/tracing/core/trace_packet.h" 30 #include "perfetto/ext/tracing/core/tracing_service.h" 31 #include "perfetto/ext/tracing/ipc/consumer_ipc_client.h" 32 #include "perfetto/tracing/core/forward_decls.h" 33 34 #include "protos/perfetto/ipc/consumer_port.ipc.h" 35 36 namespace perfetto { 37 38 namespace base { 39 class TaskRunner; 40 } // namespace base 41 42 namespace ipc { 43 class Client; 44 } // namespace ipc 45 46 class Consumer; 47 48 // Exposes a Service endpoint to Consumer(s), proxying all requests through a 49 // IPC channel to the remote Service. This class is the glue layer between the 50 // generic Service interface exposed to the clients of the library and the 51 // actual IPC transport. 52 class ConsumerIPCClientImpl : public TracingService::ConsumerEndpoint, 53 public ipc::ServiceProxy::EventListener { 54 public: 55 ConsumerIPCClientImpl(const char* service_sock_name, 56 Consumer*, 57 base::TaskRunner*); 58 ~ConsumerIPCClientImpl() override; 59 60 // TracingService::ConsumerEndpoint implementation. 61 // These methods are invoked by the actual Consumer(s) code by clients of the 62 // tracing library, which know nothing about the IPC transport. 63 void EnableTracing(const TraceConfig&, base::ScopedFile) override; 64 void StartTracing() override; 65 void ChangeTraceConfig(const TraceConfig&) override; 66 void DisableTracing() override; 67 void ReadBuffers() override; 68 void FreeBuffers() override; 69 void Flush(uint32_t timeout_ms, FlushCallback) override; 70 void Detach(const std::string& key) override; 71 void Attach(const std::string& key) override; 72 void GetTraceStats() override; 73 void ObserveEvents(uint32_t enabled_event_types) override; 74 void QueryServiceState(QueryServiceStateCallback) override; 75 void QueryCapabilities(QueryCapabilitiesCallback) override; 76 void SaveTraceForBugreport(SaveTraceForBugreportCallback) override; 77 void CloneSession(TracingSessionID) override; 78 79 // ipc::ServiceProxy::EventListener implementation. 80 // These methods are invoked by the IPC layer, which knows nothing about 81 // tracing, consumers and consumers. 82 void OnConnect() override; 83 void OnDisconnect() override; 84 85 private: 86 struct PendingQueryServiceRequest { 87 QueryServiceStateCallback callback; 88 89 // All the replies will be appended here until |has_more| == false. 90 std::vector<uint8_t> merged_resp; 91 }; 92 93 // List because we need stable iterators. 94 using PendingQueryServiceRequests = std::list<PendingQueryServiceRequest>; 95 96 void OnReadBuffersResponse( 97 ipc::AsyncResult<protos::gen::ReadBuffersResponse>); 98 void OnEnableTracingResponse( 99 ipc::AsyncResult<protos::gen::EnableTracingResponse>); 100 void OnQueryServiceStateResponse( 101 ipc::AsyncResult<protos::gen::QueryServiceStateResponse>, 102 PendingQueryServiceRequests::iterator); 103 104 // TODO(primiano): think to dtor order, do we rely on any specific sequence? 105 Consumer* const consumer_; 106 107 // The object that owns the client socket and takes care of IPC traffic. 108 std::unique_ptr<ipc::Client> ipc_channel_; 109 110 // The proxy interface for the consumer port of the service. It is bound 111 // to |ipc_channel_| and (de)serializes method invocations over the wire. 112 protos::gen::ConsumerPortProxy consumer_port_; 113 114 bool connected_ = false; 115 116 PendingQueryServiceRequests pending_query_svc_reqs_; 117 118 // When a packet is too big to fit into a ReadBuffersResponse IPC, the service 119 // will chunk it into several IPCs, each containing few slices of the packet 120 // (a packet's slice is always guaranteed to be << kIPCBufferSize). When 121 // chunking happens this field accumulates the slices received until the 122 // one with |last_slice_for_packet| == true is received. 123 TracePacket partial_packet_; 124 125 // Keep last. 126 base::WeakPtrFactory<ConsumerIPCClientImpl> weak_ptr_factory_; 127 }; 128 129 } // namespace perfetto 130 131 #endif // SRC_TRACING_IPC_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_ 132