1 /* 2 * Copyright (C) 2019 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 SRC_TRACING_TEST_API_TEST_SUPPORT_H_ 18 #define SRC_TRACING_TEST_API_TEST_SUPPORT_H_ 19 20 // This header is intended to be used only for api_integrationtest.cc and solves 21 // the following problem: api_integrationtest.cc doesn't pull any non-public 22 // perfetto header, to spot accidental public->non-public dependencies. 23 // Sometimes, however, we need to use some internal perfetto code for the test 24 // itself. This header exposes wrapper functions to achieve that without leaking 25 // internal headers. 26 27 // IMPORTANT: This header must not pull any non-public perfetto header. 28 29 #include <stdint.h> 30 31 #include <functional> 32 #include <string> 33 34 #include "perfetto/tracing.h" 35 36 namespace perfetto { 37 namespace test { 38 39 int32_t GetCurrentProcessId(); 40 41 // RAII wrapper to start and stop an in process system service. Only one at a 42 // time can be started. 43 class SystemService { 44 public: 45 static SystemService Start(); 46 SystemService() = default; SystemService(SystemService && other)47 SystemService(SystemService&& other) noexcept { *this = std::move(other); } 48 SystemService& operator=(SystemService&&) noexcept; 49 ~SystemService()50 ~SystemService() { Clean(); } 51 52 // Returns true if this SystemService has been started successfully and can be 53 // used. valid()54 bool valid() const { return valid_; } 55 56 void Clean(); 57 58 // Restarts this SystemService. Producer and consumers will be disconnected. 59 void Restart(); 60 61 private: 62 SystemService(const SystemService&) = delete; 63 SystemService& operator=(const SystemService&) = delete; 64 bool valid_ = false; 65 }; 66 67 void SyncProducers(); 68 69 void SetBatchCommitsDuration(uint32_t batch_commits_duration_ms, 70 BackendType backend_type); 71 72 bool EnableDirectSMBPatching(BackendType backend_type); 73 74 void DisableReconnectLimit(); 75 76 struct TestTempFile { 77 int fd; 78 std::string path; 79 }; 80 81 // The caller must close(2) the returned TempFile.fd. 82 TestTempFile CreateTempFile(); 83 84 class DataSourceInternalForTest { 85 public: 86 template <typename DerivedDataSource> ClearTlsState()87 static void ClearTlsState() { 88 internal::DataSourceThreadLocalState*& tls_state = 89 DerivedDataSource::tls_state_; 90 if (tls_state) { 91 tls_state = nullptr; 92 } 93 } 94 }; 95 96 class TracingMuxerImplInternalsForTest { 97 public: 98 static bool DoesSystemBackendHaveSMB(); 99 static void ClearIncrementalState(); 100 101 template <typename DerivedDataSource> ClearDataSourceTlsStateOnReset()102 static void ClearDataSourceTlsStateOnReset() { 103 AppendResetForTestingCallback( 104 [] { DataSourceInternalForTest::ClearTlsState<DerivedDataSource>(); }); 105 } 106 107 private: 108 static void AppendResetForTestingCallback(std::function<void()>); 109 }; 110 111 } // namespace test 112 } // namespace perfetto 113 114 #endif // SRC_TRACING_TEST_API_TEST_SUPPORT_H_ 115