blob: 0e1cc62be6892366e008e55ec3fc7ca090ddc053 [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#ifndef TEST_TEST_HELPER_H_
18#define TEST_TEST_HELPER_H_
19
Primiano Tucci2c5488f2019-06-01 03:27:28 +010020#include "perfetto/ext/base/scoped_file.h"
Lalit Maganti9782f492020-01-10 18:13:13 +000021#include "perfetto/ext/base/thread_task_runner.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010022#include "perfetto/ext/tracing/core/consumer.h"
Eric Seckler526921b2020-02-18 11:44:30 +000023#include "perfetto/ext/tracing/core/shared_memory_arbiter.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010024#include "perfetto/ext/tracing/core/trace_packet.h"
25#include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
Lalit Maganti9782f492020-01-10 18:13:13 +000026#include "perfetto/ext/tracing/ipc/service_ipc_host.h"
Primiano Tucci0f9e0222019-06-05 09:36:41 +010027#include "perfetto/tracing/core/trace_config.h"
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010028#include "src/base/test/test_task_runner.h"
Lalit Maganti9782f492020-01-10 18:13:13 +000029#include "src/traced/probes/probes_producer.h"
Eric Seckler326a3d32020-02-04 11:24:56 +000030#include "src/tracing/ipc/posix_shared_memory.h"
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010031#include "test/fake_producer.h"
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010032
Primiano Tuccife502c42019-12-11 01:00:27 +000033#include "protos/perfetto/trace/trace_packet.gen.h"
Primiano Tucci07e104d2018-04-03 20:45:35 +020034
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010035namespace perfetto {
36
Lalit Maganti9782f492020-01-10 18:13:13 +000037// This is used only in daemon starting integrations tests.
38class ServiceThread {
39 public:
40 ServiceThread(const std::string& producer_socket,
41 const std::string& consumer_socket)
42 : producer_socket_(producer_socket), consumer_socket_(consumer_socket) {}
43
44 ~ServiceThread() {
45 if (!runner_)
46 return;
47 runner_->PostTaskAndWaitForTesting([this]() { svc_.reset(); });
48 }
49
50 void Start() {
51 runner_ = base::ThreadTaskRunner::CreateAndStart("perfetto.svc");
52 runner_->PostTaskAndWaitForTesting([this]() {
53 svc_ = ServiceIPCHost::CreateInstance(runner_->get());
54 unlink(producer_socket_.c_str());
55 unlink(consumer_socket_.c_str());
56
57 bool res =
58 svc_->Start(producer_socket_.c_str(), consumer_socket_.c_str());
59 PERFETTO_CHECK(res);
60 });
61 }
62
63 base::ThreadTaskRunner* runner() { return runner_ ? &*runner_ : nullptr; }
64
65 private:
66 base::Optional<base::ThreadTaskRunner> runner_; // Keep first.
67
68 std::string producer_socket_;
69 std::string consumer_socket_;
70 std::unique_ptr<ServiceIPCHost> svc_;
71};
72
73// This is used only in daemon starting integrations tests.
74class ProbesProducerThread {
75 public:
76 ProbesProducerThread(const std::string& producer_socket)
77 : producer_socket_(producer_socket) {}
78
79 ~ProbesProducerThread() {
80 if (!runner_)
81 return;
82 runner_->PostTaskAndWaitForTesting([this]() { producer_.reset(); });
83 }
84
85 void Connect() {
86 runner_ = base::ThreadTaskRunner::CreateAndStart("perfetto.prd.probes");
87 runner_->PostTaskAndWaitForTesting([this]() {
88 producer_.reset(new ProbesProducer());
89 producer_->ConnectWithRetries(producer_socket_.c_str(), runner_->get());
90 });
91 }
92
93 private:
94 base::Optional<base::ThreadTaskRunner> runner_; // Keep first.
95
96 std::string producer_socket_;
97 std::unique_ptr<ProbesProducer> producer_;
98};
99
100class FakeProducerThread {
101 public:
102 FakeProducerThread(const std::string& producer_socket,
Primiano Tuccibbe68be2020-04-16 22:17:12 +0100103 std::function<void()> connect_callback,
Lalit Maganti9782f492020-01-10 18:13:13 +0000104 std::function<void()> setup_callback,
Eric Seckler326a3d32020-02-04 11:24:56 +0000105 std::function<void()> start_callback)
Lalit Maganti9782f492020-01-10 18:13:13 +0000106 : producer_socket_(producer_socket),
Primiano Tuccibbe68be2020-04-16 22:17:12 +0100107 connect_callback_(std::move(connect_callback)),
Lalit Maganti9782f492020-01-10 18:13:13 +0000108 setup_callback_(std::move(setup_callback)),
Eric Seckler526921b2020-02-18 11:44:30 +0000109 start_callback_(std::move(start_callback)) {
110 runner_ = base::ThreadTaskRunner::CreateAndStart("perfetto.prd.fake");
111 runner_->PostTaskAndWaitForTesting([this]() {
112 producer_.reset(
113 new FakeProducer("android.perfetto.FakeProducer", runner_->get()));
114 });
115 }
Lalit Maganti9782f492020-01-10 18:13:13 +0000116
117 ~FakeProducerThread() {
Lalit Maganti9782f492020-01-10 18:13:13 +0000118 runner_->PostTaskAndWaitForTesting([this]() { producer_.reset(); });
119 }
120
121 void Connect() {
Lalit Maganti9782f492020-01-10 18:13:13 +0000122 runner_->PostTaskAndWaitForTesting([this]() {
Primiano Tuccibbe68be2020-04-16 22:17:12 +0100123 producer_->Connect(producer_socket_.c_str(), std::move(connect_callback_),
124 std::move(setup_callback_), std::move(start_callback_),
125 std::move(shm_), std::move(shm_arbiter_));
Lalit Maganti9782f492020-01-10 18:13:13 +0000126 });
127 }
128
129 base::ThreadTaskRunner* runner() { return runner_ ? &*runner_ : nullptr; }
130
131 FakeProducer* producer() { return producer_.get(); }
132
Eric Seckler326a3d32020-02-04 11:24:56 +0000133 void CreateProducerProvidedSmb() {
134 PosixSharedMemory::Factory factory;
135 shm_ = factory.CreateSharedMemory(1024 * 1024);
Eric Seckler526921b2020-02-18 11:44:30 +0000136 shm_arbiter_ =
137 SharedMemoryArbiter::CreateUnboundInstance(shm_.get(), base::kPageSize);
138 }
139
140 void ProduceStartupEventBatch(const protos::gen::TestConfig& config,
141 std::function<void()> callback) {
142 PERFETTO_CHECK(shm_arbiter_);
143 producer_->ProduceStartupEventBatch(config, shm_arbiter_.get(), callback);
Eric Seckler326a3d32020-02-04 11:24:56 +0000144 }
145
Lalit Maganti9782f492020-01-10 18:13:13 +0000146 private:
147 base::Optional<base::ThreadTaskRunner> runner_; // Keep first.
148
149 std::string producer_socket_;
150 std::unique_ptr<FakeProducer> producer_;
Primiano Tuccibbe68be2020-04-16 22:17:12 +0100151 std::function<void()> connect_callback_;
Lalit Maganti9782f492020-01-10 18:13:13 +0000152 std::function<void()> setup_callback_;
Eric Seckler326a3d32020-02-04 11:24:56 +0000153 std::function<void()> start_callback_;
154 std::unique_ptr<SharedMemory> shm_;
Eric Seckler526921b2020-02-18 11:44:30 +0000155 std::unique_ptr<SharedMemoryArbiter> shm_arbiter_;
Lalit Maganti9782f492020-01-10 18:13:13 +0000156};
157
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100158class TestHelper : public Consumer {
159 public:
Primiano Tucci106605c2019-01-08 21:12:58 +0000160 static const char* GetConsumerSocketName();
Stephen Nuskoe8238112019-04-09 18:37:00 +0100161 static const char* GetProducerSocketName();
Primiano Tucci106605c2019-01-08 21:12:58 +0000162
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100163 explicit TestHelper(base::TestTaskRunner* task_runner);
164
165 // Consumer implementation.
166 void OnConnect() override;
167 void OnDisconnect() override;
Primiano Tuccidca727d2018-04-04 11:31:55 +0200168 void OnTracingDisabled() override;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100169 void OnTraceData(std::vector<TracePacket> packets, bool has_more) override;
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100170 void OnDetach(bool) override;
171 void OnAttach(bool, const TraceConfig&) override;
Eric Secklereaf29ed2019-01-23 09:53:55 +0000172 void OnTraceStats(bool, const TraceStats&) override;
Eric Seckler7b0c9452019-03-18 13:14:36 +0000173 void OnObservableEvents(const ObservableEvents&) override;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100174
175 void StartServiceIfRequired();
Primiano Tuccibbe68be2020-04-16 22:17:12 +0100176
177 // Connects the producer and waits that the service has seen the
178 // RegisterDataSource() call.
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100179 FakeProducer* ConnectFakeProducer();
Primiano Tuccibbe68be2020-04-16 22:17:12 +0100180
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100181 void ConnectConsumer();
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100182 void StartTracing(const TraceConfig& config,
183 base::ScopedFile = base::ScopedFile());
Primiano Tuccic1855302018-12-06 10:36:55 +0000184 void DisableTracing();
185 void FlushAndWait(uint32_t timeout_ms);
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100186 void ReadData(uint32_t read_count = 0);
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100187 void DetachConsumer(const std::string& key);
188 bool AttachConsumer(const std::string& key);
Eric Seckler326a3d32020-02-04 11:24:56 +0000189 void CreateProducerProvidedSmb();
190 bool IsShmemProvidedByProducer();
Eric Seckler526921b2020-02-18 11:44:30 +0000191 void ProduceStartupEventBatch(const protos::gen::TestConfig& config);
Lalit Maganti36557d82018-04-11 14:36:17 +0100192
193 void WaitForConsumerConnect();
Stephen Nuskoe8238112019-04-09 18:37:00 +0100194 void WaitForProducerSetup();
Lalit Maganti36557d82018-04-11 14:36:17 +0100195 void WaitForProducerEnabled();
Primiano Tuccic1855302018-12-06 10:36:55 +0000196 void WaitForTracingDisabled(uint32_t timeout_ms = 5000);
Florian Mayer3e6e4b42019-06-05 10:17:36 +0100197 void WaitForReadData(uint32_t read_count = 0, uint32_t timeout_ms = 5000);
Primiano Tuccibbe68be2020-04-16 22:17:12 +0100198 void SyncAndWaitProducer();
Primiano Tucci9c41ceb2020-04-14 13:23:01 +0100199 TracingServiceState QueryServiceStateAndWait();
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100200
Florian Mayer05a87c92019-01-30 13:17:51 +0000201 std::string AddID(const std::string& checkpoint) {
202 return checkpoint + "." + std::to_string(instance_num_);
203 }
204
205 std::function<void()> CreateCheckpoint(const std::string& checkpoint) {
206 return task_runner_->CreateCheckpoint(AddID(checkpoint));
207 }
208
209 void RunUntilCheckpoint(const std::string& checkpoint,
210 uint32_t timeout_ms = 5000) {
211 return task_runner_->RunUntilCheckpoint(AddID(checkpoint), timeout_ms);
212 }
213
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100214 std::function<void()> WrapTask(const std::function<void()>& function);
215
Lalit Maganti9782f492020-01-10 18:13:13 +0000216 base::ThreadTaskRunner* service_thread() { return service_thread_.runner(); }
217 base::ThreadTaskRunner* producer_thread() {
218 return fake_producer_thread_.runner();
219 }
Primiano Tuccife502c42019-12-11 01:00:27 +0000220 const std::vector<protos::gen::TracePacket>& trace() { return trace_; }
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100221
222 private:
Florian Mayer05a87c92019-01-30 13:17:51 +0000223 static uint64_t next_instance_num_;
224 uint64_t instance_num_;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100225 base::TestTaskRunner* task_runner_ = nullptr;
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100226 int cur_consumer_num_ = 0;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100227
Lalit Maganti36557d82018-04-11 14:36:17 +0100228 std::function<void()> on_connect_callback_;
229 std::function<void()> on_packets_finished_callback_;
230 std::function<void()> on_stop_tracing_callback_;
Primiano Tucci9ba1d842018-12-20 17:31:04 +0100231 std::function<void()> on_detach_callback_;
232 std::function<void(bool)> on_attach_callback_;
Lalit Maganti36557d82018-04-11 14:36:17 +0100233
Primiano Tuccife502c42019-12-11 01:00:27 +0000234 std::vector<protos::gen::TracePacket> trace_;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100235
Lalit Maganti9782f492020-01-10 18:13:13 +0000236 ServiceThread service_thread_;
237 FakeProducerThread fake_producer_thread_;
238
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100239 std::unique_ptr<TracingService::ConsumerEndpoint> endpoint_; // Keep last.
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100240};
241
242} // namespace perfetto
243
244#endif // TEST_TEST_HELPER_H_