blob: 596373732fe61ae777697e3f3dee39a80303be1a [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
Adrian Roos1e1a1282017-11-01 19:05:31 +010017#undef LOG_TAG
18#define LOG_TAG "SurfaceTracing"
19#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
21#include "SurfaceTracing.h"
Nataniel Borges2b796da2019-02-15 13:32:18 -080022#include <SurfaceFlinger.h>
Adrian Roos1e1a1282017-11-01 19:05:31 +010023
24#include <android-base/file.h>
Yiwei Zhang5434a782018-12-05 18:06:32 -080025#include <android-base/stringprintf.h>
Adrian Roos1e1a1282017-11-01 19:05:31 +010026#include <log/log.h>
27#include <utils/SystemClock.h>
28#include <utils/Trace.h>
29
30namespace android {
31
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070032SurfaceTracing::SurfaceTracing(SurfaceFlinger& flinger) : mFlinger(flinger) {}
Nataniel Borges2b796da2019-02-15 13:32:18 -080033
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070034bool SurfaceTracing::enable() {
Vishnu Nair9245d3b2019-03-22 13:38:56 -070035 std::scoped_lock lock(mTraceLock);
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070036 if (mEnabled) {
37 return false;
Vishnu Nair9245d3b2019-03-22 13:38:56 -070038 }
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070039
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
50bool 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
60bool SurfaceTracing::isEnabled() const {
61 std::scoped_lock lock(mTraceLock);
Vishnu Nair9245d3b2019-03-22 13:38:56 -070062 return mEnabled;
Nataniel Borges2b796da2019-02-15 13:32:18 -080063}
64
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070065status_t SurfaceTracing::writeToFile() {
66 std::scoped_lock lock(mTraceLock);
67 if (!mEnabled) {
68 return STATUS_OK;
69 }
70 return runner->writeToFile();
71}
72
Nataniel Borges2b796da2019-02-15 13:32:18 -080073void SurfaceTracing::notify(const char* where) {
Vishnu Nairc96ad662021-02-12 11:23:19 -080074 std::scoped_lock lock(mTraceLock);
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070075 if (mEnabled) {
76 runner->notify(where);
77 }
Vishnu Nair60db8c02020-04-02 11:55:16 -070078}
79
80void SurfaceTracing::notifyLocked(const char* where) {
Vishnu Nairc96ad662021-02-12 11:23:19 -080081 std::scoped_lock lock(mTraceLock);
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070082 if (mEnabled) {
83 runner->notifyLocked(where);
Vishnu Nair60db8c02020-04-02 11:55:16 -070084 }
Nataniel Borges2b796da2019-02-15 13:32:18 -080085}
86
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070087void SurfaceTracing::dump(std::string& result) const {
Vishnu Nair9245d3b2019-03-22 13:38:56 -070088 std::scoped_lock lock(mTraceLock);
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070089 base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
90 if (mEnabled) {
91 runner->dump(result);
92 }
Nataniel Borges2b796da2019-02-15 13:32:18 -080093}
94
Yichi Chen9c696ed2018-10-01 22:32:30 +080095void 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
102void SurfaceTracing::LayersTraceBuffer::emplace(LayersTraceProto&& proto) {
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700103 size_t protoSize = static_cast<size_t>(proto.ByteSize());
Yichi Chen9c696ed2018-10-01 22:32:30 +0800104 while (mUsedInBytes + protoSize > mSizeInBytes) {
105 if (mStorage.empty()) {
106 return;
107 }
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700108 mUsedInBytes -= static_cast<size_t>(mStorage.front().ByteSize());
Yichi Chen9c696ed2018-10-01 22:32:30 +0800109 mStorage.pop();
110 }
111 mUsedInBytes += protoSize;
112 mStorage.emplace();
113 mStorage.back().Swap(&proto);
114}
115
116void SurfaceTracing::LayersTraceBuffer::flush(LayersTraceFileProto* fileProto) {
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700117 fileProto->mutable_entry()->Reserve(static_cast<int>(mStorage.size()));
Yichi Chen9c696ed2018-10-01 22:32:30 +0800118
119 while (!mStorage.empty()) {
120 auto entry = fileProto->add_entry();
121 entry->Swap(&mStorage.front());
122 mStorage.pop();
123 }
124}
125
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700126SurfaceTracing::Runner::Runner(SurfaceFlinger& flinger, SurfaceTracing::Config& config)
127 : mFlinger(flinger), mConfig(config) {
128 mBuffer.setSize(mConfig.bufferSize);
Adrian Roos1e1a1282017-11-01 19:05:31 +0100129}
130
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700131void SurfaceTracing::Runner::notify(const char* where) {
132 LayersTraceProto entry = traceLayers(where);
133 mBuffer.emplace(std::move(entry));
Nataniel Borges2b796da2019-02-15 13:32:18 -0800134}
135
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700136status_t SurfaceTracing::Runner::stop() {
137 return writeToFile();
Adrian Roos1e1a1282017-11-01 19:05:31 +0100138}
139
chaviw0a398992021-08-13 10:13:01 -0500140LayersTraceFileProto SurfaceTracing::createLayersTraceFileProto() {
141 LayersTraceFileProto fileProto;
142 fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
143 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
144 return fileProto;
145}
146
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700147status_t SurfaceTracing::Runner::writeToFile() {
Adrian Roos1e1a1282017-11-01 19:05:31 +0100148 ATRACE_CALL();
149
chaviw0a398992021-08-13 10:13:01 -0500150 LayersTraceFileProto fileProto = createLayersTraceFileProto();
Adrian Roos1e1a1282017-11-01 19:05:31 +0100151 std::string output;
Yichi Chen9c696ed2018-10-01 22:32:30 +0800152
Yichi Chen9c696ed2018-10-01 22:32:30 +0800153 mBuffer.flush(&fileProto);
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700154 mBuffer.reset(mConfig.bufferSize);
Yichi Chen9c696ed2018-10-01 22:32:30 +0800155
156 if (!fileProto.SerializeToString(&output)) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800157 ALOGE("Could not save the proto file! Permission denied");
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700158 return PERMISSION_DENIED;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100159 }
chaviwd1759e02020-01-23 11:28:09 -0800160
161 // -rw-r--r--
162 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700163 if (!android::base::WriteStringToFile(output, DEFAULT_FILE_NAME, mode, getuid(), getgid(),
chaviwd1759e02020-01-23 11:28:09 -0800164 true)) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800165 ALOGE("Could not save the proto file! There are missing fields");
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700166 return PERMISSION_DENIED;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100167 }
168
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700169 return NO_ERROR;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100170}
171
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700172LayersTraceProto SurfaceTracing::Runner::traceLayers(const char* where) {
173 ATRACE_CALL();
174
175 LayersTraceProto entry;
176 entry.set_elapsed_realtime_nanos(elapsedRealtimeNano());
177 entry.set_where(where);
178 LayersProto layers(mFlinger.dumpDrawingStateProto(mConfig.flags));
179
180 if (flagIsSet(SurfaceTracing::TRACE_EXTRA)) {
181 mFlinger.dumpOffscreenLayersProto(layers);
182 }
183 entry.mutable_layers()->Swap(&layers);
184
185 if (flagIsSet(SurfaceTracing::TRACE_HWC)) {
186 std::string hwcDump;
187 mFlinger.dumpHwc(hwcDump);
188 entry.set_hwc_blob(hwcDump);
189 }
190 if (!flagIsSet(SurfaceTracing::TRACE_COMPOSITION)) {
191 entry.set_excludes_composition_state(true);
192 }
193 entry.set_missed_entries(mMissedTraceEntries);
chaviw0a398992021-08-13 10:13:01 -0500194 mFlinger.dumpDisplayProto(entry);
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700195 return entry;
196}
197
198void SurfaceTracing::Runner::dump(std::string& result) const {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800199 base::StringAppendF(&result, " number of entries: %zu (%.2fMB / %.2fMB)\n",
200 mBuffer.frameCount(), float(mBuffer.used()) / float(1_MB),
201 float(mBuffer.size()) / float(1_MB));
Yichi Chenadc69612018-09-15 14:51:18 +0800202}
203
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700204SurfaceTracing::AsyncRunner::AsyncRunner(SurfaceFlinger& flinger, SurfaceTracing::Config& config,
205 std::mutex& sfLock)
206 : SurfaceTracing::Runner(flinger, config), mSfLock(sfLock) {
207 mEnabled = true;
208 mThread = std::thread(&AsyncRunner::loop, this);
209}
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800210
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700211void SurfaceTracing::AsyncRunner::loop() {
212 while (mEnabled) {
213 LayersTraceProto entry;
214 bool entryAdded = traceWhenNotified(&entry);
215 if (entryAdded) {
216 mBuffer.emplace(std::move(entry));
217 }
218 if (mWriteToFile) {
219 Runner::writeToFile();
220 mWriteToFile = false;
221 }
222 }
223}
224
225bool SurfaceTracing::AsyncRunner::traceWhenNotified(LayersTraceProto* outProto) {
226 std::unique_lock<std::mutex> lock(mSfLock);
227 mCanStartTrace.wait(lock);
228 if (!mAddEntry) {
229 return false;
230 }
231 *outProto = traceLayers(mWhere);
232 mAddEntry = false;
233 mMissedTraceEntries = 0;
234 return true;
235}
236
237void SurfaceTracing::AsyncRunner::notify(const char* where) {
238 std::scoped_lock lock(mSfLock);
239 notifyLocked(where);
240}
241
242void SurfaceTracing::AsyncRunner::notifyLocked(const char* where) {
243 mWhere = where;
244 if (mAddEntry) {
245 mMissedTraceEntries++;
246 }
247 mAddEntry = true;
248 mCanStartTrace.notify_one();
249}
250
251status_t SurfaceTracing::AsyncRunner::writeToFile() {
252 mWriteToFile = true;
253 mCanStartTrace.notify_one();
254 return STATUS_OK;
255}
256
257status_t SurfaceTracing::AsyncRunner::stop() {
258 mEnabled = false;
259 mCanStartTrace.notify_one();
260 mThread.join();
261 return Runner::writeToFile();
262}
263
264} // namespace android