blob: 09fcd8ad9670fbf79bf5a6ca38a74fcd4bca53c4 [file] [log] [blame]
Vishnu Nair7891e962021-11-11 12:07:21 -08001/*
2 * Copyright 2021 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
19#include <android-base/thread_annotations.h>
20#include <layerproto/TransactionProto.h>
21#include <utils/Errors.h>
Vishnu Naird1f74982023-06-15 20:16:51 -070022#include <utils/Singleton.h>
Vishnu Nair7891e962021-11-11 12:07:21 -080023#include <utils/Timers.h>
24
Diwas Sharmaa0021172023-09-01 00:43:31 +000025#include <memory>
Vishnu Nair7891e962021-11-11 12:07:21 -080026#include <mutex>
27#include <thread>
28
Vishnu Nair81750622023-03-08 15:02:06 -080029#include "FrontEnd/DisplayInfo.h"
30#include "FrontEnd/LayerCreationArgs.h"
31#include "FrontEnd/Update.h"
Robert Carra63d52a2022-03-03 08:03:37 -080032#include "LocklessStack.h"
Vishnu Nair7891e962021-11-11 12:07:21 -080033#include "TransactionProtoParser.h"
John Reck2a3d29d2023-08-17 17:45:01 -040034#include "TransactionRingBuffer.h"
Vishnu Nair7891e962021-11-11 12:07:21 -080035
36using namespace android::surfaceflinger;
37
38namespace android {
39
Vishnu Nair7891e962021-11-11 12:07:21 -080040class SurfaceFlinger;
41class TransactionTracingTest;
Dominik Laskowski46471e62022-01-14 15:34:03 -080042
Vishnu Nair7891e962021-11-11 12:07:21 -080043/*
Diwas Sharmaa0021172023-09-01 00:43:31 +000044 * Records all committed transactions into a ring bufffer.
Vishnu Nair7891e962021-11-11 12:07:21 -080045 *
46 * Transactions come in via the binder thread. They are serialized to proto
47 * and stored in a map using the transaction id as key. Main thread will
48 * pass the list of transaction ids that are committed every vsync and notify
49 * the tracing thread. The tracing thread will then wake up and add the
50 * committed transactions to the ring buffer.
51 *
Diwas Sharmaa0021172023-09-01 00:43:31 +000052 * When generating SF dump state, we will flush the buffer to a file which
53 * will then be included in the bugreport.
Vishnu Nair7891e962021-11-11 12:07:21 -080054 *
55 */
56class TransactionTracing {
57public:
58 TransactionTracing();
59 ~TransactionTracing();
60
Vishnu Nair7891e962021-11-11 12:07:21 -080061 void addQueuedTransaction(const TransactionState&);
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050062 void addCommittedTransactions(int64_t vsyncId, nsecs_t commitTime, frontend::Update& update,
63 const frontend::DisplayInfos&, bool displayInfoChanged);
Vishnu Naird1f74982023-06-15 20:16:51 -070064 status_t writeToFile(const std::string& filename = FILE_PATH);
Vishnu Nair7891e962021-11-11 12:07:21 -080065 void setBufferSize(size_t bufferSizeInBytes);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080066 void onLayerRemoved(int layerId);
Vishnu Nair7891e962021-11-11 12:07:21 -080067 void dump(std::string&) const;
Vishnu Naird1f74982023-06-15 20:16:51 -070068 // Wait until all the committed transactions for the specified vsync id are added to the buffer.
69 void flush() EXCLUDES(mMainThreadLock);
Vishnu Nair7891e962021-11-11 12:07:21 -080070 static constexpr auto CONTINUOUS_TRACING_BUFFER_SIZE = 512 * 1024;
Diwas Sharmaa0021172023-09-01 00:43:31 +000071 static constexpr auto ACTIVE_TRACING_BUFFER_SIZE = 100 * 1024 * 1024;
Vishnu Nair81750622023-03-08 15:02:06 -080072 // version 1 - switching to support new frontend
73 static constexpr auto TRACING_VERSION = 1;
Vishnu Nair7891e962021-11-11 12:07:21 -080074
75private:
Vishnu Nair8c5b7002023-08-17 21:03:57 -070076 friend class TransactionTraceWriter;
Vishnu Nair7891e962021-11-11 12:07:21 -080077 friend class TransactionTracingTest;
Vishnu Nair81750622023-03-08 15:02:06 -080078 friend class SurfaceFlinger;
Vishnu Nair7891e962021-11-11 12:07:21 -080079
Vishnu Naird1f74982023-06-15 20:16:51 -070080 static constexpr auto DIR_NAME = "/data/misc/wmtrace/";
Vishnu Nair249fd7f2023-06-22 19:30:24 -070081 static constexpr auto FILE_NAME = "transactions_trace.winscope";
Vishnu Naird1f74982023-06-15 20:16:51 -070082 static constexpr auto FILE_PATH = "/data/misc/wmtrace/transactions_trace.winscope";
Vishnu Nair8c5b7002023-08-17 21:03:57 -070083 static std::string getFilePath(const std::string& prefix) {
84 return DIR_NAME + prefix + FILE_NAME;
85 }
Vishnu Nair7891e962021-11-11 12:07:21 -080086
87 mutable std::mutex mTraceLock;
Kean Mariottibec51fd2023-04-19 13:31:02 +000088 TransactionRingBuffer<perfetto::protos::TransactionTraceFile,
89 perfetto::protos::TransactionTraceEntry>
90 mBuffer GUARDED_BY(mTraceLock);
Diwas Sharmaa0021172023-09-01 00:43:31 +000091 size_t mBufferSizeInBytes GUARDED_BY(mTraceLock) = CONTINUOUS_TRACING_BUFFER_SIZE;
Kean Mariottibec51fd2023-04-19 13:31:02 +000092 std::unordered_map<uint64_t, perfetto::protos::TransactionState> mQueuedTransactions
Vishnu Nair7891e962021-11-11 12:07:21 -080093 GUARDED_BY(mTraceLock);
Kean Mariottibec51fd2023-04-19 13:31:02 +000094 LocklessStack<perfetto::protos::TransactionState> mTransactionQueue;
Vishnu Nair0cc69e12021-11-18 09:05:49 -080095 nsecs_t mStartingTimestamp GUARDED_BY(mTraceLock);
Kean Mariottibec51fd2023-04-19 13:31:02 +000096 std::unordered_map<int, perfetto::protos::LayerCreationArgs> mCreatedLayers
97 GUARDED_BY(mTraceLock);
Vishnu Nair81750622023-03-08 15:02:06 -080098 std::map<uint32_t /* layerId */, TracingLayerState> mStartingStates GUARDED_BY(mTraceLock);
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050099 frontend::DisplayInfos mStartingDisplayInfos GUARDED_BY(mTraceLock);
Vishnu Nair81750622023-03-08 15:02:06 -0800100
101 std::set<uint32_t /* layerId */> mRemovedLayerHandlesAtStart GUARDED_BY(mTraceLock);
102 TransactionProtoParser mProtoParser;
Vishnu Nair7891e962021-11-11 12:07:21 -0800103
104 // We do not want main thread to block so main thread will try to acquire mMainThreadLock,
105 // otherwise will push data to temporary container.
106 std::mutex mMainThreadLock;
107 std::thread mThread GUARDED_BY(mMainThreadLock);
108 bool mDone GUARDED_BY(mMainThreadLock) = false;
109 std::condition_variable mTransactionsAvailableCv;
110 std::condition_variable mTransactionsAddedToBufferCv;
Vishnu Nair81750622023-03-08 15:02:06 -0800111 struct CommittedUpdates {
Vishnu Nair7891e962021-11-11 12:07:21 -0800112 std::vector<uint64_t> transactionIds;
Vishnu Nair81750622023-03-08 15:02:06 -0800113 std::vector<LayerCreationArgs> createdLayers;
114 std::vector<uint32_t> destroyedLayerHandles;
115 bool displayInfoChanged;
Dominik Laskowski6b049ff2023-01-29 15:46:45 -0500116 frontend::DisplayInfos displayInfos;
Vishnu Nair7891e962021-11-11 12:07:21 -0800117 int64_t vsyncId;
118 int64_t timestamp;
119 };
Vishnu Nair81750622023-03-08 15:02:06 -0800120 std::vector<CommittedUpdates> mUpdates GUARDED_BY(mMainThreadLock);
121 std::vector<CommittedUpdates> mPendingUpdates; // only accessed by main thread
Vishnu Nair7891e962021-11-11 12:07:21 -0800122
Vishnu Nair81750622023-03-08 15:02:06 -0800123 std::vector<uint32_t /* layerId */> mDestroyedLayers GUARDED_BY(mMainThreadLock);
124 std::vector<uint32_t /* layerId */> mPendingDestroyedLayers; // only accessed by main thread
Vishnu Naird1f74982023-06-15 20:16:51 -0700125 int64_t mLastUpdatedVsyncId = -1;
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800126
Kean Mariottibec51fd2023-04-19 13:31:02 +0000127 perfetto::protos::TransactionTraceFile createTraceFileProto() const;
Vishnu Nair7891e962021-11-11 12:07:21 -0800128 void loop();
Vishnu Nair81750622023-03-08 15:02:06 -0800129 void addEntry(const std::vector<CommittedUpdates>& committedTransactions,
130 const std::vector<uint32_t>& removedLayers) EXCLUDES(mTraceLock);
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800131 int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock);
132 void tryPushToTracingThread() EXCLUDES(mMainThreadLock);
Diwas Sharmaa0021172023-09-01 00:43:31 +0000133 void addStartingStateToProtoLocked(perfetto::protos::TransactionTraceFile& proto)
Kean Mariottibec51fd2023-04-19 13:31:02 +0000134 REQUIRES(mTraceLock);
135 void updateStartingStateLocked(const perfetto::protos::TransactionTraceEntry& entry)
136 REQUIRES(mTraceLock);
Diwas Sharmaa0021172023-09-01 00:43:31 +0000137 // TEST
138 // Return buffer contents as trace file proto
139 perfetto::protos::TransactionTraceFile writeToProto() EXCLUDES(mMainThreadLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800140};
141
Vishnu Naird1f74982023-06-15 20:16:51 -0700142class TransactionTraceWriter : public Singleton<TransactionTraceWriter> {
143 friend class Singleton<TransactionTracing>;
144 std::function<void(const std::string& prefix, bool overwrite)> mWriterFunction =
145 [](const std::string&, bool) {};
146
147public:
148 void setWriterFunction(
Vishnu Nair8c5b7002023-08-17 21:03:57 -0700149 std::function<void(const std::string& filename, bool overwrite)> function) {
Vishnu Naird1f74982023-06-15 20:16:51 -0700150 mWriterFunction = std::move(function);
151 }
Vishnu Nair8c5b7002023-08-17 21:03:57 -0700152 void invoke(const std::string& prefix, bool overwrite) {
153 mWriterFunction(TransactionTracing::getFilePath(prefix), overwrite);
154 }
155 /* pass in a complete file path for testing */
156 void invokeForTest(const std::string& filename, bool overwrite) {
157 mWriterFunction(filename, overwrite);
158 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700159};
160
Vishnu Nair7891e962021-11-11 12:07:21 -0800161} // namespace android