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 | |
Lalit Maganti | 3f0b7c6 | 2018-04-18 19:10:09 +0100 | [diff] [blame] | 105 | void TestHelper::ReadData(uint32_t read_count) { |
| 106 | on_packets_finished_callback_ = task_runner_->CreateCheckpoint( |
| 107 | "readback.complete." + std::to_string(read_count)); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 108 | endpoint_->ReadBuffers(); |
| 109 | } |
| 110 | |
| 111 | void TestHelper::WaitForConsumerConnect() { |
| 112 | task_runner_->RunUntilCheckpoint("consumer.connected"); |
| 113 | } |
| 114 | |
| 115 | void TestHelper::WaitForProducerEnabled() { |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 116 | task_runner_->RunUntilCheckpoint("producer.enabled"); |
| 117 | } |
| 118 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 119 | void TestHelper::WaitForTracingDisabled() { |
| 120 | task_runner_->RunUntilCheckpoint("stop.tracing"); |
| 121 | } |
| 122 | |
Lalit Maganti | 3f0b7c6 | 2018-04-18 19:10:09 +0100 | [diff] [blame] | 123 | void TestHelper::WaitForReadData(uint32_t read_count) { |
| 124 | task_runner_->RunUntilCheckpoint("readback.complete." + |
| 125 | std::to_string(read_count)); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | std::function<void()> TestHelper::WrapTask( |
| 129 | const std::function<void()>& function) { |
| 130 | return [this, function] { task_runner_->PostTask(function); }; |
| 131 | } |
| 132 | |
| 133 | } // namespace perfetto |