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