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
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 #undef LOG_TAG
21 #define LOG_TAG "SurfaceTracing"
22 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
23
24 #include "SurfaceTracing.h"
25 #include <SurfaceFlinger.h>
26
27 #include <android-base/file.h>
28 #include <android-base/stringprintf.h>
29 #include <log/log.h>
30 #include <utils/SystemClock.h>
31 #include <utils/Trace.h>
32
33 namespace android {
34
SurfaceTracing(SurfaceFlinger & flinger)35 SurfaceTracing::SurfaceTracing(SurfaceFlinger& flinger)
36 : mFlinger(flinger), mSfLock(flinger.mTracingLock) {}
37
mainLoop()38 void SurfaceTracing::mainLoop() {
39 bool enabled = addFirstEntry();
40 while (enabled) {
41 LayersTraceProto entry = traceWhenNotified();
42 enabled = addTraceToBuffer(entry);
43 }
44 }
45
addFirstEntry()46 bool SurfaceTracing::addFirstEntry() {
47 LayersTraceProto entry;
48 {
49 std::scoped_lock lock(mSfLock);
50 entry = traceLayersLocked("tracing.enable");
51 }
52 return addTraceToBuffer(entry);
53 }
54
traceWhenNotified()55 LayersTraceProto SurfaceTracing::traceWhenNotified() {
56 std::unique_lock<std::mutex> lock(mSfLock);
57 mCanStartTrace.wait(lock);
58 android::base::ScopedLockAssertion assumeLock(mSfLock);
59 LayersTraceProto entry = traceLayersLocked(mWhere);
60 mTracingInProgress = false;
61 mMissedTraceEntries = 0;
62 lock.unlock();
63 return entry;
64 }
65
addTraceToBuffer(LayersTraceProto & entry)66 bool SurfaceTracing::addTraceToBuffer(LayersTraceProto& entry) {
67 std::scoped_lock lock(mTraceLock);
68 mBuffer.emplace(std::move(entry));
69 if (mWriteToFile) {
70 writeProtoFileLocked();
71 mWriteToFile = false;
72 }
73 return mEnabled;
74 }
75
notify(const char * where)76 void SurfaceTracing::notify(const char* where) {
77 std::scoped_lock lock(mSfLock);
78 notifyLocked(where);
79 }
80
notifyLocked(const char * where)81 void SurfaceTracing::notifyLocked(const char* where) {
82 mWhere = where;
83 if (mTracingInProgress) {
84 mMissedTraceEntries++;
85 }
86 mTracingInProgress = true;
87 mCanStartTrace.notify_one();
88 }
89
writeToFileAsync()90 void SurfaceTracing::writeToFileAsync() {
91 std::scoped_lock lock(mTraceLock);
92 mWriteToFile = true;
93 mCanStartTrace.notify_one();
94 }
95
reset(size_t newSize)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
emplace(LayersTraceProto && proto)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
flush(LayersTraceFileProto * fileProto)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
enable()127 bool SurfaceTracing::enable() {
128 std::scoped_lock lock(mTraceLock);
129
130 if (mEnabled) {
131 return false;
132 }
133
134 mBuffer.reset(mBufferSize);
135 mEnabled = true;
136 mThread = std::thread(&SurfaceTracing::mainLoop, this);
137 return true;
138 }
139
writeToFile()140 status_t SurfaceTracing::writeToFile() {
141 std::thread thread;
142 {
143 std::scoped_lock lock(mTraceLock);
144 thread = std::move(mThread);
145 }
146 thread.join();
147 return mLastErr;
148 }
149
disable()150 bool SurfaceTracing::disable() {
151 std::scoped_lock lock(mTraceLock);
152
153 if (!mEnabled) {
154 return false;
155 }
156
157 mEnabled = false;
158 mWriteToFile = true;
159 mCanStartTrace.notify_all();
160 return true;
161 }
162
isEnabled() const163 bool SurfaceTracing::isEnabled() const {
164 std::scoped_lock lock(mTraceLock);
165 return mEnabled;
166 }
167
setBufferSize(size_t bufferSizeInByte)168 void SurfaceTracing::setBufferSize(size_t bufferSizeInByte) {
169 std::scoped_lock lock(mTraceLock);
170 mBufferSize = bufferSizeInByte;
171 mBuffer.setSize(bufferSizeInByte);
172 }
173
setTraceFlags(uint32_t flags)174 void SurfaceTracing::setTraceFlags(uint32_t flags) {
175 std::scoped_lock lock(mSfLock);
176 mTraceFlags = flags;
177 }
178
traceLayersLocked(const char * where)179 LayersTraceProto SurfaceTracing::traceLayersLocked(const char* where) {
180 ATRACE_CALL();
181
182 LayersTraceProto entry;
183 entry.set_elapsed_realtime_nanos(elapsedRealtimeNano());
184 entry.set_where(where);
185 LayersProto layers(mFlinger.dumpDrawingStateProto(mTraceFlags));
186
187 if (flagIsSetLocked(SurfaceTracing::TRACE_EXTRA)) {
188 mFlinger.dumpOffscreenLayersProto(layers);
189 }
190 entry.mutable_layers()->Swap(&layers);
191
192 if (mTraceFlags & SurfaceTracing::TRACE_HWC) {
193 std::string hwcDump;
194 mFlinger.dumpHwc(hwcDump);
195 entry.set_hwc_blob(hwcDump);
196 }
197 if (!flagIsSetLocked(SurfaceTracing::TRACE_COMPOSITION)) {
198 entry.set_excludes_composition_state(true);
199 }
200 entry.set_missed_entries(mMissedTraceEntries);
201
202 return entry;
203 }
204
writeProtoFileLocked()205 void SurfaceTracing::writeProtoFileLocked() {
206 ATRACE_CALL();
207
208 LayersTraceFileProto fileProto;
209 std::string output;
210
211 fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
212 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
213 mBuffer.flush(&fileProto);
214 mBuffer.reset(mBufferSize);
215
216 if (!fileProto.SerializeToString(&output)) {
217 ALOGE("Could not save the proto file! Permission denied");
218 mLastErr = PERMISSION_DENIED;
219 }
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)) {
225 ALOGE("Could not save the proto file! There are missing fields");
226 mLastErr = PERMISSION_DENIED;
227 }
228
229 mLastErr = NO_ERROR;
230 }
231
dump(std::string & result) const232 void SurfaceTracing::dump(std::string& result) const {
233 std::scoped_lock lock(mTraceLock);
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));
238 }
239
240 } // namespace android
241
242 // TODO(b/129481165): remove the #pragma below and fix conversion issues
243 #pragma clang diagnostic pop // ignored "-Wconversion"
244