blob: a81f65673875a154f7d143220e51aa6326cdc778 [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 Magantibfc3d3e2018-03-22 20:28:38 +000022#include "gtest/gtest.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000023#include "perfetto/base/logging.h"
Lalit Maganti131b6e52018-03-29 18:29:31 +010024#include "perfetto/base/time.h"
Lalit Maganti8390e5b2018-03-23 10:46:05 +000025#include "perfetto/base/utils.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000026#include "perfetto/trace/test_event.pbzero.h"
27#include "perfetto/trace/trace_packet.pbzero.h"
28#include "perfetto/traced/traced.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000029#include "perfetto/tracing/core/trace_packet.h"
30#include "perfetto/tracing/core/trace_writer.h"
31
32namespace perfetto {
33
34FakeProducer::FakeProducer(const std::string& name) : name_(name) {}
35FakeProducer::~FakeProducer() = default;
36
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000037void 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 Kyostila32e0b542018-02-14 08:55:43 +000042 task_runner_ = task_runner;
Isabelle Taylor86262cb2018-03-27 16:00:54 +010043 endpoint_ = ProducerIPCClient::Connect(
Primiano Tucci5ae66da2018-03-28 15:57:34 +010044 socket_name, this, "android.perfetto.FakeProducer", task_runner);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000045 on_create_data_source_instance_ = std::move(on_create_data_source_instance);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000046}
47
48void FakeProducer::OnConnect() {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000049 PERFETTO_DCHECK_THREAD(thread_checker_);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000050 DataSourceDescriptor descriptor;
51 descriptor.set_name(name_);
Primiano Tucci9daa4832018-03-28 23:28:17 +010052 endpoint_->RegisterDataSource(descriptor);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000053}
54
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000055void FakeProducer::OnDisconnect() {
56 PERFETTO_DCHECK_THREAD(thread_checker_);
57 FAIL() << "Producer unexpectedly disconnected from the service";
58}
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000059
60void FakeProducer::CreateDataSourceInstance(
61 DataSourceInstanceID,
62 const DataSourceConfig& source_config) {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000063 PERFETTO_DCHECK_THREAD(thread_checker_);
64 trace_writer_ = endpoint_->CreateTraceWriter(
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000065 static_cast<BufferID>(source_config.target_buffer()));
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000066 rnd_engine_ = std::minstd_rand0(source_config.for_testing().seed());
67 message_count_ = source_config.for_testing().message_count();
Lalit Maganti8390e5b2018-03-23 10:46:05 +000068 message_size_ = source_config.for_testing().message_size();
Lalit Maganti131b6e52018-03-29 18:29:31 +010069 max_messages_per_second_ =
70 source_config.for_testing().max_messages_per_second();
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000071 task_runner_->PostTask(on_create_data_source_instance_);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000072}
73
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000074void FakeProducer::TearDownDataSourceInstance(DataSourceInstanceID) {
75 PERFETTO_DCHECK_THREAD(thread_checker_);
76 trace_writer_.reset();
77}
78
79// Note: this will called on a different thread.
80void FakeProducer::ProduceEventBatch(std::function<void()> callback) {
81 task_runner_->PostTask([this, callback] {
82 PERFETTO_CHECK(trace_writer_);
Primiano Tucci5ae66da2018-03-28 15:57:34 +010083 PERFETTO_CHECK(message_size_ > 1);
Lalit Maganti8390e5b2018-03-23 10:46:05 +000084 std::unique_ptr<char, base::FreeDeleter> payload(
Primiano Tucci5ae66da2018-03-28 15:57:34 +010085 static_cast<char*>(malloc(message_size_)));
86 memset(payload.get(), '.', message_size_);
87 payload.get()[message_size_ - 1] = 0;
Lalit Maganti131b6e52018-03-29 18:29:31 +010088
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 Magantibfc3d3e2018-03-22 20:28:38 +0000116 }
117 trace_writer_->Flush(callback);
118 });
119}
Lalit Maganti79f2d7b2018-01-23 18:27:33 +0000120
Isabelle Taylor69faa902018-03-21 15:42:03 +0000121void FakeProducer::OnTracingStart() {}
122
123void FakeProducer::OnTracingStop() {}
124
Lalit Maganti79f2d7b2018-01-23 18:27:33 +0000125} // namespace perfetto