xref: /aosp_15_r20/external/perfetto/src/tracing/internal/system_tracing_backend.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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/producer_ipc_client.h"
23 #include "perfetto/tracing/default_socket.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(
65         shm.get(), shmem_page_size_hint, SharedMemoryABI::ShmemMode::kDefault);
66   }
67 
68   ipc::Client::ConnArgs conn_args(GetProducerSocket(), true);
69   auto endpoint = ProducerIPCClient::Connect(
70       std::move(conn_args), args.producer, args.producer_name, args.task_runner,
71       TracingService::ProducerSMBScrapingMode::kEnabled, shmem_size_hint,
72       shmem_page_size_hint, std::move(shm), std::move(arbiter),
73       args.create_socket_async);
74   PERFETTO_CHECK(endpoint);
75   return endpoint;
76 }
77 
78 // static
GetInstance()79 TracingConsumerBackend* SystemConsumerTracingBackend::GetInstance() {
80   static auto* instance = new SystemConsumerTracingBackend();
81   return instance;
82 }
83 
SystemConsumerTracingBackend()84 SystemConsumerTracingBackend::SystemConsumerTracingBackend() {}
85 
ConnectConsumer(const ConnectConsumerArgs & args)86 std::unique_ptr<ConsumerEndpoint> SystemConsumerTracingBackend::ConnectConsumer(
87     const ConnectConsumerArgs& args) {
88 #if PERFETTO_BUILDFLAG(PERFETTO_SYSTEM_CONSUMER)
89   auto endpoint = ConsumerIPCClient::Connect(GetConsumerSocket(), args.consumer,
90                                              args.task_runner);
91   PERFETTO_CHECK(endpoint);
92   return endpoint;
93 #else
94   base::ignore_result(args);
95   PERFETTO_FATAL("System backend consumer support disabled");
96   return nullptr;
97 #endif
98 }
99 
100 }  // namespace internal
101 }  // namespace perfetto
102