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/client_identity.h"
20 #include "perfetto/ext/tracing/core/trace_writer.h"
21 #include "perfetto/ext/tracing/core/tracing_service.h"
22 #include "perfetto/protozero/scattered_heap_buffer.h"
23 #include "perfetto/tracing/core/data_source_config.h"
24 #include "perfetto/tracing/core/data_source_descriptor.h"
25 #include "protos/perfetto/common/track_event_descriptor.pbzero.h"
26 #include "src/base/test/test_task_runner.h"
27
28 using ::testing::_;
29 using ::testing::Eq;
30 using ::testing::Invoke;
31 using ::testing::InvokeWithoutArgs;
32 using ::testing::Property;
33
34 namespace perfetto {
35
36 namespace {
37
CreateDataSourceDescriptor(const std::initializer_list<std::string> & categories,uint32_t id)38 static DataSourceDescriptor CreateDataSourceDescriptor(
39 const std::initializer_list<std::string>& categories,
40 uint32_t id) {
41 DataSourceDescriptor ds_desc;
42 ds_desc.set_name("track_event");
43 ds_desc.set_id(id);
44
45 protozero::HeapBuffered<protos::pbzero::TrackEventDescriptor> ted;
46 for (auto c : categories) {
47 auto cat = ted->add_available_categories();
48 cat->set_name(c);
49 }
50 ds_desc.set_track_event_descriptor_raw(ted.SerializeAsString());
51 return ds_desc;
52 }
53
54 } // anonymous namespace
55
MockProducer(base::TestTaskRunner * task_runner)56 MockProducer::MockProducer(base::TestTaskRunner* task_runner)
57 : task_runner_(task_runner) {}
58
~MockProducer()59 MockProducer::~MockProducer() {
60 if (!service_endpoint_)
61 return;
62 static int i = 0;
63 auto checkpoint_name = "on_producer_disconnect_" + std::to_string(i++);
64 auto on_disconnect = task_runner_->CreateCheckpoint(checkpoint_name);
65 EXPECT_CALL(*this, OnDisconnect()).WillOnce(Invoke(on_disconnect));
66 service_endpoint_.reset();
67 task_runner_->RunUntilCheckpoint(checkpoint_name);
68 }
69
Connect(TracingService * svc,const std::string & producer_name,uid_t uid,pid_t pid,size_t shared_memory_size_hint_bytes,size_t shared_memory_page_size_hint_bytes,std::unique_ptr<SharedMemory> shm,bool in_process)70 void MockProducer::Connect(TracingService* svc,
71 const std::string& producer_name,
72 uid_t uid,
73 pid_t pid,
74 size_t shared_memory_size_hint_bytes,
75 size_t shared_memory_page_size_hint_bytes,
76 std::unique_ptr<SharedMemory> shm,
77 bool in_process) {
78 producer_name_ = producer_name;
79 service_endpoint_ =
80 svc->ConnectProducer(this, ClientIdentity(uid, pid), producer_name,
81 shared_memory_size_hint_bytes,
82 /*in_process=*/in_process,
83 TracingService::ProducerSMBScrapingMode::kDefault,
84 shared_memory_page_size_hint_bytes, std::move(shm));
85 auto checkpoint_name = "on_producer_connect_" + producer_name;
86 auto on_connect = task_runner_->CreateCheckpoint(checkpoint_name);
87 EXPECT_CALL(*this, OnConnect()).WillOnce(Invoke(on_connect));
88 task_runner_->RunUntilCheckpoint(checkpoint_name);
89 }
90
RegisterDataSource(const std::string & name,bool ack_stop,bool ack_start,bool handle_incremental_state_clear,bool no_flush)91 void MockProducer::RegisterDataSource(const std::string& name,
92 bool ack_stop,
93 bool ack_start,
94 bool handle_incremental_state_clear,
95 bool no_flush) {
96 DataSourceDescriptor ds_desc;
97 ds_desc.set_name(name);
98 ds_desc.set_will_notify_on_stop(ack_stop);
99 ds_desc.set_will_notify_on_start(ack_start);
100 ds_desc.set_handles_incremental_state_clear(handle_incremental_state_clear);
101 ds_desc.set_no_flush(no_flush);
102 service_endpoint_->RegisterDataSource(ds_desc);
103 }
104
UnregisterDataSource(const std::string & name)105 void MockProducer::UnregisterDataSource(const std::string& name) {
106 service_endpoint_->UnregisterDataSource(name);
107 }
108
RegisterTrackEventDataSource(const std::initializer_list<std::string> & categories,uint32_t id)109 void MockProducer::RegisterTrackEventDataSource(
110 const std::initializer_list<std::string>& categories,
111 uint32_t id) {
112 service_endpoint_->RegisterDataSource(
113 CreateDataSourceDescriptor(categories, id));
114 }
115
UpdateTrackEventDataSource(const std::initializer_list<std::string> & categories,uint32_t id)116 void MockProducer::UpdateTrackEventDataSource(
117 const std::initializer_list<std::string>& categories,
118 uint32_t id) {
119 service_endpoint_->UpdateDataSource(
120 CreateDataSourceDescriptor(categories, id));
121 }
122
RegisterTraceWriter(uint32_t writer_id,uint32_t target_buffer)123 void MockProducer::RegisterTraceWriter(uint32_t writer_id,
124 uint32_t target_buffer) {
125 service_endpoint_->RegisterTraceWriter(writer_id, target_buffer);
126 }
127
UnregisterTraceWriter(uint32_t writer_id)128 void MockProducer::UnregisterTraceWriter(uint32_t writer_id) {
129 service_endpoint_->UnregisterTraceWriter(writer_id);
130 }
131
WaitForTracingSetup()132 void MockProducer::WaitForTracingSetup() {
133 static int i = 0;
134 auto checkpoint_name =
135 "on_shmem_initialized_" + producer_name_ + "_" + std::to_string(i++);
136 auto on_tracing_enabled = task_runner_->CreateCheckpoint(checkpoint_name);
137 EXPECT_CALL(*this, OnTracingSetup()).WillOnce(Invoke(on_tracing_enabled));
138 task_runner_->RunUntilCheckpoint(checkpoint_name);
139 }
140
WaitForDataSourceSetup(const std::string & name)141 void MockProducer::WaitForDataSourceSetup(const std::string& name) {
142 static int i = 0;
143 auto checkpoint_name = "on_ds_setup_" + name + "_" + std::to_string(i++);
144 auto on_ds_start = task_runner_->CreateCheckpoint(checkpoint_name);
145 EXPECT_CALL(*this,
146 SetupDataSource(_, Property(&DataSourceConfig::name, Eq(name))))
147 .WillOnce(Invoke([on_ds_start, this](DataSourceInstanceID ds_id,
148 const DataSourceConfig& cfg) {
149 EXPECT_FALSE(data_source_instances_.count(cfg.name()));
150 auto target_buffer = static_cast<BufferID>(cfg.target_buffer());
151 auto session_id =
152 static_cast<TracingSessionID>(cfg.tracing_session_id());
153 data_source_instances_.emplace(
154 cfg.name(), EnabledDataSource{ds_id, target_buffer, session_id});
155 on_ds_start();
156 }));
157 task_runner_->RunUntilCheckpoint(checkpoint_name);
158 }
159
WaitForDataSourceStart(const std::string & name)160 void MockProducer::WaitForDataSourceStart(const std::string& name) {
161 static int i = 0;
162 auto checkpoint_name = "on_ds_start_" + name + "_" + std::to_string(i++);
163 auto on_ds_start = task_runner_->CreateCheckpoint(checkpoint_name);
164 EXPECT_CALL(*this,
165 StartDataSource(_, Property(&DataSourceConfig::name, Eq(name))))
166 .WillOnce(Invoke([on_ds_start, this](DataSourceInstanceID ds_id,
167 const DataSourceConfig& cfg) {
168 // The data source might have been seen already through
169 // WaitForDataSourceSetup().
170 if (data_source_instances_.count(cfg.name()) == 0) {
171 auto target_buffer = static_cast<BufferID>(cfg.target_buffer());
172 auto session_id =
173 static_cast<TracingSessionID>(cfg.tracing_session_id());
174 data_source_instances_.emplace(
175 cfg.name(), EnabledDataSource{ds_id, target_buffer, session_id});
176 }
177 on_ds_start();
178 }));
179 task_runner_->RunUntilCheckpoint(checkpoint_name);
180 }
181
WaitForDataSourceStop(const std::string & name)182 void MockProducer::WaitForDataSourceStop(const std::string& name) {
183 static int i = 0;
184 auto checkpoint_name = "on_ds_stop_" + name + "_" + std::to_string(i++);
185 auto on_ds_stop = task_runner_->CreateCheckpoint(checkpoint_name);
186 ASSERT_EQ(1u, data_source_instances_.count(name));
187 DataSourceInstanceID ds_id = data_source_instances_[name].id;
188 EXPECT_CALL(*this, StopDataSource(ds_id))
189 .WillOnce(InvokeWithoutArgs(on_ds_stop));
190 task_runner_->RunUntilCheckpoint(checkpoint_name);
191 data_source_instances_.erase(name);
192 }
193
CreateTraceWriter(const std::string & data_source_name,BufferExhaustedPolicy buffer_exhausted_policy)194 std::unique_ptr<TraceWriter> MockProducer::CreateTraceWriter(
195 const std::string& data_source_name,
196 BufferExhaustedPolicy buffer_exhausted_policy) {
197 PERFETTO_DCHECK(data_source_instances_.count(data_source_name));
198 BufferID buf_id = data_source_instances_[data_source_name].target_buffer;
199 return service_endpoint_->CreateTraceWriter(buf_id, buffer_exhausted_policy);
200 }
201
ExpectFlush(TraceWriter * writer_to_flush,bool reply,FlushFlags expected_flags)202 void MockProducer::ExpectFlush(TraceWriter* writer_to_flush,
203 bool reply,
204 FlushFlags expected_flags) {
205 std::vector<TraceWriter*> writers;
206 if (writer_to_flush)
207 writers.push_back(writer_to_flush);
208 ExpectFlush(writers, reply, expected_flags);
209 }
210
ExpectFlush(std::vector<TraceWriter * > writers_to_flush,bool reply,FlushFlags expected_flags)211 void MockProducer::ExpectFlush(std::vector<TraceWriter*> writers_to_flush,
212 bool reply,
213 FlushFlags expected_flags) {
214 auto& expected_call = EXPECT_CALL(*this, Flush(_, _, _, _));
215 expected_call.WillOnce(
216 Invoke([this, writers_to_flush, reply, expected_flags](
217 FlushRequestID flush_req_id, const DataSourceInstanceID*,
218 size_t, FlushFlags actual_flags) {
219 if (expected_flags.flags()) {
220 EXPECT_EQ(actual_flags, expected_flags);
221 }
222 for (auto* writer : writers_to_flush) {
223 writer->Flush();
224 }
225 if (reply) {
226 service_endpoint_->NotifyFlushComplete(flush_req_id);
227 }
228 }));
229 }
230
GetDataSourceInstanceId(const std::string & name)231 DataSourceInstanceID MockProducer::GetDataSourceInstanceId(
232 const std::string& name) {
233 auto it = data_source_instances_.find(name);
234 return it == data_source_instances_.end() ? 0 : it->second.id;
235 }
236
GetDataSourceInstance(const std::string & name)237 const MockProducer::EnabledDataSource* MockProducer::GetDataSourceInstance(
238 const std::string& name) {
239 auto it = data_source_instances_.find(name);
240 return it == data_source_instances_.end() ? nullptr : &it->second;
241 }
242
243 } // namespace perfetto
244