SurfaceTracing.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #undef LOG_TAG
  17. #define LOG_TAG "SurfaceTracing"
  18. #define ATRACE_TAG ATRACE_TAG_GRAPHICS
  19. #include "SurfaceTracing.h"
  20. #include <SurfaceFlinger.h>
  21. #include <android-base/file.h>
  22. #include <android-base/stringprintf.h>
  23. #include <log/log.h>
  24. #include <utils/SystemClock.h>
  25. #include <utils/Trace.h>
  26. namespace android {
  27. SurfaceTracing::SurfaceTracing(SurfaceFlinger& flinger)
  28. : mFlinger(flinger), mSfLock(flinger.mDrawingStateLock) {}
  29. void SurfaceTracing::mainLoop() {
  30. addFirstEntry();
  31. bool enabled = true;
  32. while (enabled) {
  33. LayersTraceProto entry = traceWhenNotified();
  34. enabled = addTraceToBuffer(entry);
  35. }
  36. }
  37. void SurfaceTracing::addFirstEntry() {
  38. LayersTraceProto entry;
  39. {
  40. std::scoped_lock lock(mSfLock);
  41. entry = traceLayersLocked("tracing.enable");
  42. }
  43. addTraceToBuffer(entry);
  44. }
  45. LayersTraceProto SurfaceTracing::traceWhenNotified() {
  46. std::unique_lock<std::mutex> lock(mSfLock);
  47. mCanStartTrace.wait(lock);
  48. android::base::ScopedLockAssertion assumeLock(mSfLock);
  49. LayersTraceProto entry = traceLayersLocked(mWhere);
  50. lock.unlock();
  51. return entry;
  52. }
  53. bool SurfaceTracing::addTraceToBuffer(LayersTraceProto& entry) {
  54. std::scoped_lock lock(mTraceLock);
  55. mBuffer.emplace(std::move(entry));
  56. if (mWriteToFile) {
  57. writeProtoFileLocked();
  58. mWriteToFile = false;
  59. }
  60. return mEnabled;
  61. }
  62. void SurfaceTracing::notify(const char* where) {
  63. std::scoped_lock lock(mSfLock);
  64. mWhere = where;
  65. mCanStartTrace.notify_one();
  66. }
  67. void SurfaceTracing::writeToFileAsync() {
  68. std::scoped_lock lock(mTraceLock);
  69. mWriteToFile = true;
  70. mCanStartTrace.notify_one();
  71. }
  72. void SurfaceTracing::LayersTraceBuffer::reset(size_t newSize) {
  73. // use the swap trick to make sure memory is released
  74. std::queue<LayersTraceProto>().swap(mStorage);
  75. mSizeInBytes = newSize;
  76. mUsedInBytes = 0U;
  77. }
  78. void SurfaceTracing::LayersTraceBuffer::emplace(LayersTraceProto&& proto) {
  79. auto protoSize = proto.ByteSize();
  80. while (mUsedInBytes + protoSize > mSizeInBytes) {
  81. if (mStorage.empty()) {
  82. return;
  83. }
  84. mUsedInBytes -= mStorage.front().ByteSize();
  85. mStorage.pop();
  86. }
  87. mUsedInBytes += protoSize;
  88. mStorage.emplace();
  89. mStorage.back().Swap(&proto);
  90. }
  91. void SurfaceTracing::LayersTraceBuffer::flush(LayersTraceFileProto* fileProto) {
  92. fileProto->mutable_entry()->Reserve(mStorage.size());
  93. while (!mStorage.empty()) {
  94. auto entry = fileProto->add_entry();
  95. entry->Swap(&mStorage.front());
  96. mStorage.pop();
  97. }
  98. }
  99. void SurfaceTracing::enable() {
  100. std::scoped_lock lock(mTraceLock);
  101. if (mEnabled) {
  102. return;
  103. }
  104. mBuffer.reset(mBufferSize);
  105. mEnabled = true;
  106. mThread = std::thread(&SurfaceTracing::mainLoop, this);
  107. }
  108. status_t SurfaceTracing::writeToFile() {
  109. mThread.join();
  110. return mLastErr;
  111. }
  112. bool SurfaceTracing::disable() {
  113. std::scoped_lock lock(mTraceLock);
  114. if (!mEnabled) {
  115. return false;
  116. }
  117. mEnabled = false;
  118. mWriteToFile = true;
  119. mCanStartTrace.notify_all();
  120. return true;
  121. }
  122. bool SurfaceTracing::isEnabled() const {
  123. std::scoped_lock lock(mTraceLock);
  124. return mEnabled;
  125. }
  126. void SurfaceTracing::setBufferSize(size_t bufferSizeInByte) {
  127. std::scoped_lock lock(mTraceLock);
  128. mBufferSize = bufferSizeInByte;
  129. mBuffer.setSize(bufferSizeInByte);
  130. }
  131. void SurfaceTracing::setTraceFlags(uint32_t flags) {
  132. std::scoped_lock lock(mSfLock);
  133. mTraceFlags = flags;
  134. }
  135. LayersTraceProto SurfaceTracing::traceLayersLocked(const char* where) {
  136. ATRACE_CALL();
  137. LayersTraceProto entry;
  138. entry.set_elapsed_realtime_nanos(elapsedRealtimeNano());
  139. entry.set_where(where);
  140. LayersProto layers(mFlinger.dumpProtoInfo(LayerVector::StateSet::Drawing, mTraceFlags));
  141. entry.mutable_layers()->Swap(&layers);
  142. return entry;
  143. }
  144. void SurfaceTracing::writeProtoFileLocked() {
  145. ATRACE_CALL();
  146. LayersTraceFileProto fileProto;
  147. std::string output;
  148. fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 |
  149. LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L);
  150. mBuffer.flush(&fileProto);
  151. mBuffer.reset(mBufferSize);
  152. if (!fileProto.SerializeToString(&output)) {
  153. ALOGE("Could not save the proto file! Permission denied");
  154. mLastErr = PERMISSION_DENIED;
  155. }
  156. if (!android::base::WriteStringToFile(output, kDefaultFileName, S_IRWXU | S_IRGRP, getuid(),
  157. getgid(), true)) {
  158. ALOGE("Could not save the proto file! There are missing fields");
  159. mLastErr = PERMISSION_DENIED;
  160. }
  161. mLastErr = NO_ERROR;
  162. }
  163. void SurfaceTracing::dump(std::string& result) const {
  164. std::scoped_lock lock(mTraceLock);
  165. base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled");
  166. base::StringAppendF(&result, " number of entries: %zu (%.2fMB / %.2fMB)\n",
  167. mBuffer.frameCount(), float(mBuffer.used()) / float(1_MB),
  168. float(mBuffer.size()) / float(1_MB));
  169. }
  170. } // namespace android