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 INCLUDE_PERFETTO_EXT_TRACING_IPC_PRODUCER_IPC_CLIENT_H_ 18 #define INCLUDE_PERFETTO_EXT_TRACING_IPC_PRODUCER_IPC_CLIENT_H_ 19 20 #include <memory> 21 #include <string> 22 23 #include "perfetto/base/export.h" 24 #include "perfetto/ext/ipc/client.h" 25 #include "perfetto/ext/tracing/core/shared_memory.h" 26 #include "perfetto/ext/tracing/core/shared_memory_arbiter.h" 27 #include "perfetto/ext/tracing/core/tracing_service.h" 28 #include "perfetto/tracing/tracing_backend.h" 29 30 namespace perfetto { 31 32 class Producer; 33 34 // Allows to connect to a remote Service through a UNIX domain socket. 35 // Exposed to: 36 // Producer(s) of the tracing library. 37 // Implemented in: 38 // src/tracing/ipc/producer/producer_ipc_client_impl.cc 39 class PERFETTO_EXPORT_COMPONENT ProducerIPCClient { 40 public: 41 enum class ConnectionFlags { 42 // Fails immediately with OnConnect(false) if the service connection cannot 43 // be established. 44 kDefault = 0, 45 46 // Keeps retrying with exponential backoff indefinitely. The caller will 47 // never see an OnConnect(false). 48 kRetryIfUnreachable = 1, 49 }; 50 51 // Connects to the producer port of the Service listening on the given 52 // |service_sock_name|. If the connection is successful, the OnConnect() 53 // method will be invoked asynchronously on the passed Producer interface. If 54 // the connection fails, OnDisconnect() will be invoked instead. The returned 55 // ProducerEndpoint serves also to delimit the scope of the callbacks invoked 56 // on the Producer interface: no more Producer callbacks are invoked 57 // immediately after its destruction and any pending callback will be dropped. 58 // To provide a producer-allocated shared memory buffer, both |shm| and 59 // |shm_arbiter| should be set. |shm_arbiter| should be an unbound 60 // SharedMemoryArbiter instance. When |shm| and |shm_arbiter| are provided, 61 // the service will attempt to adopt the provided SMB. If this fails, the 62 // ProducerEndpoint will disconnect, but the SMB and arbiter will remain valid 63 // until the client is destroyed. 64 // 65 // TODO(eseckler): Support adoption failure more gracefully. 66 // TODO(primiano): move all the existing use cases to the Connect(ConnArgs) 67 // below. Also move the functionality of ConnectionFlags into ConnArgs. 68 static std::unique_ptr<TracingService::ProducerEndpoint> Connect( 69 const char* service_sock_name, 70 Producer*, 71 const std::string& producer_name, 72 base::TaskRunner*, 73 TracingService::ProducerSMBScrapingMode smb_scraping_mode = 74 TracingService::ProducerSMBScrapingMode::kDefault, 75 size_t shared_memory_size_hint_bytes = 0, 76 size_t shared_memory_page_size_hint_bytes = 0, 77 std::unique_ptr<SharedMemory> shm = nullptr, 78 std::unique_ptr<SharedMemoryArbiter> shm_arbiter = nullptr, 79 ConnectionFlags = ConnectionFlags::kDefault); 80 81 // Overload of Connect() to support adopting a connected socket using 82 // ipc::Client::ConnArgs. 83 static std::unique_ptr<TracingService::ProducerEndpoint> Connect( 84 ipc::Client::ConnArgs, 85 Producer*, 86 const std::string& producer_name, 87 base::TaskRunner*, 88 TracingService::ProducerSMBScrapingMode smb_scraping_mode = 89 TracingService::ProducerSMBScrapingMode::kDefault, 90 size_t shared_memory_size_hint_bytes = 0, 91 size_t shared_memory_page_size_hint_bytes = 0, 92 std::unique_ptr<SharedMemory> shm = nullptr, 93 std::unique_ptr<SharedMemoryArbiter> shm_arbiter = nullptr, 94 CreateSocketAsync create_socket_async = nullptr); 95 96 protected: 97 ProducerIPCClient() = delete; 98 }; 99 100 } // namespace perfetto 101 102 #endif // INCLUDE_PERFETTO_EXT_TRACING_IPC_PRODUCER_IPC_CLIENT_H_ 103