blob: 68ecfd1b83777946173f8c9922362cd44b58bd50 [file] [log] [blame]
Adrian Roos1e1a1282017-11-01 19:05:31 +01001/*
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 Abrahamb0dbdaa2020-01-06 16:19:42 -080016
17// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Adrian Roos1e1a1282017-11-01 19:05:31 +010020#undef LOG_TAG
21#define LOG_TAG "SurfaceTracing"
22#define ATRACE_TAG ATRACE_TAG_GRAPHICS
23
24#include "SurfaceTracing.h"
Nataniel Borges2b796da2019-02-15 13:32:18 -080025#include <SurfaceFlinger.h>
Adrian Roos1e1a1282017-11-01 19:05:31 +010026
27#include <android-base/file.h>
Yiwei Zhang5434a782018-12-05 18:06:32 -080028#include <android-base/stringprintf.h>
Adrian Roos1e1a1282017-11-01 19:05:31 +010029#include <log/log.h>
30#include <utils/SystemClock.h>
31#include <utils/Trace.h>
32
33namespace android {
34
Vishnu Nair9245d3b2019-03-22 13:38:56 -070035SurfaceTracing::SurfaceTracing(SurfaceFlinger& flinger)
36 : mFlinger(flinger), mSfLock(flinger.mDrawingStateLock) {}
Nataniel Borges2b796da2019-02-15 13:32:18 -080037
Vishnu Nair9245d3b2019-03-22 13:38:56 -070038void SurfaceTracing::mainLoop() {
Vishnu Nair73454c32020-04-03 18:56:19 -070039 bool enabled = addFirstEntry();
Vishnu Nair9245d3b2019-03-22 13:38:56 -070040 while (enabled) {
41 LayersTraceProto entry = traceWhenNotified();
42 enabled = addTraceToBuffer(entry);
43 }
Nataniel Borges2b796da2019-02-15 13:32:18 -080044}
45
Vishnu Nair73454c32020-04-03 18:56:19 -070046bool SurfaceTracing::addFirstEntry() {
Alec Mouri5793c7d2020-03-10 19:55:50 -070047 const auto displayDevice = mFlinger.getDefaultDisplayDevice();
Vishnu Nair9245d3b2019-03-22 13:38:56 -070048 LayersTraceProto entry;
49 {
50 std::scoped_lock lock(mSfLock);
Alec Mouri5793c7d2020-03-10 19:55:50 -070051 entry = traceLayersLocked("tracing.enable", displayDevice);
Vishnu Nair9245d3b2019-03-22 13:38:56 -070052 }
Vishnu Nair73454c32020-04-03 18:56:19 -070053 return addTraceToBuffer(entry);
Vishnu Nair9245d3b2019-03-22 13:38:56 -070054}
55
56LayersTraceProto SurfaceTracing::traceWhenNotified() {
Alec Mouri5793c7d2020-03-10 19:55:50 -070057 const auto displayDevice = mFlinger.getDefaultDisplayDevice();
Vishnu Nair9245d3b2019-03-22 13:38:56 -070058 std::unique_lock<std::mutex> lock(mSfLock);
59 mCanStartTrace.wait(lock);
60 android::base::ScopedLockAssertion assumeLock(mSfLock);
Alec Mouri5793c7d2020-03-10 19:55:50 -070061 LayersTraceProto entry = traceLayersLocked(mWhere, displayDevice);
Vishnu Nair9245d3b2019-03-22 13:38:56 -070062 lock.unlock();
63 return entry;
64}
65
66bool SurfaceTracing::addTraceToBuffer(LayersTraceProto& entry) {
67 std::scoped_lock lock(mTraceLock);
Nataniel Borges2b796da2019-02-15 13:32:18 -080068 mBuffer.emplace(std::move(entry));
Vishnu Nair9245d3b2019-03-22 13:38:56 -070069 if (mWriteToFile) {
70 writeProtoFileLocked();
71 mWriteToFile = false;
72 }
73 return mEnabled;
Nataniel Borges2b796da2019-02-15 13:32:18 -080074}
75
76void SurfaceTracing::notify(const char* where) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -070077 std::scoped_lock lock(mSfLock);
Vishnu Nairb0159482019-03-18 12:48:46 -070078 mWhere = where;
Vishnu Nair9245d3b2019-03-22 13:38:56 -070079 mCanStartTrace.notify_one();
Nataniel Borges2b796da2019-02-15 13:32:18 -080080}
81
82void SurfaceTracing::writeToFileAsync() {
Vishnu Nair9245d3b2019-03-22 13:38:56 -070083 std::scoped_lock lock(mTraceLock);
Nataniel Borges2b796da2019-02-15 13:32:18 -080084 mWriteToFile = true;
Vishnu Nair9245d3b2019-03-22 13:38:56 -070085 mCanStartTrace.notify_one();
Nataniel Borges2b796da2019-02-15 13:32:18 -080086}
87
Yichi Chen9c696ed2018-10-01 22:32:30 +080088void SurfaceTracing::LayersTraceBuffer::reset(size_t newSize) {
89 // use the swap trick to make sure memory is released
90 std::queue<LayersTraceProto>().swap(mStorage);
91 mSizeInBytes = newSize;
92 mUsedInBytes = 0U;
93}
94
95void SurfaceTracing::LayersTraceBuffer::emplace(LayersTraceProto&& proto) {
96 auto protoSize = proto.ByteSize();
97 while (mUsedInBytes + protoSize > mSizeInBytes) {
98 if (mStorage.empty()) {
99 return;
100 }
101 mUsedInBytes -= mStorage.front().ByteSize();
102 mStorage.pop();
103 }
104 mUsedInBytes += protoSize;
105 mStorage.emplace();
106 mStorage.back().Swap(&proto);
107}
108
109void SurfaceTracing::LayersTraceBuffer::flush(LayersTraceFileProto* fileProto) {
110 fileProto->mutable_entry()->Reserve(mStorage.size());
111
112 while (!mStorage.empty()) {
113 auto entry = fileProto->add_entry();
114 entry->Swap(&mStorage.front());
115 mStorage.pop();
116 }
117}
118
Nataniel Borges2b796da2019-02-15 13:32:18 -0800119void SurfaceTracing::enable() {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700120 std::scoped_lock lock(mTraceLock);
Chia-I Wua3e7ddc2018-09-20 11:42:46 -0700121
Adrian Roos1e1a1282017-11-01 19:05:31 +0100122 if (mEnabled) {
123 return;
124 }
Nataniel Borges2b796da2019-02-15 13:32:18 -0800125 mBuffer.reset(mBufferSize);
Adrian Roos1e1a1282017-11-01 19:05:31 +0100126 mEnabled = true;
Nataniel Borges2b796da2019-02-15 13:32:18 -0800127 mThread = std::thread(&SurfaceTracing::mainLoop, this);
Adrian Roos1e1a1282017-11-01 19:05:31 +0100128}
129
Nataniel Borges2b796da2019-02-15 13:32:18 -0800130status_t SurfaceTracing::writeToFile() {
Alec Mouri50aeef62020-03-25 18:52:29 -0700131 std::thread thread;
132 {
133 std::scoped_lock lock(mTraceLock);
134 thread = std::move(mThread);
135 }
136 thread.join();
Nataniel Borges2b796da2019-02-15 13:32:18 -0800137 return mLastErr;
138}
139
140bool SurfaceTracing::disable() {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700141 std::scoped_lock lock(mTraceLock);
Chia-I Wua3e7ddc2018-09-20 11:42:46 -0700142
Adrian Roos1e1a1282017-11-01 19:05:31 +0100143 if (!mEnabled) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800144 return false;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100145 }
Nataniel Borges2b796da2019-02-15 13:32:18 -0800146
Adrian Roos1e1a1282017-11-01 19:05:31 +0100147 mEnabled = false;
Nataniel Borges2b796da2019-02-15 13:32:18 -0800148 mWriteToFile = true;
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700149 mCanStartTrace.notify_all();
Nataniel Borges2b796da2019-02-15 13:32:18 -0800150 return true;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100151}
152
Yichi Chenadc69612018-09-15 14:51:18 +0800153bool SurfaceTracing::isEnabled() const {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700154 std::scoped_lock lock(mTraceLock);
Adrian Roos1e1a1282017-11-01 19:05:31 +0100155 return mEnabled;
156}
157
Nataniel Borges2b796da2019-02-15 13:32:18 -0800158void SurfaceTracing::setBufferSize(size_t bufferSizeInByte) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700159 std::scoped_lock lock(mTraceLock);
Nataniel Borges2b796da2019-02-15 13:32:18 -0800160 mBufferSize = bufferSizeInByte;
161 mBuffer.setSize(bufferSizeInByte);
162}
163
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700164void SurfaceTracing::setTraceFlags(uint32_t flags) {
165 std::scoped_lock lock(mSfLock);
166 mTraceFlags = flags;
167}
168
Alec Mouri5793c7d2020-03-10 19:55:50 -0700169LayersTraceProto SurfaceTracing::traceLayersLocked(const char* where,
170 const sp<const DisplayDevice>& displayDevice) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800171 ATRACE_CALL();
Yichi Chenadc69612018-09-15 14:51:18 +0800172
Yichi Chen9c696ed2018-10-01 22:32:30 +0800173 LayersTraceProto entry;
174 entry.set_elapsed_realtime_nanos(elapsedRealtimeNano());
175 entry.set_where(where);
Alec Mouri5793c7d2020-03-10 19:55:50 -0700176 LayersProto layers(mFlinger.dumpDrawingStateProto(mTraceFlags, displayDevice));
Vishnu Nair0f085c62019-08-30 08:49:12 -0700177 mFlinger.dumpOffscreenLayersProto(layers);
Yichi Chen9c696ed2018-10-01 22:32:30 +0800178 entry.mutable_layers()->Swap(&layers);
179
Alec Mouri6b9e9912020-01-21 10:50:24 -0800180 if (mTraceFlags & SurfaceTracing::TRACE_HWC) {
181 std::string hwcDump;
182 mFlinger.dumpHwc(hwcDump);
183 entry.set_hwc_blob(hwcDump);
184 }
185
Nataniel Borges2b796da2019-02-15 13:32:18 -0800186 return entry;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100187}
188
Nataniel Borges2b796da2019-02-15 13:32:18 -0800189void SurfaceTracing::writeProtoFileLocked() {
Adrian Roos1e1a1282017-11-01 19:05:31 +0100190 ATRACE_CALL();
191
Yichi Chen9c696ed2018-10-01 22:32:30 +0800192 LayersTraceFileProto fileProto;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100193 std::string output;
Yichi Chen9c696ed2018-10-01 22:32:30 +0800194
195 fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
196 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
197 mBuffer.flush(&fileProto);
Nataniel Borges2b796da2019-02-15 13:32:18 -0800198 mBuffer.reset(mBufferSize);
Yichi Chen9c696ed2018-10-01 22:32:30 +0800199
200 if (!fileProto.SerializeToString(&output)) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800201 ALOGE("Could not save the proto file! Permission denied");
202 mLastErr = PERMISSION_DENIED;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100203 }
chaviwd1759e02020-01-23 11:28:09 -0800204
205 // -rw-r--r--
206 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
207 if (!android::base::WriteStringToFile(output, kDefaultFileName, mode, getuid(), getgid(),
208 true)) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800209 ALOGE("Could not save the proto file! There are missing fields");
210 mLastErr = PERMISSION_DENIED;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100211 }
212
Nataniel Borges2b796da2019-02-15 13:32:18 -0800213 mLastErr = NO_ERROR;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100214}
215
Yiwei Zhang5434a782018-12-05 18:06:32 -0800216void SurfaceTracing::dump(std::string& result) const {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700217 std::scoped_lock lock(mTraceLock);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800218 base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
219 base::StringAppendF(&result, " number of entries: %zu (%.2fMB / %.2fMB)\n",
220 mBuffer.frameCount(), float(mBuffer.used()) / float(1_MB),
221 float(mBuffer.size()) / float(1_MB));
Yichi Chenadc69612018-09-15 14:51:18 +0800222}
223
Adrian Roos1e1a1282017-11-01 19:05:31 +0100224} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800225
226// TODO(b/129481165): remove the #pragma below and fix conversion issues
227#pragma clang diagnostic pop // ignored "-Wconversion"