blob: 0aa22ede26895ce419ed9f469cbd944d08b4ec61 [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
28#include "TransactionProtoParser.h"
29
30using namespace android::surfaceflinger;
31
32namespace android {
33
34template <typename FileProto, typename EntryProto>
35class RingBuffer;
36
37class SurfaceFlinger;
38class TransactionTracingTest;
39/*
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
57 bool enable();
58 bool disable();
59 bool isEnabled() const;
60
61 void addQueuedTransaction(const TransactionState&);
62 void addCommittedTransactions(std::vector<TransactionState>& transactions, int64_t vsyncId);
63 status_t writeToFile();
64 void setBufferSize(size_t bufferSizeInBytes);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080065 void onLayerAdded(BBinder* layerHandle, int layerId, const std::string& name, uint32_t flags,
66 int parentId);
67 void onLayerRemoved(int layerId);
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;
78 bool mEnabled GUARDED_BY(mTraceLock) = false;
79 std::unique_ptr<RingBuffer<proto::TransactionTraceFile, proto::TransactionTraceEntry>> mBuffer
80 GUARDED_BY(mTraceLock);
81 size_t mBufferSizeInBytes GUARDED_BY(mTraceLock) = CONTINUOUS_TRACING_BUFFER_SIZE;
82 std::unordered_map<uint64_t, proto::TransactionState> mQueuedTransactions
83 GUARDED_BY(mTraceLock);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080084 nsecs_t mStartingTimestamp GUARDED_BY(mTraceLock);
85 std::vector<proto::LayerCreationArgs> mCreatedLayers GUARDED_BY(mTraceLock);
86 std::unordered_map<BBinder* /* layerHandle */, int32_t /* layerId */> mLayerHandles
87 GUARDED_BY(mTraceLock);
88 std::unordered_map<int32_t /* layerId */, TracingLayerState> mStartingStates
89 GUARDED_BY(mTraceLock);
Vishnu Nair7891e962021-11-11 12:07:21 -080090
91 // We do not want main thread to block so main thread will try to acquire mMainThreadLock,
92 // otherwise will push data to temporary container.
93 std::mutex mMainThreadLock;
94 std::thread mThread GUARDED_BY(mMainThreadLock);
95 bool mDone GUARDED_BY(mMainThreadLock) = false;
96 std::condition_variable mTransactionsAvailableCv;
97 std::condition_variable mTransactionsAddedToBufferCv;
98 struct CommittedTransactions {
99 std::vector<uint64_t> transactionIds;
100 int64_t vsyncId;
101 int64_t timestamp;
102 };
103 std::vector<CommittedTransactions> mCommittedTransactions GUARDED_BY(mMainThreadLock);
104 std::vector<CommittedTransactions> mPendingTransactions; // only accessed by main thread
Vishnu Nair7891e962021-11-11 12:07:21 -0800105
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800106 std::vector<int32_t /* layerId */> mRemovedLayers GUARDED_BY(mMainThreadLock);
107 std::vector<int32_t /* layerId */> mPendingRemovedLayers; // only accessed by main thread
108
109 proto::TransactionTraceFile createTraceFileProto() const;
Vishnu Nair7891e962021-11-11 12:07:21 -0800110 void loop();
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800111 void addEntry(const std::vector<CommittedTransactions>& committedTransactions,
112 const std::vector<int32_t>& removedLayers) EXCLUDES(mTraceLock);
113 int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock);
114 void tryPushToTracingThread() EXCLUDES(mMainThreadLock);
115 void addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) REQUIRES(mTraceLock);
116 void updateStartingStateLocked(const proto::TransactionTraceEntry& entry) REQUIRES(mTraceLock);
117 status_t writeToFileLocked() REQUIRES(mTraceLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800118
119 // TEST
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800120 // Wait until all the committed transactions for the specified vsync id are added to the buffer.
121 void flush(int64_t vsyncId) EXCLUDES(mMainThreadLock);
122 // Return buffer contents as trace file proto
123 proto::TransactionTraceFile writeToProto() EXCLUDES(mMainThreadLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800124};
125
126} // namespace android