blob: 57e8b10273693cc594d75a6a4599baf888670979 [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"
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010021#include "test/task_runner_thread_delegates.h"
22
Stephen Nuskoac0c1972019-06-25 13:57:13 +010023#include "perfetto/ext/tracing/ipc/default_socket.h"
Florian Mayerc29e0d32018-04-04 15:55:46 +010024
Primiano Tucci355b8c82019-08-29 08:37:51 +020025#include "protos/perfetto/trace/trace_packet.pb.h"
26#include "protos/perfetto/trace/trace_packet.pbzero.h"
Primiano Tucci07e104d2018-04-03 20:45:35 +020027
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010028namespace perfetto {
29
Florian Mayer05a87c92019-01-30 13:17:51 +000030uint64_t TestHelper::next_instance_num_ = 0;
31
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010032// If we're building on Android and starting the daemons ourselves,
33// create the sockets in a world-writable location.
34#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \
35 PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
36#define TEST_PRODUCER_SOCK_NAME "/data/local/tmp/traced_producer"
37#define TEST_CONSUMER_SOCK_NAME "/data/local/tmp/traced_consumer"
38#else
Florian Mayerc29e0d32018-04-04 15:55:46 +010039#define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket()
40#define TEST_CONSUMER_SOCK_NAME ::perfetto::GetConsumerSocket()
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010041#endif
42
43TestHelper::TestHelper(base::TestTaskRunner* task_runner)
Florian Mayer05a87c92019-01-30 13:17:51 +000044 : instance_num_(next_instance_num_++),
45 task_runner_(task_runner),
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010046 service_thread_("perfetto.svc"),
47 producer_thread_("perfetto.prd") {}
48
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) {
63 protos::TracePacket packet;
Primiano Tuccif7851ee2019-09-09 06:20:30 -070064 PERFETTO_CHECK(
65 packet.ParseFromString(encoded_packet.GetRawBytesForTesting()));
Primiano Tucci5e33cad2018-04-30 14:41:25 +010066 if (packet.has_clock_snapshot() || packet.has_trace_config() ||
Hector Dearman685f7522019-03-12 14:28:56 +000067 packet.has_trace_stats() || !packet.synchronization_marker().empty() ||
68 packet.has_system_info()) {
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010069 continue;
Primiano Tucci5e33cad2018-04-30 14:41:25 +010070 }
Primiano Tucci008cdb92019-07-19 19:52:41 +010071 PERFETTO_CHECK(packet.optional_trusted_uid_case() ==
72 protos::TracePacket::kTrustedUid);
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)
83 service_thread_.Start(std::unique_ptr<ServiceDelegate>(
84 new ServiceDelegate(TEST_PRODUCER_SOCK_NAME, TEST_CONSUMER_SOCK_NAME)));
85#endif
86}
87
88FakeProducer* TestHelper::ConnectFakeProducer() {
89 std::unique_ptr<FakeProducerDelegate> producer_delegate(
Florian Mayer05a87c92019-01-30 13:17:51 +000090 new FakeProducerDelegate(TEST_PRODUCER_SOCK_NAME,
Stephen Nuskoe8238112019-04-09 18:37:00 +010091 WrapTask(CreateCheckpoint("producer.setup")),
Florian Mayer05a87c92019-01-30 13:17:51 +000092 WrapTask(CreateCheckpoint("producer.enabled"))));
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010093 FakeProducerDelegate* producer_delegate_cached = producer_delegate.get();
94 producer_thread_.Start(std::move(producer_delegate));
95 return producer_delegate_cached->producer();
96}
97
98void TestHelper::ConnectConsumer() {
Primiano Tucci9ba1d842018-12-20 17:31:04 +010099 cur_consumer_num_++;
Florian Mayer05a87c92019-01-30 13:17:51 +0000100 on_connect_callback_ = CreateCheckpoint("consumer.connected." +
101 std::to_string(cur_consumer_num_));
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100102 endpoint_ =
103 ConsumerIPCClient::Connect(TEST_CONSUMER_SOCK_NAME, this, task_runner_);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100104}
105
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100106void TestHelper::DetachConsumer(const std::string& key) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000107 on_detach_callback_ = CreateCheckpoint("detach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100108 endpoint_->Detach(key);
Florian Mayer05a87c92019-01-30 13:17:51 +0000109 RunUntilCheckpoint("detach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100110 endpoint_.reset();
111}
112
113bool TestHelper::AttachConsumer(const std::string& key) {
114 bool success = false;
Florian Mayer05a87c92019-01-30 13:17:51 +0000115 auto checkpoint = CreateCheckpoint("attach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100116 on_attach_callback_ = [&success, checkpoint](bool s) {
117 success = s;
118 checkpoint();
119 };
120 endpoint_->Attach(key);
Florian Mayer05a87c92019-01-30 13:17:51 +0000121 RunUntilCheckpoint("attach." + key);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100122 return success;
123}
124
125void TestHelper::StartTracing(const TraceConfig& config,
126 base::ScopedFile file) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000127 trace_.clear();
128 on_stop_tracing_callback_ = CreateCheckpoint("stop.tracing");
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100129 endpoint_->EnableTracing(config, std::move(file));
Lalit Maganti36557d82018-04-11 14:36:17 +0100130}
131
Primiano Tuccic1855302018-12-06 10:36:55 +0000132void TestHelper::DisableTracing() {
133 endpoint_->DisableTracing();
134}
135
136void TestHelper::FlushAndWait(uint32_t timeout_ms) {
137 static int flush_num = 0;
138 std::string checkpoint_name = "flush." + std::to_string(flush_num++);
Florian Mayer05a87c92019-01-30 13:17:51 +0000139 auto checkpoint = CreateCheckpoint(checkpoint_name);
Primiano Tuccic1855302018-12-06 10:36:55 +0000140 endpoint_->Flush(timeout_ms, [checkpoint](bool) { checkpoint(); });
Florian Mayer05a87c92019-01-30 13:17:51 +0000141 RunUntilCheckpoint(checkpoint_name, timeout_ms + 1000);
Primiano Tuccic1855302018-12-06 10:36:55 +0000142}
143
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100144void TestHelper::ReadData(uint32_t read_count) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000145 on_packets_finished_callback_ =
146 CreateCheckpoint("readback.complete." + std::to_string(read_count));
Lalit Maganti36557d82018-04-11 14:36:17 +0100147 endpoint_->ReadBuffers();
148}
149
150void TestHelper::WaitForConsumerConnect() {
Florian Mayer05a87c92019-01-30 13:17:51 +0000151 RunUntilCheckpoint("consumer.connected." + std::to_string(cur_consumer_num_));
Lalit Maganti36557d82018-04-11 14:36:17 +0100152}
153
Stephen Nuskoe8238112019-04-09 18:37:00 +0100154void TestHelper::WaitForProducerSetup() {
155 RunUntilCheckpoint("producer.setup");
156}
157
Lalit Maganti36557d82018-04-11 14:36:17 +0100158void TestHelper::WaitForProducerEnabled() {
Florian Mayer05a87c92019-01-30 13:17:51 +0000159 RunUntilCheckpoint("producer.enabled");
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100160}
161
Primiano Tuccic1855302018-12-06 10:36:55 +0000162void TestHelper::WaitForTracingDisabled(uint32_t timeout_ms) {
Florian Mayer05a87c92019-01-30 13:17:51 +0000163 RunUntilCheckpoint("stop.tracing", timeout_ms);
Lalit Maganti36557d82018-04-11 14:36:17 +0100164}
165
Florian Mayer3e6e4b42019-06-05 10:17:36 +0100166void TestHelper::WaitForReadData(uint32_t read_count, uint32_t timeout_ms) {
167 RunUntilCheckpoint("readback.complete." + std::to_string(read_count),
168 timeout_ms);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100169}
170
171std::function<void()> TestHelper::WrapTask(
172 const std::function<void()>& function) {
173 return [this, function] { task_runner_->PostTask(function); };
174}
175
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100176void TestHelper::OnDetach(bool) {
177 if (on_detach_callback_)
178 std::move(on_detach_callback_)();
179}
180
181void TestHelper::OnAttach(bool success, const TraceConfig&) {
182 if (on_attach_callback_)
183 std::move(on_attach_callback_)(success);
184}
185
Eric Secklereaf29ed2019-01-23 09:53:55 +0000186void TestHelper::OnTraceStats(bool, const TraceStats&) {}
187
Eric Seckler7b0c9452019-03-18 13:14:36 +0000188void TestHelper::OnObservableEvents(const ObservableEvents&) {}
189
Primiano Tucci106605c2019-01-08 21:12:58 +0000190// static
191const char* TestHelper::GetConsumerSocketName() {
192 return TEST_CONSUMER_SOCK_NAME;
193}
194
Stephen Nuskoe8238112019-04-09 18:37:00 +0100195// static
196const char* TestHelper::GetProducerSocketName() {
197 return TEST_PRODUCER_SOCK_NAME;
198}
199
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100200} // namespace perfetto