blob: be8373b4acd57df324109900f695d90f17baa856 [file] [log] [blame]
Kean Mariotti3e68a202023-04-19 13:41:55 +00001/*
2 * Copyright 2023 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#pragma once
18
19#include "TransactionTracing.h"
20
21#include <perfetto/tracing.h>
22
23namespace android {
24
25/*
26 * Thread local storage used for fast (lock free) read of data source's properties.
27 *
28 */
29struct TransactionDataSourceTlsState {
30 template <typename TraceContext>
31 explicit TransactionDataSourceTlsState(const TraceContext& trace_context) {
32 auto dataSource = trace_context.GetDataSourceLocked();
33 mMode = dataSource.valid() ? dataSource->GetMode()
34 : TransactionTracing::Mode::MODE_CONTINUOUS;
35 }
36
37 TransactionTracing::Mode mMode;
38};
39
40struct TransactionDataSourceTraits : public perfetto::DefaultDataSourceTraits {
41 using TlsStateType = TransactionDataSourceTlsState;
42};
43
44/*
45 * Defines the Perfetto custom data source 'android.surfaceflinger.transactions'.
46 *
47 * Registers the data source with Perfetto, listens to Perfetto events (setup/start/flush/stop)
48 * and writes trace packets to Perfetto.
49 *
50 */
51class TransactionDataSource
52 : public perfetto::DataSource<TransactionDataSource, TransactionDataSourceTraits> {
53public:
54 static void Initialize(TransactionTracing&);
55 static void UnregisterTransactionTracing();
56 void OnSetup(const SetupArgs&) override;
57 void OnStart(const StartArgs&) override;
58 void OnFlush(const FlushArgs&) override;
59 void OnStop(const StopArgs&) override;
60 TransactionTracing::Mode GetMode() const;
61
62 static constexpr auto* kName = "android.surfaceflinger.transactions";
63 static constexpr perfetto::BufferExhaustedPolicy kBufferExhaustedPolicy =
64 perfetto::BufferExhaustedPolicy::kStall;
65 static constexpr bool kRequiresCallbacksUnderLock = false;
66
67private:
68 static std::atomic<TransactionTracing*> mTransactionTracing;
69 TransactionTracing::Mode mMode;
70};
71
72} // namespace android
73
74PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(android::TransactionDataSource);