blob: bc57e01890161bd324bfc413696b0fa71be58a98 [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"
Vishnu Nair7891e962021-11-11 12:07:21 -080029#include "TransactionProtoParser.h"
30
31using namespace android::surfaceflinger;
32
33namespace android {
34
Vishnu Nair7891e962021-11-11 12:07:21 -080035class SurfaceFlinger;
36class TransactionTracingTest;
Dominik Laskowski46471e62022-01-14 15:34:03 -080037
Vishnu Nair7891e962021-11-11 12:07:21 -080038/*
39 * Records all committed transactions into a ring bufffer.
40 *
41 * Transactions come in via the binder thread. They are serialized to proto
42 * and stored in a map using the transaction id as key. Main thread will
43 * pass the list of transaction ids that are committed every vsync and notify
44 * the tracing thread. The tracing thread will then wake up and add the
45 * committed transactions to the ring buffer.
46 *
47 * When generating SF dump state, we will flush the buffer to a file which
48 * will then be included in the bugreport.
49 *
50 */
51class TransactionTracing {
52public:
53 TransactionTracing();
54 ~TransactionTracing();
55
Vishnu Nair7891e962021-11-11 12:07:21 -080056 void addQueuedTransaction(const TransactionState&);
57 void addCommittedTransactions(std::vector<TransactionState>& transactions, int64_t vsyncId);
58 status_t writeToFile();
59 void setBufferSize(size_t bufferSizeInBytes);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080060 void onLayerAdded(BBinder* layerHandle, int layerId, const std::string& name, uint32_t flags,
61 int parentId);
Vishnu Nair84125ac2021-12-02 08:47:48 -080062 void onMirrorLayerAdded(BBinder* layerHandle, int layerId, const std::string& name,
63 int mirrorFromId);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080064 void onLayerRemoved(int layerId);
Vishnu Nair047fb332021-12-09 09:54:36 -080065 void onHandleRemoved(BBinder* layerHandle);
Vishnu Nair7891e962021-11-11 12:07:21 -080066 void dump(std::string&) const;
67 static constexpr auto CONTINUOUS_TRACING_BUFFER_SIZE = 512 * 1024;
68 static constexpr auto ACTIVE_TRACING_BUFFER_SIZE = 100 * 1024 * 1024;
69
70private:
71 friend class TransactionTracingTest;
72
73 static constexpr auto FILE_NAME = "/data/misc/wmtrace/transactions_trace.winscope";
74
75 mutable std::mutex mTraceLock;
Dominik Laskowski46471e62022-01-14 15:34:03 -080076 RingBuffer<proto::TransactionTraceFile, proto::TransactionTraceEntry> mBuffer
Vishnu Nair7891e962021-11-11 12:07:21 -080077 GUARDED_BY(mTraceLock);
78 size_t mBufferSizeInBytes GUARDED_BY(mTraceLock) = CONTINUOUS_TRACING_BUFFER_SIZE;
79 std::unordered_map<uint64_t, proto::TransactionState> mQueuedTransactions
80 GUARDED_BY(mTraceLock);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080081 nsecs_t mStartingTimestamp GUARDED_BY(mTraceLock);
82 std::vector<proto::LayerCreationArgs> mCreatedLayers GUARDED_BY(mTraceLock);
83 std::unordered_map<BBinder* /* layerHandle */, int32_t /* layerId */> mLayerHandles
84 GUARDED_BY(mTraceLock);
Vishnu Nair473838d2021-12-08 09:46:02 -080085 std::map<int32_t /* layerId */, TracingLayerState> mStartingStates GUARDED_BY(mTraceLock);
Vishnu Nair7891e962021-11-11 12:07:21 -080086
87 // We do not want main thread to block so main thread will try to acquire mMainThreadLock,
88 // otherwise will push data to temporary container.
89 std::mutex mMainThreadLock;
90 std::thread mThread GUARDED_BY(mMainThreadLock);
91 bool mDone GUARDED_BY(mMainThreadLock) = false;
92 std::condition_variable mTransactionsAvailableCv;
93 std::condition_variable mTransactionsAddedToBufferCv;
94 struct CommittedTransactions {
95 std::vector<uint64_t> transactionIds;
96 int64_t vsyncId;
97 int64_t timestamp;
98 };
99 std::vector<CommittedTransactions> mCommittedTransactions GUARDED_BY(mMainThreadLock);
100 std::vector<CommittedTransactions> mPendingTransactions; // only accessed by main thread
Vishnu Nair7891e962021-11-11 12:07:21 -0800101
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800102 std::vector<int32_t /* layerId */> mRemovedLayers GUARDED_BY(mMainThreadLock);
103 std::vector<int32_t /* layerId */> mPendingRemovedLayers; // only accessed by main thread
104
105 proto::TransactionTraceFile createTraceFileProto() const;
Vishnu Nair7891e962021-11-11 12:07:21 -0800106 void loop();
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800107 void addEntry(const std::vector<CommittedTransactions>& committedTransactions,
108 const std::vector<int32_t>& removedLayers) EXCLUDES(mTraceLock);
109 int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock);
110 void tryPushToTracingThread() EXCLUDES(mMainThreadLock);
111 void addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) REQUIRES(mTraceLock);
112 void updateStartingStateLocked(const proto::TransactionTraceEntry& entry) REQUIRES(mTraceLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800113
114 // TEST
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800115 // Wait until all the committed transactions for the specified vsync id are added to the buffer.
116 void flush(int64_t vsyncId) EXCLUDES(mMainThreadLock);
117 // Return buffer contents as trace file proto
118 proto::TransactionTraceFile writeToProto() EXCLUDES(mMainThreadLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800119};
120
121} // namespace android