1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2023 The Android Open Source Project 3*38e8c45fSAndroid Build Coastguard Worker * 4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*38e8c45fSAndroid Build Coastguard Worker * 8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*38e8c45fSAndroid Build Coastguard Worker * 10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License. 15*38e8c45fSAndroid Build Coastguard Worker */ 16*38e8c45fSAndroid Build Coastguard Worker 17*38e8c45fSAndroid Build Coastguard Worker #pragma once 18*38e8c45fSAndroid Build Coastguard Worker 19*38e8c45fSAndroid Build Coastguard Worker #include "TransactionTracing.h" 20*38e8c45fSAndroid Build Coastguard Worker 21*38e8c45fSAndroid Build Coastguard Worker #include <perfetto/tracing.h> 22*38e8c45fSAndroid Build Coastguard Worker 23*38e8c45fSAndroid Build Coastguard Worker namespace android { 24*38e8c45fSAndroid Build Coastguard Worker 25*38e8c45fSAndroid Build Coastguard Worker /* 26*38e8c45fSAndroid Build Coastguard Worker * Thread local storage used for fast (lock free) read of data source's properties. 27*38e8c45fSAndroid Build Coastguard Worker * 28*38e8c45fSAndroid Build Coastguard Worker */ 29*38e8c45fSAndroid Build Coastguard Worker struct TransactionDataSourceTlsState { 30*38e8c45fSAndroid Build Coastguard Worker template <typename TraceContext> TransactionDataSourceTlsStateTransactionDataSourceTlsState31*38e8c45fSAndroid Build Coastguard Worker explicit TransactionDataSourceTlsState(const TraceContext& trace_context) { 32*38e8c45fSAndroid Build Coastguard Worker auto dataSource = trace_context.GetDataSourceLocked(); 33*38e8c45fSAndroid Build Coastguard Worker mMode = dataSource.valid() ? dataSource->GetMode() 34*38e8c45fSAndroid Build Coastguard Worker : TransactionTracing::Mode::MODE_CONTINUOUS; 35*38e8c45fSAndroid Build Coastguard Worker } 36*38e8c45fSAndroid Build Coastguard Worker 37*38e8c45fSAndroid Build Coastguard Worker TransactionTracing::Mode mMode; 38*38e8c45fSAndroid Build Coastguard Worker }; 39*38e8c45fSAndroid Build Coastguard Worker 40*38e8c45fSAndroid Build Coastguard Worker struct TransactionDataSourceTraits : public perfetto::DefaultDataSourceTraits { 41*38e8c45fSAndroid Build Coastguard Worker using TlsStateType = TransactionDataSourceTlsState; 42*38e8c45fSAndroid Build Coastguard Worker }; 43*38e8c45fSAndroid Build Coastguard Worker 44*38e8c45fSAndroid Build Coastguard Worker /* 45*38e8c45fSAndroid Build Coastguard Worker * Defines the Perfetto custom data source 'android.surfaceflinger.transactions'. 46*38e8c45fSAndroid Build Coastguard Worker * 47*38e8c45fSAndroid Build Coastguard Worker * Registers the data source with Perfetto, listens to Perfetto events (setup/start/flush/stop) 48*38e8c45fSAndroid Build Coastguard Worker * and writes trace packets to Perfetto. 49*38e8c45fSAndroid Build Coastguard Worker * 50*38e8c45fSAndroid Build Coastguard Worker */ 51*38e8c45fSAndroid Build Coastguard Worker class TransactionDataSource 52*38e8c45fSAndroid Build Coastguard Worker : public perfetto::DataSource<TransactionDataSource, TransactionDataSourceTraits> { 53*38e8c45fSAndroid Build Coastguard Worker public: 54*38e8c45fSAndroid Build Coastguard Worker static void Initialize(TransactionTracing&); 55*38e8c45fSAndroid Build Coastguard Worker static void UnregisterTransactionTracing(); 56*38e8c45fSAndroid Build Coastguard Worker void OnSetup(const SetupArgs&) override; 57*38e8c45fSAndroid Build Coastguard Worker void OnStart(const StartArgs&) override; 58*38e8c45fSAndroid Build Coastguard Worker void OnFlush(const FlushArgs&) override; 59*38e8c45fSAndroid Build Coastguard Worker void OnStop(const StopArgs&) override; 60*38e8c45fSAndroid Build Coastguard Worker TransactionTracing::Mode GetMode() const; 61*38e8c45fSAndroid Build Coastguard Worker 62*38e8c45fSAndroid Build Coastguard Worker static constexpr auto* kName = "android.surfaceflinger.transactions"; 63*38e8c45fSAndroid Build Coastguard Worker static constexpr perfetto::BufferExhaustedPolicy kBufferExhaustedPolicy = 64*38e8c45fSAndroid Build Coastguard Worker perfetto::BufferExhaustedPolicy::kStall; 65*38e8c45fSAndroid Build Coastguard Worker static constexpr bool kRequiresCallbacksUnderLock = false; 66*38e8c45fSAndroid Build Coastguard Worker 67*38e8c45fSAndroid Build Coastguard Worker private: 68*38e8c45fSAndroid Build Coastguard Worker static std::atomic<TransactionTracing*> mTransactionTracing; 69*38e8c45fSAndroid Build Coastguard Worker TransactionTracing::Mode mMode; 70*38e8c45fSAndroid Build Coastguard Worker }; 71*38e8c45fSAndroid Build Coastguard Worker 72*38e8c45fSAndroid Build Coastguard Worker } // namespace android 73*38e8c45fSAndroid Build Coastguard Worker 74*38e8c45fSAndroid Build Coastguard Worker PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(android::TransactionDataSource); 75