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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "TransactionTracing" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
| 21 | #include <android-base/stringprintf.h> |
| 22 | #include <log/log.h> |
| 23 | #include <utils/SystemClock.h> |
| 24 | #include <utils/Trace.h> |
| 25 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 26 | #include "Client.h" |
| 27 | #include "FrontEnd/LayerCreationArgs.h" |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 28 | #include "TransactionTracing.h" |
| 29 | |
| 30 | namespace android { |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 31 | ANDROID_SINGLETON_STATIC_INSTANCE(android::TransactionTraceWriter) |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 32 | |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 33 | TransactionTracing::TransactionTracing() |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 34 | : mProtoParser(std::make_unique<TransactionProtoParser::FlingerDataMapper>()) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 35 | std::scoped_lock lock(mTraceLock); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 36 | |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 37 | mBuffer.setSize(mBufferSizeInBytes); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 38 | mStartingTimestamp = systemTime(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 39 | { |
| 40 | std::scoped_lock lock(mMainThreadLock); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 41 | mThread = std::thread(&TransactionTracing::loop, this); |
| 42 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 45 | TransactionTracing::~TransactionTracing() { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 46 | std::thread thread; |
| 47 | { |
| 48 | std::scoped_lock lock(mMainThreadLock); |
| 49 | mDone = true; |
| 50 | mTransactionsAvailableCv.notify_all(); |
| 51 | thread = std::move(mThread); |
| 52 | } |
| 53 | if (thread.joinable()) { |
| 54 | thread.join(); |
| 55 | } |
| 56 | |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 57 | writeToFile(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 60 | status_t TransactionTracing::writeToFile(const std::string& filename) { |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 61 | std::scoped_lock lock(mTraceLock); |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 62 | proto::TransactionTraceFile fileProto = createTraceFileProto(); |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 63 | addStartingStateToProtoLocked(fileProto); |
| 64 | return mBuffer.writeToFile(fileProto, filename); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void TransactionTracing::setBufferSize(size_t bufferSizeInBytes) { |
| 68 | std::scoped_lock lock(mTraceLock); |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 69 | mBufferSizeInBytes = bufferSizeInBytes; |
| 70 | mBuffer.setSize(mBufferSizeInBytes); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 73 | proto::TransactionTraceFile TransactionTracing::createTraceFileProto() const { |
| 74 | proto::TransactionTraceFile proto; |
| 75 | proto.set_magic_number(uint64_t(proto::TransactionTraceFile_MagicNumber_MAGIC_NUMBER_H) << 32 | |
| 76 | proto::TransactionTraceFile_MagicNumber_MAGIC_NUMBER_L); |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 77 | auto timeOffsetNs = static_cast<std::uint64_t>(systemTime(SYSTEM_TIME_REALTIME) - |
| 78 | systemTime(SYSTEM_TIME_MONOTONIC)); |
Kean Mariotti | c44fdaf | 2022-07-29 14:20:39 +0000 | [diff] [blame] | 79 | proto.set_real_to_elapsed_time_offset_nanos(timeOffsetNs); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 80 | proto.set_version(TRACING_VERSION); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 81 | return proto; |
| 82 | } |
| 83 | |
| 84 | void TransactionTracing::dump(std::string& result) const { |
| 85 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 86 | base::StringAppendF(&result, " queued transactions=%zu created layers=%zu states=%zu\n", |
| 87 | mQueuedTransactions.size(), mCreatedLayers.size(), mStartingStates.size()); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 88 | mBuffer.dump(result); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void TransactionTracing::addQueuedTransaction(const TransactionState& transaction) { |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 92 | proto::TransactionState* state = new proto::TransactionState(mProtoParser.toProto(transaction)); |
Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 93 | mTransactionQueue.push(state); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 96 | void TransactionTracing::addCommittedTransactions(int64_t vsyncId, nsecs_t commitTime, |
| 97 | frontend::Update& newUpdate, |
| 98 | const frontend::DisplayInfos& displayInfos, |
| 99 | bool displayInfoChanged) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 100 | CommittedUpdates update; |
| 101 | update.vsyncId = vsyncId; |
| 102 | update.timestamp = commitTime; |
| 103 | update.transactionIds.reserve(newUpdate.transactions.size()); |
| 104 | for (const auto& transaction : newUpdate.transactions) { |
| 105 | update.transactionIds.emplace_back(transaction.id); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 106 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 107 | update.displayInfoChanged = displayInfoChanged; |
| 108 | if (displayInfoChanged) { |
| 109 | update.displayInfos = displayInfos; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 110 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 111 | update.createdLayers = std::move(newUpdate.layerCreationArgs); |
| 112 | newUpdate.layerCreationArgs.clear(); |
| 113 | update.destroyedLayerHandles.reserve(newUpdate.destroyedHandles.size()); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 114 | for (auto& [handle, _] : newUpdate.destroyedHandles) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 115 | update.destroyedLayerHandles.push_back(handle); |
| 116 | } |
| 117 | mPendingUpdates.emplace_back(update); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 118 | tryPushToTracingThread(); |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 119 | mLastUpdatedVsyncId = vsyncId; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void TransactionTracing::loop() { |
| 123 | while (true) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 124 | std::vector<CommittedUpdates> committedUpdates; |
| 125 | std::vector<uint32_t> destroyedLayers; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 126 | { |
| 127 | std::unique_lock<std::mutex> lock(mMainThreadLock); |
| 128 | base::ScopedLockAssertion assumeLocked(mMainThreadLock); |
| 129 | mTransactionsAvailableCv.wait(lock, [&]() REQUIRES(mMainThreadLock) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 130 | return mDone || !mUpdates.empty(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 131 | }); |
| 132 | if (mDone) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 133 | mUpdates.clear(); |
| 134 | mDestroyedLayers.clear(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 135 | break; |
| 136 | } |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 137 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 138 | destroyedLayers = std::move(mDestroyedLayers); |
| 139 | mDestroyedLayers.clear(); |
| 140 | committedUpdates = std::move(mUpdates); |
| 141 | mUpdates.clear(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 142 | } // unlock mMainThreadLock |
| 143 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 144 | if (!committedUpdates.empty() || !destroyedLayers.empty()) { |
| 145 | addEntry(committedUpdates, destroyedLayers); |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 146 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 150 | void TransactionTracing::addEntry(const std::vector<CommittedUpdates>& committedUpdates, |
| 151 | const std::vector<uint32_t>& destroyedLayers) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 152 | ATRACE_CALL(); |
| 153 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 154 | std::vector<std::string> removedEntries; |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 155 | proto::TransactionTraceEntry entryProto; |
Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 156 | |
| 157 | while (auto incomingTransaction = mTransactionQueue.pop()) { |
| 158 | auto transaction = *incomingTransaction; |
Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 159 | mQueuedTransactions[incomingTransaction->transaction_id()] = transaction; |
| 160 | delete incomingTransaction; |
| 161 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 162 | for (const CommittedUpdates& update : committedUpdates) { |
| 163 | entryProto.set_elapsed_realtime_nanos(update.timestamp); |
| 164 | entryProto.set_vsync_id(update.vsyncId); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 165 | entryProto.mutable_added_layers()->Reserve( |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 166 | static_cast<int32_t>(update.createdLayers.size())); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 167 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 168 | for (const auto& args : update.createdLayers) { |
| 169 | entryProto.mutable_added_layers()->Add(std::move(mProtoParser.toProto(args))); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 170 | } |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 171 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 172 | entryProto.mutable_destroyed_layers()->Reserve( |
| 173 | static_cast<int32_t>(destroyedLayers.size())); |
| 174 | for (auto& destroyedLayer : destroyedLayers) { |
| 175 | entryProto.mutable_destroyed_layers()->Add(destroyedLayer); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 176 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 177 | entryProto.mutable_transactions()->Reserve( |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 178 | static_cast<int32_t>(update.transactionIds.size())); |
| 179 | for (const uint64_t& id : update.transactionIds) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 180 | auto it = mQueuedTransactions.find(id); |
| 181 | if (it != mQueuedTransactions.end()) { |
| 182 | entryProto.mutable_transactions()->Add(std::move(it->second)); |
| 183 | mQueuedTransactions.erase(it); |
| 184 | } else { |
Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 185 | ALOGW("Could not find transaction id %" PRIu64, id); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 188 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 189 | entryProto.mutable_destroyed_layer_handles()->Reserve( |
| 190 | static_cast<int32_t>(update.destroyedLayerHandles.size())); |
| 191 | for (auto layerId : update.destroyedLayerHandles) { |
| 192 | entryProto.mutable_destroyed_layer_handles()->Add(layerId); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 193 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 194 | |
| 195 | entryProto.set_displays_changed(update.displayInfoChanged); |
| 196 | if (update.displayInfoChanged) { |
| 197 | entryProto.mutable_displays()->Reserve( |
| 198 | static_cast<int32_t>(update.displayInfos.size())); |
| 199 | for (auto& [layerStack, displayInfo] : update.displayInfos) { |
| 200 | entryProto.mutable_displays()->Add( |
| 201 | std::move(mProtoParser.toProto(displayInfo, layerStack.id))); |
| 202 | } |
| 203 | } |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 204 | |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 205 | std::string serializedProto; |
| 206 | entryProto.SerializeToString(&serializedProto); |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 207 | entryProto.Clear(); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 208 | std::vector<std::string> entries = mBuffer.emplace(std::move(serializedProto)); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 209 | removedEntries.reserve(removedEntries.size() + entries.size()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 210 | removedEntries.insert(removedEntries.end(), std::make_move_iterator(entries.begin()), |
| 211 | std::make_move_iterator(entries.end())); |
| 212 | } |
| 213 | |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 214 | proto::TransactionTraceEntry removedEntryProto; |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 215 | for (const std::string& removedEntry : removedEntries) { |
| 216 | removedEntryProto.ParseFromString(removedEntry); |
| 217 | updateStartingStateLocked(removedEntryProto); |
| 218 | removedEntryProto.Clear(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 219 | } |
| 220 | mTransactionsAddedToBufferCv.notify_one(); |
| 221 | } |
| 222 | |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 223 | void TransactionTracing::flush() { |
| 224 | { |
| 225 | std::scoped_lock lock(mMainThreadLock); |
| 226 | // Collect any pending transactions and wait for transactions to be added to |
| 227 | mUpdates.insert(mUpdates.end(), std::make_move_iterator(mPendingUpdates.begin()), |
| 228 | std::make_move_iterator(mPendingUpdates.end())); |
| 229 | mPendingUpdates.clear(); |
| 230 | mDestroyedLayers.insert(mDestroyedLayers.end(), mPendingDestroyedLayers.begin(), |
| 231 | mPendingDestroyedLayers.end()); |
| 232 | mPendingDestroyedLayers.clear(); |
| 233 | mTransactionsAvailableCv.notify_one(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 234 | } |
| 235 | std::unique_lock<std::mutex> lock(mTraceLock); |
| 236 | base::ScopedLockAssertion assumeLocked(mTraceLock); |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 237 | mTransactionsAddedToBufferCv.wait_for(lock, std::chrono::milliseconds(100), |
| 238 | [&]() REQUIRES(mTraceLock) { |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 239 | proto::TransactionTraceEntry entry; |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 240 | if (mBuffer.used() > 0) { |
| 241 | entry.ParseFromString(mBuffer.back()); |
| 242 | } |
| 243 | return mBuffer.used() > 0 && |
| 244 | entry.vsync_id() >= mLastUpdatedVsyncId; |
| 245 | }); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 248 | void TransactionTracing::onLayerRemoved(int32_t layerId) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 249 | mPendingDestroyedLayers.emplace_back(layerId); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 250 | tryPushToTracingThread(); |
| 251 | } |
| 252 | |
| 253 | void TransactionTracing::tryPushToTracingThread() { |
| 254 | // Try to acquire the lock from main thread. |
| 255 | if (mMainThreadLock.try_lock()) { |
| 256 | // We got the lock! Collect any pending transactions and continue. |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 257 | mUpdates.insert(mUpdates.end(), std::make_move_iterator(mPendingUpdates.begin()), |
| 258 | std::make_move_iterator(mPendingUpdates.end())); |
| 259 | mPendingUpdates.clear(); |
| 260 | mDestroyedLayers.insert(mDestroyedLayers.end(), mPendingDestroyedLayers.begin(), |
| 261 | mPendingDestroyedLayers.end()); |
| 262 | mPendingDestroyedLayers.clear(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 263 | mTransactionsAvailableCv.notify_one(); |
| 264 | mMainThreadLock.unlock(); |
| 265 | } else { |
| 266 | ALOGV("Couldn't get lock"); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 270 | void TransactionTracing::updateStartingStateLocked( |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 271 | const proto::TransactionTraceEntry& removedEntry) { |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 272 | mStartingTimestamp = removedEntry.elapsed_realtime_nanos(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 273 | // Keep track of layer starting state so we can reconstruct the layer state as we purge |
| 274 | // transactions from the buffer. |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 275 | for (const proto::LayerCreationArgs& addedLayer : removedEntry.added_layers()) { |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 276 | TracingLayerState& startingState = mStartingStates[addedLayer.layer_id()]; |
| 277 | startingState.layerId = addedLayer.layer_id(); |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 278 | mProtoParser.fromProto(addedLayer, startingState.args); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // Merge layer states to starting transaction state. |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 282 | for (const proto::TransactionState& transaction : removedEntry.transactions()) { |
| 283 | for (const proto::LayerState& layerState : transaction.layer_changes()) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 284 | auto it = mStartingStates.find(layerState.layer_id()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 285 | if (it == mStartingStates.end()) { |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 286 | // TODO(b/238781169) make this log fatal when we switch over to using new fe |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 287 | ALOGW("Could not find layer id %d", layerState.layer_id()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 288 | continue; |
| 289 | } |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 290 | mProtoParser.mergeFromProto(layerState, it->second); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 294 | for (const uint32_t destroyedLayerHandleId : removedEntry.destroyed_layer_handles()) { |
| 295 | mRemovedLayerHandlesAtStart.insert(destroyedLayerHandleId); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 298 | // Clean up stale starting states since the layer has been removed and the buffer does not |
| 299 | // contain any references to the layer. |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 300 | for (const uint32_t destroyedLayerId : removedEntry.destroyed_layers()) { |
| 301 | mStartingStates.erase(destroyedLayerId); |
| 302 | mRemovedLayerHandlesAtStart.erase(destroyedLayerId); |
| 303 | } |
| 304 | |
| 305 | if (removedEntry.displays_changed()) { |
| 306 | mProtoParser.fromProto(removedEntry.displays(), mStartingDisplayInfos); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 310 | void TransactionTracing::addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) { |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 311 | if (mStartingStates.size() == 0) { |
| 312 | return; |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 315 | proto::TransactionTraceEntry* entryProto = proto.add_entry(); |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 316 | entryProto->set_elapsed_realtime_nanos(mStartingTimestamp); |
| 317 | entryProto->set_vsync_id(0); |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 318 | |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 319 | entryProto->mutable_added_layers()->Reserve(static_cast<int32_t>(mStartingStates.size())); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 320 | for (auto& [layerId, state] : mStartingStates) { |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 321 | entryProto->mutable_added_layers()->Add(mProtoParser.toProto(state.args)); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 324 | proto::TransactionState transactionProto = mProtoParser.toProto(mStartingStates); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 325 | transactionProto.set_vsync_id(0); |
| 326 | transactionProto.set_post_time(mStartingTimestamp); |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 327 | entryProto->mutable_transactions()->Add(std::move(transactionProto)); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 328 | |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 329 | entryProto->mutable_destroyed_layer_handles()->Reserve( |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 330 | static_cast<int32_t>(mRemovedLayerHandlesAtStart.size())); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 331 | for (const uint32_t destroyedLayerHandleId : mRemovedLayerHandlesAtStart) { |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 332 | entryProto->mutable_destroyed_layer_handles()->Add(destroyedLayerHandleId); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 335 | entryProto->mutable_displays()->Reserve(static_cast<int32_t>(mStartingDisplayInfos.size())); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 336 | for (auto& [layerStack, displayInfo] : mStartingDisplayInfos) { |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 337 | entryProto->mutable_displays()->Add(mProtoParser.toProto(displayInfo, layerStack.id)); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 338 | } |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 339 | } |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 340 | |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 341 | proto::TransactionTraceFile TransactionTracing::writeToProto() { |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 342 | std::scoped_lock<std::mutex> lock(mTraceLock); |
Diwas Sharma | 2401daf | 2023-09-01 00:45:24 +0000 | [diff] [blame] | 343 | proto::TransactionTraceFile proto = createTraceFileProto(); |
Diwas Sharma | a002117 | 2023-09-01 00:43:31 +0000 | [diff] [blame] | 344 | addStartingStateToProtoLocked(proto); |
| 345 | mBuffer.writeToProto(proto); |
| 346 | return proto; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | } // namespace android |