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/traced/traced.h" |
| 20 | #include "perfetto/ext/tracing/core/trace_packet.h" |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 21 | #include "test/task_runner_thread_delegates.h" |
| 22 | |
Stephen Nusko | ac0c197 | 2019-06-25 13:57:13 +0100 | [diff] [blame] | 23 | #include "perfetto/ext/tracing/ipc/default_socket.h" |
Florian Mayer | c29e0d3 | 2018-04-04 15:55:46 +0100 | [diff] [blame] | 24 | |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame] | 25 | #include "protos/perfetto/trace/trace_packet.pb.h" |
| 26 | #include "protos/perfetto/trace/trace_packet.pbzero.h" |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 27 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 28 | namespace perfetto { |
| 29 | |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 30 | uint64_t TestHelper::next_instance_num_ = 0; |
| 31 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 32 | // 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 Mayer | c29e0d3 | 2018-04-04 15:55:46 +0100 | [diff] [blame] | 39 | #define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket() |
| 40 | #define TEST_CONSUMER_SOCK_NAME ::perfetto::GetConsumerSocket() |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | TestHelper::TestHelper(base::TestTaskRunner* task_runner) |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 44 | : instance_num_(next_instance_num_++), |
| 45 | task_runner_(task_runner), |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 46 | service_thread_("perfetto.svc"), |
| 47 | producer_thread_("perfetto.prd") {} |
| 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) { |
| 63 | protos::TracePacket packet; |
Primiano Tucci | f7851ee | 2019-09-09 06:20:30 -0700 | [diff] [blame] | 64 | PERFETTO_CHECK( |
| 65 | packet.ParseFromString(encoded_packet.GetRawBytesForTesting())); |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 66 | if (packet.has_clock_snapshot() || packet.has_trace_config() || |
Hector Dearman | 685f752 | 2019-03-12 14:28:56 +0000 | [diff] [blame] | 67 | packet.has_trace_stats() || !packet.synchronization_marker().empty() || |
| 68 | packet.has_system_info()) { |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 69 | continue; |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 70 | } |
Primiano Tucci | 008cdb9 | 2019-07-19 19:52:41 +0100 | [diff] [blame] | 71 | PERFETTO_CHECK(packet.optional_trusted_uid_case() == |
| 72 | protos::TracePacket::kTrustedUid); |
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 | |
| 81 | void 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 | |
| 88 | FakeProducer* TestHelper::ConnectFakeProducer() { |
| 89 | std::unique_ptr<FakeProducerDelegate> producer_delegate( |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 90 | new FakeProducerDelegate(TEST_PRODUCER_SOCK_NAME, |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 91 | WrapTask(CreateCheckpoint("producer.setup")), |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 92 | WrapTask(CreateCheckpoint("producer.enabled")))); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 93 | FakeProducerDelegate* producer_delegate_cached = producer_delegate.get(); |
| 94 | producer_thread_.Start(std::move(producer_delegate)); |
| 95 | return producer_delegate_cached->producer(); |
| 96 | } |
| 97 | |
| 98 | void TestHelper::ConnectConsumer() { |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 99 | cur_consumer_num_++; |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 100 | on_connect_callback_ = CreateCheckpoint("consumer.connected." + |
| 101 | std::to_string(cur_consumer_num_)); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 102 | endpoint_ = |
| 103 | ConsumerIPCClient::Connect(TEST_CONSUMER_SOCK_NAME, this, task_runner_); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 106 | void TestHelper::DetachConsumer(const std::string& key) { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 107 | on_detach_callback_ = CreateCheckpoint("detach." + key); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 108 | endpoint_->Detach(key); |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 109 | RunUntilCheckpoint("detach." + key); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 110 | endpoint_.reset(); |
| 111 | } |
| 112 | |
| 113 | bool TestHelper::AttachConsumer(const std::string& key) { |
| 114 | bool success = false; |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 115 | auto checkpoint = CreateCheckpoint("attach." + key); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 116 | on_attach_callback_ = [&success, checkpoint](bool s) { |
| 117 | success = s; |
| 118 | checkpoint(); |
| 119 | }; |
| 120 | endpoint_->Attach(key); |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 121 | RunUntilCheckpoint("attach." + key); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 122 | return success; |
| 123 | } |
| 124 | |
| 125 | void TestHelper::StartTracing(const TraceConfig& config, |
| 126 | base::ScopedFile file) { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 127 | trace_.clear(); |
| 128 | on_stop_tracing_callback_ = CreateCheckpoint("stop.tracing"); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 129 | endpoint_->EnableTracing(config, std::move(file)); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 132 | void TestHelper::DisableTracing() { |
| 133 | endpoint_->DisableTracing(); |
| 134 | } |
| 135 | |
| 136 | void TestHelper::FlushAndWait(uint32_t timeout_ms) { |
| 137 | static int flush_num = 0; |
| 138 | std::string checkpoint_name = "flush." + std::to_string(flush_num++); |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 139 | auto checkpoint = CreateCheckpoint(checkpoint_name); |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 140 | endpoint_->Flush(timeout_ms, [checkpoint](bool) { checkpoint(); }); |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 141 | RunUntilCheckpoint(checkpoint_name, timeout_ms + 1000); |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Lalit Maganti | 3f0b7c6 | 2018-04-18 19:10:09 +0100 | [diff] [blame] | 144 | void TestHelper::ReadData(uint32_t read_count) { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 145 | on_packets_finished_callback_ = |
| 146 | CreateCheckpoint("readback.complete." + std::to_string(read_count)); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 147 | endpoint_->ReadBuffers(); |
| 148 | } |
| 149 | |
| 150 | void TestHelper::WaitForConsumerConnect() { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 151 | RunUntilCheckpoint("consumer.connected." + std::to_string(cur_consumer_num_)); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 154 | void TestHelper::WaitForProducerSetup() { |
| 155 | RunUntilCheckpoint("producer.setup"); |
| 156 | } |
| 157 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 158 | void TestHelper::WaitForProducerEnabled() { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 159 | RunUntilCheckpoint("producer.enabled"); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 162 | void TestHelper::WaitForTracingDisabled(uint32_t timeout_ms) { |
Florian Mayer | 05a87c9 | 2019-01-30 13:17:51 +0000 | [diff] [blame] | 163 | RunUntilCheckpoint("stop.tracing", timeout_ms); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Florian Mayer | 3e6e4b4 | 2019-06-05 10:17:36 +0100 | [diff] [blame] | 166 | void TestHelper::WaitForReadData(uint32_t read_count, uint32_t timeout_ms) { |
| 167 | RunUntilCheckpoint("readback.complete." + std::to_string(read_count), |
| 168 | timeout_ms); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | std::function<void()> TestHelper::WrapTask( |
| 172 | const std::function<void()>& function) { |
| 173 | return [this, function] { task_runner_->PostTask(function); }; |
| 174 | } |
| 175 | |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 176 | void TestHelper::OnDetach(bool) { |
| 177 | if (on_detach_callback_) |
| 178 | std::move(on_detach_callback_)(); |
| 179 | } |
| 180 | |
| 181 | void TestHelper::OnAttach(bool success, const TraceConfig&) { |
| 182 | if (on_attach_callback_) |
| 183 | std::move(on_attach_callback_)(success); |
| 184 | } |
| 185 | |
Eric Seckler | eaf29ed | 2019-01-23 09:53:55 +0000 | [diff] [blame] | 186 | void TestHelper::OnTraceStats(bool, const TraceStats&) {} |
| 187 | |
Eric Seckler | 7b0c945 | 2019-03-18 13:14:36 +0000 | [diff] [blame] | 188 | void TestHelper::OnObservableEvents(const ObservableEvents&) {} |
| 189 | |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame] | 190 | // static |
| 191 | const char* TestHelper::GetConsumerSocketName() { |
| 192 | return TEST_CONSUMER_SOCK_NAME; |
| 193 | } |
| 194 | |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 195 | // static |
| 196 | const char* TestHelper::GetProducerSocketName() { |
| 197 | return TEST_PRODUCER_SOCK_NAME; |
| 198 | } |
| 199 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 200 | } // namespace perfetto |