Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [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/fake_producer.h" |
| 18 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 19 | #include <condition_variable> |
| 20 | #include <mutex> |
Lalit Maganti | 3f5705c | 2018-03-09 12:09:44 +0000 | [diff] [blame] | 21 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 22 | #include "gtest/gtest.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 23 | #include "perfetto/base/logging.h" |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame^] | 24 | #include "perfetto/base/time.h" |
Lalit Maganti | 8390e5b | 2018-03-23 10:46:05 +0000 | [diff] [blame] | 25 | #include "perfetto/base/utils.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 26 | #include "perfetto/trace/test_event.pbzero.h" |
| 27 | #include "perfetto/trace/trace_packet.pbzero.h" |
| 28 | #include "perfetto/traced/traced.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 29 | #include "perfetto/tracing/core/trace_packet.h" |
| 30 | #include "perfetto/tracing/core/trace_writer.h" |
| 31 | |
| 32 | namespace perfetto { |
| 33 | |
| 34 | FakeProducer::FakeProducer(const std::string& name) : name_(name) {} |
| 35 | FakeProducer::~FakeProducer() = default; |
| 36 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 37 | void FakeProducer::Connect( |
| 38 | const char* socket_name, |
| 39 | base::TaskRunner* task_runner, |
| 40 | std::function<void()> on_create_data_source_instance) { |
| 41 | PERFETTO_DCHECK_THREAD(thread_checker_); |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 42 | task_runner_ = task_runner; |
Isabelle Taylor | 86262cb | 2018-03-27 16:00:54 +0100 | [diff] [blame] | 43 | endpoint_ = ProducerIPCClient::Connect( |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 44 | socket_name, this, "android.perfetto.FakeProducer", task_runner); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 45 | on_create_data_source_instance_ = std::move(on_create_data_source_instance); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void FakeProducer::OnConnect() { |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 49 | PERFETTO_DCHECK_THREAD(thread_checker_); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 50 | DataSourceDescriptor descriptor; |
| 51 | descriptor.set_name(name_); |
Primiano Tucci | 9daa483 | 2018-03-28 23:28:17 +0100 | [diff] [blame] | 52 | endpoint_->RegisterDataSource(descriptor); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 55 | void FakeProducer::OnDisconnect() { |
| 56 | PERFETTO_DCHECK_THREAD(thread_checker_); |
| 57 | FAIL() << "Producer unexpectedly disconnected from the service"; |
| 58 | } |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 59 | |
| 60 | void FakeProducer::CreateDataSourceInstance( |
| 61 | DataSourceInstanceID, |
| 62 | const DataSourceConfig& source_config) { |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 63 | PERFETTO_DCHECK_THREAD(thread_checker_); |
| 64 | trace_writer_ = endpoint_->CreateTraceWriter( |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 65 | static_cast<BufferID>(source_config.target_buffer())); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 66 | rnd_engine_ = std::minstd_rand0(source_config.for_testing().seed()); |
| 67 | message_count_ = source_config.for_testing().message_count(); |
Lalit Maganti | 8390e5b | 2018-03-23 10:46:05 +0000 | [diff] [blame] | 68 | message_size_ = source_config.for_testing().message_size(); |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame^] | 69 | max_messages_per_second_ = |
| 70 | source_config.for_testing().max_messages_per_second(); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 71 | task_runner_->PostTask(on_create_data_source_instance_); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 74 | void FakeProducer::TearDownDataSourceInstance(DataSourceInstanceID) { |
| 75 | PERFETTO_DCHECK_THREAD(thread_checker_); |
| 76 | trace_writer_.reset(); |
| 77 | } |
| 78 | |
| 79 | // Note: this will called on a different thread. |
| 80 | void FakeProducer::ProduceEventBatch(std::function<void()> callback) { |
| 81 | task_runner_->PostTask([this, callback] { |
| 82 | PERFETTO_CHECK(trace_writer_); |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 83 | PERFETTO_CHECK(message_size_ > 1); |
Lalit Maganti | 8390e5b | 2018-03-23 10:46:05 +0000 | [diff] [blame] | 84 | std::unique_ptr<char, base::FreeDeleter> payload( |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 85 | static_cast<char*>(malloc(message_size_))); |
| 86 | memset(payload.get(), '.', message_size_); |
| 87 | payload.get()[message_size_ - 1] = 0; |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame^] | 88 | |
| 89 | base::TimeMillis start = base::GetWallTimeMs(); |
| 90 | int64_t iterations = 0; |
| 91 | size_t messages_to_emit = message_count_; |
| 92 | while (messages_to_emit > 0) { |
| 93 | size_t messages_in_minibatch = |
| 94 | max_messages_per_second_ == 0 |
| 95 | ? messages_to_emit |
| 96 | : std::min(max_messages_per_second_, messages_to_emit); |
| 97 | PERFETTO_DCHECK(messages_to_emit >= messages_in_minibatch); |
| 98 | |
| 99 | for (size_t i = 0; i < messages_in_minibatch; i++) { |
| 100 | auto handle = trace_writer_->NewTracePacket(); |
| 101 | handle->set_for_testing()->set_seq_value(rnd_engine_()); |
| 102 | handle->set_for_testing()->set_str(payload.get(), message_size_); |
| 103 | } |
| 104 | messages_to_emit -= messages_in_minibatch; |
| 105 | |
| 106 | // Pause until the second boundary to make sure that we are adhering to |
| 107 | // the speed limitation. |
| 108 | if (max_messages_per_second_ > 0) { |
| 109 | int64_t expected_time_taken = ++iterations * 1000; |
| 110 | base::TimeMillis time_taken = base::GetWallTimeMs() - start; |
| 111 | while (time_taken.count() < expected_time_taken) { |
| 112 | usleep((expected_time_taken - time_taken.count()) * 1000); |
| 113 | time_taken = base::GetWallTimeMs() - start; |
| 114 | } |
| 115 | } |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 116 | } |
| 117 | trace_writer_->Flush(callback); |
| 118 | }); |
| 119 | } |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 120 | |
Isabelle Taylor | 69faa90 | 2018-03-21 15:42:03 +0000 | [diff] [blame] | 121 | void FakeProducer::OnTracingStart() {} |
| 122 | |
| 123 | void FakeProducer::OnTracingStop() {} |
| 124 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 125 | } // namespace perfetto |