Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 <android-base/thread_annotations.h> |
| 20 | #include <layerproto/TransactionProto.h> |
| 21 | #include <utils/Errors.h> |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 22 | #include <utils/Singleton.h> |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 23 | #include <utils/Timers.h> |
| 24 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 25 | #include <mutex> |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 26 | #include <optional> |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 27 | #include <thread> |
| 28 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 29 | #include "FrontEnd/DisplayInfo.h" |
| 30 | #include "FrontEnd/LayerCreationArgs.h" |
| 31 | #include "FrontEnd/Update.h" |
Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 32 | #include "LocklessStack.h" |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 33 | #include "TransactionProtoParser.h" |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 34 | #include "TransactionRingBuffer.h" |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 35 | |
| 36 | using namespace android::surfaceflinger; |
| 37 | |
| 38 | namespace android { |
| 39 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 40 | class SurfaceFlinger; |
| 41 | class TransactionTracingTest; |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 42 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 43 | /* |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 44 | * Records all committed transactions into a ring buffer. |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 45 | * |
| 46 | * Transactions come in via the binder thread. They are serialized to proto |
| 47 | * and stored in a map using the transaction id as key. Main thread will |
| 48 | * pass the list of transaction ids that are committed every vsync and notify |
| 49 | * the tracing thread. The tracing thread will then wake up and add the |
| 50 | * committed transactions to the ring buffer. |
| 51 | * |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 52 | * The traced data can then be collected via: |
| 53 | * - Perfetto (preferred). |
| 54 | * - File system, after triggering the disk write through SF backdoor. This is legacy and is going |
| 55 | * to be phased out. |
| 56 | * |
| 57 | * The Perfetto custom data source TransactionDataSource is registered with perfetto and is used |
| 58 | * to listen to perfetto events (setup, start, stop, flush) and to write trace packets to perfetto. |
| 59 | * |
| 60 | * The user can configure/start/stop tracing via /system/bin/perfetto. |
| 61 | * |
| 62 | * Tracing can operate in the following modes. |
| 63 | * |
| 64 | * ACTIVE mode: |
| 65 | * The transactions ring buffer (starting state + following committed transactions) is written |
| 66 | * (only once) to perfetto when the 'start' event is received. |
| 67 | * Transactions are then written to perfetto each time they are committed. |
| 68 | * On the receiver side, the data source is to be configured to periodically |
| 69 | * flush data to disk providing virtually infinite storage. |
| 70 | * |
| 71 | * CONTINUOUS mode: |
| 72 | * Listens to the perfetto 'flush' event (e.g. when a bugreport is taken). |
| 73 | * When a 'flush' event is received, the ring buffer of transactions (starting state + following |
| 74 | * committed transactions) is written to perfetto. On the receiver side, the data source is to be |
| 75 | * configured with a dedicated buffer large enough to store all the flushed data. |
| 76 | * |
| 77 | * |
| 78 | * E.g. start active mode tracing: |
| 79 | * |
| 80 | adb shell perfetto \ |
| 81 | -c - --txt \ |
| 82 | -o /data/misc/perfetto-traces/trace \ |
| 83 | <<EOF |
| 84 | unique_session_name: "surfaceflinger_transactions_active" |
| 85 | buffers: { |
| 86 | size_kb: 1024 |
| 87 | fill_policy: RING_BUFFER |
| 88 | } |
| 89 | data_sources: { |
| 90 | config { |
| 91 | name: "android.surfaceflinger.transactions" |
| 92 | surfaceflinger_transactions_config: { |
| 93 | mode: MODE_ACTIVE |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | write_into_file: true |
| 98 | file_write_period_ms: 100 |
| 99 | EOF |
| 100 | * |
| 101 | * |
| 102 | * E.g. start continuous mode tracing: |
| 103 | * |
| 104 | adb shell perfetto \ |
| 105 | -c - --txt \ |
| 106 | -o /data/misc/perfetto-traces/trace \ |
| 107 | <<EOF |
| 108 | unique_session_name: "surfaceflinger_transactions_continuous" |
| 109 | buffers: { |
| 110 | size_kb: 1024 |
| 111 | fill_policy: RING_BUFFER |
| 112 | } |
| 113 | data_sources: { |
| 114 | config { |
| 115 | name: "android.surfaceflinger.transactions" |
| 116 | surfaceflinger_transactions_config: { |
| 117 | mode: MODE_CONTINUOUS |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | EOF |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 122 | * |
| 123 | */ |
| 124 | class TransactionTracing { |
| 125 | public: |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 126 | using Mode = perfetto::protos::pbzero::SurfaceFlingerTransactionsConfig::Mode; |
| 127 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 128 | TransactionTracing(); |
| 129 | ~TransactionTracing(); |
| 130 | |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 131 | // Start event from perfetto data source |
| 132 | void onStart(Mode mode); |
| 133 | // Flush event from perfetto data source |
| 134 | void onFlush(Mode mode); |
| 135 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 136 | void addQueuedTransaction(const TransactionState&); |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 137 | void addCommittedTransactions(int64_t vsyncId, nsecs_t commitTime, frontend::Update& update, |
| 138 | const frontend::DisplayInfos&, bool displayInfoChanged); |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 139 | status_t writeToFile(const std::string& filename = FILE_PATH); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 140 | // Return buffer contents as trace file proto |
| 141 | perfetto::protos::TransactionTraceFile writeToProto() EXCLUDES(mMainThreadLock); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 142 | void setBufferSize(size_t bufferSizeInBytes); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 143 | void onLayerRemoved(int layerId); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 144 | void dump(std::string&) const; |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 145 | // Wait until all the committed transactions for the specified vsync id are added to the buffer. |
| 146 | void flush() EXCLUDES(mMainThreadLock); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 147 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 148 | static constexpr auto CONTINUOUS_TRACING_BUFFER_SIZE = 512 * 1024; |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 149 | static constexpr auto LEGACY_ACTIVE_TRACING_BUFFER_SIZE = 100 * 1024 * 1024; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 150 | // version 1 - switching to support new frontend |
| 151 | static constexpr auto TRACING_VERSION = 1; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 152 | |
| 153 | private: |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 154 | friend class TransactionTraceWriter; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 155 | friend class TransactionTracingTest; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 156 | friend class SurfaceFlinger; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 157 | |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 158 | static constexpr auto DIR_NAME = "/data/misc/wmtrace/"; |
Vishnu Nair | 249fd7f | 2023-06-22 19:30:24 -0700 | [diff] [blame] | 159 | static constexpr auto FILE_NAME = "transactions_trace.winscope"; |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 160 | static constexpr auto FILE_PATH = "/data/misc/wmtrace/transactions_trace.winscope"; |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 161 | static std::string getFilePath(const std::string& prefix) { |
| 162 | return DIR_NAME + prefix + FILE_NAME; |
| 163 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 164 | |
| 165 | mutable std::mutex mTraceLock; |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 166 | TransactionRingBuffer<perfetto::protos::TransactionTraceFile, |
| 167 | perfetto::protos::TransactionTraceEntry> |
| 168 | mBuffer GUARDED_BY(mTraceLock); |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 169 | std::unordered_map<uint64_t, perfetto::protos::TransactionState> mQueuedTransactions |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 170 | GUARDED_BY(mTraceLock); |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 171 | LocklessStack<perfetto::protos::TransactionState> mTransactionQueue; |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 172 | nsecs_t mStartingTimestamp GUARDED_BY(mTraceLock); |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 173 | std::unordered_map<int, perfetto::protos::LayerCreationArgs> mCreatedLayers |
| 174 | GUARDED_BY(mTraceLock); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 175 | std::map<uint32_t /* layerId */, TracingLayerState> mStartingStates GUARDED_BY(mTraceLock); |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 176 | frontend::DisplayInfos mStartingDisplayInfos GUARDED_BY(mTraceLock); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 177 | |
| 178 | std::set<uint32_t /* layerId */> mRemovedLayerHandlesAtStart GUARDED_BY(mTraceLock); |
| 179 | TransactionProtoParser mProtoParser; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 180 | |
| 181 | // We do not want main thread to block so main thread will try to acquire mMainThreadLock, |
| 182 | // otherwise will push data to temporary container. |
| 183 | std::mutex mMainThreadLock; |
| 184 | std::thread mThread GUARDED_BY(mMainThreadLock); |
| 185 | bool mDone GUARDED_BY(mMainThreadLock) = false; |
| 186 | std::condition_variable mTransactionsAvailableCv; |
| 187 | std::condition_variable mTransactionsAddedToBufferCv; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 188 | struct CommittedUpdates { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 189 | std::vector<uint64_t> transactionIds; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 190 | std::vector<LayerCreationArgs> createdLayers; |
| 191 | std::vector<uint32_t> destroyedLayerHandles; |
| 192 | bool displayInfoChanged; |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 193 | frontend::DisplayInfos displayInfos; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 194 | int64_t vsyncId; |
| 195 | int64_t timestamp; |
| 196 | }; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 197 | std::vector<CommittedUpdates> mUpdates GUARDED_BY(mMainThreadLock); |
| 198 | std::vector<CommittedUpdates> mPendingUpdates; // only accessed by main thread |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 199 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 200 | std::vector<uint32_t /* layerId */> mDestroyedLayers GUARDED_BY(mMainThreadLock); |
| 201 | std::vector<uint32_t /* layerId */> mPendingDestroyedLayers; // only accessed by main thread |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 202 | int64_t mLastUpdatedVsyncId = -1; |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 203 | |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 204 | void writeRingBufferToPerfetto(TransactionTracing::Mode mode); |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 205 | perfetto::protos::TransactionTraceFile createTraceFileProto() const; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 206 | void loop(); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 207 | void addEntry(const std::vector<CommittedUpdates>& committedTransactions, |
| 208 | const std::vector<uint32_t>& removedLayers) EXCLUDES(mTraceLock); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 209 | int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock); |
| 210 | void tryPushToTracingThread() EXCLUDES(mMainThreadLock); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 211 | std::optional<perfetto::protos::TransactionTraceEntry> createStartingStateProtoLocked() |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 212 | REQUIRES(mTraceLock); |
| 213 | void updateStartingStateLocked(const perfetto::protos::TransactionTraceEntry& entry) |
| 214 | REQUIRES(mTraceLock); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 217 | class TransactionTraceWriter : public Singleton<TransactionTraceWriter> { |
| 218 | friend class Singleton<TransactionTracing>; |
| 219 | std::function<void(const std::string& prefix, bool overwrite)> mWriterFunction = |
| 220 | [](const std::string&, bool) {}; |
| 221 | |
| 222 | public: |
| 223 | void setWriterFunction( |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 224 | std::function<void(const std::string& filename, bool overwrite)> function) { |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 225 | mWriterFunction = std::move(function); |
| 226 | } |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 227 | void invoke(const std::string& prefix, bool overwrite) { |
| 228 | mWriterFunction(TransactionTracing::getFilePath(prefix), overwrite); |
| 229 | } |
| 230 | /* pass in a complete file path for testing */ |
| 231 | void invokeForTest(const std::string& filename, bool overwrite) { |
| 232 | mWriterFunction(filename, overwrite); |
| 233 | } |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 236 | } // namespace android |