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 78 // ipc::ServiceProxy::EventListener implementation. 79 // These methods are invoked by the IPC layer, which knows nothing about 80 // tracing, consumers and consumers. 81 void OnConnect() override; 82 void OnDisconnect() override; 83 84 private: 85 struct PendingQueryServiceRequest { 86 QueryServiceStateCallback callback; 87 88 // All the replies will be appended here until |has_more| == false. 89 std::vector<uint8_t> merged_resp; 90 }; 91 92 // List because we need stable iterators. 93 using PendingQueryServiceRequests = std::list<PendingQueryServiceRequest>; 94 95 void OnReadBuffersResponse( 96 ipc::AsyncResult<protos::gen::ReadBuffersResponse>); 97 void OnEnableTracingResponse( 98 ipc::AsyncResult<protos::gen::EnableTracingResponse>); 99 void OnQueryServiceStateResponse( 100 ipc::AsyncResult<protos::gen::QueryServiceStateResponse>, 101 PendingQueryServiceRequests::iterator); 102 103 // TODO(primiano): think to dtor order, do we rely on any specific sequence? 104 Consumer* const consumer_; 105 106 // The object that owns the client socket and takes care of IPC traffic. 107 std::unique_ptr<ipc::Client> ipc_channel_; 108 109 // The proxy interface for the consumer port of the service. It is bound 110 // to |ipc_channel_| and (de)serializes method invocations over the wire. 111 protos::gen::ConsumerPortProxy consumer_port_; 112 113 bool connected_ = false; 114 115 PendingQueryServiceRequests pending_query_svc_reqs_; 116 117 // When a packet is too big to fit into a ReadBuffersResponse IPC, the service 118 // will chunk it into several IPCs, each containing few slices of the packet 119 // (a packet's slice is always guaranteed to be << kIPCBufferSize). When 120 // chunking happens this field accumulates the slices received until the 121 // one with |last_slice_for_packet| == true is received. 122 TracePacket partial_packet_; 123 124 // Keep last. 125 base::WeakPtrFactory<ConsumerIPCClientImpl> weak_ptr_factory_; 126 }; 127 128 } // namespace perfetto 129 130 #endif // SRC_TRACING_IPC_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_ 131