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 | |
| 30 | using namespace android::surfaceflinger; |
| 31 | |
| 32 | namespace android { |
| 33 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 34 | class SurfaceFlinger; |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 35 | constexpr auto operator""_MB(unsigned long long const num) { |
| 36 | return num * 1024 * 1024; |
| 37 | } |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 38 | /* |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 39 | * SurfaceTracing records layer states during surface flinging. Manages tracing state and |
| 40 | * configuration. |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 41 | */ |
| 42 | class SurfaceTracing { |
| 43 | public: |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 44 | SurfaceTracing(SurfaceFlinger& flinger); |
Dominik Laskowski | 542c9dc | 2020-04-10 12:42:02 -0700 | [diff] [blame] | 45 | bool enable(); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 46 | bool disable(); |
| 47 | status_t writeToFile(); |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 48 | bool isEnabled() const; |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 49 | /* |
| 50 | * Adds a trace entry, must be called from the drawing thread or while holding the |
| 51 | * SurfaceFlinger tracing lock. |
| 52 | */ |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 53 | void notify(const char* where); |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 54 | /* |
| 55 | * Adds a trace entry, called while holding the SurfaceFlinger tracing lock. |
| 56 | */ |
| 57 | void notifyLocked(const char* where) /* REQUIRES(mSfLock) */; |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 58 | |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 59 | void setBufferSize(size_t bufferSizeInBytes) { mConfig.bufferSize = bufferSizeInBytes; } |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 60 | void dump(std::string& result) const; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 61 | |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 62 | enum : uint32_t { |
| 63 | TRACE_CRITICAL = 1 << 0, |
| 64 | TRACE_INPUT = 1 << 1, |
Vishnu Nair | 60db8c0 | 2020-04-02 11:55:16 -0700 | [diff] [blame] | 65 | TRACE_COMPOSITION = 1 << 2, |
| 66 | TRACE_EXTRA = 1 << 3, |
| 67 | TRACE_HWC = 1 << 4, |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 68 | // Add non-geometry composition changes to the trace. |
| 69 | TRACE_BUFFERS = 1 << 5, |
| 70 | // Add entries from the drawing thread post composition. |
| 71 | TRACE_SYNC = 1 << 6, |
| 72 | TRACE_ALL = TRACE_CRITICAL | TRACE_INPUT | TRACE_COMPOSITION | TRACE_EXTRA, |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 73 | }; |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 74 | void setTraceFlags(uint32_t flags) { mConfig.flags = flags; } |
| 75 | bool flagIsSet(uint32_t flags) { return (mConfig.flags & flags) == flags; } |
chaviw | 66e11cf | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 76 | static LayersTraceFileProto createLayersTraceFileProto(); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 77 | |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 78 | private: |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 79 | class Runner; |
| 80 | static constexpr auto DEFAULT_BUFFER_SIZE = 5_MB; |
Nataniel Borges | 1388106 | 2021-08-31 10:26:28 +0000 | [diff] [blame] | 81 | static constexpr auto DEFAULT_FILE_NAME = "/data/misc/wmtrace/layers_trace.winscope"; |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 82 | |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 83 | SurfaceFlinger& mFlinger; |
| 84 | mutable std::mutex mTraceLock; |
Vishnu Nair | c96ad66 | 2021-02-12 11:23:19 -0800 | [diff] [blame] | 85 | bool mEnabled GUARDED_BY(mTraceLock) = false; |
| 86 | std::unique_ptr<Runner> runner GUARDED_BY(mTraceLock); |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 87 | |
| 88 | struct Config { |
Vishnu Nair | 4935377 | 2020-12-09 17:49:57 -0800 | [diff] [blame] | 89 | uint32_t flags = TRACE_CRITICAL | TRACE_INPUT | TRACE_SYNC; |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 90 | size_t bufferSize = DEFAULT_BUFFER_SIZE; |
| 91 | } mConfig; |
| 92 | |
| 93 | /* |
| 94 | * ring buffer. |
| 95 | */ |
| 96 | class LayersTraceBuffer { |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 97 | public: |
| 98 | size_t size() const { return mSizeInBytes; } |
| 99 | size_t used() const { return mUsedInBytes; } |
| 100 | size_t frameCount() const { return mStorage.size(); } |
| 101 | |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 102 | void setSize(size_t newSize) { mSizeInBytes = newSize; } |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 103 | void reset(size_t newSize); |
| 104 | void emplace(LayersTraceProto&& proto); |
| 105 | void flush(LayersTraceFileProto* fileProto); |
| 106 | |
| 107 | private: |
| 108 | size_t mUsedInBytes = 0U; |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 109 | size_t mSizeInBytes = DEFAULT_BUFFER_SIZE; |
Yichi Chen | 9c696ed | 2018-10-01 22:32:30 +0800 | [diff] [blame] | 110 | std::queue<LayersTraceProto> mStorage; |
| 111 | }; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 112 | |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 113 | /* |
| 114 | * Implements a synchronous way of adding trace entries. This must be called |
| 115 | * from the drawing thread. |
| 116 | */ |
| 117 | class Runner { |
| 118 | public: |
| 119 | Runner(SurfaceFlinger& flinger, SurfaceTracing::Config& config); |
| 120 | virtual ~Runner() = default; |
| 121 | virtual status_t stop(); |
| 122 | virtual status_t writeToFile(); |
| 123 | virtual void notify(const char* where); |
| 124 | /* Cannot be called with a synchronous runner. */ |
| 125 | virtual void notifyLocked(const char* /* where */) {} |
| 126 | void dump(std::string& result) const; |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 127 | |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 128 | protected: |
| 129 | bool flagIsSet(uint32_t flags) { return (mConfig.flags & flags) == flags; } |
| 130 | SurfaceFlinger& mFlinger; |
| 131 | SurfaceTracing::Config mConfig; |
| 132 | SurfaceTracing::LayersTraceBuffer mBuffer; |
| 133 | uint32_t mMissedTraceEntries = 0; |
| 134 | LayersTraceProto traceLayers(const char* where); |
| 135 | }; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 136 | |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 137 | /* |
| 138 | * Implements asynchronous way to add trace entries called from a separate thread while holding |
| 139 | * the SurfaceFlinger tracing lock. Trace entries may be missed if the tracing thread is not |
| 140 | * scheduled in time. |
| 141 | */ |
| 142 | class AsyncRunner : public Runner { |
| 143 | public: |
| 144 | AsyncRunner(SurfaceFlinger& flinger, SurfaceTracing::Config& config, std::mutex& sfLock); |
| 145 | virtual ~AsyncRunner() = default; |
| 146 | status_t stop() override; |
| 147 | status_t writeToFile() override; |
| 148 | void notify(const char* where) override; |
| 149 | void notifyLocked(const char* where); |
Nataniel Borges | 2b796da | 2019-02-15 13:32:18 -0800 | [diff] [blame] | 150 | |
Vishnu Nair | 31a8dbc | 2020-10-27 17:37:49 -0700 | [diff] [blame] | 151 | private: |
| 152 | std::mutex& mSfLock; |
| 153 | std::condition_variable mCanStartTrace; |
| 154 | std::thread mThread; |
| 155 | const char* mWhere = ""; |
| 156 | bool mWriteToFile = false; |
| 157 | bool mEnabled = false; |
| 158 | bool mAddEntry = false; |
| 159 | void loop(); |
| 160 | bool traceWhenNotified(LayersTraceProto* outProto); |
| 161 | }; |
Adrian Roos | 1e1a128 | 2017-11-01 19:05:31 +0100 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | } // namespace android |