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 | |
| 26 | #include "RingBuffer.h" |
| 27 | #include "TransactionTracing.h" |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | TransactionTracing::TransactionTracing() { |
| 32 | mBuffer = std::make_unique< |
| 33 | RingBuffer<proto::TransactionTraceFile, proto::TransactionTraceEntry>>(); |
| 34 | } |
| 35 | |
| 36 | TransactionTracing::~TransactionTracing() = default; |
| 37 | |
| 38 | bool TransactionTracing::enable() { |
| 39 | std::scoped_lock lock(mTraceLock); |
| 40 | if (mEnabled) { |
| 41 | return false; |
| 42 | } |
| 43 | mBuffer->setSize(mBufferSizeInBytes); |
| 44 | mEnabled = true; |
| 45 | { |
| 46 | std::scoped_lock lock(mMainThreadLock); |
| 47 | mDone = false; |
| 48 | mThread = std::thread(&TransactionTracing::loop, this); |
| 49 | } |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | bool TransactionTracing::disable() { |
| 54 | std::thread thread; |
| 55 | { |
| 56 | std::scoped_lock lock(mMainThreadLock); |
| 57 | mDone = true; |
| 58 | mTransactionsAvailableCv.notify_all(); |
| 59 | thread = std::move(mThread); |
| 60 | } |
| 61 | if (thread.joinable()) { |
| 62 | thread.join(); |
| 63 | } |
| 64 | |
| 65 | std::scoped_lock lock(mTraceLock); |
| 66 | if (!mEnabled) { |
| 67 | return false; |
| 68 | } |
| 69 | mEnabled = false; |
| 70 | |
| 71 | proto::TransactionTraceFile fileProto = createTraceFileProto(); |
| 72 | mBuffer->writeToFile(fileProto, FILE_NAME); |
| 73 | mQueuedTransactions.clear(); |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool TransactionTracing::isEnabled() const { |
| 78 | std::scoped_lock lock(mTraceLock); |
| 79 | return mEnabled; |
| 80 | } |
| 81 | |
| 82 | status_t TransactionTracing::writeToFile() { |
| 83 | std::scoped_lock lock(mTraceLock); |
| 84 | if (!mEnabled) { |
| 85 | return STATUS_OK; |
| 86 | } |
| 87 | proto::TransactionTraceFile fileProto = createTraceFileProto(); |
| 88 | return mBuffer->writeToFile(fileProto, FILE_NAME); |
| 89 | } |
| 90 | |
| 91 | void TransactionTracing::setBufferSize(size_t bufferSizeInBytes) { |
| 92 | std::scoped_lock lock(mTraceLock); |
| 93 | mBufferSizeInBytes = bufferSizeInBytes; |
| 94 | mBuffer->setSize(mBufferSizeInBytes); |
| 95 | } |
| 96 | |
| 97 | proto::TransactionTraceFile TransactionTracing::createTraceFileProto() const { |
| 98 | proto::TransactionTraceFile proto; |
| 99 | proto.set_magic_number(uint64_t(proto::TransactionTraceFile_MagicNumber_MAGIC_NUMBER_H) << 32 | |
| 100 | proto::TransactionTraceFile_MagicNumber_MAGIC_NUMBER_L); |
| 101 | return proto; |
| 102 | } |
| 103 | |
| 104 | void TransactionTracing::dump(std::string& result) const { |
| 105 | std::scoped_lock lock(mTraceLock); |
| 106 | base::StringAppendF(&result, "Transaction tracing state: %s\n", |
| 107 | mEnabled ? "enabled" : "disabled"); |
| 108 | base::StringAppendF(&result, " queued transactions: %d\n", |
| 109 | static_cast<uint32_t>(mQueuedTransactions.size())); |
| 110 | mBuffer->dump(result); |
| 111 | } |
| 112 | |
| 113 | void TransactionTracing::addQueuedTransaction(const TransactionState& transaction) { |
| 114 | std::scoped_lock lock(mTraceLock); |
| 115 | ATRACE_CALL(); |
| 116 | if (!mEnabled) { |
| 117 | return; |
| 118 | } |
| 119 | mQueuedTransactions[transaction.id] = |
| 120 | TransactionProtoParser::toProto(transaction, nullptr, nullptr); |
| 121 | } |
| 122 | |
| 123 | void TransactionTracing::addCommittedTransactions(std::vector<TransactionState>& transactions, |
| 124 | int64_t vsyncId) { |
| 125 | CommittedTransactions committedTransactions; |
| 126 | committedTransactions.vsyncId = vsyncId; |
| 127 | committedTransactions.timestamp = systemTime(); |
| 128 | committedTransactions.transactionIds.reserve(transactions.size()); |
| 129 | for (const auto& transaction : transactions) { |
| 130 | committedTransactions.transactionIds.emplace_back(transaction.id); |
| 131 | } |
| 132 | |
| 133 | // Try to acquire the lock from main thread, but don't block if we cannot acquire the lock. Add |
| 134 | // it to pending transactions that we can collect later. |
| 135 | if (mMainThreadLock.try_lock()) { |
| 136 | // We got the lock! Collect any pending transactions and continue. |
| 137 | mCommittedTransactions.insert(mCommittedTransactions.end(), |
| 138 | std::make_move_iterator(mPendingTransactions.begin()), |
| 139 | std::make_move_iterator(mPendingTransactions.end())); |
| 140 | mPendingTransactions.clear(); |
| 141 | mCommittedTransactions.emplace_back(committedTransactions); |
| 142 | mTransactionsAvailableCv.notify_one(); |
| 143 | mMainThreadLock.unlock(); |
| 144 | } else { |
| 145 | mPendingTransactions.emplace_back(committedTransactions); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void TransactionTracing::loop() { |
| 150 | while (true) { |
| 151 | std::vector<CommittedTransactions> committedTransactions; |
| 152 | { |
| 153 | std::unique_lock<std::mutex> lock(mMainThreadLock); |
| 154 | base::ScopedLockAssertion assumeLocked(mMainThreadLock); |
| 155 | mTransactionsAvailableCv.wait(lock, [&]() REQUIRES(mMainThreadLock) { |
| 156 | return mDone || !mCommittedTransactions.empty(); |
| 157 | }); |
| 158 | if (mDone) { |
| 159 | mCommittedTransactions.clear(); |
| 160 | break; |
| 161 | } |
| 162 | committedTransactions = std::move(mCommittedTransactions); |
| 163 | mCommittedTransactions.clear(); |
| 164 | } // unlock mMainThreadLock |
| 165 | |
| 166 | addEntry(committedTransactions); |
| 167 | |
| 168 | mTransactionsAddedToBufferCv.notify_one(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void TransactionTracing::addEntry(const std::vector<CommittedTransactions>& committedTransactions) { |
| 173 | ATRACE_CALL(); |
| 174 | std::scoped_lock lock(mTraceLock); |
| 175 | std::vector<proto::TransactionTraceEntry> removedEntries; |
| 176 | for (const CommittedTransactions& entry : committedTransactions) { |
| 177 | proto::TransactionTraceEntry entryProto; |
| 178 | entryProto.set_elapsed_realtime_nanos(entry.timestamp); |
| 179 | entryProto.set_vsync_id(entry.vsyncId); |
| 180 | entryProto.mutable_transactions()->Reserve( |
| 181 | static_cast<int32_t>(entry.transactionIds.size())); |
| 182 | for (const uint64_t& id : entry.transactionIds) { |
| 183 | auto it = mQueuedTransactions.find(id); |
| 184 | if (it != mQueuedTransactions.end()) { |
| 185 | entryProto.mutable_transactions()->Add(std::move(it->second)); |
| 186 | mQueuedTransactions.erase(it); |
| 187 | } else { |
| 188 | ALOGE("Could not find transaction id %" PRIu64, id); |
| 189 | } |
| 190 | } |
| 191 | mBuffer->emplace(std::move(entryProto)); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void TransactionTracing::flush() { |
| 196 | std::unique_lock<std::mutex> lock(mMainThreadLock); |
| 197 | base::ScopedLockAssertion assumeLocked(mMainThreadLock); |
| 198 | mTransactionsAddedToBufferCv.wait(lock, [&]() REQUIRES(mMainThreadLock) { |
| 199 | return mCommittedTransactions.empty(); |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | } // namespace android |