| 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> | 
|  | 22 | #include <utils/Timers.h> | 
|  | 23 |  | 
|  | 24 | #include <memory> | 
|  | 25 | #include <mutex> | 
|  | 26 | #include <thread> | 
|  | 27 |  | 
| Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 28 | #include "RingBuffer.h" | 
| Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 29 | #include "LocklessStack.h" | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 30 | #include "TransactionProtoParser.h" | 
|  | 31 |  | 
|  | 32 | using namespace android::surfaceflinger; | 
|  | 33 |  | 
|  | 34 | namespace android { | 
|  | 35 |  | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 36 | class SurfaceFlinger; | 
|  | 37 | class TransactionTracingTest; | 
| Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 38 |  | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 39 | /* | 
|  | 40 | * Records all committed transactions into a ring bufffer. | 
|  | 41 | * | 
|  | 42 | * Transactions come in via the binder thread. They are serialized to proto | 
|  | 43 | * and stored in a map using the transaction id as key. Main thread will | 
|  | 44 | * pass the list of transaction ids that are committed every vsync and notify | 
|  | 45 | * the tracing thread. The tracing thread will then wake up and add the | 
|  | 46 | * committed transactions to the ring buffer. | 
|  | 47 | * | 
|  | 48 | * When generating SF dump state, we will flush the buffer to a file which | 
|  | 49 | * will then be included in the bugreport. | 
|  | 50 | * | 
|  | 51 | */ | 
|  | 52 | class TransactionTracing { | 
|  | 53 | public: | 
|  | 54 | TransactionTracing(); | 
|  | 55 | ~TransactionTracing(); | 
|  | 56 |  | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 57 | void addQueuedTransaction(const TransactionState&); | 
|  | 58 | void addCommittedTransactions(std::vector<TransactionState>& transactions, int64_t vsyncId); | 
| Vishnu Nair | d8f5e9f | 2022-02-03 10:23:28 -0800 | [diff] [blame] | 59 | status_t writeToFile(std::string filename = FILE_NAME); | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 60 | void setBufferSize(size_t bufferSizeInBytes); | 
| Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 61 | void onLayerAdded(BBinder* layerHandle, int layerId, const std::string& name, uint32_t flags, | 
|  | 62 | int parentId); | 
| Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 63 | void onMirrorLayerAdded(BBinder* layerHandle, int layerId, const std::string& name, | 
|  | 64 | int mirrorFromId); | 
| Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 65 | void onLayerRemoved(int layerId); | 
| Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 66 | void onHandleRemoved(BBinder* layerHandle); | 
| Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 67 | void onLayerAddedToDrawingState(int layerId, int64_t vsyncId); | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 68 | void dump(std::string&) const; | 
|  | 69 | static constexpr auto CONTINUOUS_TRACING_BUFFER_SIZE = 512 * 1024; | 
|  | 70 | static constexpr auto ACTIVE_TRACING_BUFFER_SIZE = 100 * 1024 * 1024; | 
|  | 71 |  | 
|  | 72 | private: | 
|  | 73 | friend class TransactionTracingTest; | 
|  | 74 |  | 
|  | 75 | static constexpr auto FILE_NAME = "/data/misc/wmtrace/transactions_trace.winscope"; | 
|  | 76 |  | 
|  | 77 | mutable std::mutex mTraceLock; | 
| Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 78 | RingBuffer<proto::TransactionTraceFile, proto::TransactionTraceEntry> mBuffer | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 79 | GUARDED_BY(mTraceLock); | 
|  | 80 | size_t mBufferSizeInBytes GUARDED_BY(mTraceLock) = CONTINUOUS_TRACING_BUFFER_SIZE; | 
|  | 81 | std::unordered_map<uint64_t, proto::TransactionState> mQueuedTransactions | 
|  | 82 | GUARDED_BY(mTraceLock); | 
| Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 83 | LocklessStack<proto::TransactionState> mTransactionQueue; | 
| Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 84 | nsecs_t mStartingTimestamp GUARDED_BY(mTraceLock); | 
| Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 85 | std::unordered_map<int, proto::LayerCreationArgs> mCreatedLayers GUARDED_BY(mTraceLock); | 
| Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 86 | std::unordered_map<BBinder* /* layerHandle */, int32_t /* layerId */> mLayerHandles | 
|  | 87 | GUARDED_BY(mTraceLock); | 
| Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 88 | std::vector<std::pair<BBinder* /* layerHandle */, int32_t /* layerId */>> mRemovedLayerHandles | 
|  | 89 | GUARDED_BY(mTraceLock); | 
| Vishnu Nair | 473838d | 2021-12-08 09:46:02 -0800 | [diff] [blame] | 90 | std::map<int32_t /* layerId */, TracingLayerState> mStartingStates GUARDED_BY(mTraceLock); | 
| Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 91 | std::set<int32_t /* layerId */> mRemovedLayerHandlesAtStart GUARDED_BY(mTraceLock); | 
| Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 92 | TransactionProtoParser mProtoParser GUARDED_BY(mTraceLock); | 
| Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 93 | // Parses the transaction to proto without holding any tracing locks so we can generate proto | 
|  | 94 | // in the binder thread without any contention. | 
|  | 95 | TransactionProtoParser mLockfreeProtoParser; | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 96 |  | 
|  | 97 | // We do not want main thread to block so main thread will try to acquire mMainThreadLock, | 
|  | 98 | // otherwise will push data to temporary container. | 
|  | 99 | std::mutex mMainThreadLock; | 
|  | 100 | std::thread mThread GUARDED_BY(mMainThreadLock); | 
|  | 101 | bool mDone GUARDED_BY(mMainThreadLock) = false; | 
|  | 102 | std::condition_variable mTransactionsAvailableCv; | 
|  | 103 | std::condition_variable mTransactionsAddedToBufferCv; | 
|  | 104 | struct CommittedTransactions { | 
|  | 105 | std::vector<uint64_t> transactionIds; | 
| Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 106 | std::vector<int32_t> createdLayerIds; | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 107 | int64_t vsyncId; | 
|  | 108 | int64_t timestamp; | 
|  | 109 | }; | 
|  | 110 | std::vector<CommittedTransactions> mCommittedTransactions GUARDED_BY(mMainThreadLock); | 
|  | 111 | std::vector<CommittedTransactions> mPendingTransactions; // only accessed by main thread | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 112 |  | 
| Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 113 | std::vector<int32_t /* layerId */> mRemovedLayers GUARDED_BY(mMainThreadLock); | 
|  | 114 | std::vector<int32_t /* layerId */> mPendingRemovedLayers; // only accessed by main thread | 
|  | 115 |  | 
|  | 116 | proto::TransactionTraceFile createTraceFileProto() const; | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 117 | void loop(); | 
| Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 118 | void addEntry(const std::vector<CommittedTransactions>& committedTransactions, | 
|  | 119 | const std::vector<int32_t>& removedLayers) EXCLUDES(mTraceLock); | 
|  | 120 | int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock); | 
|  | 121 | void tryPushToTracingThread() EXCLUDES(mMainThreadLock); | 
|  | 122 | void addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) REQUIRES(mTraceLock); | 
|  | 123 | void updateStartingStateLocked(const proto::TransactionTraceEntry& entry) REQUIRES(mTraceLock); | 
| Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 124 | CommittedTransactions& findOrCreateCommittedTransactionRecord(int64_t vsyncId); | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 125 | // TEST | 
| Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 126 | // Wait until all the committed transactions for the specified vsync id are added to the buffer. | 
|  | 127 | void flush(int64_t vsyncId) EXCLUDES(mMainThreadLock); | 
|  | 128 | // Return buffer contents as trace file proto | 
|  | 129 | proto::TransactionTraceFile writeToProto() EXCLUDES(mMainThreadLock); | 
| Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 130 | }; | 
|  | 131 |  | 
|  | 132 | } // namespace android |