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