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 | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 22 | #include "perfetto/base/logging.h" |
Eric Seckler | 83dcc8c | 2019-08-21 12:18:43 +0100 | [diff] [blame] | 23 | #include "perfetto/base/time.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 24 | #include "perfetto/ext/base/utils.h" |
| 25 | #include "perfetto/ext/traced/traced.h" |
| 26 | #include "perfetto/ext/tracing/core/trace_packet.h" |
| 27 | #include "perfetto/ext/tracing/core/trace_writer.h" |
Primiano Tucci | 3b39fc7 | 2019-06-22 19:05:23 +0100 | [diff] [blame] | 28 | #include "perfetto/tracing/core/data_source_config.h" |
| 29 | #include "perfetto/tracing/core/test_config.h" |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame^] | 30 | #include "protos/perfetto/trace/test_event.pbzero.h" |
| 31 | #include "protos/perfetto/trace/trace_packet.pbzero.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 32 | |
| 33 | namespace perfetto { |
| 34 | |
| 35 | FakeProducer::FakeProducer(const std::string& name) : name_(name) {} |
| 36 | FakeProducer::~FakeProducer() = default; |
| 37 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 38 | void FakeProducer::Connect( |
| 39 | const char* socket_name, |
| 40 | base::TaskRunner* task_runner, |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 41 | std::function<void()> on_setup_data_source_instance, |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 42 | std::function<void()> on_create_data_source_instance) { |
| 43 | PERFETTO_DCHECK_THREAD(thread_checker_); |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 44 | task_runner_ = task_runner; |
Isabelle Taylor | 86262cb | 2018-03-27 16:00:54 +0100 | [diff] [blame] | 45 | endpoint_ = ProducerIPCClient::Connect( |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 46 | socket_name, this, "android.perfetto.FakeProducer", task_runner); |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 47 | on_setup_data_source_instance_ = std::move(on_setup_data_source_instance); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 48 | on_create_data_source_instance_ = std::move(on_create_data_source_instance); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void FakeProducer::OnConnect() { |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 52 | PERFETTO_DCHECK_THREAD(thread_checker_); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 53 | DataSourceDescriptor descriptor; |
| 54 | descriptor.set_name(name_); |
Primiano Tucci | 9daa483 | 2018-03-28 23:28:17 +0100 | [diff] [blame] | 55 | endpoint_->RegisterDataSource(descriptor); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 58 | void FakeProducer::OnDisconnect() { |
| 59 | PERFETTO_DCHECK_THREAD(thread_checker_); |
Primiano Tucci | 008cdb9 | 2019-07-19 19:52:41 +0100 | [diff] [blame] | 60 | PERFETTO_FATAL("Producer unexpectedly disconnected from the service"); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 61 | } |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 62 | |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 63 | void FakeProducer::SetupDataSource(DataSourceInstanceID, |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 64 | const DataSourceConfig&) { |
| 65 | task_runner_->PostTask(on_setup_data_source_instance_); |
| 66 | } |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 67 | |
Primiano Tucci | afb72b5 | 2018-09-25 09:37:24 +0100 | [diff] [blame] | 68 | void FakeProducer::StartDataSource(DataSourceInstanceID, |
| 69 | const DataSourceConfig& source_config) { |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 70 | PERFETTO_DCHECK_THREAD(thread_checker_); |
| 71 | trace_writer_ = endpoint_->CreateTraceWriter( |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 72 | static_cast<BufferID>(source_config.target_buffer())); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 73 | rnd_engine_ = std::minstd_rand0(source_config.for_testing().seed()); |
| 74 | message_count_ = source_config.for_testing().message_count(); |
Lalit Maganti | 8390e5b | 2018-03-23 10:46:05 +0000 | [diff] [blame] | 75 | message_size_ = source_config.for_testing().message_size(); |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 76 | max_messages_per_second_ = |
| 77 | source_config.for_testing().max_messages_per_second(); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 78 | if (source_config.for_testing().send_batch_on_register()) { |
| 79 | ProduceEventBatch(on_create_data_source_instance_); |
| 80 | } else { |
| 81 | task_runner_->PostTask(on_create_data_source_instance_); |
| 82 | } |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Primiano Tucci | afb72b5 | 2018-09-25 09:37:24 +0100 | [diff] [blame] | 85 | void FakeProducer::StopDataSource(DataSourceInstanceID) { |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 86 | PERFETTO_DCHECK_THREAD(thread_checker_); |
| 87 | trace_writer_.reset(); |
| 88 | } |
| 89 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 90 | // Note: this can be called on a different thread. |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 91 | void FakeProducer::ProduceEventBatch(std::function<void()> callback) { |
| 92 | task_runner_->PostTask([this, callback] { |
| 93 | PERFETTO_CHECK(trace_writer_); |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 94 | PERFETTO_CHECK(message_size_ > 1); |
Lalit Maganti | 8390e5b | 2018-03-23 10:46:05 +0000 | [diff] [blame] | 95 | std::unique_ptr<char, base::FreeDeleter> payload( |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 96 | static_cast<char*>(malloc(message_size_))); |
| 97 | memset(payload.get(), '.', message_size_); |
| 98 | payload.get()[message_size_ - 1] = 0; |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 99 | |
| 100 | base::TimeMillis start = base::GetWallTimeMs(); |
| 101 | int64_t iterations = 0; |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 102 | uint32_t messages_to_emit = message_count_; |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 103 | while (messages_to_emit > 0) { |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 104 | uint32_t messages_in_minibatch = |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 105 | max_messages_per_second_ == 0 |
| 106 | ? messages_to_emit |
| 107 | : std::min(max_messages_per_second_, messages_to_emit); |
| 108 | PERFETTO_DCHECK(messages_to_emit >= messages_in_minibatch); |
| 109 | |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 110 | for (uint32_t i = 0; i < messages_in_minibatch; i++) { |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 111 | auto handle = trace_writer_->NewTracePacket(); |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 112 | handle->set_for_testing()->set_seq_value( |
| 113 | static_cast<uint32_t>(rnd_engine_())); |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 114 | handle->set_for_testing()->set_str(payload.get(), message_size_); |
| 115 | } |
| 116 | messages_to_emit -= messages_in_minibatch; |
Lalit Maganti | 3f0b7c6 | 2018-04-18 19:10:09 +0100 | [diff] [blame] | 117 | iterations++; |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 118 | |
| 119 | // Pause until the second boundary to make sure that we are adhering to |
| 120 | // the speed limitation. |
| 121 | if (max_messages_per_second_ > 0) { |
Lalit Maganti | 3f0b7c6 | 2018-04-18 19:10:09 +0100 | [diff] [blame] | 122 | int64_t expected_time_taken = iterations * 1000; |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 123 | base::TimeMillis time_taken = base::GetWallTimeMs() - start; |
| 124 | while (time_taken.count() < expected_time_taken) { |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 125 | usleep(static_cast<useconds_t>( |
| 126 | (expected_time_taken - time_taken.count()) * 1000)); |
Lalit Maganti | 131b6e5 | 2018-03-29 18:29:31 +0100 | [diff] [blame] | 127 | time_taken = base::GetWallTimeMs() - start; |
| 128 | } |
| 129 | } |
Lalit Maganti | 3f0b7c6 | 2018-04-18 19:10:09 +0100 | [diff] [blame] | 130 | trace_writer_->Flush(messages_to_emit > 0 ? [] {} : callback); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 131 | } |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 132 | }); |
| 133 | } |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 134 | |
Primiano Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 135 | void FakeProducer::OnTracingSetup() {} |
Isabelle Taylor | 69faa90 | 2018-03-21 15:42:03 +0000 | [diff] [blame] | 136 | |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 137 | void FakeProducer::Flush(FlushRequestID flush_request_id, |
| 138 | const DataSourceInstanceID*, |
| 139 | size_t num_data_sources) { |
| 140 | PERFETTO_DCHECK(num_data_sources > 0); |
| 141 | if (trace_writer_) |
| 142 | trace_writer_->Flush(); |
| 143 | endpoint_->NotifyFlushComplete(flush_request_id); |
| 144 | } |
| 145 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 146 | } // namespace perfetto |