blob: 55c08bc7dad480a630306172c542bea16c5b3beb [file] [log] [blame]
Lalit Magantic4c3ceb2018-03-29 20:38:13 +01001/*
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
Primiano Tucci2c5488f2019-06-01 03:27:28 +010019#include "perfetto/ext/tracing/core/trace_packet.h"
Stephen Nuskoac0c1972019-06-25 13:57:13 +010020#include "perfetto/ext/tracing/ipc/default_socket.h"
Primiano Tucci9c41ceb2020-04-14 13:23:01 +010021#include "perfetto/tracing/core/tracing_service_state.h"
Florian Mayerc29e0d32018-04-04 15:55:46 +010022
Primiano Tucci355b8c82019-08-29 08:37:51 +020023#include "protos/perfetto/trace/trace_packet.pbzero.h"
Primiano Tucci07e104d2018-04-03 20:45:35 +020024
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010025namespace perfetto {
26
Florian Mayer05a87c92019-01-30 13:17:51 +000027uint64_t TestHelper::next_instance_num_ = 0;
28
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010029// If we're building on Android and starting the daemons ourselves,
30// create the sockets in a world-writable location.
31#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \
32 PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
33#define TEST_PRODUCER_SOCK_NAME "/data/local/tmp/traced_producer"
34#define TEST_CONSUMER_SOCK_NAME "/data/local/tmp/traced_consumer"
35#else
Florian Mayerc29e0d32018-04-04 15:55:46 +010036#define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket()
37#define TEST_CONSUMER_SOCK_NAME ::perfetto::GetConsumerSocket()
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010038#endif
39
40TestHelper::TestHelper(base::TestTaskRunner* task_runner)
Florian Mayer05a87c92019-01-30 13:17:51 +000041 : instance_num_(next_instance_num_++),
42 task_runner_(task_runner),
Lalit Maganti9782f492020-01-10 18:13:13 +000043 service_thread_(TEST_PRODUCER_SOCK_NAME, TEST_CONSUMER_SOCK_NAME),
44 fake_producer_thread_(TEST_PRODUCER_SOCK_NAME,
Primiano Tuccibbe68be2020-04-16 22:17:12 +010045 WrapTask(CreateCheckpoint("producer.connect")),
Lalit Maganti9782f492020-01-10 18:13:13 +000046 WrapTask(CreateCheckpoint("producer.setup")),
47 WrapTask(CreateCheckpoint("producer.enabled"))) {}
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010048
49void TestHelper::OnConnect() {
Lalit Maganti36557d82018-04-11 14:36:17 +010050 std::move(on_connect_callback_)();
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010051}
52
53void TestHelper::OnDisconnect() {
Primiano Tucci008cdb92019-07-19 19:52:41 +010054 PERFETTO_FATAL("Consumer unexpectedly disconnected from the service");
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010055}
56
Lalit Maganti36557d82018-04-11 14:36:17 +010057void TestHelper::OnTracingDisabled() {
58 std::move(on_stop_tracing_callback_)();
59}
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010060
61void TestHelper::OnTraceData(std::vector<TracePacket> packets, bool has_more) {
Primiano Tucci07e104d2018-04-03 20:45:35 +020062 for (auto& encoded_packet : packets) {
Primiano Tuccife502c42019-12-11 01:00:27 +000063 protos::gen::TracePacket packet;
Primiano Tuccif7851ee2019-09-09 06:20:30 -070064 PERFETTO_CHECK(
65 packet.ParseFromString(encoded_packet.GetRawBytesForTesting()));
Florian Mayer51016d32020-09-17 11:34:44 +010066 full_trace_.push_back(packet);
Primiano Tucci5e33cad2018-04-30 14:41:25 +010067 if (packet.has_clock_snapshot() || packet.has_trace_config() ||
Hector Dearman685f7522019-03-12 14:28:56 +000068 packet.has_trace_stats() || !packet.synchronization_marker().empty() ||
Primiano Tucci79e4dcb2020-04-08 09:51:02 +010069 packet.has_system_info() || packet.has_service_event()) {
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010070 continue;
Primiano Tucci5e33cad2018-04-30 14:41:25 +010071 }
Primiano Tuccife502c42019-12-11 01:00:27 +000072 PERFETTO_CHECK(packet.has_trusted_uid());
Lalit Maganti36557d82018-04-11 14:36:17 +010073 trace_.push_back(std::move(packet));
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010074 }
75
76 if (!has_more) {
Lalit Maganti36557d82018-04-11 14:36:17 +010077 std::move(on_packets_finished_callback_)();
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010078 }
79}
80
Sami Kyostilaf99230f2020-10-15 10:38:32 +000081void TestHelper::StartService() {
82 service_thread_.Start();
83}
84
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010085void TestHelper::StartServiceIfRequired() {
86#if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
Sami Kyostilaf99230f2020-10-15 10:38:32 +000087 StartService();
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010088#endif
89}
90
91FakeProducer* TestHelper::ConnectFakeProducer() {
Lalit Maganti9782f492020-01-10 18:13:13 +000092 fake_producer_thread_.Connect();
Primiano Tuccibbe68be2020-04-16 22:17:12 +010093 // This will wait until the service has seen the RegisterDataSource() call
94 // (because of the Sync() in FakeProducer::OnConnect()).
95 RunUntilCheckpoint("producer.connect");
Lalit Maganti9782f492020-01-10 18:13:13 +000096 return fake_producer_thread_.producer();
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010097}
98
99void TestHelper::ConnectConsumer() {
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100100 cur_consumer_num_++;
Florian Mayer05a87c92019-01-30 13:17:51 +0000101 on_connect_callback_ = CreateCheckpoint("consumer.connected." +
102 std::to_string(cur_consumer_num_));
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100103 endpoint_ =
104 ConsumerIPCClient::Connect(TEST_CONSUMER_SOCK_NAME, this, task_runner_);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100105}
106
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100107void TestHelper::DetachConsumer(const std::string& key) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000108 on_detach_callback_ = CreateCheckpoint("detach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100109 endpoint_->Detach(key);
Florian Mayer05a87c92019-01-30 13:17:51 +0000110 RunUntilCheckpoint("detach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100111 endpoint_.reset();
112}
113
114bool TestHelper::AttachConsumer(const std::string& key) {
115 bool success = false;
Florian Mayer05a87c92019-01-30 13:17:51 +0000116 auto checkpoint = CreateCheckpoint("attach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100117 on_attach_callback_ = [&success, checkpoint](bool s) {
118 success = s;
119 checkpoint();
120 };
121 endpoint_->Attach(key);
Florian Mayer05a87c92019-01-30 13:17:51 +0000122 RunUntilCheckpoint("attach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100123 return success;
124}
125
Eric Seckler326a3d32020-02-04 11:24:56 +0000126void TestHelper::CreateProducerProvidedSmb() {
127 fake_producer_thread_.CreateProducerProvidedSmb();
128}
129
130bool TestHelper::IsShmemProvidedByProducer() {
131 return fake_producer_thread_.producer()->IsShmemProvidedByProducer();
132}
133
Eric Seckler526921b2020-02-18 11:44:30 +0000134void TestHelper::ProduceStartupEventBatch(
135 const protos::gen::TestConfig& config) {
136 auto on_data_written = CreateCheckpoint("startup_data_written");
137 fake_producer_thread_.ProduceStartupEventBatch(config,
138 WrapTask(on_data_written));
139 RunUntilCheckpoint("startup_data_written");
140}
141
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100142void TestHelper::StartTracing(const TraceConfig& config,
143 base::ScopedFile file) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000144 trace_.clear();
145 on_stop_tracing_callback_ = CreateCheckpoint("stop.tracing");
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100146 endpoint_->EnableTracing(config, std::move(file));
Lalit Maganti36557d82018-04-11 14:36:17 +0100147}
148
Primiano Tuccic1855302018-12-06 10:36:55 +0000149void TestHelper::DisableTracing() {
150 endpoint_->DisableTracing();
151}
152
153void TestHelper::FlushAndWait(uint32_t timeout_ms) {
154 static int flush_num = 0;
155 std::string checkpoint_name = "flush." + std::to_string(flush_num++);
Florian Mayer05a87c92019-01-30 13:17:51 +0000156 auto checkpoint = CreateCheckpoint(checkpoint_name);
Primiano Tuccic1855302018-12-06 10:36:55 +0000157 endpoint_->Flush(timeout_ms, [checkpoint](bool) { checkpoint(); });
Florian Mayer05a87c92019-01-30 13:17:51 +0000158 RunUntilCheckpoint(checkpoint_name, timeout_ms + 1000);
Primiano Tuccic1855302018-12-06 10:36:55 +0000159}
160
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100161void TestHelper::ReadData(uint32_t read_count) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000162 on_packets_finished_callback_ =
163 CreateCheckpoint("readback.complete." + std::to_string(read_count));
Lalit Maganti36557d82018-04-11 14:36:17 +0100164 endpoint_->ReadBuffers();
165}
166
167void TestHelper::WaitForConsumerConnect() {
Florian Mayer05a87c92019-01-30 13:17:51 +0000168 RunUntilCheckpoint("consumer.connected." + std::to_string(cur_consumer_num_));
Lalit Maganti36557d82018-04-11 14:36:17 +0100169}
170
Stephen Nuskoe8238112019-04-09 18:37:00 +0100171void TestHelper::WaitForProducerSetup() {
172 RunUntilCheckpoint("producer.setup");
173}
174
Lalit Maganti36557d82018-04-11 14:36:17 +0100175void TestHelper::WaitForProducerEnabled() {
Florian Mayer05a87c92019-01-30 13:17:51 +0000176 RunUntilCheckpoint("producer.enabled");
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100177}
178
Primiano Tuccic1855302018-12-06 10:36:55 +0000179void TestHelper::WaitForTracingDisabled(uint32_t timeout_ms) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000180 RunUntilCheckpoint("stop.tracing", timeout_ms);
Lalit Maganti36557d82018-04-11 14:36:17 +0100181}
182
Florian Mayer3e6e4b42019-06-05 10:17:36 +0100183void TestHelper::WaitForReadData(uint32_t read_count, uint32_t timeout_ms) {
184 RunUntilCheckpoint("readback.complete." + std::to_string(read_count),
185 timeout_ms);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100186}
187
Primiano Tuccibbe68be2020-04-16 22:17:12 +0100188void TestHelper::SyncAndWaitProducer() {
189 static int sync_id = 0;
190 std::string checkpoint_name = "producer_sync_" + std::to_string(++sync_id);
191 auto checkpoint = CreateCheckpoint(checkpoint_name);
192 fake_producer_thread_.producer()->Sync(
193 [this, &checkpoint] { task_runner_->PostTask(checkpoint); });
194 RunUntilCheckpoint(checkpoint_name);
195}
196
Primiano Tucci9c41ceb2020-04-14 13:23:01 +0100197TracingServiceState TestHelper::QueryServiceStateAndWait() {
198 TracingServiceState res;
199 auto checkpoint = CreateCheckpoint("query_svc_state");
200 auto callback = [&checkpoint, &res](bool, const TracingServiceState& tss) {
201 res = tss;
202 checkpoint();
203 };
204 endpoint_->QueryServiceState(callback);
205 RunUntilCheckpoint("query_svc_state");
206 return res;
207}
208
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100209std::function<void()> TestHelper::WrapTask(
210 const std::function<void()>& function) {
211 return [this, function] { task_runner_->PostTask(function); };
212}
213
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100214void TestHelper::OnDetach(bool) {
215 if (on_detach_callback_)
216 std::move(on_detach_callback_)();
217}
218
219void TestHelper::OnAttach(bool success, const TraceConfig&) {
220 if (on_attach_callback_)
221 std::move(on_attach_callback_)(success);
222}
223
Eric Secklereaf29ed2019-01-23 09:53:55 +0000224void TestHelper::OnTraceStats(bool, const TraceStats&) {}
225
Eric Seckler7b0c9452019-03-18 13:14:36 +0000226void TestHelper::OnObservableEvents(const ObservableEvents&) {}
227
Primiano Tucci106605c2019-01-08 21:12:58 +0000228// static
229const char* TestHelper::GetConsumerSocketName() {
230 return TEST_CONSUMER_SOCK_NAME;
231}
232
Stephen Nuskoe8238112019-04-09 18:37:00 +0100233// static
234const char* TestHelper::GetProducerSocketName() {
235 return TEST_PRODUCER_SOCK_NAME;
236}
237
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100238} // namespace perfetto