blob: f0b895d0473d6923bcd76d8f1f1612bbce0056d4 [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 Nair60db8c02020-04-02 11:55:16 -070062 mTracingInProgress = false;
63 mMissedTraceEntries = 0;
Vishnu Nair9245d3b2019-03-22 13:38:56 -070064 lock.unlock();
65 return entry;
66}
67
68bool SurfaceTracing::addTraceToBuffer(LayersTraceProto& entry) {
69 std::scoped_lock lock(mTraceLock);
Nataniel Borges2b796da2019-02-15 13:32:18 -080070 mBuffer.emplace(std::move(entry));
Vishnu Nair9245d3b2019-03-22 13:38:56 -070071 if (mWriteToFile) {
72 writeProtoFileLocked();
73 mWriteToFile = false;
74 }
75 return mEnabled;
Nataniel Borges2b796da2019-02-15 13:32:18 -080076}
77
78void SurfaceTracing::notify(const char* where) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -070079 std::scoped_lock lock(mSfLock);
Vishnu Nair60db8c02020-04-02 11:55:16 -070080 notifyLocked(where);
81}
82
83void SurfaceTracing::notifyLocked(const char* where) {
Vishnu Nairb0159482019-03-18 12:48:46 -070084 mWhere = where;
Vishnu Nair60db8c02020-04-02 11:55:16 -070085 if (mTracingInProgress) {
86 mMissedTraceEntries++;
87 }
88 mTracingInProgress = true;
Vishnu Nair9245d3b2019-03-22 13:38:56 -070089 mCanStartTrace.notify_one();
Nataniel Borges2b796da2019-02-15 13:32:18 -080090}
91
92void SurfaceTracing::writeToFileAsync() {
Vishnu Nair9245d3b2019-03-22 13:38:56 -070093 std::scoped_lock lock(mTraceLock);
Nataniel Borges2b796da2019-02-15 13:32:18 -080094 mWriteToFile = true;
Vishnu Nair9245d3b2019-03-22 13:38:56 -070095 mCanStartTrace.notify_one();
Nataniel Borges2b796da2019-02-15 13:32:18 -080096}
97
Yichi Chen9c696ed2018-10-01 22:32:30 +080098void SurfaceTracing::LayersTraceBuffer::reset(size_t newSize) {
99 // use the swap trick to make sure memory is released
100 std::queue<LayersTraceProto>().swap(mStorage);
101 mSizeInBytes = newSize;
102 mUsedInBytes = 0U;
103}
104
105void SurfaceTracing::LayersTraceBuffer::emplace(LayersTraceProto&& proto) {
106 auto protoSize = proto.ByteSize();
107 while (mUsedInBytes + protoSize > mSizeInBytes) {
108 if (mStorage.empty()) {
109 return;
110 }
111 mUsedInBytes -= mStorage.front().ByteSize();
112 mStorage.pop();
113 }
114 mUsedInBytes += protoSize;
115 mStorage.emplace();
116 mStorage.back().Swap(&proto);
117}
118
119void SurfaceTracing::LayersTraceBuffer::flush(LayersTraceFileProto* fileProto) {
120 fileProto->mutable_entry()->Reserve(mStorage.size());
121
122 while (!mStorage.empty()) {
123 auto entry = fileProto->add_entry();
124 entry->Swap(&mStorage.front());
125 mStorage.pop();
126 }
127}
128
Nataniel Borges2b796da2019-02-15 13:32:18 -0800129void SurfaceTracing::enable() {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700130 std::scoped_lock lock(mTraceLock);
Chia-I Wua3e7ddc2018-09-20 11:42:46 -0700131
Adrian Roos1e1a1282017-11-01 19:05:31 +0100132 if (mEnabled) {
133 return;
134 }
Nataniel Borges2b796da2019-02-15 13:32:18 -0800135 mBuffer.reset(mBufferSize);
Adrian Roos1e1a1282017-11-01 19:05:31 +0100136 mEnabled = true;
Nataniel Borges2b796da2019-02-15 13:32:18 -0800137 mThread = std::thread(&SurfaceTracing::mainLoop, this);
Adrian Roos1e1a1282017-11-01 19:05:31 +0100138}
139
Nataniel Borges2b796da2019-02-15 13:32:18 -0800140status_t SurfaceTracing::writeToFile() {
Alec Mouri50aeef62020-03-25 18:52:29 -0700141 std::thread thread;
142 {
143 std::scoped_lock lock(mTraceLock);
144 thread = std::move(mThread);
145 }
146 thread.join();
Nataniel Borges2b796da2019-02-15 13:32:18 -0800147 return mLastErr;
148}
149
150bool SurfaceTracing::disable() {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700151 std::scoped_lock lock(mTraceLock);
Chia-I Wua3e7ddc2018-09-20 11:42:46 -0700152
Adrian Roos1e1a1282017-11-01 19:05:31 +0100153 if (!mEnabled) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800154 return false;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100155 }
Nataniel Borges2b796da2019-02-15 13:32:18 -0800156
Adrian Roos1e1a1282017-11-01 19:05:31 +0100157 mEnabled = false;
Nataniel Borges2b796da2019-02-15 13:32:18 -0800158 mWriteToFile = true;
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700159 mCanStartTrace.notify_all();
Nataniel Borges2b796da2019-02-15 13:32:18 -0800160 return true;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100161}
162
Yichi Chenadc69612018-09-15 14:51:18 +0800163bool SurfaceTracing::isEnabled() const {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700164 std::scoped_lock lock(mTraceLock);
Adrian Roos1e1a1282017-11-01 19:05:31 +0100165 return mEnabled;
166}
167
Nataniel Borges2b796da2019-02-15 13:32:18 -0800168void SurfaceTracing::setBufferSize(size_t bufferSizeInByte) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700169 std::scoped_lock lock(mTraceLock);
Nataniel Borges2b796da2019-02-15 13:32:18 -0800170 mBufferSize = bufferSizeInByte;
171 mBuffer.setSize(bufferSizeInByte);
172}
173
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700174void SurfaceTracing::setTraceFlags(uint32_t flags) {
175 std::scoped_lock lock(mSfLock);
176 mTraceFlags = flags;
177}
178
Alec Mouri5793c7d2020-03-10 19:55:50 -0700179LayersTraceProto SurfaceTracing::traceLayersLocked(const char* where,
180 const sp<const DisplayDevice>& displayDevice) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800181 ATRACE_CALL();
Yichi Chenadc69612018-09-15 14:51:18 +0800182
Yichi Chen9c696ed2018-10-01 22:32:30 +0800183 LayersTraceProto entry;
184 entry.set_elapsed_realtime_nanos(elapsedRealtimeNano());
185 entry.set_where(where);
Alec Mouri5793c7d2020-03-10 19:55:50 -0700186 LayersProto layers(mFlinger.dumpDrawingStateProto(mTraceFlags, displayDevice));
Vishnu Nair60db8c02020-04-02 11:55:16 -0700187
188 if (flagIsSetLocked(SurfaceTracing::TRACE_EXTRA)) {
189 mFlinger.dumpOffscreenLayersProto(layers);
190 }
Yichi Chen9c696ed2018-10-01 22:32:30 +0800191 entry.mutable_layers()->Swap(&layers);
192
Alec Mouri6b9e9912020-01-21 10:50:24 -0800193 if (mTraceFlags & SurfaceTracing::TRACE_HWC) {
194 std::string hwcDump;
195 mFlinger.dumpHwc(hwcDump);
196 entry.set_hwc_blob(hwcDump);
197 }
Vishnu Nair60db8c02020-04-02 11:55:16 -0700198 if (!flagIsSetLocked(SurfaceTracing::TRACE_COMPOSITION)) {
199 entry.set_excludes_composition_state(true);
200 }
201 entry.set_missed_entries(mMissedTraceEntries);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800202
Nataniel Borges2b796da2019-02-15 13:32:18 -0800203 return entry;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100204}
205
Nataniel Borges2b796da2019-02-15 13:32:18 -0800206void SurfaceTracing::writeProtoFileLocked() {
Adrian Roos1e1a1282017-11-01 19:05:31 +0100207 ATRACE_CALL();
208
Yichi Chen9c696ed2018-10-01 22:32:30 +0800209 LayersTraceFileProto fileProto;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100210 std::string output;
Yichi Chen9c696ed2018-10-01 22:32:30 +0800211
212 fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
213 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
214 mBuffer.flush(&fileProto);
Nataniel Borges2b796da2019-02-15 13:32:18 -0800215 mBuffer.reset(mBufferSize);
Yichi Chen9c696ed2018-10-01 22:32:30 +0800216
217 if (!fileProto.SerializeToString(&output)) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800218 ALOGE("Could not save the proto file! Permission denied");
219 mLastErr = PERMISSION_DENIED;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100220 }
chaviwd1759e02020-01-23 11:28:09 -0800221
222 // -rw-r--r--
223 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
224 if (!android::base::WriteStringToFile(output, kDefaultFileName, mode, getuid(), getgid(),
225 true)) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800226 ALOGE("Could not save the proto file! There are missing fields");
227 mLastErr = PERMISSION_DENIED;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100228 }
229
Nataniel Borges2b796da2019-02-15 13:32:18 -0800230 mLastErr = NO_ERROR;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100231}
232
Yiwei Zhang5434a782018-12-05 18:06:32 -0800233void SurfaceTracing::dump(std::string& result) const {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700234 std::scoped_lock lock(mTraceLock);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800235 base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
236 base::StringAppendF(&result, " number of entries: %zu (%.2fMB / %.2fMB)\n",
237 mBuffer.frameCount(), float(mBuffer.used()) / float(1_MB),
238 float(mBuffer.size()) / float(1_MB));
Yichi Chenadc69612018-09-15 14:51:18 +0800239}
240
Adrian Roos1e1a1282017-11-01 19:05:31 +0100241} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800242
243// TODO(b/129481165): remove the #pragma below and fix conversion issues
244#pragma clang diagnostic pop // ignored "-Wconversion"