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 | 8390e5b | 2018-03-23 10:46:05 +0000 | [diff] [blame] | 24 | #include "perfetto/base/utils.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 25 | #include "perfetto/trace/test_event.pbzero.h" |
| 26 | #include "perfetto/trace/trace_packet.pbzero.h" |
| 27 | #include "perfetto/traced/traced.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 28 | #include "perfetto/tracing/core/trace_packet.h" |
| 29 | #include "perfetto/tracing/core/trace_writer.h" |
| 30 | |
| 31 | namespace perfetto { |
| 32 | |
| 33 | FakeProducer::FakeProducer(const std::string& name) : name_(name) {} |
| 34 | FakeProducer::~FakeProducer() = default; |
| 35 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 36 | void FakeProducer::Connect( |
| 37 | const char* socket_name, |
| 38 | base::TaskRunner* task_runner, |
| 39 | std::function<void()> on_create_data_source_instance) { |
| 40 | PERFETTO_DCHECK_THREAD(thread_checker_); |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 41 | task_runner_ = task_runner; |
Isabelle Taylor | 86262cb | 2018-03-27 16:00:54 +0100 | [diff] [blame] | 42 | endpoint_ = ProducerIPCClient::Connect( |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 43 | socket_name, this, "android.perfetto.FakeProducer", task_runner); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 44 | on_create_data_source_instance_ = std::move(on_create_data_source_instance); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void FakeProducer::OnConnect() { |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 48 | PERFETTO_DCHECK_THREAD(thread_checker_); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 49 | DataSourceDescriptor descriptor; |
| 50 | descriptor.set_name(name_); |
Primiano Tucci | 9daa483 | 2018-03-28 23:28:17 +0100 | [diff] [blame] | 51 | endpoint_->RegisterDataSource(descriptor); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 54 | void FakeProducer::OnDisconnect() { |
| 55 | PERFETTO_DCHECK_THREAD(thread_checker_); |
| 56 | FAIL() << "Producer unexpectedly disconnected from the service"; |
| 57 | } |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 58 | |
| 59 | void FakeProducer::CreateDataSourceInstance( |
| 60 | DataSourceInstanceID, |
| 61 | const DataSourceConfig& source_config) { |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 62 | PERFETTO_DCHECK_THREAD(thread_checker_); |
| 63 | trace_writer_ = endpoint_->CreateTraceWriter( |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 64 | static_cast<BufferID>(source_config.target_buffer())); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 65 | rnd_engine_ = std::minstd_rand0(source_config.for_testing().seed()); |
| 66 | message_count_ = source_config.for_testing().message_count(); |
Lalit Maganti | 8390e5b | 2018-03-23 10:46:05 +0000 | [diff] [blame] | 67 | message_size_ = source_config.for_testing().message_size(); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 68 | task_runner_->PostTask(on_create_data_source_instance_); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 71 | void FakeProducer::TearDownDataSourceInstance(DataSourceInstanceID) { |
| 72 | PERFETTO_DCHECK_THREAD(thread_checker_); |
| 73 | trace_writer_.reset(); |
| 74 | } |
| 75 | |
| 76 | // Note: this will called on a different thread. |
| 77 | void FakeProducer::ProduceEventBatch(std::function<void()> callback) { |
| 78 | task_runner_->PostTask([this, callback] { |
| 79 | PERFETTO_CHECK(trace_writer_); |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 80 | PERFETTO_CHECK(message_size_ > 1); |
Lalit Maganti | 8390e5b | 2018-03-23 10:46:05 +0000 | [diff] [blame] | 81 | std::unique_ptr<char, base::FreeDeleter> payload( |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 82 | static_cast<char*>(malloc(message_size_))); |
| 83 | memset(payload.get(), '.', message_size_); |
| 84 | payload.get()[message_size_ - 1] = 0; |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 85 | for (size_t i = 0; i < message_count_; i++) { |
| 86 | auto handle = trace_writer_->NewTracePacket(); |
| 87 | handle->set_for_testing()->set_seq_value(rnd_engine_()); |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 88 | handle->set_for_testing()->set_str(payload.get(), message_size_); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 89 | } |
| 90 | trace_writer_->Flush(callback); |
| 91 | }); |
| 92 | } |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 93 | |
Isabelle Taylor | 69faa90 | 2018-03-21 15:42:03 +0000 | [diff] [blame] | 94 | void FakeProducer::OnTracingStart() {} |
| 95 | |
| 96 | void FakeProducer::OnTracingStop() {} |
| 97 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 98 | } // namespace perfetto |