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 | |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 25 | #include <memory> |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 26 | #include <mutex> |
| 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 | /* |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 44 | * Records all committed transactions into a ring bufffer. |
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 | * |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 52 | * When generating SF dump state, we will flush the buffer to a file which |
| 53 | * will then be included in the bugreport. |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 54 | * |
| 55 | */ |
| 56 | class TransactionTracing { |
| 57 | public: |
| 58 | TransactionTracing(); |
| 59 | ~TransactionTracing(); |
| 60 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 61 | void addQueuedTransaction(const TransactionState&); |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 62 | void addCommittedTransactions(int64_t vsyncId, nsecs_t commitTime, frontend::Update& update, |
| 63 | const frontend::DisplayInfos&, bool displayInfoChanged); |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 64 | status_t writeToFile(const std::string& filename = FILE_PATH); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 65 | void setBufferSize(size_t bufferSizeInBytes); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 66 | void onLayerRemoved(int layerId); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 67 | void dump(std::string&) const; |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 68 | // Wait until all the committed transactions for the specified vsync id are added to the buffer. |
| 69 | void flush() EXCLUDES(mMainThreadLock); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 70 | static constexpr auto CONTINUOUS_TRACING_BUFFER_SIZE = 512 * 1024; |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 71 | static constexpr auto ACTIVE_TRACING_BUFFER_SIZE = 100 * 1024 * 1024; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 72 | // version 1 - switching to support new frontend |
| 73 | static constexpr auto TRACING_VERSION = 1; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 74 | |
| 75 | private: |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 76 | friend class TransactionTraceWriter; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 77 | friend class TransactionTracingTest; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 78 | friend class SurfaceFlinger; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 79 | |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 80 | static constexpr auto DIR_NAME = "/data/misc/wmtrace/"; |
Vishnu Nair | 249fd7f | 2023-06-22 19:30:24 -0700 | [diff] [blame] | 81 | static constexpr auto FILE_NAME = "transactions_trace.winscope"; |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 82 | static constexpr auto FILE_PATH = "/data/misc/wmtrace/transactions_trace.winscope"; |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 83 | static std::string getFilePath(const std::string& prefix) { |
| 84 | return DIR_NAME + prefix + FILE_NAME; |
| 85 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 86 | |
| 87 | mutable std::mutex mTraceLock; |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 88 | TransactionRingBuffer<perfetto::protos::TransactionTraceFile, |
| 89 | perfetto::protos::TransactionTraceEntry> |
| 90 | mBuffer GUARDED_BY(mTraceLock); |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 91 | size_t mBufferSizeInBytes GUARDED_BY(mTraceLock) = CONTINUOUS_TRACING_BUFFER_SIZE; |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 92 | std::unordered_map<uint64_t, perfetto::protos::TransactionState> mQueuedTransactions |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 93 | GUARDED_BY(mTraceLock); |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 94 | LocklessStack<perfetto::protos::TransactionState> mTransactionQueue; |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 95 | nsecs_t mStartingTimestamp GUARDED_BY(mTraceLock); |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 96 | std::unordered_map<int, perfetto::protos::LayerCreationArgs> mCreatedLayers |
| 97 | GUARDED_BY(mTraceLock); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 98 | std::map<uint32_t /* layerId */, TracingLayerState> mStartingStates GUARDED_BY(mTraceLock); |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 99 | frontend::DisplayInfos mStartingDisplayInfos GUARDED_BY(mTraceLock); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 100 | |
| 101 | std::set<uint32_t /* layerId */> mRemovedLayerHandlesAtStart GUARDED_BY(mTraceLock); |
| 102 | TransactionProtoParser mProtoParser; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 103 | |
| 104 | // We do not want main thread to block so main thread will try to acquire mMainThreadLock, |
| 105 | // otherwise will push data to temporary container. |
| 106 | std::mutex mMainThreadLock; |
| 107 | std::thread mThread GUARDED_BY(mMainThreadLock); |
| 108 | bool mDone GUARDED_BY(mMainThreadLock) = false; |
| 109 | std::condition_variable mTransactionsAvailableCv; |
| 110 | std::condition_variable mTransactionsAddedToBufferCv; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 111 | struct CommittedUpdates { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 112 | std::vector<uint64_t> transactionIds; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 113 | std::vector<LayerCreationArgs> createdLayers; |
| 114 | std::vector<uint32_t> destroyedLayerHandles; |
| 115 | bool displayInfoChanged; |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 116 | frontend::DisplayInfos displayInfos; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 117 | int64_t vsyncId; |
| 118 | int64_t timestamp; |
| 119 | }; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 120 | std::vector<CommittedUpdates> mUpdates GUARDED_BY(mMainThreadLock); |
| 121 | std::vector<CommittedUpdates> mPendingUpdates; // only accessed by main thread |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 122 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 123 | std::vector<uint32_t /* layerId */> mDestroyedLayers GUARDED_BY(mMainThreadLock); |
| 124 | std::vector<uint32_t /* layerId */> mPendingDestroyedLayers; // only accessed by main thread |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 125 | int64_t mLastUpdatedVsyncId = -1; |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 126 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 127 | perfetto::protos::TransactionTraceFile createTraceFileProto() const; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 128 | void loop(); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 129 | void addEntry(const std::vector<CommittedUpdates>& committedTransactions, |
| 130 | const std::vector<uint32_t>& removedLayers) EXCLUDES(mTraceLock); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 131 | int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock); |
| 132 | void tryPushToTracingThread() EXCLUDES(mMainThreadLock); |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 133 | void addStartingStateToProtoLocked(perfetto::protos::TransactionTraceFile& proto) |
| 134 | REQUIRES(mTraceLock); |
| 135 | void updateStartingStateLocked(const perfetto::protos::TransactionTraceEntry& entry) |
| 136 | REQUIRES(mTraceLock); |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 137 | // TEST |
| 138 | // Return buffer contents as trace file proto |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 139 | perfetto::protos::TransactionTraceFile writeToProto() EXCLUDES(mMainThreadLock); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 142 | class TransactionTraceWriter : public Singleton<TransactionTraceWriter> { |
| 143 | friend class Singleton<TransactionTracing>; |
| 144 | std::function<void(const std::string& prefix, bool overwrite)> mWriterFunction = |
| 145 | [](const std::string&, bool) {}; |
| 146 | |
| 147 | public: |
| 148 | void setWriterFunction( |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 149 | std::function<void(const std::string& filename, bool overwrite)> function) { |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 150 | mWriterFunction = std::move(function); |
| 151 | } |
Vishnu Nair | 8c5b700 | 2023-08-17 21:03:57 -0700 | [diff] [blame] | 152 | void invoke(const std::string& prefix, bool overwrite) { |
| 153 | mWriterFunction(TransactionTracing::getFilePath(prefix), overwrite); |
| 154 | } |
| 155 | /* pass in a complete file path for testing */ |
| 156 | void invokeForTest(const std::string& filename, bool overwrite) { |
| 157 | mWriterFunction(filename, overwrite); |
| 158 | } |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 161 | } // namespace android |