Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 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> |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 21 | #include <random> |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 24 | #include "perfetto/ext/base/thread_checker.h" |
Primiano Tucci | 10c9e9e | 2021-01-08 13:04:40 +0100 | [diff] [blame] | 25 | #include "perfetto/ext/base/unix_socket.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 26 | #include "perfetto/ext/tracing/core/producer.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 27 | #include "perfetto/ext/tracing/ipc/producer_ipc_client.h" |
Primiano Tucci | 0f9e022 | 2019-06-05 09:36:41 +0100 | [diff] [blame] | 28 | #include "perfetto/tracing/core/data_source_descriptor.h" |
| 29 | #include "perfetto/tracing/core/trace_config.h" |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 30 | #include "src/base/test/test_task_runner.h" |
| 31 | |
| 32 | namespace perfetto { |
| 33 | |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 34 | namespace protos { |
| 35 | namespace gen { |
| 36 | class TestConfig; |
| 37 | } // namespace gen |
| 38 | } // namespace protos |
| 39 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 40 | class FakeProducer : public Producer { |
| 41 | public: |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 42 | explicit FakeProducer(const std::string& name, base::TaskRunner* task_runner); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 43 | ~FakeProducer() override; |
| 44 | |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 45 | void Connect(const char* socket_name, |
Primiano Tucci | bbe68be | 2020-04-16 22:17:12 +0100 | [diff] [blame] | 46 | std::function<void()> on_connect, |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 47 | std::function<void()> on_setup_data_source_instance, |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 48 | std::function<void()> on_create_data_source_instance, |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 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 = [] {}); |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 59 | |
| 60 | // Produces a batch of events (as configured in the DataSourceConfig) and |
| 61 | // posts a callback when the service acknowledges the commit. |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 62 | void ProduceEventBatch(std::function<void()> callback = [] {}); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 63 | |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 64 | void RegisterDataSource(const DataSourceDescriptor&); |
| 65 | void CommitData(const CommitDataRequest&, std::function<void()> callback); |
Primiano Tucci | bbe68be | 2020-04-16 22:17:12 +0100 | [diff] [blame] | 66 | void Sync(std::function<void()> callback); |
Primiano Tucci | f26cb9c | 2022-01-18 23:19:36 +0000 | [diff] [blame^] | 67 | void ActivateTrigger(const std::string& trigger_name); |
Primiano Tucci | 9c41ceb | 2020-04-14 13:23:01 +0100 | [diff] [blame] | 68 | |
Eric Seckler | 326a3d3 | 2020-02-04 11:24:56 +0000 | [diff] [blame] | 69 | bool IsShmemProvidedByProducer() const { |
| 70 | return endpoint_->IsShmemProvidedByProducer(); |
| 71 | } |
| 72 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 73 | // Producer implementation. |
| 74 | void OnConnect() override; |
| 75 | void OnDisconnect() override; |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 76 | void SetupDataSource(DataSourceInstanceID, |
| 77 | const DataSourceConfig& source_config) override; |
Primiano Tucci | afb72b5 | 2018-09-25 09:37:24 +0100 | [diff] [blame] | 78 | void StartDataSource(DataSourceInstanceID, |
| 79 | const DataSourceConfig& source_config) override; |
| 80 | void StopDataSource(DataSourceInstanceID) override; |
Primiano Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 81 | void OnTracingSetup() override; |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 82 | void Flush(FlushRequestID, const DataSourceInstanceID*, size_t) override; |
Ryan Savitski | bdaa962 | 2019-05-13 10:55:32 +0100 | [diff] [blame] | 83 | void ClearIncrementalState(const DataSourceInstanceID* /*data_source_ids*/, |
| 84 | size_t /*num_data_sources*/) override {} |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 85 | |
Stephen Nusko | 1af720e | 2020-11-18 14:04:16 -0500 | [diff] [blame] | 86 | // For testing, access to the fd used to communicate with the TracingService. |
Primiano Tucci | 10c9e9e | 2021-01-08 13:04:40 +0100 | [diff] [blame] | 87 | base::SocketHandle unix_socket_fd(); |
Stephen Nusko | 1af720e | 2020-11-18 14:04:16 -0500 | [diff] [blame] | 88 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 89 | private: |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 90 | void SetupFromConfig(const protos::gen::TestConfig& config); |
| 91 | void EmitEventBatchOnTaskRunner(std::function<void()> callback); |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 92 | |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 93 | base::ThreadChecker thread_checker_; |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 94 | std::string name_; |
Eric Seckler | 526921b | 2020-02-18 11:44:30 +0000 | [diff] [blame] | 95 | base::TaskRunner* task_runner_ = nullptr; |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 96 | std::minstd_rand0 rnd_engine_; |
Primiano Tucci | f026990 | 2018-04-13 11:13:19 +0100 | [diff] [blame] | 97 | uint32_t message_size_ = 0; |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 98 | uint32_t message_count_ = 0; |
| 99 | uint32_t max_messages_per_second_ = 0; |
Primiano Tucci | bbe68be | 2020-04-16 22:17:12 +0100 | [diff] [blame] | 100 | std::function<void()> on_connect_; |
Stephen Nusko | e823811 | 2019-04-09 18:37:00 +0100 | [diff] [blame] | 101 | std::function<void()> on_setup_data_source_instance_; |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 102 | std::function<void()> on_create_data_source_instance_; |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 103 | std::unique_ptr<TracingService::ProducerEndpoint> endpoint_; |
Lalit Maganti | bfc3d3e | 2018-03-22 20:28:38 +0000 | [diff] [blame] | 104 | std::unique_ptr<TraceWriter> trace_writer_; |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace perfetto |
| 108 | |
| 109 | #endif // TEST_FAKE_PRODUCER_H_ |