Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | */ |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 16 | |
| 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 20 | #undef LOG_TAG |
| 21 | #define LOG_TAG "SurfaceTracing" |
| 22 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 23 | |
| 24 | #include "SurfaceTracing.h" |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 25 | #include <SurfaceFlinger.h> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 26 | |
| 27 | #include <android-base/file.h> |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 29 | #include <log/log.h> |
| 30 | #include <utils/SystemClock.h> |
| 31 | #include <utils/Trace.h> |
| 32 | |
| 33 | namespace android { |
| 34 | |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 35 | SurfaceTracing::SurfaceTracing(SurfaceFlinger& flinger) |
| 36 | : mFlinger(flinger), mSfLock(flinger.mDrawingStateLock) {} |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 37 | |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 38 | void SurfaceTracing::mainLoop() { |
| 39 | addFirstEntry(); |
| 40 | bool enabled = true; |
| 41 | while (enabled) { |
| 42 | LayersTraceProto entry = traceWhenNotified(); |
| 43 | enabled = addTraceToBuffer(entry); |
| 44 | } |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 47 | void SurfaceTracing::addFirstEntry() { |
Alec Mouri | 5793c7d | 2020-03-10 19:55:50 -0700 | [diff] [blame] | 48 | const auto displayDevice = mFlinger.getDefaultDisplayDevice(); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 49 | LayersTraceProto entry; |
| 50 | { |
| 51 | std::scoped_lock lock(mSfLock); |
Alec Mouri | 5793c7d | 2020-03-10 19:55:50 -0700 | [diff] [blame] | 52 | entry = traceLayersLocked("tracing.enable", displayDevice); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 53 | } |
| 54 | addTraceToBuffer(entry); |
| 55 | } |
| 56 | |
| 57 | LayersTraceProto SurfaceTracing::traceWhenNotified() { |
Alec Mouri | 5793c7d | 2020-03-10 19:55:50 -0700 | [diff] [blame] | 58 | const auto displayDevice = mFlinger.getDefaultDisplayDevice(); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 59 | std::unique_lock<std::mutex> lock(mSfLock); |
| 60 | mCanStartTrace.wait(lock); |
| 61 | android::base::ScopedLockAssertion assumeLock(mSfLock); |
Alec Mouri | 5793c7d | 2020-03-10 19:55:50 -0700 | [diff] [blame] | 62 | LayersTraceProto entry = traceLayersLocked(mWhere, displayDevice); |
Vishnu Nair | 60db8c0 | 2020-04-02 11:55:16 -0700 | [diff] [blame^] | 63 | mTracingInProgress = false; |
| 64 | mMissedTraceEntries = 0; |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 65 | lock.unlock(); |
| 66 | return entry; |
| 67 | } |
| 68 | |
| 69 | bool SurfaceTracing::addTraceToBuffer(LayersTraceProto& entry) { |
| 70 | std::scoped_lock lock(mTraceLock); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 71 | mBuffer.emplace(std::move(entry)); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 72 | if (mWriteToFile) { |
| 73 | writeProtoFileLocked(); |
| 74 | mWriteToFile = false; |
| 75 | } |
| 76 | return mEnabled; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void SurfaceTracing::notify(const char* where) { |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 80 | std::scoped_lock lock(mSfLock); |
Vishnu Nair | 60db8c0 | 2020-04-02 11:55:16 -0700 | [diff] [blame^] | 81 | notifyLocked(where); |
| 82 | } |
| 83 | |
| 84 | void SurfaceTracing::notifyLocked(const char* where) { |
Vishnu Nair | b015948 | 2019-03-18 12:48:46 -0700 | [diff] [blame] | 85 | mWhere = where; |
Vishnu Nair | 60db8c0 | 2020-04-02 11:55:16 -0700 | [diff] [blame^] | 86 | if (mTracingInProgress) { |
| 87 | mMissedTraceEntries++; |
| 88 | } |
| 89 | mTracingInProgress = true; |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 90 | mCanStartTrace.notify_one(); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void SurfaceTracing::writeToFileAsync() { |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 94 | std::scoped_lock lock(mTraceLock); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 95 | mWriteToFile = true; |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 96 | mCanStartTrace.notify_one(); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 99 | void SurfaceTracing::LayersTraceBuffer::reset(size_t newSize) { |
| 100 | // use the swap trick to make sure memory is released |
| 101 | std::queue<LayersTraceProto>().swap(mStorage); |
| 102 | mSizeInBytes = newSize; |
| 103 | mUsedInBytes = 0U; |
| 104 | } |
| 105 | |
| 106 | void SurfaceTracing::LayersTraceBuffer::emplace(LayersTraceProto&& proto) { |
| 107 | auto protoSize = proto.ByteSize(); |
| 108 | while (mUsedInBytes + protoSize > mSizeInBytes) { |
| 109 | if (mStorage.empty()) { |
| 110 | return; |
| 111 | } |
| 112 | mUsedInBytes -= mStorage.front().ByteSize(); |
| 113 | mStorage.pop(); |
| 114 | } |
| 115 | mUsedInBytes += protoSize; |
| 116 | mStorage.emplace(); |
| 117 | mStorage.back().Swap(&proto); |
| 118 | } |
| 119 | |
| 120 | void SurfaceTracing::LayersTraceBuffer::flush(LayersTraceFileProto* fileProto) { |
| 121 | fileProto->mutable_entry()->Reserve(mStorage.size()); |
| 122 | |
| 123 | while (!mStorage.empty()) { |
| 124 | auto entry = fileProto->add_entry(); |
| 125 | entry->Swap(&mStorage.front()); |
| 126 | mStorage.pop(); |
| 127 | } |
| 128 | } |
| 129 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 130 | void SurfaceTracing::enable() { |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 131 | std::scoped_lock lock(mTraceLock); |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 132 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 133 | if (mEnabled) { |
| 134 | return; |
| 135 | } |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 136 | mBuffer.reset(mBufferSize); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 137 | mEnabled = true; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 138 | mThread = std::thread(&SurfaceTracing::mainLoop, this); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 141 | status_t SurfaceTracing::writeToFile() { |
Alec Mouri | 50aeef6 | 2020-03-25 18:52:29 -0700 | [diff] [blame] | 142 | std::thread thread; |
| 143 | { |
| 144 | std::scoped_lock lock(mTraceLock); |
| 145 | thread = std::move(mThread); |
| 146 | } |
| 147 | thread.join(); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 148 | return mLastErr; |
| 149 | } |
| 150 | |
| 151 | bool SurfaceTracing::disable() { |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 152 | std::scoped_lock lock(mTraceLock); |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 153 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 154 | if (!mEnabled) { |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 155 | return false; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 156 | } |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 157 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 158 | mEnabled = false; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 159 | mWriteToFile = true; |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 160 | mCanStartTrace.notify_all(); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 161 | return true; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 164 | bool SurfaceTracing::isEnabled() const { |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 165 | std::scoped_lock lock(mTraceLock); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 166 | return mEnabled; |
| 167 | } |
| 168 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 169 | void SurfaceTracing::setBufferSize(size_t bufferSizeInByte) { |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 170 | std::scoped_lock lock(mTraceLock); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 171 | mBufferSize = bufferSizeInByte; |
| 172 | mBuffer.setSize(bufferSizeInByte); |
| 173 | } |
| 174 | |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 175 | void SurfaceTracing::setTraceFlags(uint32_t flags) { |
| 176 | std::scoped_lock lock(mSfLock); |
| 177 | mTraceFlags = flags; |
| 178 | } |
| 179 | |
Alec Mouri | 5793c7d | 2020-03-10 19:55:50 -0700 | [diff] [blame] | 180 | LayersTraceProto SurfaceTracing::traceLayersLocked(const char* where, |
| 181 | const sp<const DisplayDevice>& displayDevice) { |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 182 | ATRACE_CALL(); |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 183 | |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 184 | LayersTraceProto entry; |
| 185 | entry.set_elapsed_realtime_nanos(elapsedRealtimeNano()); |
| 186 | entry.set_where(where); |
Alec Mouri | 5793c7d | 2020-03-10 19:55:50 -0700 | [diff] [blame] | 187 | LayersProto layers(mFlinger.dumpDrawingStateProto(mTraceFlags, displayDevice)); |
Vishnu Nair | 60db8c0 | 2020-04-02 11:55:16 -0700 | [diff] [blame^] | 188 | |
| 189 | if (flagIsSetLocked(SurfaceTracing::TRACE_EXTRA)) { |
| 190 | mFlinger.dumpOffscreenLayersProto(layers); |
| 191 | } |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 192 | entry.mutable_layers()->Swap(&layers); |
| 193 | |
Alec Mouri | 6b9e991 | 2020-01-21 10:50:24 -0800 | [diff] [blame] | 194 | if (mTraceFlags & SurfaceTracing::TRACE_HWC) { |
| 195 | std::string hwcDump; |
| 196 | mFlinger.dumpHwc(hwcDump); |
| 197 | entry.set_hwc_blob(hwcDump); |
| 198 | } |
Vishnu Nair | 60db8c0 | 2020-04-02 11:55:16 -0700 | [diff] [blame^] | 199 | if (!flagIsSetLocked(SurfaceTracing::TRACE_COMPOSITION)) { |
| 200 | entry.set_excludes_composition_state(true); |
| 201 | } |
| 202 | entry.set_missed_entries(mMissedTraceEntries); |
Alec Mouri | 6b9e991 | 2020-01-21 10:50:24 -0800 | [diff] [blame] | 203 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 204 | return entry; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 207 | void SurfaceTracing::writeProtoFileLocked() { |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 208 | ATRACE_CALL(); |
| 209 | |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 210 | LayersTraceFileProto fileProto; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 211 | std::string output; |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 212 | |
| 213 | fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 | |
| 214 | LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L); |
| 215 | mBuffer.flush(&fileProto); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 216 | mBuffer.reset(mBufferSize); |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 217 | |
| 218 | if (!fileProto.SerializeToString(&output)) { |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 219 | ALOGE("Could not save the proto file! Permission denied"); |
| 220 | mLastErr = PERMISSION_DENIED; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 221 | } |
chaviw | d1759e0 | 2020-01-23 11:28:09 -0800 | [diff] [blame] | 222 | |
| 223 | // -rw-r--r-- |
| 224 | const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| 225 | if (!android::base::WriteStringToFile(output, kDefaultFileName, mode, getuid(), getgid(), |
| 226 | true)) { |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 227 | ALOGE("Could not save the proto file! There are missing fields"); |
| 228 | mLastErr = PERMISSION_DENIED; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 231 | mLastErr = NO_ERROR; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 234 | void SurfaceTracing::dump(std::string& result) const { |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 235 | std::scoped_lock lock(mTraceLock); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 236 | base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled"); |
| 237 | base::StringAppendF(&result, " number of entries: %zu (%.2fMB / %.2fMB)\n", |
| 238 | mBuffer.frameCount(), float(mBuffer.used()) / float(1_MB), |
| 239 | float(mBuffer.size()) / float(1_MB)); |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 240 | } |
| 241 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 242 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 243 | |
| 244 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 245 | #pragma clang diagnostic pop // ignored "-Wconversion" |