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