1 /*
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>
21 #include <random>
22 #include <string>
23 
24 #include "perfetto/ext/base/thread_checker.h"
25 #include "perfetto/ext/base/unix_socket.h"
26 #include "perfetto/ext/tracing/core/producer.h"
27 #include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
28 #include "perfetto/tracing/core/data_source_descriptor.h"
29 #include "perfetto/tracing/core/trace_config.h"
30 #include "src/base/test/test_task_runner.h"
31 
32 namespace perfetto {
33 
34 namespace protos {
35 namespace gen {
36 class TestConfig;
37 }  // namespace gen
38 }  // namespace protos
39 
40 class FakeProducer : public Producer {
41  public:
42   explicit FakeProducer(const std::string& name, base::TaskRunner* task_runner);
43   ~FakeProducer() override;
44 
45   void Connect(const char* socket_name,
46                std::function<void()> on_connect,
47                std::function<void()> on_setup_data_source_instance,
48                std::function<void()> on_create_data_source_instance,
49                std::unique_ptr<SharedMemory> shm = nullptr,
50                std::unique_ptr<SharedMemoryArbiter> shm_arbiter = nullptr);
51 
52   // Produces a batch of events (as configured by the passed config) before the
53   // producer is connected to the service using the provided unbound arbiter.
54   // Posts |callback| once the data was written. May only be called once.
55   void ProduceStartupEventBatch(
56       const protos::gen::TestConfig& config,
57       SharedMemoryArbiter* arbiter,
58       std::function<void()> callback = [] {});
59 
60   // Produces a batch of events (as configured in the DataSourceConfig) and
61   // posts a callback when the service acknowledges the commit.
62   void ProduceEventBatch(std::function<void()> callback = [] {});
63 
64   void RegisterDataSource(const DataSourceDescriptor&);
65   void CommitData(const CommitDataRequest&, std::function<void()> callback);
66   void Sync(std::function<void()> callback);
67 
IsShmemProvidedByProducer()68   bool IsShmemProvidedByProducer() const {
69     return endpoint_->IsShmemProvidedByProducer();
70   }
71 
72   // Producer implementation.
73   void OnConnect() override;
74   void OnDisconnect() override;
75   void SetupDataSource(DataSourceInstanceID,
76                        const DataSourceConfig& source_config) override;
77   void StartDataSource(DataSourceInstanceID,
78                        const DataSourceConfig& source_config) override;
79   void StopDataSource(DataSourceInstanceID) override;
80   void OnTracingSetup() override;
81   void Flush(FlushRequestID, const DataSourceInstanceID*, size_t) override;
ClearIncrementalState(const DataSourceInstanceID *,size_t)82   void ClearIncrementalState(const DataSourceInstanceID* /*data_source_ids*/,
83                              size_t /*num_data_sources*/) override {}
84 
85   // For testing, access to the fd used to communicate with the TracingService.
86   base::SocketHandle unix_socket_fd();
87 
88  private:
89   void SetupFromConfig(const protos::gen::TestConfig& config);
90   void EmitEventBatchOnTaskRunner(std::function<void()> callback);
91 
92   base::ThreadChecker thread_checker_;
93   std::string name_;
94   base::TaskRunner* task_runner_ = nullptr;
95   std::minstd_rand0 rnd_engine_;
96   uint32_t message_size_ = 0;
97   uint32_t message_count_ = 0;
98   uint32_t max_messages_per_second_ = 0;
99   std::function<void()> on_connect_;
100   std::function<void()> on_setup_data_source_instance_;
101   std::function<void()> on_create_data_source_instance_;
102   std::unique_ptr<TracingService::ProducerEndpoint> endpoint_;
103   std::unique_ptr<TraceWriter> trace_writer_;
104 };
105 
106 }  // namespace perfetto
107 
108 #endif  // TEST_FAKE_PRODUCER_H_
109