xref: /aosp_15_r20/external/perfetto/test/fake_producer.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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   void ActivateTrigger(const std::string& trigger_name);
68 
IsShmemProvidedByProducer()69   bool IsShmemProvidedByProducer() const {
70     return endpoint_->IsShmemProvidedByProducer();
71   }
72 
73   // Producer implementation.
74   void OnConnect() override;
75   void OnDisconnect() override;
76   void SetupDataSource(DataSourceInstanceID,
77                        const DataSourceConfig& source_config) override;
78   void StartDataSource(DataSourceInstanceID,
79                        const DataSourceConfig& source_config) override;
80   void StopDataSource(DataSourceInstanceID) override;
81   void OnTracingSetup() override;
82   void Flush(FlushRequestID,
83              const DataSourceInstanceID*,
84              size_t,
85              FlushFlags) override;
ClearIncrementalState(const DataSourceInstanceID *,size_t)86   void ClearIncrementalState(const DataSourceInstanceID* /*data_source_ids*/,
87                              size_t /*num_data_sources*/) override {}
88 
89   // For testing, access to the fd used to communicate with the TracingService.
90   base::SocketHandle unix_socket_fd();
91 
92  private:
93   void SetupFromConfig(const protos::gen::TestConfig& config);
94   void EmitEventBatchOnTaskRunner(std::function<void()> callback);
95 
96   base::ThreadChecker thread_checker_;
97   std::string name_;
98   base::TaskRunner* task_runner_ = nullptr;
99   std::minstd_rand0 rnd_engine_;
100   uint32_t message_size_ = 0;
101   uint32_t message_count_ = 0;
102   uint32_t max_messages_per_second_ = 0;
103   std::function<void()> on_connect_;
104   std::function<void()> on_setup_data_source_instance_;
105   std::function<void()> on_create_data_source_instance_;
106   std::unique_ptr<TracingService::ProducerEndpoint> endpoint_;
107   std::unique_ptr<TraceWriter> trace_writer_;
108 };
109 
110 }  // namespace perfetto
111 
112 #endif  // TEST_FAKE_PRODUCER_H_
113