test_helper.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_TEST_HELPER_H_
  17. #define TEST_TEST_HELPER_H_
  18. #include "perfetto/ext/base/scoped_file.h"
  19. #include "perfetto/ext/base/thread_task_runner.h"
  20. #include "perfetto/ext/tracing/core/consumer.h"
  21. #include "perfetto/ext/tracing/core/shared_memory_arbiter.h"
  22. #include "perfetto/ext/tracing/core/trace_packet.h"
  23. #include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
  24. #include "perfetto/ext/tracing/ipc/service_ipc_host.h"
  25. #include "perfetto/tracing/core/trace_config.h"
  26. #include "src/base/test/test_task_runner.h"
  27. #include "src/traced/probes/probes_producer.h"
  28. #include "src/tracing/ipc/posix_shared_memory.h"
  29. #include "test/fake_producer.h"
  30. #include "protos/perfetto/trace/trace_packet.gen.h"
  31. namespace perfetto {
  32. // This is used only in daemon starting integrations tests.
  33. class ServiceThread {
  34. public:
  35. ServiceThread(const std::string& producer_socket,
  36. const std::string& consumer_socket)
  37. : producer_socket_(producer_socket), consumer_socket_(consumer_socket) {}
  38. ~ServiceThread() {
  39. if (!runner_)
  40. return;
  41. runner_->PostTaskAndWaitForTesting([this]() { svc_.reset(); });
  42. }
  43. void Start() {
  44. runner_ = base::ThreadTaskRunner::CreateAndStart("perfetto.svc");
  45. runner_->PostTaskAndWaitForTesting([this]() {
  46. svc_ = ServiceIPCHost::CreateInstance(runner_->get());
  47. unlink(producer_socket_.c_str());
  48. unlink(consumer_socket_.c_str());
  49. bool res =
  50. svc_->Start(producer_socket_.c_str(), consumer_socket_.c_str());
  51. PERFETTO_CHECK(res);
  52. });
  53. }
  54. base::ThreadTaskRunner* runner() { return runner_ ? &*runner_ : nullptr; }
  55. private:
  56. base::Optional<base::ThreadTaskRunner> runner_; // Keep first.
  57. std::string producer_socket_;
  58. std::string consumer_socket_;
  59. std::unique_ptr<ServiceIPCHost> svc_;
  60. };
  61. // This is used only in daemon starting integrations tests.
  62. class ProbesProducerThread {
  63. public:
  64. ProbesProducerThread(const std::string& producer_socket)
  65. : producer_socket_(producer_socket) {}
  66. ~ProbesProducerThread() {
  67. if (!runner_)
  68. return;
  69. runner_->PostTaskAndWaitForTesting([this]() { producer_.reset(); });
  70. }
  71. void Connect() {
  72. runner_ = base::ThreadTaskRunner::CreateAndStart("perfetto.prd.probes");
  73. runner_->PostTaskAndWaitForTesting([this]() {
  74. producer_.reset(new ProbesProducer());
  75. producer_->ConnectWithRetries(producer_socket_.c_str(), runner_->get());
  76. });
  77. }
  78. private:
  79. base::Optional<base::ThreadTaskRunner> runner_; // Keep first.
  80. std::string producer_socket_;
  81. std::unique_ptr<ProbesProducer> producer_;
  82. };
  83. class FakeProducerThread {
  84. public:
  85. FakeProducerThread(const std::string& producer_socket,
  86. std::function<void()> connect_callback,
  87. std::function<void()> setup_callback,
  88. std::function<void()> start_callback)
  89. : producer_socket_(producer_socket),
  90. connect_callback_(std::move(connect_callback)),
  91. setup_callback_(std::move(setup_callback)),
  92. start_callback_(std::move(start_callback)) {
  93. runner_ = base::ThreadTaskRunner::CreateAndStart("perfetto.prd.fake");
  94. runner_->PostTaskAndWaitForTesting([this]() {
  95. producer_.reset(
  96. new FakeProducer("android.perfetto.FakeProducer", runner_->get()));
  97. });
  98. }
  99. ~FakeProducerThread() {
  100. runner_->PostTaskAndWaitForTesting([this]() { producer_.reset(); });
  101. }
  102. void Connect() {
  103. runner_->PostTaskAndWaitForTesting([this]() {
  104. producer_->Connect(producer_socket_.c_str(), std::move(connect_callback_),
  105. std::move(setup_callback_), std::move(start_callback_),
  106. std::move(shm_), std::move(shm_arbiter_));
  107. });
  108. }
  109. base::ThreadTaskRunner* runner() { return runner_ ? &*runner_ : nullptr; }
  110. FakeProducer* producer() { return producer_.get(); }
  111. void CreateProducerProvidedSmb() {
  112. PosixSharedMemory::Factory factory;
  113. shm_ = factory.CreateSharedMemory(1024 * 1024);
  114. shm_arbiter_ = SharedMemoryArbiter::CreateUnboundInstance(shm_.get(), 4096);
  115. }
  116. void ProduceStartupEventBatch(const protos::gen::TestConfig& config,
  117. std::function<void()> callback) {
  118. PERFETTO_CHECK(shm_arbiter_);
  119. producer_->ProduceStartupEventBatch(config, shm_arbiter_.get(), callback);
  120. }
  121. private:
  122. base::Optional<base::ThreadTaskRunner> runner_; // Keep first.
  123. std::string producer_socket_;
  124. std::unique_ptr<FakeProducer> producer_;
  125. std::function<void()> connect_callback_;
  126. std::function<void()> setup_callback_;
  127. std::function<void()> start_callback_;
  128. std::unique_ptr<SharedMemory> shm_;
  129. std::unique_ptr<SharedMemoryArbiter> shm_arbiter_;
  130. };
  131. class TestHelper : public Consumer {
  132. public:
  133. static const char* GetConsumerSocketName();
  134. static const char* GetProducerSocketName();
  135. explicit TestHelper(base::TestTaskRunner* task_runner);
  136. // Consumer implementation.
  137. void OnConnect() override;
  138. void OnDisconnect() override;
  139. void OnTracingDisabled() override;
  140. void OnTraceData(std::vector<TracePacket> packets, bool has_more) override;
  141. void OnDetach(bool) override;
  142. void OnAttach(bool, const TraceConfig&) override;
  143. void OnTraceStats(bool, const TraceStats&) override;
  144. void OnObservableEvents(const ObservableEvents&) override;
  145. void StartServiceIfRequired();
  146. // Connects the producer and waits that the service has seen the
  147. // RegisterDataSource() call.
  148. FakeProducer* ConnectFakeProducer();
  149. void ConnectConsumer();
  150. void StartTracing(const TraceConfig& config,
  151. base::ScopedFile = base::ScopedFile());
  152. void DisableTracing();
  153. void FlushAndWait(uint32_t timeout_ms);
  154. void ReadData(uint32_t read_count = 0);
  155. void DetachConsumer(const std::string& key);
  156. bool AttachConsumer(const std::string& key);
  157. void CreateProducerProvidedSmb();
  158. bool IsShmemProvidedByProducer();
  159. void ProduceStartupEventBatch(const protos::gen::TestConfig& config);
  160. void WaitForConsumerConnect();
  161. void WaitForProducerSetup();
  162. void WaitForProducerEnabled();
  163. void WaitForTracingDisabled(uint32_t timeout_ms = 5000);
  164. void WaitForReadData(uint32_t read_count = 0, uint32_t timeout_ms = 5000);
  165. void SyncAndWaitProducer();
  166. TracingServiceState QueryServiceStateAndWait();
  167. std::string AddID(const std::string& checkpoint) {
  168. return checkpoint + "." + std::to_string(instance_num_);
  169. }
  170. std::function<void()> CreateCheckpoint(const std::string& checkpoint) {
  171. return task_runner_->CreateCheckpoint(AddID(checkpoint));
  172. }
  173. void RunUntilCheckpoint(const std::string& checkpoint,
  174. uint32_t timeout_ms = 5000) {
  175. return task_runner_->RunUntilCheckpoint(AddID(checkpoint), timeout_ms);
  176. }
  177. std::function<void()> WrapTask(const std::function<void()>& function);
  178. base::ThreadTaskRunner* service_thread() { return service_thread_.runner(); }
  179. base::ThreadTaskRunner* producer_thread() {
  180. return fake_producer_thread_.runner();
  181. }
  182. const std::vector<protos::gen::TracePacket>& full_trace() {
  183. return full_trace_;
  184. }
  185. const std::vector<protos::gen::TracePacket>& trace() { return trace_; }
  186. private:
  187. static uint64_t next_instance_num_;
  188. uint64_t instance_num_;
  189. base::TestTaskRunner* task_runner_ = nullptr;
  190. int cur_consumer_num_ = 0;
  191. std::function<void()> on_connect_callback_;
  192. std::function<void()> on_packets_finished_callback_;
  193. std::function<void()> on_stop_tracing_callback_;
  194. std::function<void()> on_detach_callback_;
  195. std::function<void(bool)> on_attach_callback_;
  196. std::vector<protos::gen::TracePacket> full_trace_;
  197. std::vector<protos::gen::TracePacket> trace_;
  198. ServiceThread service_thread_;
  199. FakeProducerThread fake_producer_thread_;
  200. std::unique_ptr<TracingService::ConsumerEndpoint> endpoint_; // Keep last.
  201. };
  202. } // namespace perfetto
  203. #endif // TEST_TEST_HELPER_H_