• 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/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 
46  private:
47   base::PagedMemory mem_;
48 };
49 
50 class InProcessShmFactory : public SharedMemory::Factory {
51  public:
52   ~InProcessShmFactory() override;
53   std::unique_ptr<SharedMemory> CreateSharedMemory(size_t) override;
54 };
55 
56 InProcessShm::~InProcessShm() = default;
57 
InProcessShm(size_t size)58 InProcessShm::InProcessShm(size_t size)
59     : mem_(base::PagedMemory::Allocate(size)) {}
60 
start() const61 void* InProcessShm::start() const {
62   return mem_.Get();
63 }
64 
size() const65 size_t InProcessShm::size() const {
66   return mem_.size();
67 }
68 
69 InProcessShmFactory::~InProcessShmFactory() = default;
CreateSharedMemory(size_t size)70 std::unique_ptr<SharedMemory> InProcessShmFactory::CreateSharedMemory(
71     size_t size) {
72   return std::unique_ptr<SharedMemory>(new InProcessShm(size));
73 }
74 
75 }  // namespace
76 
77 // static
GetInstance()78 TracingBackend* InProcessTracingBackend::GetInstance() {
79   static auto* instance = new InProcessTracingBackend();
80   return instance;
81 }
82 
InProcessTracingBackend()83 InProcessTracingBackend::InProcessTracingBackend() {}
84 
ConnectProducer(const ConnectProducerArgs & args)85 std::unique_ptr<ProducerEndpoint> InProcessTracingBackend::ConnectProducer(
86     const ConnectProducerArgs& args) {
87   PERFETTO_DCHECK(args.task_runner->RunsTasksOnCurrentThread());
88 
89   // This should never happen as we can have at most one in-process backend.
90   if (service_)
91     PERFETTO_FATAL("InProcessTracingBackend initialized twice");
92 
93   return GetOrCreateService(args.task_runner)
94       ->ConnectProducer(args.producer, /*uid=*/0, args.producer_name,
95                         args.shmem_size_hint_bytes,
96                         /*in_process=*/true,
97                         TracingService::ProducerSMBScrapingMode::kEnabled,
98                         args.shmem_page_size_hint_bytes);
99 }
100 
ConnectConsumer(const ConnectConsumerArgs & args)101 std::unique_ptr<ConsumerEndpoint> InProcessTracingBackend::ConnectConsumer(
102     const ConnectConsumerArgs& args) {
103   return GetOrCreateService(args.task_runner)
104       ->ConnectConsumer(args.consumer, /*uid=*/0);
105 }
106 
GetOrCreateService(base::TaskRunner * task_runner)107 TracingService* InProcessTracingBackend::GetOrCreateService(
108     base::TaskRunner* task_runner) {
109   if (!service_) {
110     std::unique_ptr<InProcessShmFactory> shm(new InProcessShmFactory());
111     service_ = TracingService::CreateInstance(std::move(shm), task_runner);
112     service_->SetSMBScrapingEnabled(true);
113   }
114   return service_.get();
115 }
116 
117 }  // namespace internal
118 }  // namespace perfetto
119