blob: f397e8b658653322864c9efb3f33184d588a49cb [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/traced/traced.h"
20#include "perfetto/ext/tracing/core/trace_packet.h"
Stephen Nuskoac0c1972019-06-25 13:57:13 +010021#include "perfetto/ext/tracing/ipc/default_socket.h"
Primiano Tucci9c41ceb2020-04-14 13:23:01 +010022#include "perfetto/tracing/core/tracing_service_state.h"
Florian Mayerc29e0d32018-04-04 15:55:46 +010023
Primiano Tucci355b8c82019-08-29 08:37:51 +020024#include "protos/perfetto/trace/trace_packet.pbzero.h"
Primiano Tucci07e104d2018-04-03 20:45:35 +020025
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010026namespace perfetto {
27
Florian Mayer05a87c92019-01-30 13:17:51 +000028uint64_t TestHelper::next_instance_num_ = 0;
29
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010030// If we're building on Android and starting the daemons ourselves,
31// create the sockets in a world-writable location.
32#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \
33 PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
34#define TEST_PRODUCER_SOCK_NAME "/data/local/tmp/traced_producer"
35#define TEST_CONSUMER_SOCK_NAME "/data/local/tmp/traced_consumer"
36#else
Florian Mayerc29e0d32018-04-04 15:55:46 +010037#define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket()
38#define TEST_CONSUMER_SOCK_NAME ::perfetto::GetConsumerSocket()
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010039#endif
40
41TestHelper::TestHelper(base::TestTaskRunner* task_runner)
Florian Mayer05a87c92019-01-30 13:17:51 +000042 : instance_num_(next_instance_num_++),
43 task_runner_(task_runner),
Lalit Maganti9782f492020-01-10 18:13:13 +000044 service_thread_(TEST_PRODUCER_SOCK_NAME, TEST_CONSUMER_SOCK_NAME),
45 fake_producer_thread_(TEST_PRODUCER_SOCK_NAME,
Primiano Tuccibbe68be2020-04-16 22:17:12 +010046 WrapTask(CreateCheckpoint("producer.connect")),
Lalit Maganti9782f492020-01-10 18:13:13 +000047 WrapTask(CreateCheckpoint("producer.setup")),
48 WrapTask(CreateCheckpoint("producer.enabled"))) {}
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010049
50void TestHelper::OnConnect() {
Lalit Maganti36557d82018-04-11 14:36:17 +010051 std::move(on_connect_callback_)();
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010052}
53
54void TestHelper::OnDisconnect() {
Primiano Tucci008cdb92019-07-19 19:52:41 +010055 PERFETTO_FATAL("Consumer unexpectedly disconnected from the service");
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010056}
57
Lalit Maganti36557d82018-04-11 14:36:17 +010058void TestHelper::OnTracingDisabled() {
59 std::move(on_stop_tracing_callback_)();
60}
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010061
62void TestHelper::OnTraceData(std::vector<TracePacket> packets, bool has_more) {
Primiano Tucci07e104d2018-04-03 20:45:35 +020063 for (auto& encoded_packet : packets) {
Primiano Tuccife502c42019-12-11 01:00:27 +000064 protos::gen::TracePacket packet;
Primiano Tuccif7851ee2019-09-09 06:20:30 -070065 PERFETTO_CHECK(
66 packet.ParseFromString(encoded_packet.GetRawBytesForTesting()));
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
81void TestHelper::StartServiceIfRequired() {
82#if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
Lalit Maganti9782f492020-01-10 18:13:13 +000083 service_thread_.Start();
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010084#endif
85}
86
87FakeProducer* TestHelper::ConnectFakeProducer() {
Lalit Maganti9782f492020-01-10 18:13:13 +000088 fake_producer_thread_.Connect();
Primiano Tuccibbe68be2020-04-16 22:17:12 +010089 // This will wait until the service has seen the RegisterDataSource() call
90 // (because of the Sync() in FakeProducer::OnConnect()).
91 RunUntilCheckpoint("producer.connect");
Lalit Maganti9782f492020-01-10 18:13:13 +000092 return fake_producer_thread_.producer();
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010093}
94
95void TestHelper::ConnectConsumer() {
Primiano Tucci9ba1d842018-12-20 17:31:04 +010096 cur_consumer_num_++;
Florian Mayer05a87c92019-01-30 13:17:51 +000097 on_connect_callback_ = CreateCheckpoint("consumer.connected." +
98 std::to_string(cur_consumer_num_));
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010099 endpoint_ =
100 ConsumerIPCClient::Connect(TEST_CONSUMER_SOCK_NAME, this, task_runner_);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100101}
102
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100103void TestHelper::DetachConsumer(const std::string& key) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000104 on_detach_callback_ = CreateCheckpoint("detach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100105 endpoint_->Detach(key);
Florian Mayer05a87c92019-01-30 13:17:51 +0000106 RunUntilCheckpoint("detach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100107 endpoint_.reset();
108}
109
110bool TestHelper::AttachConsumer(const std::string& key) {
111 bool success = false;
Florian Mayer05a87c92019-01-30 13:17:51 +0000112 auto checkpoint = CreateCheckpoint("attach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100113 on_attach_callback_ = [&success, checkpoint](bool s) {
114 success = s;
115 checkpoint();
116 };
117 endpoint_->Attach(key);
Florian Mayer05a87c92019-01-30 13:17:51 +0000118 RunUntilCheckpoint("attach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100119 return success;
120}
121
Eric Seckler326a3d32020-02-04 11:24:56 +0000122void TestHelper::CreateProducerProvidedSmb() {
123 fake_producer_thread_.CreateProducerProvidedSmb();
124}
125
126bool TestHelper::IsShmemProvidedByProducer() {
127 return fake_producer_thread_.producer()->IsShmemProvidedByProducer();
128}
129
Eric Seckler526921b2020-02-18 11:44:30 +0000130void TestHelper::ProduceStartupEventBatch(
131 const protos::gen::TestConfig& config) {
132 auto on_data_written = CreateCheckpoint("startup_data_written");
133 fake_producer_thread_.ProduceStartupEventBatch(config,
134 WrapTask(on_data_written));
135 RunUntilCheckpoint("startup_data_written");
136}
137
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100138void TestHelper::StartTracing(const TraceConfig& config,
139 base::ScopedFile file) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000140 trace_.clear();
141 on_stop_tracing_callback_ = CreateCheckpoint("stop.tracing");
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100142 endpoint_->EnableTracing(config, std::move(file));
Lalit Maganti36557d82018-04-11 14:36:17 +0100143}
144
Primiano Tuccic1855302018-12-06 10:36:55 +0000145void TestHelper::DisableTracing() {
146 endpoint_->DisableTracing();
147}
148
149void TestHelper::FlushAndWait(uint32_t timeout_ms) {
150 static int flush_num = 0;
151 std::string checkpoint_name = "flush." + std::to_string(flush_num++);
Florian Mayer05a87c92019-01-30 13:17:51 +0000152 auto checkpoint = CreateCheckpoint(checkpoint_name);
Primiano Tuccic1855302018-12-06 10:36:55 +0000153 endpoint_->Flush(timeout_ms, [checkpoint](bool) { checkpoint(); });
Florian Mayer05a87c92019-01-30 13:17:51 +0000154 RunUntilCheckpoint(checkpoint_name, timeout_ms + 1000);
Primiano Tuccic1855302018-12-06 10:36:55 +0000155}
156
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100157void TestHelper::ReadData(uint32_t read_count) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000158 on_packets_finished_callback_ =
159 CreateCheckpoint("readback.complete." + std::to_string(read_count));
Lalit Maganti36557d82018-04-11 14:36:17 +0100160 endpoint_->ReadBuffers();
161}
162
163void TestHelper::WaitForConsumerConnect() {
Florian Mayer05a87c92019-01-30 13:17:51 +0000164 RunUntilCheckpoint("consumer.connected." + std::to_string(cur_consumer_num_));
Lalit Maganti36557d82018-04-11 14:36:17 +0100165}
166
Stephen Nuskoe8238112019-04-09 18:37:00 +0100167void TestHelper::WaitForProducerSetup() {
168 RunUntilCheckpoint("producer.setup");
169}
170
Lalit Maganti36557d82018-04-11 14:36:17 +0100171void TestHelper::WaitForProducerEnabled() {
Florian Mayer05a87c92019-01-30 13:17:51 +0000172 RunUntilCheckpoint("producer.enabled");
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100173}
174
Primiano Tuccic1855302018-12-06 10:36:55 +0000175void TestHelper::WaitForTracingDisabled(uint32_t timeout_ms) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000176 RunUntilCheckpoint("stop.tracing", timeout_ms);
Lalit Maganti36557d82018-04-11 14:36:17 +0100177}
178
Florian Mayer3e6e4b42019-06-05 10:17:36 +0100179void TestHelper::WaitForReadData(uint32_t read_count, uint32_t timeout_ms) {
180 RunUntilCheckpoint("readback.complete." + std::to_string(read_count),
181 timeout_ms);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100182}
183
Primiano Tuccibbe68be2020-04-16 22:17:12 +0100184void TestHelper::SyncAndWaitProducer() {
185 static int sync_id = 0;
186 std::string checkpoint_name = "producer_sync_" + std::to_string(++sync_id);
187 auto checkpoint = CreateCheckpoint(checkpoint_name);
188 fake_producer_thread_.producer()->Sync(
189 [this, &checkpoint] { task_runner_->PostTask(checkpoint); });
190 RunUntilCheckpoint(checkpoint_name);
191}
192
Primiano Tucci9c41ceb2020-04-14 13:23:01 +0100193TracingServiceState TestHelper::QueryServiceStateAndWait() {
194 TracingServiceState res;
195 auto checkpoint = CreateCheckpoint("query_svc_state");
196 auto callback = [&checkpoint, &res](bool, const TracingServiceState& tss) {
197 res = tss;
198 checkpoint();
199 };
200 endpoint_->QueryServiceState(callback);
201 RunUntilCheckpoint("query_svc_state");
202 return res;
203}
204
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100205std::function<void()> TestHelper::WrapTask(
206 const std::function<void()>& function) {
207 return [this, function] { task_runner_->PostTask(function); };
208}
209
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100210void TestHelper::OnDetach(bool) {
211 if (on_detach_callback_)
212 std::move(on_detach_callback_)();
213}
214
215void TestHelper::OnAttach(bool success, const TraceConfig&) {
216 if (on_attach_callback_)
217 std::move(on_attach_callback_)(success);
218}
219
Eric Secklereaf29ed2019-01-23 09:53:55 +0000220void TestHelper::OnTraceStats(bool, const TraceStats&) {}
221
Eric Seckler7b0c9452019-03-18 13:14:36 +0000222void TestHelper::OnObservableEvents(const ObservableEvents&) {}
223
Primiano Tucci106605c2019-01-08 21:12:58 +0000224// static
225const char* TestHelper::GetConsumerSocketName() {
226 return TEST_CONSUMER_SOCK_NAME;
227}
228
Stephen Nuskoe8238112019-04-09 18:37:00 +0100229// static
230const char* TestHelper::GetProducerSocketName() {
231 return TEST_PRODUCER_SOCK_NAME;
232}
233
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100234} // namespace perfetto