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/in_process_tracing_backend.h"
18
19 #include "perfetto/base/logging.h"
20 #include "perfetto/base/task_runner.h"
21 #include "perfetto/ext/base/paged_memory.h"
22 #include "perfetto/ext/tracing/core/shared_memory.h"
23 #include "perfetto/ext/tracing/core/tracing_service.h"
24
25 // TODO(primiano): When the in-process backend is used, we should never end up
26 // in a situation where the thread where the TracingService and Producer live
27 // writes a packet and hence can get into the GetNewChunk() stall.
28 // This would happen only if the API client code calls Trace() from one of the
29 // callbacks it receives (e.g. OnStart(), OnStop()). We should either cause a
30 // hard crash or ignore traces from that thread if that happens, because it
31 // will deadlock (the Service will never free up the SMB because won't ever get
32 // to run the task).
33
34 namespace perfetto {
35 namespace internal {
36
37 namespace {
38
39 class InProcessShm : public SharedMemory {
40 public:
41 explicit InProcessShm(size_t size);
42 ~InProcessShm() override;
43 void* start() const override;
44 size_t size() const override;
45 int fd() const override;
46
47 private:
48 base::PagedMemory mem_;
49 };
50
51 class InProcessShmFactory : public SharedMemory::Factory {
52 public:
53 ~InProcessShmFactory() override;
54 std::unique_ptr<SharedMemory> CreateSharedMemory(size_t) override;
55 };
56
57 InProcessShm::~InProcessShm() = default;
58
InProcessShm(size_t size)59 InProcessShm::InProcessShm(size_t size)
60 : mem_(base::PagedMemory::Allocate(size)) {}
61
start() const62 void* InProcessShm::start() const {
63 return mem_.Get();
64 }
65
size() const66 size_t InProcessShm::size() const {
67 return mem_.size();
68 }
69
fd() const70 int InProcessShm::fd() const {
71 return -1;
72 }
73
74 InProcessShmFactory::~InProcessShmFactory() = default;
CreateSharedMemory(size_t size)75 std::unique_ptr<SharedMemory> InProcessShmFactory::CreateSharedMemory(
76 size_t size) {
77 return std::unique_ptr<SharedMemory>(new InProcessShm(size));
78 }
79
80 } // namespace
81
82 // static
GetInstance()83 TracingBackend* InProcessTracingBackend::GetInstance() {
84 static auto* instance = new InProcessTracingBackend();
85 return instance;
86 }
87
InProcessTracingBackend()88 InProcessTracingBackend::InProcessTracingBackend() {}
89
ConnectProducer(const ConnectProducerArgs & args)90 std::unique_ptr<ProducerEndpoint> InProcessTracingBackend::ConnectProducer(
91 const ConnectProducerArgs& args) {
92 PERFETTO_DCHECK(args.task_runner->RunsTasksOnCurrentThread());
93
94 // This should never happen as we can have at most one in-process backend.
95 if (service_)
96 PERFETTO_FATAL("InProcessTracingBackend initialized twice");
97
98 return GetOrCreateService(args.task_runner)
99 ->ConnectProducer(args.producer, /*uid=*/0, args.producer_name,
100 args.shmem_size_hint_bytes,
101 /*in_process=*/true,
102 TracingService::ProducerSMBScrapingMode::kEnabled,
103 args.shmem_page_size_hint_bytes);
104 }
105
ConnectConsumer(const ConnectConsumerArgs & args)106 std::unique_ptr<ConsumerEndpoint> InProcessTracingBackend::ConnectConsumer(
107 const ConnectConsumerArgs& args) {
108 return GetOrCreateService(args.task_runner)
109 ->ConnectConsumer(args.consumer, /*uid=*/0);
110 }
111
GetOrCreateService(base::TaskRunner * task_runner)112 TracingService* InProcessTracingBackend::GetOrCreateService(
113 base::TaskRunner* task_runner) {
114 if (!service_) {
115 std::unique_ptr<InProcessShmFactory> shm(new InProcessShmFactory());
116 service_ = TracingService::CreateInstance(std::move(shm), task_runner);
117 service_->SetSMBScrapingEnabled(true);
118 }
119 return service_.get();
120 }
121
122 } // namespace internal
123 } // namespace perfetto
124