blob: 9d28744f12f40cca37a26227b4dde1803ad266b7 [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 Maganti8390e5b2018-03-23 10:46:05 +000024#include "perfetto/base/utils.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000025#include "perfetto/trace/test_event.pbzero.h"
26#include "perfetto/trace/trace_packet.pbzero.h"
27#include "perfetto/traced/traced.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000028#include "perfetto/tracing/core/trace_packet.h"
29#include "perfetto/tracing/core/trace_writer.h"
30
31namespace perfetto {
32
33FakeProducer::FakeProducer(const std::string& name) : name_(name) {}
34FakeProducer::~FakeProducer() = default;
35
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000036void 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 Kyostila32e0b542018-02-14 08:55:43 +000041 task_runner_ = task_runner;
Isabelle Taylor86262cb2018-03-27 16:00:54 +010042 endpoint_ = ProducerIPCClient::Connect(
Primiano Tucci5ae66da2018-03-28 15:57:34 +010043 socket_name, this, "android.perfetto.FakeProducer", task_runner);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000044 on_create_data_source_instance_ = std::move(on_create_data_source_instance);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000045}
46
47void FakeProducer::OnConnect() {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000048 PERFETTO_DCHECK_THREAD(thread_checker_);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000049 DataSourceDescriptor descriptor;
50 descriptor.set_name(name_);
Primiano Tucci9daa4832018-03-28 23:28:17 +010051 endpoint_->RegisterDataSource(descriptor);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000052}
53
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000054void FakeProducer::OnDisconnect() {
55 PERFETTO_DCHECK_THREAD(thread_checker_);
56 FAIL() << "Producer unexpectedly disconnected from the service";
57}
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000058
59void FakeProducer::CreateDataSourceInstance(
60 DataSourceInstanceID,
61 const DataSourceConfig& source_config) {
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000062 PERFETTO_DCHECK_THREAD(thread_checker_);
63 trace_writer_ = endpoint_->CreateTraceWriter(
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000064 static_cast<BufferID>(source_config.target_buffer()));
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000065 rnd_engine_ = std::minstd_rand0(source_config.for_testing().seed());
66 message_count_ = source_config.for_testing().message_count();
Lalit Maganti8390e5b2018-03-23 10:46:05 +000067 message_size_ = source_config.for_testing().message_size();
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000068 task_runner_->PostTask(on_create_data_source_instance_);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000069}
70
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000071void FakeProducer::TearDownDataSourceInstance(DataSourceInstanceID) {
72 PERFETTO_DCHECK_THREAD(thread_checker_);
73 trace_writer_.reset();
74}
75
76// Note: this will called on a different thread.
77void FakeProducer::ProduceEventBatch(std::function<void()> callback) {
78 task_runner_->PostTask([this, callback] {
79 PERFETTO_CHECK(trace_writer_);
Primiano Tucci5ae66da2018-03-28 15:57:34 +010080 PERFETTO_CHECK(message_size_ > 1);
Lalit Maganti8390e5b2018-03-23 10:46:05 +000081 std::unique_ptr<char, base::FreeDeleter> payload(
Primiano Tucci5ae66da2018-03-28 15:57:34 +010082 static_cast<char*>(malloc(message_size_)));
83 memset(payload.get(), '.', message_size_);
84 payload.get()[message_size_ - 1] = 0;
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000085 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 Tucci5ae66da2018-03-28 15:57:34 +010088 handle->set_for_testing()->set_str(payload.get(), message_size_);
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000089 }
90 trace_writer_->Flush(callback);
91 });
92}
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000093
Isabelle Taylor69faa902018-03-21 15:42:03 +000094void FakeProducer::OnTracingStart() {}
95
96void FakeProducer::OnTracingStop() {}
97
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000098} // namespace perfetto