blob: 175e8e6d27fd3179affc77d597507a90479fdb4b [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
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000022#include "perfetto/base/logging.h"
Eric Seckler83dcc8c2019-08-21 12:18:43 +010023#include "perfetto/base/time.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010024#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 Tucci3b39fc72019-06-22 19:05:23 +010028#include "perfetto/tracing/core/data_source_config.h"
29#include "perfetto/tracing/core/test_config.h"
Primiano Tucci355b8c82019-08-29 08:37:51 +020030#include "protos/perfetto/trace/test_event.pbzero.h"
31#include "protos/perfetto/trace/trace_packet.pbzero.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000032
33namespace perfetto {
34
35FakeProducer::FakeProducer(const std::string& name) : name_(name) {}
36FakeProducer::~FakeProducer() = default;
37
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000038void FakeProducer::Connect(
39 const char* socket_name,
40 base::TaskRunner* task_runner,
Stephen Nuskoe8238112019-04-09 18:37:00 +010041 std::function<void()> on_setup_data_source_instance,
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000042 std::function<void()> on_create_data_source_instance) {
43 PERFETTO_DCHECK_THREAD(thread_checker_);
Sami Kyostila32e0b542018-02-14 08:55:43 +000044 task_runner_ = task_runner;
Isabelle Taylor86262cb2018-03-27 16:00:54 +010045 endpoint_ = ProducerIPCClient::Connect(
Primiano Tucci5ae66da2018-03-28 15:57:34 +010046 socket_name, this, "android.perfetto.FakeProducer", task_runner);
Stephen Nuskoe8238112019-04-09 18:37:00 +010047 on_setup_data_source_instance_ = std::move(on_setup_data_source_instance);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000048 on_create_data_source_instance_ = std::move(on_create_data_source_instance);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000049}
50
51void FakeProducer::OnConnect() {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000052 PERFETTO_DCHECK_THREAD(thread_checker_);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000053 DataSourceDescriptor descriptor;
54 descriptor.set_name(name_);
Primiano Tucci9daa4832018-03-28 23:28:17 +010055 endpoint_->RegisterDataSource(descriptor);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000056}
57
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000058void FakeProducer::OnDisconnect() {
59 PERFETTO_DCHECK_THREAD(thread_checker_);
Primiano Tucci008cdb92019-07-19 19:52:41 +010060 PERFETTO_FATAL("Producer unexpectedly disconnected from the service");
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000061}
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000062
Primiano Tucci674076d2018-10-01 10:41:09 +010063void FakeProducer::SetupDataSource(DataSourceInstanceID,
Stephen Nuskoe8238112019-04-09 18:37:00 +010064 const DataSourceConfig&) {
65 task_runner_->PostTask(on_setup_data_source_instance_);
66}
Primiano Tucci674076d2018-10-01 10:41:09 +010067
Primiano Tucciafb72b52018-09-25 09:37:24 +010068void FakeProducer::StartDataSource(DataSourceInstanceID,
69 const DataSourceConfig& source_config) {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000070 PERFETTO_DCHECK_THREAD(thread_checker_);
71 trace_writer_ = endpoint_->CreateTraceWriter(
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000072 static_cast<BufferID>(source_config.target_buffer()));
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000073 rnd_engine_ = std::minstd_rand0(source_config.for_testing().seed());
74 message_count_ = source_config.for_testing().message_count();
Lalit Maganti8390e5b2018-03-23 10:46:05 +000075 message_size_ = source_config.for_testing().message_size();
Lalit Maganti131b6e52018-03-29 18:29:31 +010076 max_messages_per_second_ =
77 source_config.for_testing().max_messages_per_second();
Lalit Maganti36557d82018-04-11 14:36:17 +010078 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 Maganti79f2d7b2018-01-23 18:27:33 +000083}
84
Primiano Tucciafb72b52018-09-25 09:37:24 +010085void FakeProducer::StopDataSource(DataSourceInstanceID) {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000086 PERFETTO_DCHECK_THREAD(thread_checker_);
87 trace_writer_.reset();
88}
89
Lalit Maganti36557d82018-04-11 14:36:17 +010090// Note: this can be called on a different thread.
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000091void FakeProducer::ProduceEventBatch(std::function<void()> callback) {
92 task_runner_->PostTask([this, callback] {
93 PERFETTO_CHECK(trace_writer_);
Primiano Tucci5ae66da2018-03-28 15:57:34 +010094 PERFETTO_CHECK(message_size_ > 1);
Lalit Maganti8390e5b2018-03-23 10:46:05 +000095 std::unique_ptr<char, base::FreeDeleter> payload(
Primiano Tucci5ae66da2018-03-28 15:57:34 +010096 static_cast<char*>(malloc(message_size_)));
97 memset(payload.get(), '.', message_size_);
98 payload.get()[message_size_ - 1] = 0;
Lalit Maganti131b6e52018-03-29 18:29:31 +010099
100 base::TimeMillis start = base::GetWallTimeMs();
101 int64_t iterations = 0;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100102 uint32_t messages_to_emit = message_count_;
Lalit Maganti131b6e52018-03-29 18:29:31 +0100103 while (messages_to_emit > 0) {
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100104 uint32_t messages_in_minibatch =
Lalit Maganti131b6e52018-03-29 18:29:31 +0100105 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 Tucci3cbb10a2018-04-10 17:52:40 +0100110 for (uint32_t i = 0; i < messages_in_minibatch; i++) {
Lalit Maganti131b6e52018-03-29 18:29:31 +0100111 auto handle = trace_writer_->NewTracePacket();
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100112 handle->set_for_testing()->set_seq_value(
113 static_cast<uint32_t>(rnd_engine_()));
Lalit Maganti131b6e52018-03-29 18:29:31 +0100114 handle->set_for_testing()->set_str(payload.get(), message_size_);
115 }
116 messages_to_emit -= messages_in_minibatch;
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100117 iterations++;
Lalit Maganti131b6e52018-03-29 18:29:31 +0100118
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 Maganti3f0b7c62018-04-18 19:10:09 +0100122 int64_t expected_time_taken = iterations * 1000;
Lalit Maganti131b6e52018-03-29 18:29:31 +0100123 base::TimeMillis time_taken = base::GetWallTimeMs() - start;
124 while (time_taken.count() < expected_time_taken) {
Primiano Tucci3cbb10a2018-04-10 17:52:40 +0100125 usleep(static_cast<useconds_t>(
126 (expected_time_taken - time_taken.count()) * 1000));
Lalit Maganti131b6e52018-03-29 18:29:31 +0100127 time_taken = base::GetWallTimeMs() - start;
128 }
129 }
Lalit Maganti3f0b7c62018-04-18 19:10:09 +0100130 trace_writer_->Flush(messages_to_emit > 0 ? [] {} : callback);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000131 }
Lalit Magantibfc3d3e2018-03-22 20:28:38 +0000132 });
133}
Lalit Maganti79f2d7b2018-01-23 18:27:33 +0000134
Primiano Tuccidca727d2018-04-04 11:31:55 +0200135void FakeProducer::OnTracingSetup() {}
Isabelle Taylor69faa902018-03-21 15:42:03 +0000136
Primiano Tuccid52e6272018-04-06 19:06:53 +0200137void 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 Maganti79f2d7b2018-01-23 18:27:33 +0000146} // namespace perfetto