• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 "perfetto/tracing/internal/system_tracing_backend.h"
18 
19 #include "perfetto/base/logging.h"
20 #include "perfetto/base/task_runner.h"
21 #include "perfetto/ext/tracing/core/tracing_service.h"
22 #include "perfetto/ext/tracing/ipc/default_socket.h"
23 #include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
24 
25 #if PERFETTO_BUILDFLAG(PERFETTO_SYSTEM_CONSUMER)
26 #include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
27 #endif
28 
29 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
30 #include "src/tracing/ipc/shared_memory_windows.h"
31 #else
32 #include "src/tracing/ipc/posix_shared_memory.h"
33 #endif
34 
35 namespace perfetto {
36 namespace internal {
37 
38 // static
GetInstance()39 TracingProducerBackend* SystemProducerTracingBackend::GetInstance() {
40   static auto* instance = new SystemProducerTracingBackend();
41   return instance;
42 }
43 
SystemProducerTracingBackend()44 SystemProducerTracingBackend::SystemProducerTracingBackend() {}
45 
ConnectProducer(const ConnectProducerArgs & args)46 std::unique_ptr<ProducerEndpoint> SystemProducerTracingBackend::ConnectProducer(
47     const ConnectProducerArgs& args) {
48   PERFETTO_DCHECK(args.task_runner->RunsTasksOnCurrentThread());
49 
50   std::unique_ptr<SharedMemory> shm;
51   std::unique_ptr<SharedMemoryArbiter> arbiter;
52   uint32_t shmem_size_hint = args.shmem_size_hint_bytes;
53   uint32_t shmem_page_size_hint = args.shmem_page_size_hint_bytes;
54   if (args.use_producer_provided_smb) {
55     if (shmem_size_hint == 0)
56       shmem_size_hint = TracingService::kDefaultShmSize;
57     if (shmem_page_size_hint == 0)
58       shmem_page_size_hint = TracingService::kDefaultShmPageSize;
59 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
60     shm = SharedMemoryWindows::Create(shmem_size_hint);
61 #else
62     shm = PosixSharedMemory::Create(shmem_size_hint);
63 #endif
64     arbiter = SharedMemoryArbiter::CreateUnboundInstance(shm.get(),
65                                                          shmem_page_size_hint);
66   }
67 
68   auto endpoint = ProducerIPCClient::Connect(
69       GetProducerSocket(), args.producer, args.producer_name, args.task_runner,
70       TracingService::ProducerSMBScrapingMode::kEnabled, shmem_size_hint,
71       shmem_page_size_hint, std::move(shm), std::move(arbiter),
72       ProducerIPCClient::ConnectionFlags::kRetryIfUnreachable);
73   PERFETTO_CHECK(endpoint);
74   return endpoint;
75 }
76 
77 // static
GetInstance()78 TracingConsumerBackend* SystemConsumerTracingBackend::GetInstance() {
79   static auto* instance = new SystemConsumerTracingBackend();
80   return instance;
81 }
82 
SystemConsumerTracingBackend()83 SystemConsumerTracingBackend::SystemConsumerTracingBackend() {}
84 
ConnectConsumer(const ConnectConsumerArgs & args)85 std::unique_ptr<ConsumerEndpoint> SystemConsumerTracingBackend::ConnectConsumer(
86     const ConnectConsumerArgs& args) {
87 #if PERFETTO_BUILDFLAG(PERFETTO_SYSTEM_CONSUMER)
88   auto endpoint = ConsumerIPCClient::Connect(GetConsumerSocket(), args.consumer,
89                                              args.task_runner);
90   PERFETTO_CHECK(endpoint);
91   return endpoint;
92 #else
93   base::ignore_result(args);
94   PERFETTO_FATAL("System backend consumer support disabled");
95   return nullptr;
96 #endif
97 }
98 
99 }  // namespace internal
100 }  // namespace perfetto
101