1 /*
2 * Copyright (C) 2021 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/tracing_backend_fake.h"
18
19 #include "perfetto/base/task_runner.h"
20 #include "perfetto/ext/base/weak_ptr.h"
21 #include "perfetto/ext/tracing/core/consumer.h"
22 #include "perfetto/ext/tracing/core/producer.h"
23 #include "perfetto/ext/tracing/core/trace_writer.h"
24 #include "perfetto/ext/tracing/core/tracing_service.h"
25
26 namespace perfetto {
27 namespace internal {
28
29 namespace {
30
31 class UnsupportedProducerEndpoint : public ProducerEndpoint {
32 public:
UnsupportedProducerEndpoint(Producer * producer,base::TaskRunner * task_runner)33 UnsupportedProducerEndpoint(Producer* producer, base::TaskRunner* task_runner)
34 : producer_(producer), task_runner_(task_runner) {
35 // The SDK will attempt to reconnect the producer, so instead we allow it
36 // to connect successfully, but never start any sessions.
37 auto weak_ptr = weak_ptr_factory_.GetWeakPtr();
38 task_runner_->PostTask([weak_ptr] {
39 if (weak_ptr)
40 weak_ptr->producer_->OnConnect();
41 });
42 }
~UnsupportedProducerEndpoint()43 ~UnsupportedProducerEndpoint() override { producer_->OnDisconnect(); }
44
RegisterDataSource(const DataSourceDescriptor &)45 void RegisterDataSource(const DataSourceDescriptor&) override {}
UnregisterDataSource(const std::string &)46 void UnregisterDataSource(const std::string& /*name*/) override {}
47
RegisterTraceWriter(uint32_t,uint32_t)48 void RegisterTraceWriter(uint32_t /*writer_id*/,
49 uint32_t /*target_buffer*/) override {}
UnregisterTraceWriter(uint32_t)50 void UnregisterTraceWriter(uint32_t /*writer_id*/) override {}
51
CommitData(const CommitDataRequest &,CommitDataCallback callback={})52 void CommitData(const CommitDataRequest&,
53 CommitDataCallback callback = {}) override {
54 callback();
55 }
56
shared_memory() const57 SharedMemory* shared_memory() const override { return nullptr; }
shared_buffer_page_size_kb() const58 size_t shared_buffer_page_size_kb() const override { return 0; }
59
CreateTraceWriter(BufferID,BufferExhaustedPolicy=BufferExhaustedPolicy::kDefault)60 std::unique_ptr<TraceWriter> CreateTraceWriter(
61 BufferID /*target_buffer*/,
62 BufferExhaustedPolicy = BufferExhaustedPolicy::kDefault) override {
63 return nullptr;
64 }
65
MaybeSharedMemoryArbiter()66 SharedMemoryArbiter* MaybeSharedMemoryArbiter() override { return nullptr; }
IsShmemProvidedByProducer() const67 bool IsShmemProvidedByProducer() const override { return false; }
68
NotifyFlushComplete(FlushRequestID)69 void NotifyFlushComplete(FlushRequestID) override {}
NotifyDataSourceStarted(DataSourceInstanceID)70 void NotifyDataSourceStarted(DataSourceInstanceID) override {}
NotifyDataSourceStopped(DataSourceInstanceID)71 void NotifyDataSourceStopped(DataSourceInstanceID) override {}
ActivateTriggers(const std::vector<std::string> &)72 void ActivateTriggers(const std::vector<std::string>&) override {}
73
Sync(std::function<void ()> callback)74 void Sync(std::function<void()> callback) override { callback(); }
75
76 private:
77 Producer* const producer_;
78 base::TaskRunner* const task_runner_;
79 base::WeakPtrFactory<UnsupportedProducerEndpoint> weak_ptr_factory_{
80 this}; // Keep last.
81 };
82
83 class UnsupportedConsumerEndpoint : public ConsumerEndpoint {
84 public:
UnsupportedConsumerEndpoint(Consumer * consumer,base::TaskRunner * task_runner)85 UnsupportedConsumerEndpoint(Consumer* consumer, base::TaskRunner* task_runner)
86 : consumer_(consumer), task_runner_(task_runner) {
87 // The SDK will not to reconnect the consumer, so we just disconnect it
88 // immediately, which will cancel the tracing session.
89 auto weak_this = weak_ptr_factory_.GetWeakPtr();
90 task_runner_->PostTask([weak_this] {
91 if (weak_this)
92 weak_this->consumer_->OnDisconnect();
93 });
94 }
95 ~UnsupportedConsumerEndpoint() override = default;
96
EnableTracing(const TraceConfig &,base::ScopedFile=base::ScopedFile ())97 void EnableTracing(const TraceConfig&,
98 base::ScopedFile = base::ScopedFile()) override {}
ChangeTraceConfig(const TraceConfig &)99 void ChangeTraceConfig(const TraceConfig&) override {}
100
StartTracing()101 void StartTracing() override {}
DisableTracing()102 void DisableTracing() override {}
103
Flush(uint32_t,FlushCallback callback)104 void Flush(uint32_t /*timeout_ms*/, FlushCallback callback) override {
105 callback(/*success=*/false);
106 }
107
ReadBuffers()108 void ReadBuffers() override {}
FreeBuffers()109 void FreeBuffers() override {}
110
Detach(const std::string &)111 void Detach(const std::string& /*key*/) override {}
Attach(const std::string &)112 void Attach(const std::string& /*key*/) override {}
113
GetTraceStats()114 void GetTraceStats() override {}
ObserveEvents(uint32_t)115 void ObserveEvents(uint32_t /*events_mask*/) override {}
QueryServiceState(QueryServiceStateCallback)116 void QueryServiceState(QueryServiceStateCallback) override {}
QueryCapabilities(QueryCapabilitiesCallback)117 void QueryCapabilities(QueryCapabilitiesCallback) override {}
118
SaveTraceForBugreport(SaveTraceForBugreportCallback)119 void SaveTraceForBugreport(SaveTraceForBugreportCallback) override {}
120
121 private:
122 Consumer* const consumer_;
123 base::TaskRunner* const task_runner_;
124 base::WeakPtrFactory<UnsupportedConsumerEndpoint> weak_ptr_factory_{
125 this}; // Keep last.
126 };
127
128 } // namespace
129
130 // static
GetInstance()131 TracingBackend* TracingBackendFake::GetInstance() {
132 static auto* instance = new TracingBackendFake();
133 return instance;
134 }
135
136 TracingBackendFake::TracingBackendFake() = default;
137
ConnectProducer(const ConnectProducerArgs & args)138 std::unique_ptr<ProducerEndpoint> TracingBackendFake::ConnectProducer(
139 const ConnectProducerArgs& args) {
140 return std::unique_ptr<ProducerEndpoint>(
141 new UnsupportedProducerEndpoint(args.producer, args.task_runner));
142 }
143
ConnectConsumer(const ConnectConsumerArgs & args)144 std::unique_ptr<ConsumerEndpoint> TracingBackendFake::ConnectConsumer(
145 const ConnectConsumerArgs& args) {
146 return std::unique_ptr<ConsumerEndpoint>(
147 new UnsupportedConsumerEndpoint(args.consumer, args.task_runner));
148 }
149
150 } // namespace internal
151 } // namespace perfetto
152