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 | */ |
| 16 | #undef LOG_TAG |
| 17 | #define LOG_TAG "SurfaceTracing" |
| 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | |
| 20 | #include "SurfaceTracing.h" |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 21 | #include <SurfaceFlinger.h> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 22 | |
| 23 | #include <android-base/file.h> |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 25 | #include <log/log.h> |
| 26 | #include <utils/SystemClock.h> |
| 27 | #include <utils/Trace.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 31 | void SurfaceTracing::mainLoop() { |
| 32 | bool enabled = true; |
| 33 | // Upon activation, logs the first frame |
| 34 | traceLayers("tracing.enable"); |
| 35 | do { |
| 36 | std::unique_lock<std::mutex> sfLock(mFlinger.mDrawingStateLock); |
| 37 | mConditionalVariable.wait(sfLock); |
| 38 | LayersTraceProto entry = traceLayersLocked(mWhere); |
| 39 | sfLock.unlock(); |
| 40 | { |
| 41 | std::scoped_lock bufferLock(mTraceLock); |
| 42 | mBuffer.emplace(std::move(entry)); |
| 43 | if (mWriteToFile) { |
| 44 | writeProtoFileLocked(); |
| 45 | mWriteToFile = false; |
| 46 | } |
| 47 | |
| 48 | enabled = mEnabled; |
| 49 | } |
| 50 | } while (enabled); |
| 51 | } |
| 52 | |
| 53 | void SurfaceTracing::traceLayers(const char* where) { |
| 54 | std::unique_lock<std::mutex> sfLock(mFlinger.mDrawingStateLock); |
| 55 | LayersTraceProto entry = traceLayersLocked(where); |
| 56 | sfLock.unlock(); |
| 57 | std::scoped_lock bufferLock(mTraceLock); |
| 58 | mBuffer.emplace(std::move(entry)); |
| 59 | } |
| 60 | |
| 61 | void SurfaceTracing::notify(const char* where) { |
| 62 | std::lock_guard<std::mutex> sfLock(mFlinger.mDrawingStateLock); |
| 63 | mWhere = strdup(where); |
| 64 | mConditionalVariable.notify_one(); |
| 65 | } |
| 66 | |
| 67 | void SurfaceTracing::writeToFileAsync() { |
| 68 | std::lock_guard<std::mutex> bufferLock(mTraceLock); |
| 69 | mWriteToFile = true; |
| 70 | mConditionalVariable.notify_one(); |
| 71 | } |
| 72 | |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 73 | void SurfaceTracing::LayersTraceBuffer::reset(size_t newSize) { |
| 74 | // use the swap trick to make sure memory is released |
| 75 | std::queue<LayersTraceProto>().swap(mStorage); |
| 76 | mSizeInBytes = newSize; |
| 77 | mUsedInBytes = 0U; |
| 78 | } |
| 79 | |
| 80 | void SurfaceTracing::LayersTraceBuffer::emplace(LayersTraceProto&& proto) { |
| 81 | auto protoSize = proto.ByteSize(); |
| 82 | while (mUsedInBytes + protoSize > mSizeInBytes) { |
| 83 | if (mStorage.empty()) { |
| 84 | return; |
| 85 | } |
| 86 | mUsedInBytes -= mStorage.front().ByteSize(); |
| 87 | mStorage.pop(); |
| 88 | } |
| 89 | mUsedInBytes += protoSize; |
| 90 | mStorage.emplace(); |
| 91 | mStorage.back().Swap(&proto); |
| 92 | } |
| 93 | |
| 94 | void SurfaceTracing::LayersTraceBuffer::flush(LayersTraceFileProto* fileProto) { |
| 95 | fileProto->mutable_entry()->Reserve(mStorage.size()); |
| 96 | |
| 97 | while (!mStorage.empty()) { |
| 98 | auto entry = fileProto->add_entry(); |
| 99 | entry->Swap(&mStorage.front()); |
| 100 | mStorage.pop(); |
| 101 | } |
| 102 | } |
| 103 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 104 | void SurfaceTracing::enable() { |
| 105 | std::lock_guard<std::mutex> bufferLock(mTraceLock); |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 106 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 107 | if (mEnabled) { |
| 108 | return; |
| 109 | } |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 110 | |
| 111 | mBuffer.reset(mBufferSize); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 112 | mEnabled = true; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 113 | mThread = std::thread(&SurfaceTracing::mainLoop, this); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 116 | status_t SurfaceTracing::writeToFile() { |
| 117 | mThread.join(); |
| 118 | return mLastErr; |
| 119 | } |
| 120 | |
| 121 | bool SurfaceTracing::disable() { |
| 122 | std::lock_guard<std::mutex> bufferLock(mTraceLock); |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 123 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 124 | if (!mEnabled) { |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 125 | return false; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 126 | } |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 127 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 128 | mEnabled = false; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 129 | mWriteToFile = true; |
| 130 | mConditionalVariable.notify_all(); |
| 131 | return true; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 134 | bool SurfaceTracing::isEnabled() const { |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 135 | std::lock_guard<std::mutex> bufferLock(mTraceLock); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 136 | return mEnabled; |
| 137 | } |
| 138 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 139 | void SurfaceTracing::setBufferSize(size_t bufferSizeInByte) { |
| 140 | std::lock_guard<std::mutex> bufferLock(mTraceLock); |
| 141 | mBufferSize = bufferSizeInByte; |
| 142 | mBuffer.setSize(bufferSizeInByte); |
| 143 | } |
| 144 | |
| 145 | LayersTraceProto SurfaceTracing::traceLayersLocked(const char* where) { |
| 146 | ATRACE_CALL(); |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 147 | |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 148 | LayersTraceProto entry; |
| 149 | entry.set_elapsed_realtime_nanos(elapsedRealtimeNano()); |
| 150 | entry.set_where(where); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 151 | LayersProto layers(mFlinger.dumpProtoInfo(LayerVector::StateSet::Drawing)); |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 152 | entry.mutable_layers()->Swap(&layers); |
| 153 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 154 | return entry; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 157 | void SurfaceTracing::writeProtoFileLocked() { |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 158 | ATRACE_CALL(); |
| 159 | |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 160 | LayersTraceFileProto fileProto; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 161 | std::string output; |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 162 | |
| 163 | fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 | |
| 164 | LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L); |
| 165 | mBuffer.flush(&fileProto); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 166 | mBuffer.reset(mBufferSize); |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 167 | |
| 168 | if (!fileProto.SerializeToString(&output)) { |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 169 | ALOGE("Could not save the proto file! Permission denied"); |
| 170 | mLastErr = PERMISSION_DENIED; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 171 | } |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 172 | if (!android::base::WriteStringToFile(output, kDefaultFileName, S_IRWXU | S_IRGRP, getuid(), |
| 173 | getgid(), true)) { |
| 174 | ALOGE("Could not save the proto file! There are missing fields"); |
| 175 | mLastErr = PERMISSION_DENIED; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 178 | mLastErr = NO_ERROR; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 181 | void SurfaceTracing::dump(std::string& result) const { |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 182 | std::lock_guard<std::mutex> bufferLock(mTraceLock); |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 183 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 184 | base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled"); |
| 185 | base::StringAppendF(&result, " number of entries: %zu (%.2fMB / %.2fMB)\n", |
| 186 | mBuffer.frameCount(), float(mBuffer.used()) / float(1_MB), |
| 187 | float(mBuffer.size()) / float(1_MB)); |
Yichi Chen | adc6961 | 2018-09-15 14:51:18 +0800 | [diff] [blame] | 188 | } |
| 189 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 190 | } // namespace android |