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