blob: 944c8cb5a13e2df3933ede28b565c69f45b3ed5a [file] [log] [blame]
Lalit Maganti79f2d7b2018-01-23 18:27:33 +00001/*
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 Magantibfc3d3e2018-03-22 20:28:38 +000019#include <condition_variable>
20#include <mutex>
Lalit Maganti3f5705c2018-03-09 12:09:44 +000021
Primiano Tucci2c5488f2019-06-01 03:27:28 +010022#include <gtest/gtest.h>
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000023#include "perfetto/base/logging.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010024#include "perfetto/ext/base/time.h"
25#include "perfetto/ext/base/utils.h"
26#include "perfetto/ext/traced/traced.h"
27#include "perfetto/ext/tracing/core/trace_packet.h"
28#include "perfetto/ext/tracing/core/trace_writer.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000029#include "perfetto/trace/test_event.pbzero.h"
30#include "perfetto/trace/trace_packet.pbzero.h"
Primiano Tucci3b39fc72019-06-22 19:05:23 +010031#include "perfetto/tracing/core/data_source_config.h"
32#include "perfetto/tracing/core/test_config.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000033
34namespace perfetto {
35
36FakeProducer::FakeProducer(const std::string& name) : name_(name) {}
37FakeProducer::~FakeProducer() = default;
38
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000039void FakeProducer::Connect(
40 const char* socket_name,
41 base::TaskRunner* task_runner,
Stephen Nuskoe8238112019-04-09 18:37:00 +010042 std::function<void()> on_setup_data_source_instance,
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000043 std::function<void()> on_create_data_source_instance) {
44 PERFETTO_DCHECK_THREAD(thread_checker_);
Sami Kyostila32e0b542018-02-14 08:55:43 +000045 task_runner_ = task_runner;
Isabelle Taylor86262cb2018-03-27 16:00:54 +010046 endpoint_ = ProducerIPCClient::Connect(
Primiano Tucci5ae66da2018-03-28 15:57:34 +010047 socket_name, this, "android.perfetto.FakeProducer", task_runner);
Stephen Nuskoe8238112019-04-09 18:37:00 +010048 on_setup_data_source_instance_ = std::move(on_setup_data_source_instance);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000049 on_create_data_source_instance_ = std::move(on_create_data_source_instance);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000050}
51
52void FakeProducer::OnConnect() {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000053 PERFETTO_DCHECK_THREAD(thread_checker_);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000054 DataSourceDescriptor descriptor;
55 descriptor.set_name(name_);
Primiano Tucci9daa4832018-03-28 23:28:17 +010056 endpoint_->RegisterDataSource(descriptor);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000057}
58
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000059void FakeProducer::OnDisconnect() {
60 PERFETTO_DCHECK_THREAD(thread_checker_);
61 FAIL() << "Producer unexpectedly disconnected from the service";
62}
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000063
Primiano Tucci674076d2018-10-01 10:41:09 +010064void FakeProducer::SetupDataSource(DataSourceInstanceID,
Stephen Nuskoe8238112019-04-09 18:37:00 +010065 const DataSourceConfig&) {
66 task_runner_->PostTask(on_setup_data_source_instance_);
67}
Primiano Tucci674076d2018-10-01 10:41:09 +010068
Primiano Tucciafb72b52018-09-25 09:37:24 +010069void FakeProducer::StartDataSource(DataSourceInstanceID,
70 const DataSourceConfig& source_config) {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000071 PERFETTO_DCHECK_THREAD(thread_checker_);
72 trace_writer_ = endpoint_->CreateTraceWriter(
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000073 static_cast<BufferID>(source_config.target_buffer()));
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000074 rnd_engine_ = std::minstd_rand0(source_config.for_testing().seed());
75 message_count_ = source_config.for_testing().message_count();
Lalit Maganti8390e5b2018-03-23 10:46:05 +000076 message_size_ = source_config.for_testing().message_size();
Lalit Maganti131b6e52018-03-29 18:29:31 +010077 max_messages_per_second_ =
78 source_config.for_testing().max_messages_per_second();
Lalit Maganti36557d82018-04-11 14:36:17 +010079 if (source_config.for_testing().send_batch_on_register()) {
80 ProduceEventBatch(on_create_data_source_instance_);
81 } else {
82 task_runner_->PostTask(on_create_data_source_instance_);
83 }
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000084}
85
Primiano Tucciafb72b52018-09-25 09:37:24 +010086void FakeProducer::StopDataSource(DataSourceInstanceID) {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000087 PERFETTO_DCHECK_THREAD(thread_checker_);
88 trace_writer_.reset();
89}
90
Lalit Maganti36557d82018-04-11 14:36:17 +010091// Note: this can be called on a different thread.
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000092void FakeProducer::ProduceEventBatch(std::function<void()> callback) {
93 task_runner_->PostTask([this, callback] {
94 PERFETTO_CHECK(trace_writer_);
Primiano Tucci5ae66da2018-03-28 15:57:34 +010095 PERFETTO_CHECK(message_size_ > 1);
Lalit Maganti8390e5b2018-03-23 10:46:05 +000096 std::unique_ptr<char, base::FreeDeleter> payload(
Primiano Tucci5ae66da2018-03-28 15:57:34 +010097 static_cast<char*>(malloc(message_size_)));
98 memset(payload.get(), '.', message_size_);
99 payload.get()[message_size_ - 1] = 0;
Lalit Maganti131b6e52018-03-29 18:29:31 +0100100
101 base::TimeMillis start = base::GetWallTimeMs();
102 int64_t iterations = 0;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100103 uint32_t messages_to_emit = message_count_;
Lalit Maganti131b6e52018-03-29 18:29:31 +0100104 while (messages_to_emit > 0) {
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100105 uint32_t messages_in_minibatch =
Lalit Maganti131b6e52018-03-29 18:29:31 +0100106 max_messages_per_second_ == 0
107 ? messages_to_emit
108 : std::min(max_messages_per_second_, messages_to_emit);
109 PERFETTO_DCHECK(messages_to_emit >= messages_in_minibatch);
110
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100111 for (uint32_t i = 0; i < messages_in_minibatch; i++) {
Lalit Maganti131b6e52018-03-29 18:29:31 +0100112 auto handle = trace_writer_->NewTracePacket();
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100113 handle->set_for_testing()->set_seq_value(
114 static_cast<uint32_t>(rnd_engine_()));
Lalit Maganti131b6e52018-03-29 18:29:31 +0100115 handle->set_for_testing()->set_str(payload.get(), message_size_);
116 }
117 messages_to_emit -= messages_in_minibatch;
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100118 iterations++;
Lalit Maganti131b6e52018-03-29 18:29:31 +0100119
120 // Pause until the second boundary to make sure that we are adhering to
121 // the speed limitation.
122 if (max_messages_per_second_ > 0) {
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100123 int64_t expected_time_taken = iterations * 1000;
Lalit Maganti131b6e52018-03-29 18:29:31 +0100124 base::TimeMillis time_taken = base::GetWallTimeMs() - start;
125 while (time_taken.count() < expected_time_taken) {
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100126 usleep(static_cast<useconds_t>(
127 (expected_time_taken - time_taken.count()) * 1000));
Lalit Maganti131b6e52018-03-29 18:29:31 +0100128 time_taken = base::GetWallTimeMs() - start;
129 }
130 }
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100131 trace_writer_->Flush(messages_to_emit > 0 ? [] {} : callback);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000132 }
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000133 });
134}
Lalit Maganti79f2d7b2018-01-23 18:27:33 +0000135
Primiano Tuccidca727d2018-04-04 11:31:55 +0200136void FakeProducer::OnTracingSetup() {}
Isabelle Taylor69faa902018-03-21 15:42:03 +0000137
Primiano Tuccid52e6272018-04-06 19:06:53 +0200138void FakeProducer::Flush(FlushRequestID flush_request_id,
139 const DataSourceInstanceID*,
140 size_t num_data_sources) {
141 PERFETTO_DCHECK(num_data_sources > 0);
142 if (trace_writer_)
143 trace_writer_->Flush();
144 endpoint_->NotifyFlushComplete(flush_request_id);
145}
146
Lalit Maganti79f2d7b2018-01-23 18:27:33 +0000147} // namespace perfetto