• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/tracing/ipc/service/service_ipc_host_impl.h"
18 
19 #include <list>
20 
21 #include "perfetto/base/logging.h"
22 #include "perfetto/base/task_runner.h"
23 #include "perfetto/ext/ipc/host.h"
24 #include "perfetto/ext/tracing/core/tracing_service.h"
25 #include "perfetto/tracing/default_socket.h"
26 #include "src/tracing/ipc/service/consumer_ipc_service.h"
27 #include "src/tracing/ipc/service/producer_ipc_service.h"
28 #include "src/tracing/ipc/service/relay_ipc_service.h"
29 
30 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
31 #include "src/tracing/ipc/shared_memory_windows.h"
32 #else
33 #include "src/tracing/ipc/posix_shared_memory.h"
34 #endif
35 
36 namespace perfetto {
37 
38 namespace {
39 constexpr uint32_t kProducerSocketTxTimeoutMs = 10;
40 
CreateIpcHost(base::TaskRunner * task_runner,ListenEndpoint ep)41 std::unique_ptr<ipc::Host> CreateIpcHost(base::TaskRunner* task_runner,
42                                          ListenEndpoint ep) {
43   if (!ep.sock_name.empty()) {
44     PERFETTO_DCHECK(!ep.sock_handle && !ep.ipc_host);
45     return ipc::Host::CreateInstance(ep.sock_name.c_str(), task_runner);
46   }
47   if (ep.sock_handle) {
48     PERFETTO_DCHECK(!ep.ipc_host);
49     return ipc::Host::CreateInstance(std::move(ep.sock_handle), task_runner);
50   }
51   PERFETTO_DCHECK(ep.ipc_host);
52   return std::move(ep.ipc_host);
53 }
54 
55 }  // namespace
56 
57 // TODO(fmayer): implement per-uid connection limit (b/69093705).
58 
59 // Implements the publicly exposed factory method declared in
60 // include/tracing/posix_ipc/posix_service_host.h.
CreateInstance(base::TaskRunner * task_runner,TracingService::InitOpts init_opts)61 std::unique_ptr<ServiceIPCHost> ServiceIPCHost::CreateInstance(
62     base::TaskRunner* task_runner,
63     TracingService::InitOpts init_opts) {
64   return std::unique_ptr<ServiceIPCHost>(
65       new ServiceIPCHostImpl(task_runner, init_opts));
66 }
67 
ServiceIPCHostImpl(base::TaskRunner * task_runner,TracingService::InitOpts init_opts)68 ServiceIPCHostImpl::ServiceIPCHostImpl(base::TaskRunner* task_runner,
69                                        TracingService::InitOpts init_opts)
70     : task_runner_(task_runner), init_opts_(init_opts) {}
71 
~ServiceIPCHostImpl()72 ServiceIPCHostImpl::~ServiceIPCHostImpl() {}
73 
Start(std::list<ListenEndpoint> producer_sockets,ListenEndpoint consumer_socket)74 bool ServiceIPCHostImpl::Start(std::list<ListenEndpoint> producer_sockets,
75                                ListenEndpoint consumer_socket) {
76   PERFETTO_CHECK(!svc_);  // Check if already started.
77 
78   // Initialize the IPC transport.
79   for (auto& sock : producer_sockets) {
80     producer_ipc_ports_.emplace_back(
81         CreateIpcHost(task_runner_, std::move(sock)));
82   }
83   consumer_ipc_port_ = CreateIpcHost(task_runner_, std::move(consumer_socket));
84 
85   return DoStart();
86 }
87 
DoStart()88 bool ServiceIPCHostImpl::DoStart() {
89   // Create and initialize the platform-independent tracing business logic.
90 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
91   std::unique_ptr<SharedMemory::Factory> shm_factory(
92       new SharedMemoryWindows::Factory());
93 #else
94   std::unique_ptr<SharedMemory::Factory> shm_factory(
95       new PosixSharedMemory::Factory());
96 #endif
97   svc_ = TracingService::CreateInstance(std::move(shm_factory), task_runner_,
98                                         init_opts_);
99 
100   if (producer_ipc_ports_.empty() || !consumer_ipc_port_ ||
101       std::any_of(producer_ipc_ports_.begin(), producer_ipc_ports_.end(),
102                   [](const std::unique_ptr<ipc::Host>& port) {
103                     return port == nullptr;
104                   })) {
105     Shutdown();
106     return false;
107   }
108 
109   // Lower the timeout for blocking socket sends to producers as we shouldn't
110   // normally exhaust the kernel send buffer unless the producer is
111   // unresponsive. We'll drop the connection if the timeout is hit (see
112   // UnixSocket::Send). Context in b/236813972, b/193234818.
113   // Consumer port continues using the default timeout (10s) as there are
114   // generally fewer consumer processes, and they're better behaved. Also the
115   // consumer port ipcs might exhaust the send buffer under normal operation
116   // due to large messages such as ReadBuffersResponse.
117   for (auto& producer_ipc_port : producer_ipc_ports_)
118     producer_ipc_port->SetSocketSendTimeoutMs(kProducerSocketTxTimeoutMs);
119 
120   // TODO(fmayer): add a test that destroyes the ServiceIPCHostImpl soon after
121   // Start() and checks that no spurious callbacks are issued.
122   for (auto& producer_ipc_port : producer_ipc_ports_) {
123     bool producer_service_exposed = producer_ipc_port->ExposeService(
124         std::unique_ptr<ipc::Service>(new ProducerIPCService(svc_.get())));
125     PERFETTO_CHECK(producer_service_exposed);
126 
127     if (!init_opts_.enable_relay_endpoint)
128       continue;
129     // Expose a secondary service for sync with remote relay service
130     // if requested.
131     bool relay_service_exposed = producer_ipc_port->ExposeService(
132         std::unique_ptr<ipc::Service>(new RelayIPCService(svc_.get())));
133     PERFETTO_CHECK(relay_service_exposed);
134   }
135 
136   bool consumer_service_exposed = consumer_ipc_port_->ExposeService(
137       std::unique_ptr<ipc::Service>(new ConsumerIPCService(svc_.get())));
138   PERFETTO_CHECK(consumer_service_exposed);
139 
140   return true;
141 }
142 
service() const143 TracingService* ServiceIPCHostImpl::service() const {
144   return svc_.get();
145 }
146 
Shutdown()147 void ServiceIPCHostImpl::Shutdown() {
148   // TODO(primiano): add a test that causes the Shutdown() and checks that no
149   // spurious callbacks are issued.
150   producer_ipc_ports_.clear();
151   consumer_ipc_port_.reset();
152   svc_.reset();
153 }
154 
155 // Definitions for the base class ctor/dtor.
156 ServiceIPCHost::ServiceIPCHost() = default;
157 ServiceIPCHost::~ServiceIPCHost() = default;
158 
159 // Definitions for ListenEndpoint, declared in service_ipc_host.h.
ListenEndpoint(const char * socket_name)160 ListenEndpoint::ListenEndpoint(const char* socket_name)
161     : sock_name(socket_name) {}
ListenEndpoint(std::string socket_name)162 ListenEndpoint::ListenEndpoint(std::string socket_name)
163     : sock_name(std::move(socket_name)) {}
ListenEndpoint(base::ScopedSocketHandle sh)164 ListenEndpoint::ListenEndpoint(base::ScopedSocketHandle sh)
165     : sock_handle(std::move(sh)) {}
ListenEndpoint(std::unique_ptr<ipc::Host> ih)166 ListenEndpoint::ListenEndpoint(std::unique_ptr<ipc::Host> ih)
167     : ipc_host(std::move(ih)) {}
168 ListenEndpoint::ListenEndpoint(ListenEndpoint&&) noexcept = default;
169 ListenEndpoint& ListenEndpoint::operator=(ListenEndpoint&&) = default;
170 ListenEndpoint::~ListenEndpoint() = default;
171 
172 // Definitions for overloads of Start, declared in service_ipc_host.h.
173 
Start(const char * producer_socket_names,const char * consumer_socket_name)174 bool ServiceIPCHost::Start(const char* producer_socket_names,
175                            const char* consumer_socket_name) {
176   std::list<ListenEndpoint> eps;
177   for (const auto& sock_name : TokenizeProducerSockets(producer_socket_names)) {
178     eps.emplace_back(ListenEndpoint(sock_name));
179   }
180   return Start(std::move(eps), ListenEndpoint(consumer_socket_name));
181 }
182 
Start(base::ScopedSocketHandle producer_socket_fd,base::ScopedSocketHandle consumer_socket_fd)183 bool ServiceIPCHost::Start(base::ScopedSocketHandle producer_socket_fd,
184                            base::ScopedSocketHandle consumer_socket_fd) {
185   std::list<ListenEndpoint> eps;
186   eps.emplace_back(ListenEndpoint(std::move(producer_socket_fd)));
187   return Start(std::move(eps), ListenEndpoint(std::move(consumer_socket_fd)));
188 }
189 
Start(std::unique_ptr<ipc::Host> producer_host,std::unique_ptr<ipc::Host> consumer_host)190 bool ServiceIPCHost::Start(std::unique_ptr<ipc::Host> producer_host,
191                            std::unique_ptr<ipc::Host> consumer_host) {
192   std::list<ListenEndpoint> eps;
193   eps.emplace_back(ListenEndpoint(std::move(producer_host)));
194   return Start(std::move(eps), ListenEndpoint(std::move(consumer_host)));
195 }
196 
197 }  // namespace perfetto
198