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 | |
| 19 | #include "gtest/gtest.h" |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 20 | #include "perfetto/traced/traced.h" |
| 21 | #include "perfetto/tracing/core/trace_packet.h" |
| 22 | #include "test/task_runner_thread_delegates.h" |
| 23 | |
Florian Mayer | c29e0d3 | 2018-04-04 15:55:46 +0100 | [diff] [blame] | 24 | #include "src/tracing/ipc/default_socket.h" |
| 25 | |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 26 | #include "perfetto/trace/trace_packet.pb.h" |
| 27 | #include "perfetto/trace/trace_packet.pbzero.h" |
| 28 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 29 | namespace perfetto { |
| 30 | |
| 31 | // If we're building on Android and starting the daemons ourselves, |
| 32 | // create the sockets in a world-writable location. |
| 33 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \ |
| 34 | PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS) |
| 35 | #define TEST_PRODUCER_SOCK_NAME "/data/local/tmp/traced_producer" |
| 36 | #define TEST_CONSUMER_SOCK_NAME "/data/local/tmp/traced_consumer" |
| 37 | #else |
Florian Mayer | c29e0d3 | 2018-04-04 15:55:46 +0100 | [diff] [blame] | 38 | #define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket() |
| 39 | #define TEST_CONSUMER_SOCK_NAME ::perfetto::GetConsumerSocket() |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | TestHelper::TestHelper(base::TestTaskRunner* task_runner) |
| 43 | : task_runner_(task_runner), |
| 44 | service_thread_("perfetto.svc"), |
| 45 | producer_thread_("perfetto.prd") {} |
| 46 | |
| 47 | void TestHelper::OnConnect() { |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 48 | std::move(on_connect_callback_)(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void TestHelper::OnDisconnect() { |
| 52 | FAIL() << "Consumer unexpectedly disconnected from the service"; |
| 53 | } |
| 54 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 55 | void TestHelper::OnTracingDisabled() { |
| 56 | std::move(on_stop_tracing_callback_)(); |
| 57 | } |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 58 | |
| 59 | void TestHelper::OnTraceData(std::vector<TracePacket> packets, bool has_more) { |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 60 | for (auto& encoded_packet : packets) { |
| 61 | protos::TracePacket packet; |
| 62 | ASSERT_TRUE(encoded_packet.Decode(&packet)); |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 63 | if (packet.has_clock_snapshot() || packet.has_trace_config() || |
Primiano Tucci | 9754d0d | 2018-09-15 12:41:46 +0100 | [diff] [blame] | 64 | packet.has_trace_stats() || !packet.synchronization_marker().empty()) { |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 65 | continue; |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 66 | } |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 67 | ASSERT_EQ(protos::TracePacket::kTrustedUid, |
Primiano Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 68 | packet.optional_trusted_uid_case()); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 69 | trace_.push_back(std::move(packet)); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | if (!has_more) { |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 73 | std::move(on_packets_finished_callback_)(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | void TestHelper::StartServiceIfRequired() { |
| 78 | #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS) |
| 79 | service_thread_.Start(std::unique_ptr<ServiceDelegate>( |
| 80 | new ServiceDelegate(TEST_PRODUCER_SOCK_NAME, TEST_CONSUMER_SOCK_NAME))); |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | FakeProducer* TestHelper::ConnectFakeProducer() { |
| 85 | std::unique_ptr<FakeProducerDelegate> producer_delegate( |
| 86 | new FakeProducerDelegate( |
| 87 | TEST_PRODUCER_SOCK_NAME, |
| 88 | WrapTask(task_runner_->CreateCheckpoint("producer.enabled")))); |
| 89 | FakeProducerDelegate* producer_delegate_cached = producer_delegate.get(); |
| 90 | producer_thread_.Start(std::move(producer_delegate)); |
| 91 | return producer_delegate_cached->producer(); |
| 92 | } |
| 93 | |
| 94 | void TestHelper::ConnectConsumer() { |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 95 | on_connect_callback_ = task_runner_->CreateCheckpoint("consumer.connected"); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 96 | endpoint_ = |
| 97 | ConsumerIPCClient::Connect(TEST_CONSUMER_SOCK_NAME, this, task_runner_); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void TestHelper::StartTracing(const TraceConfig& config) { |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 101 | on_stop_tracing_callback_ = task_runner_->CreateCheckpoint("stop.tracing"); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 102 | endpoint_->EnableTracing(config); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame^] | 105 | void TestHelper::DisableTracing() { |
| 106 | endpoint_->DisableTracing(); |
| 107 | } |
| 108 | |
| 109 | void TestHelper::FlushAndWait(uint32_t timeout_ms) { |
| 110 | static int flush_num = 0; |
| 111 | std::string checkpoint_name = "flush." + std::to_string(flush_num++); |
| 112 | auto checkpoint = task_runner_->CreateCheckpoint(checkpoint_name); |
| 113 | endpoint_->Flush(timeout_ms, [checkpoint](bool) { checkpoint(); }); |
| 114 | task_runner_->RunUntilCheckpoint(checkpoint_name, timeout_ms + 1000); |
| 115 | } |
| 116 | |
Lalit Maganti | 3f0b7c6 | 2018-04-18 19:10:09 +0100 | [diff] [blame] | 117 | void TestHelper::ReadData(uint32_t read_count) { |
| 118 | on_packets_finished_callback_ = task_runner_->CreateCheckpoint( |
| 119 | "readback.complete." + std::to_string(read_count)); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 120 | endpoint_->ReadBuffers(); |
| 121 | } |
| 122 | |
| 123 | void TestHelper::WaitForConsumerConnect() { |
| 124 | task_runner_->RunUntilCheckpoint("consumer.connected"); |
| 125 | } |
| 126 | |
| 127 | void TestHelper::WaitForProducerEnabled() { |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 128 | task_runner_->RunUntilCheckpoint("producer.enabled"); |
| 129 | } |
| 130 | |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame^] | 131 | void TestHelper::WaitForTracingDisabled(uint32_t timeout_ms) { |
| 132 | task_runner_->RunUntilCheckpoint("stop.tracing", timeout_ms); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Lalit Maganti | 3f0b7c6 | 2018-04-18 19:10:09 +0100 | [diff] [blame] | 135 | void TestHelper::WaitForReadData(uint32_t read_count) { |
| 136 | task_runner_->RunUntilCheckpoint("readback.complete." + |
| 137 | std::to_string(read_count)); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | std::function<void()> TestHelper::WrapTask( |
| 141 | const std::function<void()>& function) { |
| 142 | return [this, function] { task_runner_->PostTask(function); }; |
| 143 | } |
| 144 | |
| 145 | } // namespace perfetto |