SurfaceTracing.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #pragma once
  17. #include <layerproto/LayerProtoHeader.h>
  18. #include <utils/Errors.h>
  19. #include <utils/StrongPointer.h>
  20. #include <android-base/thread_annotations.h>
  21. #include <condition_variable>
  22. #include <memory>
  23. #include <mutex>
  24. #include <queue>
  25. #include <thread>
  26. using namespace android::surfaceflinger;
  27. namespace android {
  28. class SurfaceFlinger;
  29. constexpr auto operator""_MB(unsigned long long const num) {
  30. return num * 1024 * 1024;
  31. }
  32. /*
  33. * SurfaceTracing records layer states during surface flinging.
  34. */
  35. class SurfaceTracing {
  36. public:
  37. explicit SurfaceTracing(SurfaceFlinger& flinger);
  38. void enable();
  39. bool disable();
  40. status_t writeToFile();
  41. bool isEnabled() const;
  42. void notify(const char* where);
  43. void setBufferSize(size_t bufferSizeInByte);
  44. void writeToFileAsync();
  45. void dump(std::string& result) const;
  46. enum : uint32_t {
  47. TRACE_CRITICAL = 1 << 0,
  48. TRACE_INPUT = 1 << 1,
  49. TRACE_EXTRA = 1 << 2,
  50. TRACE_ALL = 0xffffffff
  51. };
  52. void setTraceFlags(uint32_t flags);
  53. private:
  54. static constexpr auto kDefaultBufferCapInByte = 100_MB;
  55. static constexpr auto kDefaultFileName = "/data/misc/wmtrace/layers_trace.pb";
  56. class LayersTraceBuffer { // ring buffer
  57. public:
  58. size_t size() const { return mSizeInBytes; }
  59. size_t used() const { return mUsedInBytes; }
  60. size_t frameCount() const { return mStorage.size(); }
  61. void setSize(size_t newSize) { mSizeInBytes = newSize; }
  62. void reset(size_t newSize);
  63. void emplace(LayersTraceProto&& proto);
  64. void flush(LayersTraceFileProto* fileProto);
  65. private:
  66. size_t mUsedInBytes = 0U;
  67. size_t mSizeInBytes = 0U;
  68. std::queue<LayersTraceProto> mStorage;
  69. };
  70. void mainLoop();
  71. void addFirstEntry();
  72. LayersTraceProto traceWhenNotified();
  73. LayersTraceProto traceLayersLocked(const char* where) REQUIRES(mSfLock);
  74. // Returns true if trace is enabled.
  75. bool addTraceToBuffer(LayersTraceProto& entry);
  76. void writeProtoFileLocked() REQUIRES(mTraceLock);
  77. const SurfaceFlinger& mFlinger;
  78. status_t mLastErr = NO_ERROR;
  79. std::thread mThread;
  80. std::condition_variable mCanStartTrace;
  81. std::mutex& mSfLock;
  82. uint32_t mTraceFlags GUARDED_BY(mSfLock) = TRACE_ALL;
  83. const char* mWhere GUARDED_BY(mSfLock) = "";
  84. mutable std::mutex mTraceLock;
  85. LayersTraceBuffer mBuffer GUARDED_BY(mTraceLock);
  86. size_t mBufferSize GUARDED_BY(mTraceLock) = kDefaultBufferCapInByte;
  87. bool mEnabled GUARDED_BY(mTraceLock) = false;
  88. bool mWriteToFile GUARDED_BY(mTraceLock) = false;
  89. };
  90. } // namespace android