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