Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 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 | |
| 17 | #pragma once |
| 18 | |
Alec Mouri | 5793c7d | 2020-03-10 19:55:50 -0700 | [diff] [blame] | 19 | #include <android-base/thread_annotations.h> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 20 | #include <layerproto/LayerProtoHeader.h> |
| 21 | #include <utils/Errors.h> |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 22 | #include <utils/StrongPointer.h> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 23 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 24 | #include <condition_variable> |
Chia-I Wu | a3e7ddc | 2018-09-20 11:42:46 -0700 | [diff] [blame] | 25 | #include <memory> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 26 | #include <mutex> |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 27 | #include <queue> |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 28 | #include <thread> |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 29 | |
Alec Mouri | 5793c7d | 2020-03-10 19:55:50 -0700 | [diff] [blame] | 30 | #include "DisplayDevice.h" |
| 31 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 32 | using namespace android::surfaceflinger; |
| 33 | |
| 34 | namespace android { |
| 35 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 36 | class SurfaceFlinger; |
| 37 | |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 38 | constexpr auto operator""_MB(unsigned long long const num) { |
| 39 | return num * 1024 * 1024; |
| 40 | } |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 41 | /* |
| 42 | * SurfaceTracing records layer states during surface flinging. |
| 43 | */ |
| 44 | class SurfaceTracing { |
| 45 | public: |
Dominik Laskowski | 9dab343 | 2019-03-27 13:21:10 -0700 | [diff] [blame] | 46 | explicit SurfaceTracing(SurfaceFlinger& flinger); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 47 | void enable(); |
| 48 | bool disable(); |
| 49 | status_t writeToFile(); |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 50 | bool isEnabled() const; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 51 | void notify(const char* where); |
Vishnu Nair | 60db8c0 | 2020-04-02 11:55:16 -0700 | [diff] [blame^] | 52 | void notifyLocked(const char* where) NO_THREAD_SAFETY_ANALYSIS /* REQUIRES(mSfLock) */; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 53 | |
| 54 | void setBufferSize(size_t bufferSizeInByte); |
| 55 | void writeToFileAsync(); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 56 | void dump(std::string& result) const; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 57 | |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 58 | enum : uint32_t { |
| 59 | TRACE_CRITICAL = 1 << 0, |
| 60 | TRACE_INPUT = 1 << 1, |
Vishnu Nair | 60db8c0 | 2020-04-02 11:55:16 -0700 | [diff] [blame^] | 61 | TRACE_COMPOSITION = 1 << 2, |
| 62 | TRACE_EXTRA = 1 << 3, |
| 63 | TRACE_HWC = 1 << 4, |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 64 | TRACE_ALL = 0xffffffff |
| 65 | }; |
| 66 | void setTraceFlags(uint32_t flags); |
Vishnu Nair | 60db8c0 | 2020-04-02 11:55:16 -0700 | [diff] [blame^] | 67 | bool flagIsSetLocked(uint32_t flags) NO_THREAD_SAFETY_ANALYSIS /* REQUIRES(mSfLock) */ { |
| 68 | return (mTraceFlags & flags) == flags; |
| 69 | } |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 70 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 71 | private: |
Robert Delgado | 626670f | 2019-06-27 15:55:27 -0700 | [diff] [blame] | 72 | static constexpr auto kDefaultBufferCapInByte = 5_MB; |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 73 | static constexpr auto kDefaultFileName = "/data/misc/wmtrace/layers_trace.pb"; |
| 74 | |
| 75 | class LayersTraceBuffer { // ring buffer |
| 76 | public: |
| 77 | size_t size() const { return mSizeInBytes; } |
| 78 | size_t used() const { return mUsedInBytes; } |
| 79 | size_t frameCount() const { return mStorage.size(); } |
| 80 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 81 | void setSize(size_t newSize) { mSizeInBytes = newSize; } |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 82 | void reset(size_t newSize); |
| 83 | void emplace(LayersTraceProto&& proto); |
| 84 | void flush(LayersTraceFileProto* fileProto); |
| 85 | |
| 86 | private: |
| 87 | size_t mUsedInBytes = 0U; |
| 88 | size_t mSizeInBytes = 0U; |
| 89 | std::queue<LayersTraceProto> mStorage; |
| 90 | }; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 91 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 92 | void mainLoop(); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 93 | void addFirstEntry(); |
| 94 | LayersTraceProto traceWhenNotified(); |
Alec Mouri | 5793c7d | 2020-03-10 19:55:50 -0700 | [diff] [blame] | 95 | LayersTraceProto traceLayersLocked(const char* where, |
| 96 | const sp<const DisplayDevice>& displayDevice) |
| 97 | REQUIRES(mSfLock); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 98 | |
| 99 | // Returns true if trace is enabled. |
| 100 | bool addTraceToBuffer(LayersTraceProto& entry); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 101 | void writeProtoFileLocked() REQUIRES(mTraceLock); |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 102 | |
Alec Mouri | 5793c7d | 2020-03-10 19:55:50 -0700 | [diff] [blame] | 103 | SurfaceFlinger& mFlinger; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 104 | status_t mLastErr = NO_ERROR; |
| 105 | std::thread mThread; |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 106 | std::condition_variable mCanStartTrace; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 107 | |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 108 | std::mutex& mSfLock; |
chaviw | d8fcca9 | 2020-03-06 13:07:18 -0800 | [diff] [blame] | 109 | uint32_t mTraceFlags GUARDED_BY(mSfLock) = TRACE_CRITICAL | TRACE_INPUT; |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 110 | const char* mWhere GUARDED_BY(mSfLock) = ""; |
Vishnu Nair | 60db8c0 | 2020-04-02 11:55:16 -0700 | [diff] [blame^] | 111 | uint32_t mMissedTraceEntries GUARDED_BY(mSfLock) = 0; |
| 112 | bool mTracingInProgress GUARDED_BY(mSfLock) = false; |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 113 | |
| 114 | mutable std::mutex mTraceLock; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 115 | LayersTraceBuffer mBuffer GUARDED_BY(mTraceLock); |
| 116 | size_t mBufferSize GUARDED_BY(mTraceLock) = kDefaultBufferCapInByte; |
| 117 | bool mEnabled GUARDED_BY(mTraceLock) = false; |
| 118 | bool mWriteToFile GUARDED_BY(mTraceLock) = false; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | } // namespace android |