blob: 15a503d8341c80697e9b87cef14bba93534e1544 [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 */
16
17#pragma once
18
Alec Mouri5793c7d2020-03-10 19:55:50 -070019#include <android-base/thread_annotations.h>
Adrian Roos1e1a1282017-11-01 19:05:31 +010020#include <layerproto/LayerProtoHeader.h>
21#include <utils/Errors.h>
Nataniel Borges2b796da2019-02-15 13:32:18 -080022#include <utils/StrongPointer.h>
Adrian Roos1e1a1282017-11-01 19:05:31 +010023
Nataniel Borges2b796da2019-02-15 13:32:18 -080024#include <condition_variable>
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070025#include <memory>
Adrian Roos1e1a1282017-11-01 19:05:31 +010026#include <mutex>
Yichi Chen9c696ed2018-10-01 22:32:30 +080027#include <queue>
Nataniel Borges2b796da2019-02-15 13:32:18 -080028#include <thread>
Adrian Roos1e1a1282017-11-01 19:05:31 +010029
30using namespace android::surfaceflinger;
31
32namespace android {
33
Nataniel Borges2b796da2019-02-15 13:32:18 -080034class SurfaceFlinger;
Yichi Chen9c696ed2018-10-01 22:32:30 +080035constexpr auto operator""_MB(unsigned long long const num) {
36 return num * 1024 * 1024;
37}
Adrian Roos1e1a1282017-11-01 19:05:31 +010038/*
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070039 * SurfaceTracing records layer states during surface flinging. Manages tracing state and
40 * configuration.
Adrian Roos1e1a1282017-11-01 19:05:31 +010041 */
42class SurfaceTracing {
43public:
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070044 SurfaceTracing(SurfaceFlinger& flinger);
Dominik Laskowski542c9dc2020-04-10 12:42:02 -070045 bool enable();
Nataniel Borges2b796da2019-02-15 13:32:18 -080046 bool disable();
47 status_t writeToFile();
Yichi Chen9c696ed2018-10-01 22:32:30 +080048 bool isEnabled() const;
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070049 /*
50 * Adds a trace entry, must be called from the drawing thread or while holding the
51 * SurfaceFlinger tracing lock.
52 */
Nataniel Borges2b796da2019-02-15 13:32:18 -080053 void notify(const char* where);
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070054 /*
55 * Adds a trace entry, called while holding the SurfaceFlinger tracing lock.
56 */
57 void notifyLocked(const char* where) /* REQUIRES(mSfLock) */;
Nataniel Borges2b796da2019-02-15 13:32:18 -080058
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070059 void setBufferSize(size_t bufferSizeInBytes) { mConfig.bufferSize = bufferSizeInBytes; }
Yiwei Zhang5434a782018-12-05 18:06:32 -080060 void dump(std::string& result) const;
Adrian Roos1e1a1282017-11-01 19:05:31 +010061
Vishnu Nair9245d3b2019-03-22 13:38:56 -070062 enum : uint32_t {
63 TRACE_CRITICAL = 1 << 0,
64 TRACE_INPUT = 1 << 1,
Vishnu Nair60db8c02020-04-02 11:55:16 -070065 TRACE_COMPOSITION = 1 << 2,
66 TRACE_EXTRA = 1 << 3,
67 TRACE_HWC = 1 << 4,
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070068 // 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 Nair9245d3b2019-03-22 13:38:56 -070073 };
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070074 void setTraceFlags(uint32_t flags) { mConfig.flags = flags; }
75 bool flagIsSet(uint32_t flags) { return (mConfig.flags & flags) == flags; }
Vishnu Nair9245d3b2019-03-22 13:38:56 -070076
Adrian Roos1e1a1282017-11-01 19:05:31 +010077private:
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070078 class Runner;
79 static constexpr auto DEFAULT_BUFFER_SIZE = 5_MB;
80 static constexpr auto DEFAULT_FILE_NAME = "/data/misc/wmtrace/layers_trace.pb";
Yichi Chen9c696ed2018-10-01 22:32:30 +080081
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070082 SurfaceFlinger& mFlinger;
83 mutable std::mutex mTraceLock;
Vishnu Nairc96ad662021-02-12 11:23:19 -080084 bool mEnabled GUARDED_BY(mTraceLock) = false;
85 std::unique_ptr<Runner> runner GUARDED_BY(mTraceLock);
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070086
87 struct Config {
Vishnu Nair49353772020-12-09 17:49:57 -080088 uint32_t flags = TRACE_CRITICAL | TRACE_INPUT | TRACE_SYNC;
Vishnu Nair31a8dbc2020-10-27 17:37:49 -070089 size_t bufferSize = DEFAULT_BUFFER_SIZE;
90 } mConfig;
91
92 /*
93 * ring buffer.
94 */
95 class LayersTraceBuffer {
Yichi Chen9c696ed2018-10-01 22:32:30 +080096 public:
97 size_t size() const { return mSizeInBytes; }
98 size_t used() const { return mUsedInBytes; }
99 size_t frameCount() const { return mStorage.size(); }
100
Nataniel Borges2b796da2019-02-15 13:32:18 -0800101 void setSize(size_t newSize) { mSizeInBytes = newSize; }
Yichi Chen9c696ed2018-10-01 22:32:30 +0800102 void reset(size_t newSize);
103 void emplace(LayersTraceProto&& proto);
104 void flush(LayersTraceFileProto* fileProto);
105
106 private:
107 size_t mUsedInBytes = 0U;
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700108 size_t mSizeInBytes = DEFAULT_BUFFER_SIZE;
Yichi Chen9c696ed2018-10-01 22:32:30 +0800109 std::queue<LayersTraceProto> mStorage;
110 };
Adrian Roos1e1a1282017-11-01 19:05:31 +0100111
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700112 /*
113 * Implements a synchronous way of adding trace entries. This must be called
114 * from the drawing thread.
115 */
116 class Runner {
117 public:
118 Runner(SurfaceFlinger& flinger, SurfaceTracing::Config& config);
119 virtual ~Runner() = default;
120 virtual status_t stop();
121 virtual status_t writeToFile();
122 virtual void notify(const char* where);
123 /* Cannot be called with a synchronous runner. */
124 virtual void notifyLocked(const char* /* where */) {}
125 void dump(std::string& result) const;
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700126
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700127 protected:
128 bool flagIsSet(uint32_t flags) { return (mConfig.flags & flags) == flags; }
129 SurfaceFlinger& mFlinger;
130 SurfaceTracing::Config mConfig;
131 SurfaceTracing::LayersTraceBuffer mBuffer;
132 uint32_t mMissedTraceEntries = 0;
133 LayersTraceProto traceLayers(const char* where);
134 };
Adrian Roos1e1a1282017-11-01 19:05:31 +0100135
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700136 /*
137 * Implements asynchronous way to add trace entries called from a separate thread while holding
138 * the SurfaceFlinger tracing lock. Trace entries may be missed if the tracing thread is not
139 * scheduled in time.
140 */
141 class AsyncRunner : public Runner {
142 public:
143 AsyncRunner(SurfaceFlinger& flinger, SurfaceTracing::Config& config, std::mutex& sfLock);
144 virtual ~AsyncRunner() = default;
145 status_t stop() override;
146 status_t writeToFile() override;
147 void notify(const char* where) override;
148 void notifyLocked(const char* where);
Nataniel Borges2b796da2019-02-15 13:32:18 -0800149
Vishnu Nair31a8dbc2020-10-27 17:37:49 -0700150 private:
151 std::mutex& mSfLock;
152 std::condition_variable mCanStartTrace;
153 std::thread mThread;
154 const char* mWhere = "";
155 bool mWriteToFile = false;
156 bool mEnabled = false;
157 bool mAddEntry = false;
158 void loop();
159 bool traceWhenNotified(LayersTraceProto* outProto);
160 };
Adrian Roos1e1a1282017-11-01 19:05:31 +0100161};
162
163} // namespace android