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