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 "test/test_helper.h"
18
19 #include "perfetto/base/compiler.h"
20 #include "perfetto/ext/tracing/core/trace_packet.h"
21 #include "perfetto/ext/tracing/ipc/default_socket.h"
22 #include "perfetto/tracing/core/tracing_service_state.h"
23
24 #include "protos/perfetto/trace/trace_packet.pbzero.h"
25
26 namespace perfetto {
27
28 namespace {
ProducerSocketForMode(TestHelper::Mode mode)29 const char* ProducerSocketForMode(TestHelper::Mode mode) {
30 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
31 base::ignore_result(mode);
32 return ::perfetto::GetProducerSocket();
33 #else
34 switch (mode) {
35 case TestHelper::Mode::kStartDaemons:
36 return "/data/local/tmp/traced_producer";
37 case TestHelper::Mode::kUseSystemService:
38 return ::perfetto::GetProducerSocket();
39 }
40 #endif
41 }
42
ConsumerSocketForMode(TestHelper::Mode mode)43 const char* ConsumerSocketForMode(TestHelper::Mode mode) {
44 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
45 base::ignore_result(mode);
46 return ::perfetto::GetConsumerSocket();
47 #else
48 switch (mode) {
49 case TestHelper::Mode::kStartDaemons:
50 return "/data/local/tmp/traced_consumer";
51 case TestHelper::Mode::kUseSystemService:
52 return ::perfetto::GetConsumerSocket();
53 }
54 #endif
55 }
56 } // namespace
57
58 uint64_t TestHelper::next_instance_num_ = 0;
59 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
60 TestHelper::Mode TestHelper::kDefaultMode = Mode::kStartDaemons;
61 #else
62 TestHelper::Mode TestHelper::kDefaultMode = Mode::kUseSystemService;
63 #endif
64
TestHelper(base::TestTaskRunner * task_runner,Mode mode)65 TestHelper::TestHelper(base::TestTaskRunner* task_runner, Mode mode)
66 : instance_num_(next_instance_num_++),
67 task_runner_(task_runner),
68 mode_(mode),
69 producer_socket_(ProducerSocketForMode(mode)),
70 consumer_socket_(ConsumerSocketForMode(mode)),
71 service_thread_(producer_socket_, consumer_socket_),
72 fake_producer_thread_(producer_socket_,
73 WrapTask(CreateCheckpoint("producer.connect")),
74 WrapTask(CreateCheckpoint("producer.setup")),
75 WrapTask(CreateCheckpoint("producer.enabled"))) {}
76
OnConnect()77 void TestHelper::OnConnect() {
78 std::move(on_connect_callback_)();
79 }
80
OnDisconnect()81 void TestHelper::OnDisconnect() {
82 PERFETTO_FATAL("Consumer unexpectedly disconnected from the service");
83 }
84
OnTracingDisabled(const std::string &)85 void TestHelper::OnTracingDisabled(const std::string& /*error*/) {
86 std::move(on_stop_tracing_callback_)();
87 on_stop_tracing_callback_ = nullptr;
88 }
89
ReadTraceData(std::vector<TracePacket> packets)90 void TestHelper::ReadTraceData(std::vector<TracePacket> packets) {
91 for (auto& encoded_packet : packets) {
92 protos::gen::TracePacket packet;
93 PERFETTO_CHECK(
94 packet.ParseFromString(encoded_packet.GetRawBytesForTesting()));
95 full_trace_.push_back(packet);
96 if (packet.has_clock_snapshot() || packet.has_trace_config() ||
97 packet.has_trace_stats() || !packet.synchronization_marker().empty() ||
98 packet.has_system_info() || packet.has_service_event()) {
99 continue;
100 }
101 PERFETTO_CHECK(packet.has_trusted_uid());
102 trace_.push_back(std::move(packet));
103 }
104 }
105
OnTraceData(std::vector<TracePacket> packets,bool has_more)106 void TestHelper::OnTraceData(std::vector<TracePacket> packets, bool has_more) {
107 ReadTraceData(std::move(packets));
108 if (!has_more) {
109 std::move(on_packets_finished_callback_)();
110 }
111 }
112
StartServiceIfRequired()113 void TestHelper::StartServiceIfRequired() {
114 if (mode_ == Mode::kStartDaemons)
115 service_thread_.Start();
116 }
117
ConnectFakeProducer()118 FakeProducer* TestHelper::ConnectFakeProducer() {
119 fake_producer_thread_.Connect();
120 // This will wait until the service has seen the RegisterDataSource() call
121 // (because of the Sync() in FakeProducer::OnConnect()).
122 RunUntilCheckpoint("producer.connect");
123 return fake_producer_thread_.producer();
124 }
125
ConnectConsumer()126 void TestHelper::ConnectConsumer() {
127 cur_consumer_num_++;
128 on_connect_callback_ = CreateCheckpoint("consumer.connected." +
129 std::to_string(cur_consumer_num_));
130 endpoint_ = ConsumerIPCClient::Connect(consumer_socket_, this, task_runner_);
131 }
132
DetachConsumer(const std::string & key)133 void TestHelper::DetachConsumer(const std::string& key) {
134 on_detach_callback_ = CreateCheckpoint("detach." + key);
135 endpoint_->Detach(key);
136 RunUntilCheckpoint("detach." + key);
137 endpoint_.reset();
138 }
139
AttachConsumer(const std::string & key)140 bool TestHelper::AttachConsumer(const std::string& key) {
141 bool success = false;
142 auto checkpoint = CreateCheckpoint("attach." + key);
143 on_attach_callback_ = [&success, checkpoint](bool s) {
144 success = s;
145 checkpoint();
146 };
147 endpoint_->Attach(key);
148 RunUntilCheckpoint("attach." + key);
149 return success;
150 }
151
SaveTraceForBugreportAndWait()152 bool TestHelper::SaveTraceForBugreportAndWait() {
153 bool success = false;
154 auto checkpoint = CreateCheckpoint("bugreport");
155 auto callback = [&success, checkpoint](bool s, const std::string&) {
156 success = s;
157 checkpoint();
158 };
159 endpoint_->SaveTraceForBugreport(callback);
160 RunUntilCheckpoint("bugreport");
161 return success;
162 }
163
CreateProducerProvidedSmb()164 void TestHelper::CreateProducerProvidedSmb() {
165 fake_producer_thread_.CreateProducerProvidedSmb();
166 }
167
IsShmemProvidedByProducer()168 bool TestHelper::IsShmemProvidedByProducer() {
169 return fake_producer_thread_.producer()->IsShmemProvidedByProducer();
170 }
171
ProduceStartupEventBatch(const protos::gen::TestConfig & config)172 void TestHelper::ProduceStartupEventBatch(
173 const protos::gen::TestConfig& config) {
174 auto on_data_written = CreateCheckpoint("startup_data_written");
175 fake_producer_thread_.ProduceStartupEventBatch(config,
176 WrapTask(on_data_written));
177 RunUntilCheckpoint("startup_data_written");
178 }
179
StartTracing(const TraceConfig & config,base::ScopedFile file)180 void TestHelper::StartTracing(const TraceConfig& config,
181 base::ScopedFile file) {
182 PERFETTO_CHECK(!on_stop_tracing_callback_);
183 trace_.clear();
184 on_stop_tracing_callback_ =
185 CreateCheckpoint("stop.tracing" + std::to_string(++trace_count_));
186 endpoint_->EnableTracing(config, std::move(file));
187 }
188
DisableTracing()189 void TestHelper::DisableTracing() {
190 endpoint_->DisableTracing();
191 }
192
FlushAndWait(uint32_t timeout_ms)193 void TestHelper::FlushAndWait(uint32_t timeout_ms) {
194 static int flush_num = 0;
195 std::string checkpoint_name = "flush." + std::to_string(flush_num++);
196 auto checkpoint = CreateCheckpoint(checkpoint_name);
197 endpoint_->Flush(timeout_ms, [checkpoint](bool) { checkpoint(); });
198 RunUntilCheckpoint(checkpoint_name, timeout_ms + 1000);
199 }
200
ReadData(uint32_t read_count)201 void TestHelper::ReadData(uint32_t read_count) {
202 on_packets_finished_callback_ =
203 CreateCheckpoint("readback.complete." + std::to_string(read_count));
204 endpoint_->ReadBuffers();
205 }
206
FreeBuffers()207 void TestHelper::FreeBuffers() {
208 endpoint_->FreeBuffers();
209 }
210
WaitForConsumerConnect()211 void TestHelper::WaitForConsumerConnect() {
212 RunUntilCheckpoint("consumer.connected." + std::to_string(cur_consumer_num_));
213 }
214
WaitForProducerSetup()215 void TestHelper::WaitForProducerSetup() {
216 RunUntilCheckpoint("producer.setup");
217 }
218
WaitForProducerEnabled()219 void TestHelper::WaitForProducerEnabled() {
220 RunUntilCheckpoint("producer.enabled");
221 }
222
WaitForTracingDisabled(uint32_t timeout_ms)223 void TestHelper::WaitForTracingDisabled(uint32_t timeout_ms) {
224 RunUntilCheckpoint(std::string("stop.tracing") + std::to_string(trace_count_),
225 timeout_ms);
226 }
227
WaitForReadData(uint32_t read_count,uint32_t timeout_ms)228 void TestHelper::WaitForReadData(uint32_t read_count, uint32_t timeout_ms) {
229 RunUntilCheckpoint("readback.complete." + std::to_string(read_count),
230 timeout_ms);
231 }
232
SyncAndWaitProducer()233 void TestHelper::SyncAndWaitProducer() {
234 static int sync_id = 0;
235 std::string checkpoint_name = "producer_sync_" + std::to_string(++sync_id);
236 auto checkpoint = CreateCheckpoint(checkpoint_name);
237 fake_producer_thread_.producer()->Sync(
238 [this, &checkpoint] { task_runner_->PostTask(checkpoint); });
239 RunUntilCheckpoint(checkpoint_name);
240 }
241
QueryServiceStateAndWait()242 TracingServiceState TestHelper::QueryServiceStateAndWait() {
243 TracingServiceState res;
244 auto checkpoint = CreateCheckpoint("query_svc_state");
245 auto callback = [&checkpoint, &res](bool, const TracingServiceState& tss) {
246 res = tss;
247 checkpoint();
248 };
249 endpoint_->QueryServiceState(callback);
250 RunUntilCheckpoint("query_svc_state");
251 return res;
252 }
253
WrapTask(const std::function<void ()> & function)254 std::function<void()> TestHelper::WrapTask(
255 const std::function<void()>& function) {
256 return [this, function] { task_runner_->PostTask(function); };
257 }
258
OnDetach(bool)259 void TestHelper::OnDetach(bool) {
260 if (on_detach_callback_)
261 std::move(on_detach_callback_)();
262 }
263
OnAttach(bool success,const TraceConfig &)264 void TestHelper::OnAttach(bool success, const TraceConfig&) {
265 if (on_attach_callback_)
266 std::move(on_attach_callback_)(success);
267 }
268
OnTraceStats(bool,const TraceStats &)269 void TestHelper::OnTraceStats(bool, const TraceStats&) {}
270
OnObservableEvents(const ObservableEvents &)271 void TestHelper::OnObservableEvents(const ObservableEvents&) {}
272
273 // static
GetDefaultModeConsumerSocketName()274 const char* TestHelper::GetDefaultModeConsumerSocketName() {
275 return ConsumerSocketForMode(TestHelper::kDefaultMode);
276 }
277
278 // static
GetDefaultModeProducerSocketName()279 const char* TestHelper::GetDefaultModeProducerSocketName() {
280 return ProducerSocketForMode(TestHelper::kDefaultMode);
281 }
282
283 } // namespace perfetto
284