blob: 20c8d7a3aa81456146e29aea73c1c24210266454 [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() {
39 addFirstEntry();
40 bool enabled = true;
41 while (enabled) {
42 LayersTraceProto entry = traceWhenNotified();
43 enabled = addTraceToBuffer(entry);
44 }
Nataniel Borges2b796da2019-02-15 13:32:18 -080045}
46
Vishnu Nair9245d3b2019-03-22 13:38:56 -070047void SurfaceTracing::addFirstEntry() {
Alec Mouri5793c7d2020-03-10 19:55:50 -070048 const auto displayDevice = mFlinger.getDefaultDisplayDevice();
Vishnu Nair9245d3b2019-03-22 13:38:56 -070049 LayersTraceProto entry;
50 {
51 std::scoped_lock lock(mSfLock);
Alec Mouri5793c7d2020-03-10 19:55:50 -070052 entry = traceLayersLocked("tracing.enable", displayDevice);
Vishnu Nair9245d3b2019-03-22 13:38:56 -070053 }
54 addTraceToBuffer(entry);
55}
56
57LayersTraceProto SurfaceTracing::traceWhenNotified() {
Alec Mouri5793c7d2020-03-10 19:55:50 -070058 const auto displayDevice = mFlinger.getDefaultDisplayDevice();
Vishnu Nair9245d3b2019-03-22 13:38:56 -070059 std::unique_lock<std::mutex> lock(mSfLock);
60 mCanStartTrace.wait(lock);
61 android::base::ScopedLockAssertion assumeLock(mSfLock);
Alec Mouri5793c7d2020-03-10 19:55:50 -070062 LayersTraceProto entry = traceLayersLocked(mWhere, displayDevice);
Vishnu Nair9245d3b2019-03-22 13:38:56 -070063 lock.unlock();
64 return entry;
65}
66
67bool SurfaceTracing::addTraceToBuffer(LayersTraceProto& entry) {
68 std::scoped_lock lock(mTraceLock);
Nataniel Borges2b796da2019-02-15 13:32:18 -080069 mBuffer.emplace(std::move(entry));
Vishnu Nair9245d3b2019-03-22 13:38:56 -070070 if (mWriteToFile) {
71 writeProtoFileLocked();
72 mWriteToFile = false;
73 }
74 return mEnabled;
Nataniel Borges2b796da2019-02-15 13:32:18 -080075}
76
77void SurfaceTracing::notify(const char* where) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -070078 std::scoped_lock lock(mSfLock);
Vishnu Nairb0159482019-03-18 12:48:46 -070079 mWhere = where;
Vishnu Nair9245d3b2019-03-22 13:38:56 -070080 mCanStartTrace.notify_one();
Nataniel Borges2b796da2019-02-15 13:32:18 -080081}
82
83void SurfaceTracing::writeToFileAsync() {
Vishnu Nair9245d3b2019-03-22 13:38:56 -070084 std::scoped_lock lock(mTraceLock);
Nataniel Borges2b796da2019-02-15 13:32:18 -080085 mWriteToFile = true;
Vishnu Nair9245d3b2019-03-22 13:38:56 -070086 mCanStartTrace.notify_one();
Nataniel Borges2b796da2019-02-15 13:32:18 -080087}
88
Yichi Chen9c696ed2018-10-01 22:32:30 +080089void SurfaceTracing::LayersTraceBuffer::reset(size_t newSize) {
90 // use the swap trick to make sure memory is released
91 std::queue<LayersTraceProto>().swap(mStorage);
92 mSizeInBytes = newSize;
93 mUsedInBytes = 0U;
94}
95
96void SurfaceTracing::LayersTraceBuffer::emplace(LayersTraceProto&& proto) {
97 auto protoSize = proto.ByteSize();
98 while (mUsedInBytes + protoSize > mSizeInBytes) {
99 if (mStorage.empty()) {
100 return;
101 }
102 mUsedInBytes -= mStorage.front().ByteSize();
103 mStorage.pop();
104 }
105 mUsedInBytes += protoSize;
106 mStorage.emplace();
107 mStorage.back().Swap(&proto);
108}
109
110void SurfaceTracing::LayersTraceBuffer::flush(LayersTraceFileProto* fileProto) {
111 fileProto->mutable_entry()->Reserve(mStorage.size());
112
113 while (!mStorage.empty()) {
114 auto entry = fileProto->add_entry();
115 entry->Swap(&mStorage.front());
116 mStorage.pop();
117 }
118}
119
Nataniel Borges2b796da2019-02-15 13:32:18 -0800120void SurfaceTracing::enable() {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700121 std::scoped_lock lock(mTraceLock);
Chia-I Wua3e7ddc2018-09-20 11:42:46 -0700122
Adrian Roos1e1a1282017-11-01 19:05:31 +0100123 if (mEnabled) {
124 return;
125 }
Nataniel Borges2b796da2019-02-15 13:32:18 -0800126 mBuffer.reset(mBufferSize);
Adrian Roos1e1a1282017-11-01 19:05:31 +0100127 mEnabled = true;
Nataniel Borges2b796da2019-02-15 13:32:18 -0800128 mThread = std::thread(&SurfaceTracing::mainLoop, this);
Adrian Roos1e1a1282017-11-01 19:05:31 +0100129}
130
Nataniel Borges2b796da2019-02-15 13:32:18 -0800131status_t SurfaceTracing::writeToFile() {
132 mThread.join();
133 return mLastErr;
134}
135
136bool SurfaceTracing::disable() {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700137 std::scoped_lock lock(mTraceLock);
Chia-I Wua3e7ddc2018-09-20 11:42:46 -0700138
Adrian Roos1e1a1282017-11-01 19:05:31 +0100139 if (!mEnabled) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800140 return false;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100141 }
Nataniel Borges2b796da2019-02-15 13:32:18 -0800142
Adrian Roos1e1a1282017-11-01 19:05:31 +0100143 mEnabled = false;
Nataniel Borges2b796da2019-02-15 13:32:18 -0800144 mWriteToFile = true;
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700145 mCanStartTrace.notify_all();
Nataniel Borges2b796da2019-02-15 13:32:18 -0800146 return true;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100147}
148
Yichi Chenadc69612018-09-15 14:51:18 +0800149bool SurfaceTracing::isEnabled() const {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700150 std::scoped_lock lock(mTraceLock);
Adrian Roos1e1a1282017-11-01 19:05:31 +0100151 return mEnabled;
152}
153
Nataniel Borges2b796da2019-02-15 13:32:18 -0800154void SurfaceTracing::setBufferSize(size_t bufferSizeInByte) {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700155 std::scoped_lock lock(mTraceLock);
Nataniel Borges2b796da2019-02-15 13:32:18 -0800156 mBufferSize = bufferSizeInByte;
157 mBuffer.setSize(bufferSizeInByte);
158}
159
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700160void SurfaceTracing::setTraceFlags(uint32_t flags) {
161 std::scoped_lock lock(mSfLock);
162 mTraceFlags = flags;
163}
164
Alec Mouri5793c7d2020-03-10 19:55:50 -0700165LayersTraceProto SurfaceTracing::traceLayersLocked(const char* where,
166 const sp<const DisplayDevice>& displayDevice) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800167 ATRACE_CALL();
Yichi Chenadc69612018-09-15 14:51:18 +0800168
Yichi Chen9c696ed2018-10-01 22:32:30 +0800169 LayersTraceProto entry;
170 entry.set_elapsed_realtime_nanos(elapsedRealtimeNano());
171 entry.set_where(where);
Alec Mouri5793c7d2020-03-10 19:55:50 -0700172 LayersProto layers(mFlinger.dumpDrawingStateProto(mTraceFlags, displayDevice));
Vishnu Nair0f085c62019-08-30 08:49:12 -0700173 mFlinger.dumpOffscreenLayersProto(layers);
Yichi Chen9c696ed2018-10-01 22:32:30 +0800174 entry.mutable_layers()->Swap(&layers);
175
Alec Mouri6b9e9912020-01-21 10:50:24 -0800176 if (mTraceFlags & SurfaceTracing::TRACE_HWC) {
177 std::string hwcDump;
178 mFlinger.dumpHwc(hwcDump);
179 entry.set_hwc_blob(hwcDump);
180 }
181
Nataniel Borges2b796da2019-02-15 13:32:18 -0800182 return entry;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100183}
184
Nataniel Borges2b796da2019-02-15 13:32:18 -0800185void SurfaceTracing::writeProtoFileLocked() {
Adrian Roos1e1a1282017-11-01 19:05:31 +0100186 ATRACE_CALL();
187
Yichi Chen9c696ed2018-10-01 22:32:30 +0800188 LayersTraceFileProto fileProto;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100189 std::string output;
Yichi Chen9c696ed2018-10-01 22:32:30 +0800190
191 fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
192 LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
193 mBuffer.flush(&fileProto);
Nataniel Borges2b796da2019-02-15 13:32:18 -0800194 mBuffer.reset(mBufferSize);
Yichi Chen9c696ed2018-10-01 22:32:30 +0800195
196 if (!fileProto.SerializeToString(&output)) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800197 ALOGE("Could not save the proto file! Permission denied");
198 mLastErr = PERMISSION_DENIED;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100199 }
chaviwd1759e02020-01-23 11:28:09 -0800200
201 // -rw-r--r--
202 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
203 if (!android::base::WriteStringToFile(output, kDefaultFileName, mode, getuid(), getgid(),
204 true)) {
Nataniel Borges2b796da2019-02-15 13:32:18 -0800205 ALOGE("Could not save the proto file! There are missing fields");
206 mLastErr = PERMISSION_DENIED;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100207 }
208
Nataniel Borges2b796da2019-02-15 13:32:18 -0800209 mLastErr = NO_ERROR;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100210}
211
Yiwei Zhang5434a782018-12-05 18:06:32 -0800212void SurfaceTracing::dump(std::string& result) const {
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700213 std::scoped_lock lock(mTraceLock);
Yiwei Zhang5434a782018-12-05 18:06:32 -0800214 base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
215 base::StringAppendF(&result, " number of entries: %zu (%.2fMB / %.2fMB)\n",
216 mBuffer.frameCount(), float(mBuffer.used()) / float(1_MB),
217 float(mBuffer.size()) / float(1_MB));
Yichi Chenadc69612018-09-15 14:51:18 +0800218}
219
Adrian Roos1e1a1282017-11-01 19:05:31 +0100220} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800221
222// TODO(b/129481165): remove the #pragma below and fix conversion issues
223#pragma clang diagnostic pop // ignored "-Wconversion"