blob: f71d90e4badd485823abf17e2512e0b76e17c220 [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#ifndef TEST_FAKE_PRODUCER_H_
18#define TEST_FAKE_PRODUCER_H_
19
20#include <memory>
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000021#include <random>
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000022#include <string>
23
Primiano Tucci2c5488f2019-06-01 03:27:28 +010024#include "perfetto/ext/base/thread_checker.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010025#include "perfetto/ext/tracing/core/producer.h"
Primiano Tucci2c5488f2019-06-01 03:27:28 +010026#include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
Primiano Tucci0f9e0222019-06-05 09:36:41 +010027#include "perfetto/tracing/core/data_source_descriptor.h"
28#include "perfetto/tracing/core/trace_config.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000029#include "src/base/test/test_task_runner.h"
30
31namespace perfetto {
32
Eric Seckler526921b2020-02-18 11:44:30 +000033namespace protos {
34namespace gen {
35class TestConfig;
36} // namespace gen
37} // namespace protos
38
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000039class FakeProducer : public Producer {
40 public:
Eric Seckler526921b2020-02-18 11:44:30 +000041 explicit FakeProducer(const std::string& name, base::TaskRunner* task_runner);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000042 ~FakeProducer() override;
43
Sami Kyostila32e0b542018-02-14 08:55:43 +000044 void Connect(const char* socket_name,
Stephen Nuskoe8238112019-04-09 18:37:00 +010045 std::function<void()> on_setup_data_source_instance,
Eric Seckler326a3d32020-02-04 11:24:56 +000046 std::function<void()> on_create_data_source_instance,
Eric Seckler526921b2020-02-18 11:44:30 +000047 std::unique_ptr<SharedMemory> shm = nullptr,
48 std::unique_ptr<SharedMemoryArbiter> shm_arbiter = nullptr);
49
50 // Produces a batch of events (as configured by the passed config) before the
51 // producer is connected to the service using the provided unbound arbiter.
52 // Posts |callback| once the data was written. May only be called once.
53 void ProduceStartupEventBatch(
54 const protos::gen::TestConfig& config,
55 SharedMemoryArbiter* arbiter,
56 std::function<void()> callback = [] {});
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000057
58 // Produces a batch of events (as configured in the DataSourceConfig) and
59 // posts a callback when the service acknowledges the commit.
Lalit Maganti36557d82018-04-11 14:36:17 +010060 void ProduceEventBatch(std::function<void()> callback = [] {});
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000061
Eric Seckler326a3d32020-02-04 11:24:56 +000062 bool IsShmemProvidedByProducer() const {
63 return endpoint_->IsShmemProvidedByProducer();
64 }
65
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000066 // Producer implementation.
67 void OnConnect() override;
68 void OnDisconnect() override;
Primiano Tucci674076d2018-10-01 10:41:09 +010069 void SetupDataSource(DataSourceInstanceID,
70 const DataSourceConfig& source_config) override;
Primiano Tucciafb72b52018-09-25 09:37:24 +010071 void StartDataSource(DataSourceInstanceID,
72 const DataSourceConfig& source_config) override;
73 void StopDataSource(DataSourceInstanceID) override;
Primiano Tuccidca727d2018-04-04 11:31:55 +020074 void OnTracingSetup() override;
Primiano Tuccid52e6272018-04-06 19:06:53 +020075 void Flush(FlushRequestID, const DataSourceInstanceID*, size_t) override;
Ryan Savitskibdaa9622019-05-13 10:55:32 +010076 void ClearIncrementalState(const DataSourceInstanceID* /*data_source_ids*/,
77 size_t /*num_data_sources*/) override {}
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000078
79 private:
Eric Seckler526921b2020-02-18 11:44:30 +000080 void SetupFromConfig(const protos::gen::TestConfig& config);
81 void EmitEventBatchOnTaskRunner(std::function<void()> callback);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000082
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000083 base::ThreadChecker thread_checker_;
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000084 std::string name_;
Eric Seckler526921b2020-02-18 11:44:30 +000085 base::TaskRunner* task_runner_ = nullptr;
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000086 std::minstd_rand0 rnd_engine_;
Primiano Tuccif0269902018-04-13 11:13:19 +010087 uint32_t message_size_ = 0;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010088 uint32_t message_count_ = 0;
89 uint32_t max_messages_per_second_ = 0;
Stephen Nuskoe8238112019-04-09 18:37:00 +010090 std::function<void()> on_setup_data_source_instance_;
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000091 std::function<void()> on_create_data_source_instance_;
Florian Mayer6a1a4d52018-06-08 16:47:07 +010092 std::unique_ptr<TracingService::ProducerEndpoint> endpoint_;
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000093 std::unique_ptr<TraceWriter> trace_writer_;
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000094};
95
96} // namespace perfetto
97
98#endif // TEST_FAKE_PRODUCER_H_