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