• 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   return GetOrCreateService(args.task_runner)
89       ->ConnectProducer(args.producer, /*uid=*/0, /*pid=*/0, args.producer_name,
90                         args.shmem_size_hint_bytes,
91                         /*in_process=*/true,
92                         TracingService::ProducerSMBScrapingMode::kEnabled,
93                         args.shmem_page_size_hint_bytes);
94 }
95 
ConnectConsumer(const ConnectConsumerArgs & args)96 std::unique_ptr<ConsumerEndpoint> InProcessTracingBackend::ConnectConsumer(
97     const ConnectConsumerArgs& args) {
98   return GetOrCreateService(args.task_runner)
99       ->ConnectConsumer(args.consumer, /*uid=*/0);
100 }
101 
GetOrCreateService(base::TaskRunner * task_runner)102 TracingService* InProcessTracingBackend::GetOrCreateService(
103     base::TaskRunner* task_runner) {
104   if (!service_) {
105     std::unique_ptr<InProcessShmFactory> shm(new InProcessShmFactory());
106     service_ = TracingService::CreateInstance(std::move(shm), task_runner);
107     service_->SetSMBScrapingEnabled(true);
108   }
109   return service_.get();
110 }
111 
112 }  // namespace internal
113 }  // namespace perfetto
114