blob: 31bca5fc66b9262129de0ee35497a7a50600f943 [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
25#include <memory>
26#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/*
44 * Records all committed transactions into a ring bufffer.
45 *
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 *
52 * When generating SF dump state, we will flush the buffer to a file which
53 * will then be included in the bugreport.
54 *
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;
71 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:
76 friend class TransactionTracingTest;
Vishnu Nair81750622023-03-08 15:02:06 -080077 friend class SurfaceFlinger;
Vishnu Nair7891e962021-11-11 12:07:21 -080078
Vishnu Naird1f74982023-06-15 20:16:51 -070079 static constexpr auto DIR_NAME = "/data/misc/wmtrace/";
Vishnu Nair249fd7f2023-06-22 19:30:24 -070080 static constexpr auto FILE_NAME = "transactions_trace.winscope";
Vishnu Naird1f74982023-06-15 20:16:51 -070081 static constexpr auto FILE_PATH = "/data/misc/wmtrace/transactions_trace.winscope";
Vishnu Nair7891e962021-11-11 12:07:21 -080082
83 mutable std::mutex mTraceLock;
John Reck2a3d29d2023-08-17 17:45:01 -040084 TransactionRingBuffer<proto::TransactionTraceFile, proto::TransactionTraceEntry> mBuffer
Vishnu Nair7891e962021-11-11 12:07:21 -080085 GUARDED_BY(mTraceLock);
86 size_t mBufferSizeInBytes GUARDED_BY(mTraceLock) = CONTINUOUS_TRACING_BUFFER_SIZE;
87 std::unordered_map<uint64_t, proto::TransactionState> mQueuedTransactions
88 GUARDED_BY(mTraceLock);
Robert Carra63d52a2022-03-03 08:03:37 -080089 LocklessStack<proto::TransactionState> mTransactionQueue;
Vishnu Nair0cc69e12021-11-18 09:05:49 -080090 nsecs_t mStartingTimestamp GUARDED_BY(mTraceLock);
Vishnu Nair286f4f92022-06-08 16:37:39 -070091 std::unordered_map<int, proto::LayerCreationArgs> mCreatedLayers GUARDED_BY(mTraceLock);
Vishnu Nair81750622023-03-08 15:02:06 -080092 std::map<uint32_t /* layerId */, TracingLayerState> mStartingStates GUARDED_BY(mTraceLock);
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050093 frontend::DisplayInfos mStartingDisplayInfos GUARDED_BY(mTraceLock);
Vishnu Nair81750622023-03-08 15:02:06 -080094
95 std::set<uint32_t /* layerId */> mRemovedLayerHandlesAtStart GUARDED_BY(mTraceLock);
96 TransactionProtoParser mProtoParser;
Vishnu Nair7891e962021-11-11 12:07:21 -080097
98 // We do not want main thread to block so main thread will try to acquire mMainThreadLock,
99 // otherwise will push data to temporary container.
100 std::mutex mMainThreadLock;
101 std::thread mThread GUARDED_BY(mMainThreadLock);
102 bool mDone GUARDED_BY(mMainThreadLock) = false;
103 std::condition_variable mTransactionsAvailableCv;
104 std::condition_variable mTransactionsAddedToBufferCv;
Vishnu Nair81750622023-03-08 15:02:06 -0800105 struct CommittedUpdates {
Vishnu Nair7891e962021-11-11 12:07:21 -0800106 std::vector<uint64_t> transactionIds;
Vishnu Nair81750622023-03-08 15:02:06 -0800107 std::vector<LayerCreationArgs> createdLayers;
108 std::vector<uint32_t> destroyedLayerHandles;
109 bool displayInfoChanged;
Dominik Laskowski6b049ff2023-01-29 15:46:45 -0500110 frontend::DisplayInfos displayInfos;
Vishnu Nair7891e962021-11-11 12:07:21 -0800111 int64_t vsyncId;
112 int64_t timestamp;
113 };
Vishnu Nair81750622023-03-08 15:02:06 -0800114 std::vector<CommittedUpdates> mUpdates GUARDED_BY(mMainThreadLock);
115 std::vector<CommittedUpdates> mPendingUpdates; // only accessed by main thread
Vishnu Nair7891e962021-11-11 12:07:21 -0800116
Vishnu Nair81750622023-03-08 15:02:06 -0800117 std::vector<uint32_t /* layerId */> mDestroyedLayers GUARDED_BY(mMainThreadLock);
118 std::vector<uint32_t /* layerId */> mPendingDestroyedLayers; // only accessed by main thread
Vishnu Naird1f74982023-06-15 20:16:51 -0700119 int64_t mLastUpdatedVsyncId = -1;
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800120
121 proto::TransactionTraceFile createTraceFileProto() const;
Vishnu Nair7891e962021-11-11 12:07:21 -0800122 void loop();
Vishnu Nair81750622023-03-08 15:02:06 -0800123 void addEntry(const std::vector<CommittedUpdates>& committedTransactions,
124 const std::vector<uint32_t>& removedLayers) EXCLUDES(mTraceLock);
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800125 int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock);
126 void tryPushToTracingThread() EXCLUDES(mMainThreadLock);
127 void addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) REQUIRES(mTraceLock);
128 void updateStartingStateLocked(const proto::TransactionTraceEntry& entry) REQUIRES(mTraceLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800129 // TEST
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800130 // Return buffer contents as trace file proto
131 proto::TransactionTraceFile writeToProto() EXCLUDES(mMainThreadLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800132};
133
Vishnu Naird1f74982023-06-15 20:16:51 -0700134class TransactionTraceWriter : public Singleton<TransactionTraceWriter> {
135 friend class Singleton<TransactionTracing>;
136 std::function<void(const std::string& prefix, bool overwrite)> mWriterFunction =
137 [](const std::string&, bool) {};
138
139public:
140 void setWriterFunction(
141 std::function<void(const std::string& prefix, bool overwrite)> function) {
142 mWriterFunction = std::move(function);
143 }
144 void invoke(const std::string& prefix, bool overwrite) { mWriterFunction(prefix, overwrite); }
145};
146
Vishnu Nair7891e962021-11-11 12:07:21 -0800147} // namespace android