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