blob: 2f5ee87039bd6f81aa93c3125c13e1113697588e [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>
22#include <utils/Timers.h>
23
24#include <memory>
25#include <mutex>
26#include <thread>
27
Dominik Laskowski46471e62022-01-14 15:34:03 -080028#include "RingBuffer.h"
Robert Carra63d52a2022-03-03 08:03:37 -080029#include "LocklessStack.h"
Vishnu Nair7891e962021-11-11 12:07:21 -080030#include "TransactionProtoParser.h"
31
32using namespace android::surfaceflinger;
33
34namespace android {
35
Vishnu Nair7891e962021-11-11 12:07:21 -080036class SurfaceFlinger;
37class TransactionTracingTest;
Dominik Laskowski46471e62022-01-14 15:34:03 -080038
Vishnu Nair7891e962021-11-11 12:07:21 -080039/*
40 * Records all committed transactions into a ring bufffer.
41 *
42 * Transactions come in via the binder thread. They are serialized to proto
43 * and stored in a map using the transaction id as key. Main thread will
44 * pass the list of transaction ids that are committed every vsync and notify
45 * the tracing thread. The tracing thread will then wake up and add the
46 * committed transactions to the ring buffer.
47 *
48 * When generating SF dump state, we will flush the buffer to a file which
49 * will then be included in the bugreport.
50 *
51 */
52class TransactionTracing {
53public:
54 TransactionTracing();
55 ~TransactionTracing();
56
Vishnu Nair7891e962021-11-11 12:07:21 -080057 void addQueuedTransaction(const TransactionState&);
58 void addCommittedTransactions(std::vector<TransactionState>& transactions, int64_t vsyncId);
Vishnu Naird8f5e9f2022-02-03 10:23:28 -080059 status_t writeToFile(std::string filename = FILE_NAME);
Vishnu Nair7891e962021-11-11 12:07:21 -080060 void setBufferSize(size_t bufferSizeInBytes);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080061 void onLayerAdded(BBinder* layerHandle, int layerId, const std::string& name, uint32_t flags,
62 int parentId);
Vishnu Nair84125ac2021-12-02 08:47:48 -080063 void onMirrorLayerAdded(BBinder* layerHandle, int layerId, const std::string& name,
64 int mirrorFromId);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080065 void onLayerRemoved(int layerId);
Vishnu Nair047fb332021-12-09 09:54:36 -080066 void onHandleRemoved(BBinder* layerHandle);
Vishnu Nair286f4f92022-06-08 16:37:39 -070067 void onLayerAddedToDrawingState(int layerId, int64_t vsyncId);
Vishnu Nair7891e962021-11-11 12:07:21 -080068 void dump(std::string&) const;
69 static constexpr auto CONTINUOUS_TRACING_BUFFER_SIZE = 512 * 1024;
70 static constexpr auto ACTIVE_TRACING_BUFFER_SIZE = 100 * 1024 * 1024;
71
72private:
73 friend class TransactionTracingTest;
74
75 static constexpr auto FILE_NAME = "/data/misc/wmtrace/transactions_trace.winscope";
76
77 mutable std::mutex mTraceLock;
Dominik Laskowski46471e62022-01-14 15:34:03 -080078 RingBuffer<proto::TransactionTraceFile, proto::TransactionTraceEntry> mBuffer
Vishnu Nair7891e962021-11-11 12:07:21 -080079 GUARDED_BY(mTraceLock);
80 size_t mBufferSizeInBytes GUARDED_BY(mTraceLock) = CONTINUOUS_TRACING_BUFFER_SIZE;
81 std::unordered_map<uint64_t, proto::TransactionState> mQueuedTransactions
82 GUARDED_BY(mTraceLock);
Robert Carra63d52a2022-03-03 08:03:37 -080083 LocklessStack<proto::TransactionState> mTransactionQueue;
Vishnu Nair0cc69e12021-11-18 09:05:49 -080084 nsecs_t mStartingTimestamp GUARDED_BY(mTraceLock);
Vishnu Nair286f4f92022-06-08 16:37:39 -070085 std::unordered_map<int, proto::LayerCreationArgs> mCreatedLayers GUARDED_BY(mTraceLock);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080086 std::unordered_map<BBinder* /* layerHandle */, int32_t /* layerId */> mLayerHandles
87 GUARDED_BY(mTraceLock);
Vishnu Naird37343b2022-01-12 16:18:56 -080088 std::vector<int32_t /* layerId */> mRemovedLayerHandles GUARDED_BY(mTraceLock);
Vishnu Nair473838d2021-12-08 09:46:02 -080089 std::map<int32_t /* layerId */, TracingLayerState> mStartingStates GUARDED_BY(mTraceLock);
Vishnu Nair685cfef2022-02-02 10:01:25 -080090 TransactionProtoParser mProtoParser GUARDED_BY(mTraceLock);
Robert Carra63d52a2022-03-03 08:03:37 -080091 // Parses the transaction to proto without holding any tracing locks so we can generate proto
92 // in the binder thread without any contention.
93 TransactionProtoParser mLockfreeProtoParser;
Vishnu Nair7891e962021-11-11 12:07:21 -080094
95 // We do not want main thread to block so main thread will try to acquire mMainThreadLock,
96 // otherwise will push data to temporary container.
97 std::mutex mMainThreadLock;
98 std::thread mThread GUARDED_BY(mMainThreadLock);
99 bool mDone GUARDED_BY(mMainThreadLock) = false;
100 std::condition_variable mTransactionsAvailableCv;
101 std::condition_variable mTransactionsAddedToBufferCv;
102 struct CommittedTransactions {
103 std::vector<uint64_t> transactionIds;
Vishnu Nair286f4f92022-06-08 16:37:39 -0700104 std::vector<int32_t> createdLayerIds;
Vishnu Nair7891e962021-11-11 12:07:21 -0800105 int64_t vsyncId;
106 int64_t timestamp;
107 };
108 std::vector<CommittedTransactions> mCommittedTransactions GUARDED_BY(mMainThreadLock);
109 std::vector<CommittedTransactions> mPendingTransactions; // only accessed by main thread
Vishnu Nair7891e962021-11-11 12:07:21 -0800110
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800111 std::vector<int32_t /* layerId */> mRemovedLayers GUARDED_BY(mMainThreadLock);
112 std::vector<int32_t /* layerId */> mPendingRemovedLayers; // only accessed by main thread
113
114 proto::TransactionTraceFile createTraceFileProto() const;
Vishnu Nair7891e962021-11-11 12:07:21 -0800115 void loop();
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800116 void addEntry(const std::vector<CommittedTransactions>& committedTransactions,
117 const std::vector<int32_t>& removedLayers) EXCLUDES(mTraceLock);
118 int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock);
119 void tryPushToTracingThread() EXCLUDES(mMainThreadLock);
120 void addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) REQUIRES(mTraceLock);
121 void updateStartingStateLocked(const proto::TransactionTraceEntry& entry) REQUIRES(mTraceLock);
Vishnu Nair286f4f92022-06-08 16:37:39 -0700122 CommittedTransactions& findOrCreateCommittedTransactionRecord(int64_t vsyncId);
Vishnu Nair7891e962021-11-11 12:07:21 -0800123 // TEST
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800124 // Wait until all the committed transactions for the specified vsync id are added to the buffer.
125 void flush(int64_t vsyncId) EXCLUDES(mMainThreadLock);
126 // Return buffer contents as trace file proto
127 proto::TransactionTraceFile writeToProto() EXCLUDES(mMainThreadLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800128};
129
130} // namespace android