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" |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 19 | |
| 20 | #include <android-base/stringprintf.h> |
| 21 | #include <log/log.h> |
| 22 | #include <utils/SystemClock.h> |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 23 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 24 | #include "Client.h" |
| 25 | #include "FrontEnd/LayerCreationArgs.h" |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 26 | #include "TransactionDataSource.h" |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 27 | #include "TransactionTracing.h" |
| 28 | |
| 29 | namespace android { |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 30 | ANDROID_SINGLETON_STATIC_INSTANCE(android::TransactionTraceWriter) |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 31 | |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 32 | TransactionTracing::TransactionTracing() |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 33 | : mProtoParser(std::make_unique<TransactionProtoParser::FlingerDataMapper>()) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 34 | std::scoped_lock lock(mTraceLock); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 35 | |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 36 | mBuffer.setSize(CONTINUOUS_TRACING_BUFFER_SIZE); |
| 37 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 38 | mStartingTimestamp = systemTime(); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 39 | |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 40 | { |
| 41 | std::scoped_lock lock(mMainThreadLock); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 42 | mThread = std::thread(&TransactionTracing::loop, this); |
| 43 | } |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 44 | |
| 45 | TransactionDataSource::Initialize(*this); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 48 | TransactionTracing::~TransactionTracing() { |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 49 | TransactionDataSource::UnregisterTransactionTracing(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 50 | std::thread thread; |
| 51 | { |
| 52 | std::scoped_lock lock(mMainThreadLock); |
| 53 | mDone = true; |
| 54 | mTransactionsAvailableCv.notify_all(); |
| 55 | thread = std::move(mThread); |
| 56 | } |
| 57 | if (thread.joinable()) { |
| 58 | thread.join(); |
| 59 | } |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 60 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 61 | |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 62 | void TransactionTracing::onStart(TransactionTracing::Mode mode) { |
| 63 | // In "active" mode write the ring buffer (starting state + following sequence of transactions) |
| 64 | // to perfetto when tracing starts (only once). |
| 65 | if (mode != Mode::MODE_ACTIVE) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | writeRingBufferToPerfetto(TransactionTracing::Mode::MODE_ACTIVE); |
| 70 | |
Kean Mariotti | 8c5abc8 | 2023-09-20 09:28:07 +0000 | [diff] [blame] | 71 | ALOGD("Started active mode tracing (wrote initial transactions ring buffer to perfetto)"); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void TransactionTracing::onFlush(TransactionTracing::Mode mode) { |
| 75 | // In "continuous" mode write the ring buffer (starting state + following sequence of |
| 76 | // transactions) to perfetto when a "flush" event is received (bugreport is taken or tracing is |
| 77 | // stopped). |
| 78 | if (mode != Mode::MODE_CONTINUOUS) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | writeRingBufferToPerfetto(TransactionTracing::Mode::MODE_CONTINUOUS); |
| 83 | |
Kean Mariotti | 8c5abc8 | 2023-09-20 09:28:07 +0000 | [diff] [blame] | 84 | ALOGD("Flushed continuous mode tracing (wrote transactions ring buffer to perfetto"); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void TransactionTracing::writeRingBufferToPerfetto(TransactionTracing::Mode mode) { |
| 88 | // Write the ring buffer (starting state + following sequence of transactions) to perfetto |
| 89 | // tracing sessions with the specified mode. |
| 90 | const auto fileProto = writeToProto(); |
| 91 | |
| 92 | TransactionDataSource::Trace([&](TransactionDataSource::TraceContext context) { |
| 93 | // Write packets only to tracing sessions with specified mode |
| 94 | if (context.GetCustomTlsState()->mMode != mode) { |
| 95 | return; |
| 96 | } |
| 97 | for (const auto& entryProto : fileProto.entry()) { |
| 98 | const auto entryBytes = entryProto.SerializeAsString(); |
| 99 | |
| 100 | auto packet = context.NewTracePacket(); |
| 101 | packet->set_timestamp(static_cast<uint64_t>(entryProto.elapsed_realtime_nanos())); |
| 102 | packet->set_timestamp_clock_id(perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC); |
| 103 | |
| 104 | auto* transactionsProto = packet->set_surfaceflinger_transactions(); |
| 105 | transactionsProto->AppendRawProtoBytes(entryBytes.data(), entryBytes.size()); |
| 106 | } |
| 107 | { |
| 108 | // TODO (b/162206162): remove empty packet when perfetto bug is fixed. |
| 109 | // It is currently needed in order not to lose the last trace entry. |
| 110 | context.NewTracePacket(); |
| 111 | } |
| 112 | }); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 115 | status_t TransactionTracing::writeToFile(const std::string& filename) { |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 116 | auto fileProto = writeToProto(); |
| 117 | |
| 118 | std::string output; |
| 119 | if (!fileProto.SerializeToString(&output)) { |
| 120 | ALOGE("Could not serialize proto."); |
| 121 | return UNKNOWN_ERROR; |
| 122 | } |
| 123 | |
| 124 | // -rw-r--r-- |
| 125 | const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| 126 | if (!android::base::WriteStringToFile(output, filename, mode, getuid(), getgid(), true)) { |
| 127 | ALOGE("Could not save the proto file %s", filename.c_str()); |
| 128 | return PERMISSION_DENIED; |
| 129 | } |
| 130 | |
| 131 | return NO_ERROR; |
| 132 | } |
| 133 | |
| 134 | perfetto::protos::TransactionTraceFile TransactionTracing::writeToProto() { |
| 135 | std::scoped_lock<std::mutex> lock(mTraceLock); |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 136 | perfetto::protos::TransactionTraceFile fileProto = createTraceFileProto(); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 137 | const auto startingStateProto = createStartingStateProtoLocked(); |
| 138 | if (startingStateProto) { |
| 139 | *fileProto.add_entry() = std::move(*startingStateProto); |
| 140 | } |
| 141 | mBuffer.writeToProto(fileProto); |
| 142 | return fileProto; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void TransactionTracing::setBufferSize(size_t bufferSizeInBytes) { |
| 146 | std::scoped_lock lock(mTraceLock); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 147 | mBuffer.setSize(bufferSizeInBytes); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 150 | perfetto::protos::TransactionTraceFile TransactionTracing::createTraceFileProto() const { |
| 151 | perfetto::protos::TransactionTraceFile proto; |
| 152 | proto.set_magic_number( |
| 153 | uint64_t(perfetto::protos::TransactionTraceFile_MagicNumber_MAGIC_NUMBER_H) << 32 | |
| 154 | perfetto::protos::TransactionTraceFile_MagicNumber_MAGIC_NUMBER_L); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 155 | auto timeOffsetNs = static_cast<uint64_t>(systemTime(SYSTEM_TIME_REALTIME) - |
| 156 | systemTime(SYSTEM_TIME_MONOTONIC)); |
Kean Mariotti | c44fdaf | 2022-07-29 14:20:39 +0000 | [diff] [blame] | 157 | proto.set_real_to_elapsed_time_offset_nanos(timeOffsetNs); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 158 | proto.set_version(TRACING_VERSION); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 159 | return proto; |
| 160 | } |
| 161 | |
| 162 | void TransactionTracing::dump(std::string& result) const { |
| 163 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 164 | base::StringAppendF(&result, " queued transactions=%zu created layers=%zu states=%zu\n", |
| 165 | mQueuedTransactions.size(), mCreatedLayers.size(), mStartingStates.size()); |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 166 | mBuffer.dump(result); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void TransactionTracing::addQueuedTransaction(const TransactionState& transaction) { |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 170 | perfetto::protos::TransactionState* state = |
| 171 | new perfetto::protos::TransactionState(mProtoParser.toProto(transaction)); |
Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 172 | mTransactionQueue.push(state); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 175 | void TransactionTracing::addCommittedTransactions(int64_t vsyncId, nsecs_t commitTime, |
| 176 | frontend::Update& newUpdate, |
| 177 | const frontend::DisplayInfos& displayInfos, |
| 178 | bool displayInfoChanged) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 179 | CommittedUpdates update; |
| 180 | update.vsyncId = vsyncId; |
| 181 | update.timestamp = commitTime; |
| 182 | update.transactionIds.reserve(newUpdate.transactions.size()); |
| 183 | for (const auto& transaction : newUpdate.transactions) { |
| 184 | update.transactionIds.emplace_back(transaction.id); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 185 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 186 | update.displayInfoChanged = displayInfoChanged; |
| 187 | if (displayInfoChanged) { |
| 188 | update.displayInfos = displayInfos; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 189 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 190 | update.createdLayers = std::move(newUpdate.layerCreationArgs); |
| 191 | newUpdate.layerCreationArgs.clear(); |
| 192 | update.destroyedLayerHandles.reserve(newUpdate.destroyedHandles.size()); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 193 | for (auto& [handle, _] : newUpdate.destroyedHandles) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 194 | update.destroyedLayerHandles.push_back(handle); |
| 195 | } |
| 196 | mPendingUpdates.emplace_back(update); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 197 | tryPushToTracingThread(); |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 198 | mLastUpdatedVsyncId = vsyncId; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void TransactionTracing::loop() { |
| 202 | while (true) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 203 | std::vector<CommittedUpdates> committedUpdates; |
| 204 | std::vector<uint32_t> destroyedLayers; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 205 | { |
| 206 | std::unique_lock<std::mutex> lock(mMainThreadLock); |
| 207 | base::ScopedLockAssertion assumeLocked(mMainThreadLock); |
| 208 | mTransactionsAvailableCv.wait(lock, [&]() REQUIRES(mMainThreadLock) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 209 | return mDone || !mUpdates.empty(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 210 | }); |
| 211 | if (mDone) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 212 | mUpdates.clear(); |
| 213 | mDestroyedLayers.clear(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 214 | break; |
| 215 | } |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 216 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 217 | destroyedLayers = std::move(mDestroyedLayers); |
| 218 | mDestroyedLayers.clear(); |
| 219 | committedUpdates = std::move(mUpdates); |
| 220 | mUpdates.clear(); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 221 | } // unlock mMainThreadLock |
| 222 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 223 | if (!committedUpdates.empty() || !destroyedLayers.empty()) { |
| 224 | addEntry(committedUpdates, destroyedLayers); |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 225 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 229 | void TransactionTracing::addEntry(const std::vector<CommittedUpdates>& committedUpdates, |
| 230 | const std::vector<uint32_t>& destroyedLayers) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 231 | std::scoped_lock lock(mTraceLock); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 232 | std::vector<std::string> removedEntries; |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 233 | perfetto::protos::TransactionTraceEntry entryProto; |
Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 234 | |
| 235 | while (auto incomingTransaction = mTransactionQueue.pop()) { |
| 236 | auto transaction = *incomingTransaction; |
Robert Carr | a63d52a | 2022-03-03 08:03:37 -0800 | [diff] [blame] | 237 | mQueuedTransactions[incomingTransaction->transaction_id()] = transaction; |
| 238 | delete incomingTransaction; |
| 239 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 240 | for (const CommittedUpdates& update : committedUpdates) { |
| 241 | entryProto.set_elapsed_realtime_nanos(update.timestamp); |
| 242 | entryProto.set_vsync_id(update.vsyncId); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 243 | entryProto.mutable_added_layers()->Reserve( |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 244 | static_cast<int32_t>(update.createdLayers.size())); |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 245 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 246 | for (const auto& args : update.createdLayers) { |
Yi Kong | 01b8126 | 2024-08-12 07:30:15 +0800 | [diff] [blame] | 247 | entryProto.mutable_added_layers()->Add(mProtoParser.toProto(args)); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 248 | } |
Vishnu Nair | 286f4f9 | 2022-06-08 16:37:39 -0700 | [diff] [blame] | 249 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 250 | entryProto.mutable_destroyed_layers()->Reserve( |
| 251 | static_cast<int32_t>(destroyedLayers.size())); |
| 252 | for (auto& destroyedLayer : destroyedLayers) { |
| 253 | entryProto.mutable_destroyed_layers()->Add(destroyedLayer); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 254 | } |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 255 | entryProto.mutable_transactions()->Reserve( |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 256 | static_cast<int32_t>(update.transactionIds.size())); |
| 257 | for (const uint64_t& id : update.transactionIds) { |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 258 | auto it = mQueuedTransactions.find(id); |
| 259 | if (it != mQueuedTransactions.end()) { |
| 260 | entryProto.mutable_transactions()->Add(std::move(it->second)); |
| 261 | mQueuedTransactions.erase(it); |
| 262 | } else { |
Vishnu Nair | 047fb33 | 2021-12-09 09:54:36 -0800 | [diff] [blame] | 263 | ALOGW("Could not find transaction id %" PRIu64, id); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 266 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 267 | entryProto.mutable_destroyed_layer_handles()->Reserve( |
| 268 | static_cast<int32_t>(update.destroyedLayerHandles.size())); |
| 269 | for (auto layerId : update.destroyedLayerHandles) { |
| 270 | entryProto.mutable_destroyed_layer_handles()->Add(layerId); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 271 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 272 | |
| 273 | entryProto.set_displays_changed(update.displayInfoChanged); |
| 274 | if (update.displayInfoChanged) { |
| 275 | entryProto.mutable_displays()->Reserve( |
| 276 | static_cast<int32_t>(update.displayInfos.size())); |
| 277 | for (auto& [layerStack, displayInfo] : update.displayInfos) { |
| 278 | entryProto.mutable_displays()->Add( |
Yi Kong | 01b8126 | 2024-08-12 07:30:15 +0800 | [diff] [blame] | 279 | mProtoParser.toProto(displayInfo, layerStack.id)); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 280 | } |
| 281 | } |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 282 | |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 283 | std::string serializedProto; |
| 284 | entryProto.SerializeToString(&serializedProto); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 285 | |
| 286 | TransactionDataSource::Trace([&](TransactionDataSource::TraceContext context) { |
| 287 | // In "active" mode write each committed transaction to perfetto. |
| 288 | // Note: the starting state is written (once) when the perfetto "start" event is |
| 289 | // received. |
| 290 | if (context.GetCustomTlsState()->mMode != Mode::MODE_ACTIVE) { |
| 291 | return; |
| 292 | } |
| 293 | { |
| 294 | auto packet = context.NewTracePacket(); |
| 295 | packet->set_timestamp(static_cast<uint64_t>(entryProto.elapsed_realtime_nanos())); |
| 296 | packet->set_timestamp_clock_id(perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC); |
| 297 | auto* transactions = packet->set_surfaceflinger_transactions(); |
| 298 | transactions->AppendRawProtoBytes(serializedProto.data(), serializedProto.size()); |
| 299 | } |
| 300 | { |
| 301 | // TODO (b/162206162): remove empty packet when perfetto bug is fixed. |
| 302 | // It is currently needed in order not to lose the last trace entry. |
| 303 | context.NewTracePacket(); |
| 304 | } |
| 305 | }); |
| 306 | |
Dominik Laskowski | 46471e6 | 2022-01-14 15:34:03 -0800 | [diff] [blame] | 307 | std::vector<std::string> entries = mBuffer.emplace(std::move(serializedProto)); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 308 | removedEntries.reserve(removedEntries.size() + entries.size()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 309 | removedEntries.insert(removedEntries.end(), std::make_move_iterator(entries.begin()), |
| 310 | std::make_move_iterator(entries.end())); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 311 | |
| 312 | entryProto.Clear(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 315 | perfetto::protos::TransactionTraceEntry removedEntryProto; |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 316 | for (const std::string& removedEntry : removedEntries) { |
| 317 | removedEntryProto.ParseFromString(removedEntry); |
| 318 | updateStartingStateLocked(removedEntryProto); |
| 319 | removedEntryProto.Clear(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 320 | } |
| 321 | mTransactionsAddedToBufferCv.notify_one(); |
| 322 | } |
| 323 | |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 324 | void TransactionTracing::flush() { |
| 325 | { |
| 326 | std::scoped_lock lock(mMainThreadLock); |
| 327 | // Collect any pending transactions and wait for transactions to be added to |
| 328 | mUpdates.insert(mUpdates.end(), std::make_move_iterator(mPendingUpdates.begin()), |
| 329 | std::make_move_iterator(mPendingUpdates.end())); |
| 330 | mPendingUpdates.clear(); |
| 331 | mDestroyedLayers.insert(mDestroyedLayers.end(), mPendingDestroyedLayers.begin(), |
| 332 | mPendingDestroyedLayers.end()); |
| 333 | mPendingDestroyedLayers.clear(); |
| 334 | mTransactionsAvailableCv.notify_one(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 335 | } |
| 336 | std::unique_lock<std::mutex> lock(mTraceLock); |
| 337 | base::ScopedLockAssertion assumeLocked(mTraceLock); |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 338 | mTransactionsAddedToBufferCv.wait_for(lock, std::chrono::milliseconds(100), |
| 339 | [&]() REQUIRES(mTraceLock) { |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 340 | perfetto::protos::TransactionTraceEntry entry; |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 341 | if (mBuffer.used() > 0) { |
| 342 | entry.ParseFromString(mBuffer.back()); |
| 343 | } |
| 344 | return mBuffer.used() > 0 && |
| 345 | entry.vsync_id() >= mLastUpdatedVsyncId; |
| 346 | }); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 349 | void TransactionTracing::onLayerRemoved(int32_t layerId) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 350 | mPendingDestroyedLayers.emplace_back(layerId); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 351 | tryPushToTracingThread(); |
| 352 | } |
| 353 | |
| 354 | void TransactionTracing::tryPushToTracingThread() { |
| 355 | // Try to acquire the lock from main thread. |
| 356 | if (mMainThreadLock.try_lock()) { |
| 357 | // We got the lock! Collect any pending transactions and continue. |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 358 | mUpdates.insert(mUpdates.end(), std::make_move_iterator(mPendingUpdates.begin()), |
| 359 | std::make_move_iterator(mPendingUpdates.end())); |
| 360 | mPendingUpdates.clear(); |
| 361 | mDestroyedLayers.insert(mDestroyedLayers.end(), mPendingDestroyedLayers.begin(), |
| 362 | mPendingDestroyedLayers.end()); |
| 363 | mPendingDestroyedLayers.clear(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 364 | mTransactionsAvailableCv.notify_one(); |
| 365 | mMainThreadLock.unlock(); |
| 366 | } else { |
| 367 | ALOGV("Couldn't get lock"); |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 371 | void TransactionTracing::updateStartingStateLocked( |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 372 | const perfetto::protos::TransactionTraceEntry& removedEntry) { |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 373 | mStartingTimestamp = removedEntry.elapsed_realtime_nanos(); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 374 | // Keep track of layer starting state so we can reconstruct the layer state as we purge |
| 375 | // transactions from the buffer. |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 376 | for (const perfetto::protos::LayerCreationArgs& addedLayer : removedEntry.added_layers()) { |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 377 | TracingLayerState& startingState = mStartingStates[addedLayer.layer_id()]; |
| 378 | startingState.layerId = addedLayer.layer_id(); |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 379 | mProtoParser.fromProto(addedLayer, startingState.args); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // Merge layer states to starting transaction state. |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 383 | for (const perfetto::protos::TransactionState& transaction : removedEntry.transactions()) { |
| 384 | for (const perfetto::protos::LayerState& layerState : transaction.layer_changes()) { |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 385 | auto it = mStartingStates.find(layerState.layer_id()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 386 | if (it == mStartingStates.end()) { |
Vishnu Nair | 20e1f96 | 2023-03-29 15:58:34 -0700 | [diff] [blame] | 387 | // 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] | 388 | ALOGW("Could not find layer id %d", layerState.layer_id()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 389 | continue; |
| 390 | } |
Vishnu Nair | 685cfef | 2022-02-02 10:01:25 -0800 | [diff] [blame] | 391 | mProtoParser.mergeFromProto(layerState, it->second); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 395 | for (const uint32_t destroyedLayerHandleId : removedEntry.destroyed_layer_handles()) { |
| 396 | mRemovedLayerHandlesAtStart.insert(destroyedLayerHandleId); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 399 | // Clean up stale starting states since the layer has been removed and the buffer does not |
| 400 | // contain any references to the layer. |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 401 | for (const uint32_t destroyedLayerId : removedEntry.destroyed_layers()) { |
| 402 | mStartingStates.erase(destroyedLayerId); |
| 403 | mRemovedLayerHandlesAtStart.erase(destroyedLayerId); |
| 404 | } |
| 405 | |
| 406 | if (removedEntry.displays_changed()) { |
| 407 | mProtoParser.fromProto(removedEntry.displays(), mStartingDisplayInfos); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 411 | std::optional<perfetto::protos::TransactionTraceEntry> |
| 412 | TransactionTracing::createStartingStateProtoLocked() { |
| 413 | if (mStartingStates.empty()) { |
| 414 | return std::nullopt; |
Vishnu Nair | 84125ac | 2021-12-02 08:47:48 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 417 | perfetto::protos::TransactionTraceEntry entryProto; |
| 418 | entryProto.set_elapsed_realtime_nanos(mStartingTimestamp); |
| 419 | entryProto.set_vsync_id(0); |
Vishnu Nair | b8f2a2d | 2022-01-13 08:10:10 -0800 | [diff] [blame] | 420 | |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 421 | entryProto.mutable_added_layers()->Reserve(static_cast<int32_t>(mStartingStates.size())); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 422 | for (auto& [layerId, state] : mStartingStates) { |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 423 | entryProto.mutable_added_layers()->Add(mProtoParser.toProto(state.args)); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 426 | perfetto::protos::TransactionState transactionProto = mProtoParser.toProto(mStartingStates); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 427 | transactionProto.set_vsync_id(0); |
| 428 | transactionProto.set_post_time(mStartingTimestamp); |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 429 | entryProto.mutable_transactions()->Add(std::move(transactionProto)); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 430 | |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 431 | entryProto.mutable_destroyed_layer_handles()->Reserve( |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 432 | static_cast<int32_t>(mRemovedLayerHandlesAtStart.size())); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 433 | for (const uint32_t destroyedLayerHandleId : mRemovedLayerHandlesAtStart) { |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 434 | entryProto.mutable_destroyed_layer_handles()->Add(destroyedLayerHandleId); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 437 | entryProto.mutable_displays()->Reserve(static_cast<int32_t>(mStartingDisplayInfos.size())); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 438 | for (auto& [layerStack, displayInfo] : mStartingDisplayInfos) { |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 439 | entryProto.mutable_displays()->Add(mProtoParser.toProto(displayInfo, layerStack.id)); |
Vishnu Nair | 3e40cdd | 2022-06-08 16:50:17 -0700 | [diff] [blame] | 440 | } |
Vishnu Nair | 7bebc9f | 2023-12-08 22:56:28 +0000 | [diff] [blame] | 441 | entryProto.set_displays_changed(true); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 442 | |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 443 | return entryProto; |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | } // namespace android |