Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 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 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 19 | #include "perfetto/ext/tracing/core/trace_packet.h" |
Stephen Nusko | ac0c197 | 2019-06-25 13:57:13 +0100 | [diff] [blame] | 20 | #include "perfetto/ext/tracing/ipc/default_socket.h" |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 21 | #include "perfetto/tracing/core/tracing_service_state.h" |
Florian Mayer | c29e0d3 | 2018-04-04 15:55:46 +0100 | [diff] [blame] | 22 | |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame] | 23 | #include "protos/perfetto/trace/trace_packet.pbzero.h" |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 24 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 25 | namespace perfetto { |
| 26 | |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 27 | uint64_t TestHelper::next_instance_num_ = 0; |
| 28 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 29 | // 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 Mayer | c29e0d3 | 2018-04-04 15:55:46 +0100 | [diff] [blame] | 36 | #define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket() |
| 37 | #define TEST_CONSUMER_SOCK_NAME ::perfetto::GetConsumerSocket() |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | TestHelper::TestHelper(base::TestTaskRunner* task_runner) |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 41 | : instance_num_(next_instance_num_++), |
| 42 | task_runner_(task_runner), |
Lalit Maganti | 9782f49 | 2020-01-10 18:13:13 +0000 | [diff] [blame] | 43 | service_thread_(TEST_PRODUCER_SOCK_NAME, TEST_CONSUMER_SOCK_NAME), |
| 44 | fake_producer_thread_(TEST_PRODUCER_SOCK_NAME, |
Primiano Tucci | bbe68be | 2020-04-16 22:17:12 +0100 | [diff] [blame] | 45 | WrapTask(CreateCheckpoint("producer.connect")), |
Lalit Maganti | 9782f49 | 2020-01-10 18:13:13 +0000 | [diff] [blame] | 46 | WrapTask(CreateCheckpoint("producer.setup")), |
| 47 | WrapTask(CreateCheckpoint("producer.enabled"))) {} |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 48 | |
| 49 | void TestHelper::OnConnect() { |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 50 | std::move(on_connect_callback_)(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void TestHelper::OnDisconnect() { |
Primiano Tucci | 008cdb9 | 2019-07-19 19:52:41 +0100 | [diff] [blame] | 54 | PERFETTO_FATAL("Consumer unexpectedly disconnected from the service"); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 57 | void TestHelper::OnTracingDisabled() { |
| 58 | std::move(on_stop_tracing_callback_)(); |
| 59 | } |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 60 | |
| 61 | void TestHelper::OnTraceData(std::vector<TracePacket> packets, bool has_more) { |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 62 | for (auto& encoded_packet : packets) { |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 63 | protos::gen::TracePacket packet; |
Primiano Tucci | f7851ee | 2019-09-09 06:20:30 -0700 | [diff] [blame] | 64 | PERFETTO_CHECK( |
| 65 | packet.ParseFromString(encoded_packet.GetRawBytesForTesting())); |
Florian Mayer | 51016d3 | 2020-09-17 11:34:44 +0100 | [diff] [blame] | 66 | full_trace_.push_back(packet); |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 67 | if (packet.has_clock_snapshot() || packet.has_trace_config() || |
Hector Dearman | 685f752 | 2019-03-12 14:28:56 +0000 | [diff] [blame] | 68 | packet.has_trace_stats() || !packet.synchronization_marker().empty() || |
Primiano Tucci | 79e4dcb | 2020-04-08 09:51:02 +0100 | [diff] [blame] | 69 | packet.has_system_info() || packet.has_service_event()) { |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 70 | continue; |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 71 | } |
Primiano Tucci | fe502c4 | 2019-12-11 01:00:27 +0000 | [diff] [blame] | 72 | PERFETTO_CHECK(packet.has_trusted_uid()); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 73 | trace_.push_back(std::move(packet)); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (!has_more) { |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 77 | std::move(on_packets_finished_callback_)(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Sami Kyostila | f99230f | 2020-10-15 10:38:32 +0000 | [diff] [blame] | 81 | void TestHelper::StartService() { |
| 82 | service_thread_.Start(); |
| 83 | } |
| 84 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 85 | void TestHelper::StartServiceIfRequired() { |
| 86 | #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS) |
Sami Kyostila | f99230f | 2020-10-15 10:38:32 +0000 | [diff] [blame] | 87 | StartService(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 88 | #endif |
| 89 | } |
| 90 | |
| 91 | FakeProducer* TestHelper::ConnectFakeProducer() { |
Lalit Maganti | 9782f49 | 2020-01-10 18:13:13 +0000 | [diff] [blame] | 92 | fake_producer_thread_.Connect(); |
Primiano Tucci | bbe68be | 2020-04-16 22:17:12 +0100 | [diff] [blame] | 93 | // This will wait until the service has seen the RegisterDataSource() call |
| 94 | // (because of the Sync() in FakeProducer::OnConnect()). |
| 95 | RunUntilCheckpoint("producer.connect"); |
Lalit Maganti | 9782f49 | 2020-01-10 18:13:13 +0000 | [diff] [blame] | 96 | return fake_producer_thread_.producer(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void TestHelper::ConnectConsumer() { |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 100 | cur_consumer_num_++; |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 101 | on_connect_callback_ = CreateCheckpoint("consumer.connected." + |
| 102 | std::to_string(cur_consumer_num_)); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 103 | endpoint_ = |
| 104 | ConsumerIPCClient::Connect(TEST_CONSUMER_SOCK_NAME, this, task_runner_); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 107 | void TestHelper::DetachConsumer(const std::string& key) { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 108 | on_detach_callback_ = CreateCheckpoint("detach." + key); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 109 | endpoint_->Detach(key); |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 110 | RunUntilCheckpoint("detach." + key); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 111 | endpoint_.reset(); |
| 112 | } |
| 113 | |
| 114 | bool TestHelper::AttachConsumer(const std::string& key) { |
| 115 | bool success = false; |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 116 | auto checkpoint = CreateCheckpoint("attach." + key); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 117 | on_attach_callback_ = [&success, checkpoint](bool s) { |
| 118 | success = s; |
| 119 | checkpoint(); |
| 120 | }; |
| 121 | endpoint_->Attach(key); |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 122 | RunUntilCheckpoint("attach." + key); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 123 | return success; |
| 124 | } |
| 125 | |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 126 | void TestHelper::CreateProducerProvidedSmb() { |
| 127 | fake_producer_thread_.CreateProducerProvidedSmb(); |
| 128 | } |
| 129 | |
| 130 | bool TestHelper::IsShmemProvidedByProducer() { |
| 131 | return fake_producer_thread_.producer()->IsShmemProvidedByProducer(); |
| 132 | } |
| 133 | |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 134 | void 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 Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 142 | void TestHelper::StartTracing(const TraceConfig& config, |
| 143 | base::ScopedFile file) { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 144 | trace_.clear(); |
| 145 | on_stop_tracing_callback_ = CreateCheckpoint("stop.tracing"); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 146 | endpoint_->EnableTracing(config, std::move(file)); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 149 | void TestHelper::DisableTracing() { |
| 150 | endpoint_->DisableTracing(); |
| 151 | } |
| 152 | |
| 153 | void TestHelper::FlushAndWait(uint32_t timeout_ms) { |
| 154 | static int flush_num = 0; |
| 155 | std::string checkpoint_name = "flush." + std::to_string(flush_num++); |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 156 | auto checkpoint = CreateCheckpoint(checkpoint_name); |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 157 | endpoint_->Flush(timeout_ms, [checkpoint](bool) { checkpoint(); }); |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 158 | RunUntilCheckpoint(checkpoint_name, timeout_ms + 1000); |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Lalit Maganti | 3f0b7c6 | 2018-04-18 19:10:09 +0100 | [diff] [blame] | 161 | void TestHelper::ReadData(uint32_t read_count) { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 162 | on_packets_finished_callback_ = |
| 163 | CreateCheckpoint("readback.complete." + std::to_string(read_count)); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 164 | endpoint_->ReadBuffers(); |
| 165 | } |
| 166 | |
| 167 | void TestHelper::WaitForConsumerConnect() { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 168 | RunUntilCheckpoint("consumer.connected." + std::to_string(cur_consumer_num_)); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 171 | void TestHelper::WaitForProducerSetup() { |
| 172 | RunUntilCheckpoint("producer.setup"); |
| 173 | } |
| 174 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 175 | void TestHelper::WaitForProducerEnabled() { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 176 | RunUntilCheckpoint("producer.enabled"); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 179 | void TestHelper::WaitForTracingDisabled(uint32_t timeout_ms) { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 180 | RunUntilCheckpoint("stop.tracing", timeout_ms); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Florian Mayer | 3e6e4b4 | 2019-06-05 10:17:36 +0100 | [diff] [blame] | 183 | void TestHelper::WaitForReadData(uint32_t read_count, uint32_t timeout_ms) { |
| 184 | RunUntilCheckpoint("readback.complete." + std::to_string(read_count), |
| 185 | timeout_ms); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Primiano Tucci | bbe68be | 2020-04-16 22:17:12 +0100 | [diff] [blame] | 188 | void 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 Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 197 | TracingServiceState 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 Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 209 | std::function<void()> TestHelper::WrapTask( |
| 210 | const std::function<void()>& function) { |
| 211 | return [this, function] { task_runner_->PostTask(function); }; |
| 212 | } |
| 213 | |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 214 | void TestHelper::OnDetach(bool) { |
| 215 | if (on_detach_callback_) |
| 216 | std::move(on_detach_callback_)(); |
| 217 | } |
| 218 | |
| 219 | void TestHelper::OnAttach(bool success, const TraceConfig&) { |
| 220 | if (on_attach_callback_) |
| 221 | std::move(on_attach_callback_)(success); |
| 222 | } |
| 223 | |
Eric Seckler | eaf29ed | 2019-01-23 09:53:55 +0000 | [diff] [blame] | 224 | void TestHelper::OnTraceStats(bool, const TraceStats&) {} |
| 225 | |
Eric Seckler | 7b0c945 | 2019-03-18 13:14:36 +0000 | [diff] [blame] | 226 | void TestHelper::OnObservableEvents(const ObservableEvents&) {} |
| 227 | |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 228 | // static |
| 229 | const char* TestHelper::GetConsumerSocketName() { |
| 230 | return TEST_CONSUMER_SOCK_NAME; |
| 231 | } |
| 232 | |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 233 | // static |
| 234 | const char* TestHelper::GetProducerSocketName() { |
| 235 | return TEST_PRODUCER_SOCK_NAME; |
| 236 | } |
| 237 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 238 | } // namespace perfetto |