fake_producer.h 3.8 KB

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