1 /*
2 * Copyright (C) 2018 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 "src/tracing/test/mock_producer.h"
18
19 #include "perfetto/ext/tracing/core/trace_writer.h"
20 #include "perfetto/ext/tracing/core/tracing_service.h"
21 #include "perfetto/tracing/core/data_source_config.h"
22 #include "perfetto/tracing/core/data_source_descriptor.h"
23 #include "src/base/test/test_task_runner.h"
24
25 using ::testing::_;
26 using ::testing::Eq;
27 using ::testing::Invoke;
28 using ::testing::InvokeWithoutArgs;
29 using ::testing::Property;
30
31 namespace perfetto {
32
MockProducer(base::TestTaskRunner * task_runner)33 MockProducer::MockProducer(base::TestTaskRunner* task_runner)
34 : task_runner_(task_runner) {}
35
~MockProducer()36 MockProducer::~MockProducer() {
37 if (!service_endpoint_)
38 return;
39 static int i = 0;
40 auto checkpoint_name = "on_producer_disconnect_" + std::to_string(i++);
41 auto on_disconnect = task_runner_->CreateCheckpoint(checkpoint_name);
42 EXPECT_CALL(*this, OnDisconnect()).WillOnce(Invoke(on_disconnect));
43 service_endpoint_.reset();
44 task_runner_->RunUntilCheckpoint(checkpoint_name);
45 }
46
Connect(TracingService * svc,const std::string & producer_name,uid_t uid,size_t shared_memory_size_hint_bytes,size_t shared_memory_page_size_hint_bytes,std::unique_ptr<SharedMemory> shm)47 void MockProducer::Connect(TracingService* svc,
48 const std::string& producer_name,
49 uid_t uid,
50 size_t shared_memory_size_hint_bytes,
51 size_t shared_memory_page_size_hint_bytes,
52 std::unique_ptr<SharedMemory> shm) {
53 producer_name_ = producer_name;
54 service_endpoint_ = svc->ConnectProducer(
55 this, uid, producer_name, shared_memory_size_hint_bytes,
56 /*in_process=*/true, TracingService::ProducerSMBScrapingMode::kDefault,
57 shared_memory_page_size_hint_bytes, std::move(shm));
58 auto checkpoint_name = "on_producer_connect_" + producer_name;
59 auto on_connect = task_runner_->CreateCheckpoint(checkpoint_name);
60 EXPECT_CALL(*this, OnConnect()).WillOnce(Invoke(on_connect));
61 task_runner_->RunUntilCheckpoint(checkpoint_name);
62 }
63
RegisterDataSource(const std::string & name,bool ack_stop,bool ack_start,bool handle_incremental_state_clear)64 void MockProducer::RegisterDataSource(const std::string& name,
65 bool ack_stop,
66 bool ack_start,
67 bool handle_incremental_state_clear) {
68 DataSourceDescriptor ds_desc;
69 ds_desc.set_name(name);
70 ds_desc.set_will_notify_on_stop(ack_stop);
71 ds_desc.set_will_notify_on_start(ack_start);
72 ds_desc.set_handles_incremental_state_clear(handle_incremental_state_clear);
73 service_endpoint_->RegisterDataSource(ds_desc);
74 }
75
UnregisterDataSource(const std::string & name)76 void MockProducer::UnregisterDataSource(const std::string& name) {
77 service_endpoint_->UnregisterDataSource(name);
78 }
79
RegisterTraceWriter(uint32_t writer_id,uint32_t target_buffer)80 void MockProducer::RegisterTraceWriter(uint32_t writer_id,
81 uint32_t target_buffer) {
82 service_endpoint_->RegisterTraceWriter(writer_id, target_buffer);
83 }
84
UnregisterTraceWriter(uint32_t writer_id)85 void MockProducer::UnregisterTraceWriter(uint32_t writer_id) {
86 service_endpoint_->UnregisterTraceWriter(writer_id);
87 }
88
WaitForTracingSetup()89 void MockProducer::WaitForTracingSetup() {
90 static int i = 0;
91 auto checkpoint_name =
92 "on_shmem_initialized_" + producer_name_ + "_" + std::to_string(i++);
93 auto on_tracing_enabled = task_runner_->CreateCheckpoint(checkpoint_name);
94 EXPECT_CALL(*this, OnTracingSetup()).WillOnce(Invoke(on_tracing_enabled));
95 task_runner_->RunUntilCheckpoint(checkpoint_name);
96 }
97
WaitForDataSourceSetup(const std::string & name)98 void MockProducer::WaitForDataSourceSetup(const std::string& name) {
99 static int i = 0;
100 auto checkpoint_name = "on_ds_setup_" + name + "_" + std::to_string(i++);
101 auto on_ds_start = task_runner_->CreateCheckpoint(checkpoint_name);
102 EXPECT_CALL(*this,
103 SetupDataSource(_, Property(&DataSourceConfig::name, Eq(name))))
104 .WillOnce(Invoke([on_ds_start, this](DataSourceInstanceID ds_id,
105 const DataSourceConfig& cfg) {
106 EXPECT_FALSE(data_source_instances_.count(cfg.name()));
107 auto target_buffer = static_cast<BufferID>(cfg.target_buffer());
108 auto session_id =
109 static_cast<TracingSessionID>(cfg.tracing_session_id());
110 data_source_instances_.emplace(
111 cfg.name(), EnabledDataSource{ds_id, target_buffer, session_id});
112 on_ds_start();
113 }));
114 task_runner_->RunUntilCheckpoint(checkpoint_name);
115 }
116
WaitForDataSourceStart(const std::string & name)117 void MockProducer::WaitForDataSourceStart(const std::string& name) {
118 static int i = 0;
119 auto checkpoint_name = "on_ds_start_" + name + "_" + std::to_string(i++);
120 auto on_ds_start = task_runner_->CreateCheckpoint(checkpoint_name);
121 EXPECT_CALL(*this,
122 StartDataSource(_, Property(&DataSourceConfig::name, Eq(name))))
123 .WillOnce(Invoke([on_ds_start, this](DataSourceInstanceID ds_id,
124 const DataSourceConfig& cfg) {
125 // The data source might have been seen already through
126 // WaitForDataSourceSetup().
127 if (data_source_instances_.count(cfg.name()) == 0) {
128 auto target_buffer = static_cast<BufferID>(cfg.target_buffer());
129 auto session_id =
130 static_cast<TracingSessionID>(cfg.tracing_session_id());
131 data_source_instances_.emplace(
132 cfg.name(), EnabledDataSource{ds_id, target_buffer, session_id});
133 }
134 on_ds_start();
135 }));
136 task_runner_->RunUntilCheckpoint(checkpoint_name);
137 }
138
WaitForDataSourceStop(const std::string & name)139 void MockProducer::WaitForDataSourceStop(const std::string& name) {
140 static int i = 0;
141 auto checkpoint_name = "on_ds_stop_" + name + "_" + std::to_string(i++);
142 auto on_ds_stop = task_runner_->CreateCheckpoint(checkpoint_name);
143 ASSERT_EQ(1u, data_source_instances_.count(name));
144 DataSourceInstanceID ds_id = data_source_instances_[name].id;
145 EXPECT_CALL(*this, StopDataSource(ds_id))
146 .WillOnce(InvokeWithoutArgs(on_ds_stop));
147 task_runner_->RunUntilCheckpoint(checkpoint_name);
148 data_source_instances_.erase(name);
149 }
150
CreateTraceWriter(const std::string & data_source_name)151 std::unique_ptr<TraceWriter> MockProducer::CreateTraceWriter(
152 const std::string& data_source_name) {
153 PERFETTO_DCHECK(data_source_instances_.count(data_source_name));
154 BufferID buf_id = data_source_instances_[data_source_name].target_buffer;
155 return service_endpoint_->CreateTraceWriter(buf_id);
156 }
157
WaitForFlush(TraceWriter * writer_to_flush,bool reply)158 void MockProducer::WaitForFlush(TraceWriter* writer_to_flush, bool reply) {
159 std::vector<TraceWriter*> writers;
160 if (writer_to_flush)
161 writers.push_back(writer_to_flush);
162 WaitForFlush(writers, reply);
163 }
164
WaitForFlush(std::vector<TraceWriter * > writers_to_flush,bool reply)165 void MockProducer::WaitForFlush(std::vector<TraceWriter*> writers_to_flush,
166 bool reply) {
167 auto& expected_call = EXPECT_CALL(*this, Flush(_, _, _));
168 expected_call.WillOnce(Invoke(
169 [this, writers_to_flush, reply](FlushRequestID flush_req_id,
170 const DataSourceInstanceID*, size_t) {
171 for (auto* writer : writers_to_flush)
172 writer->Flush();
173 if (reply)
174 service_endpoint_->NotifyFlushComplete(flush_req_id);
175 }));
176 }
177
GetDataSourceInstanceId(const std::string & name)178 DataSourceInstanceID MockProducer::GetDataSourceInstanceId(
179 const std::string& name) {
180 auto it = data_source_instances_.find(name);
181 return it == data_source_instances_.end() ? 0 : it->second.id;
182 }
183
GetDataSourceInstance(const std::string & name)184 const MockProducer::EnabledDataSource* MockProducer::GetDataSourceInstance(
185 const std::string& name) {
186 auto it = data_source_instances_.find(name);
187 return it == data_source_instances_.end() ? nullptr : &it->second;
188 }
189
190 } // namespace perfetto
191