blob: c20f22a94bd8427463f41cfd9b91133c700cb1a0 [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 Naird37343b2022-01-12 16:18:56 -080085 std::vector<int32_t /* layerId */> mRemovedLayerHandles GUARDED_BY(mTraceLock);
Vishnu Nair473838d2021-12-08 09:46:02 -080086 std::map<int32_t /* layerId */, TracingLayerState> mStartingStates GUARDED_BY(mTraceLock);
Vishnu Nair685cfef2022-02-02 10:01:25 -080087 TransactionProtoParser mProtoParser GUARDED_BY(mTraceLock);
Vishnu Nair7891e962021-11-11 12:07:21 -080088
89 // We do not want main thread to block so main thread will try to acquire mMainThreadLock,
90 // otherwise will push data to temporary container.
91 std::mutex mMainThreadLock;
92 std::thread mThread GUARDED_BY(mMainThreadLock);
93 bool mDone GUARDED_BY(mMainThreadLock) = false;
94 std::condition_variable mTransactionsAvailableCv;
95 std::condition_variable mTransactionsAddedToBufferCv;
96 struct CommittedTransactions {
97 std::vector<uint64_t> transactionIds;
98 int64_t vsyncId;
99 int64_t timestamp;
100 };
101 std::vector<CommittedTransactions> mCommittedTransactions GUARDED_BY(mMainThreadLock);
102 std::vector<CommittedTransactions> mPendingTransactions; // only accessed by main thread
Vishnu Nair7891e962021-11-11 12:07:21 -0800103
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800104 std::vector<int32_t /* layerId */> mRemovedLayers GUARDED_BY(mMainThreadLock);
105 std::vector<int32_t /* layerId */> mPendingRemovedLayers; // only accessed by main thread
106
107 proto::TransactionTraceFile createTraceFileProto() const;
Vishnu Nair7891e962021-11-11 12:07:21 -0800108 void loop();
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800109 void addEntry(const std::vector<CommittedTransactions>& committedTransactions,
110 const std::vector<int32_t>& removedLayers) EXCLUDES(mTraceLock);
111 int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock);
112 void tryPushToTracingThread() EXCLUDES(mMainThreadLock);
113 void addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) REQUIRES(mTraceLock);
114 void updateStartingStateLocked(const proto::TransactionTraceEntry& entry) REQUIRES(mTraceLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800115
116 // TEST
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800117 // Wait until all the committed transactions for the specified vsync id are added to the buffer.
118 void flush(int64_t vsyncId) EXCLUDES(mMainThreadLock);
119 // Return buffer contents as trace file proto
120 proto::TransactionTraceFile writeToProto() EXCLUDES(mMainThreadLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800121};
122
123} // namespace android