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