• 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 
26 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
27 #include "src/tracing/ipc/shared_memory_windows.h"
28 #else
29 #include "src/tracing/ipc/posix_shared_memory.h"
30 #endif
31 
32 namespace perfetto {
33 
34 namespace {
35 constexpr uint32_t kProducerSocketTxTimeoutMs = 10;
36 }
37 
38 // TODO(fmayer): implement per-uid connection limit (b/69093705).
39 
40 // Implements the publicly exposed factory method declared in
41 // include/tracing/posix_ipc/posix_service_host.h.
CreateInstance(base::TaskRunner * task_runner)42 std::unique_ptr<ServiceIPCHost> ServiceIPCHost::CreateInstance(
43     base::TaskRunner* task_runner) {
44   return std::unique_ptr<ServiceIPCHost>(new ServiceIPCHostImpl(task_runner));
45 }
46 
ServiceIPCHostImpl(base::TaskRunner * task_runner)47 ServiceIPCHostImpl::ServiceIPCHostImpl(base::TaskRunner* task_runner)
48     : task_runner_(task_runner) {}
49 
~ServiceIPCHostImpl()50 ServiceIPCHostImpl::~ServiceIPCHostImpl() {}
51 
Start(const char * producer_socket_name,const char * consumer_socket_name)52 bool ServiceIPCHostImpl::Start(const char* producer_socket_name,
53                                const char* consumer_socket_name) {
54   PERFETTO_CHECK(!svc_);  // Check if already started.
55 
56   // Initialize the IPC transport.
57   producer_ipc_port_ =
58       ipc::Host::CreateInstance(producer_socket_name, task_runner_);
59   consumer_ipc_port_ =
60       ipc::Host::CreateInstance(consumer_socket_name, task_runner_);
61   return DoStart();
62 }
63 
Start(base::ScopedSocketHandle producer_socket_fd,base::ScopedSocketHandle consumer_socket_fd)64 bool ServiceIPCHostImpl::Start(base::ScopedSocketHandle producer_socket_fd,
65                                base::ScopedSocketHandle consumer_socket_fd) {
66   PERFETTO_CHECK(!svc_);  // Check if already started.
67 
68   // Initialize the IPC transport.
69   producer_ipc_port_ =
70       ipc::Host::CreateInstance(std::move(producer_socket_fd), task_runner_);
71   consumer_ipc_port_ =
72       ipc::Host::CreateInstance(std::move(consumer_socket_fd), task_runner_);
73   return DoStart();
74 }
75 
DoStart()76 bool ServiceIPCHostImpl::DoStart() {
77   // Create and initialize the platform-independent tracing business logic.
78 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
79   std::unique_ptr<SharedMemory::Factory> shm_factory(
80       new SharedMemoryWindows::Factory());
81 #else
82   std::unique_ptr<SharedMemory::Factory> shm_factory(
83       new PosixSharedMemory::Factory());
84 #endif
85   svc_ = TracingService::CreateInstance(std::move(shm_factory), task_runner_);
86 
87   if (!producer_ipc_port_ || !consumer_ipc_port_) {
88     Shutdown();
89     return false;
90   }
91 
92   // Lower the timeout for blocking socket sends to producers as we shouldn't
93   // normally exhaust the kernel send buffer unless the producer is
94   // unresponsive. We'll drop the connection if the timeout is hit (see
95   // UnixSocket::Send). Context in b/236813972, b/193234818.
96   // Consumer port continues using the default timeout (10s) as there are
97   // generally fewer consumer processes, and they're better behaved. Also the
98   // consumer port ipcs might exhaust the send buffer under normal operation
99   // due to large messages such as ReadBuffersResponse.
100   producer_ipc_port_->SetSocketSendTimeoutMs(kProducerSocketTxTimeoutMs);
101 
102   // TODO(fmayer): add a test that destroyes the ServiceIPCHostImpl soon after
103   // Start() and checks that no spurious callbacks are issued.
104   bool producer_service_exposed = producer_ipc_port_->ExposeService(
105       std::unique_ptr<ipc::Service>(new ProducerIPCService(svc_.get())));
106   PERFETTO_CHECK(producer_service_exposed);
107 
108   bool consumer_service_exposed = consumer_ipc_port_->ExposeService(
109       std::unique_ptr<ipc::Service>(new ConsumerIPCService(svc_.get())));
110   PERFETTO_CHECK(consumer_service_exposed);
111 
112   return true;
113 }
114 
service() const115 TracingService* ServiceIPCHostImpl::service() const {
116   return svc_.get();
117 }
118 
Shutdown()119 void ServiceIPCHostImpl::Shutdown() {
120   // TODO(primiano): add a test that causes the Shutdown() and checks that no
121   // spurious callbacks are issued.
122   producer_ipc_port_.reset();
123   consumer_ipc_port_.reset();
124   svc_.reset();
125 }
126 
127 // Definitions for the base class ctor/dtor.
128 ServiceIPCHost::ServiceIPCHost() = default;
129 ServiceIPCHost::~ServiceIPCHost() = default;
130 
131 }  // namespace perfetto
132