blob: 0e566276502904ef76ef57243c760f5f160ef55f [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
Vishnu Nair81750622023-03-08 15:02:06 -080028#include "FrontEnd/DisplayInfo.h"
29#include "FrontEnd/LayerCreationArgs.h"
30#include "FrontEnd/Update.h"
Robert Carra63d52a2022-03-03 08:03:37 -080031#include "LocklessStack.h"
Vishnu Nair81750622023-03-08 15:02:06 -080032#include "RingBuffer.h"
Vishnu Nair7891e962021-11-11 12:07:21 -080033#include "TransactionProtoParser.h"
34
35using namespace android::surfaceflinger;
36
37namespace android {
38
Vishnu Nair7891e962021-11-11 12:07:21 -080039class SurfaceFlinger;
40class TransactionTracingTest;
Dominik Laskowski46471e62022-01-14 15:34:03 -080041
Vishnu Nair7891e962021-11-11 12:07:21 -080042/*
43 * Records all committed transactions into a ring bufffer.
44 *
45 * Transactions come in via the binder thread. They are serialized to proto
46 * and stored in a map using the transaction id as key. Main thread will
47 * pass the list of transaction ids that are committed every vsync and notify
48 * the tracing thread. The tracing thread will then wake up and add the
49 * committed transactions to the ring buffer.
50 *
51 * When generating SF dump state, we will flush the buffer to a file which
52 * will then be included in the bugreport.
53 *
54 */
55class TransactionTracing {
56public:
57 TransactionTracing();
58 ~TransactionTracing();
59
Vishnu Nair7891e962021-11-11 12:07:21 -080060 void addQueuedTransaction(const TransactionState&);
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050061 void addCommittedTransactions(int64_t vsyncId, nsecs_t commitTime, frontend::Update& update,
62 const frontend::DisplayInfos&, bool displayInfoChanged);
Vishnu Naird8f5e9f2022-02-03 10:23:28 -080063 status_t writeToFile(std::string filename = FILE_NAME);
Vishnu Nair7891e962021-11-11 12:07:21 -080064 void setBufferSize(size_t bufferSizeInBytes);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080065 void onLayerRemoved(int layerId);
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;
Vishnu Nair81750622023-03-08 15:02:06 -080069 // version 1 - switching to support new frontend
70 static constexpr auto TRACING_VERSION = 1;
Vishnu Nair7891e962021-11-11 12:07:21 -080071
72private:
73 friend class TransactionTracingTest;
Vishnu Nair81750622023-03-08 15:02:06 -080074 friend class SurfaceFlinger;
Vishnu Nair7891e962021-11-11 12:07:21 -080075
76 static constexpr auto FILE_NAME = "/data/misc/wmtrace/transactions_trace.winscope";
77
78 mutable std::mutex mTraceLock;
Dominik Laskowski46471e62022-01-14 15:34:03 -080079 RingBuffer<proto::TransactionTraceFile, proto::TransactionTraceEntry> mBuffer
Vishnu Nair7891e962021-11-11 12:07:21 -080080 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);
Robert Carra63d52a2022-03-03 08:03:37 -080084 LocklessStack<proto::TransactionState> mTransactionQueue;
Vishnu Nair0cc69e12021-11-18 09:05:49 -080085 nsecs_t mStartingTimestamp GUARDED_BY(mTraceLock);
Vishnu Nair286f4f92022-06-08 16:37:39 -070086 std::unordered_map<int, proto::LayerCreationArgs> mCreatedLayers GUARDED_BY(mTraceLock);
Vishnu Nair81750622023-03-08 15:02:06 -080087 std::map<uint32_t /* layerId */, TracingLayerState> mStartingStates GUARDED_BY(mTraceLock);
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050088 frontend::DisplayInfos mStartingDisplayInfos GUARDED_BY(mTraceLock);
Vishnu Nair81750622023-03-08 15:02:06 -080089
90 std::set<uint32_t /* layerId */> mRemovedLayerHandlesAtStart GUARDED_BY(mTraceLock);
91 TransactionProtoParser mProtoParser;
Vishnu Nair7891e962021-11-11 12:07:21 -080092
93 // We do not want main thread to block so main thread will try to acquire mMainThreadLock,
94 // otherwise will push data to temporary container.
95 std::mutex mMainThreadLock;
96 std::thread mThread GUARDED_BY(mMainThreadLock);
97 bool mDone GUARDED_BY(mMainThreadLock) = false;
98 std::condition_variable mTransactionsAvailableCv;
99 std::condition_variable mTransactionsAddedToBufferCv;
Vishnu Nair81750622023-03-08 15:02:06 -0800100 struct CommittedUpdates {
Vishnu Nair7891e962021-11-11 12:07:21 -0800101 std::vector<uint64_t> transactionIds;
Vishnu Nair81750622023-03-08 15:02:06 -0800102 std::vector<LayerCreationArgs> createdLayers;
103 std::vector<uint32_t> destroyedLayerHandles;
104 bool displayInfoChanged;
Dominik Laskowski6b049ff2023-01-29 15:46:45 -0500105 frontend::DisplayInfos displayInfos;
Vishnu Nair7891e962021-11-11 12:07:21 -0800106 int64_t vsyncId;
107 int64_t timestamp;
108 };
Vishnu Nair81750622023-03-08 15:02:06 -0800109 std::vector<CommittedUpdates> mUpdates GUARDED_BY(mMainThreadLock);
110 std::vector<CommittedUpdates> mPendingUpdates; // only accessed by main thread
Vishnu Nair7891e962021-11-11 12:07:21 -0800111
Vishnu Nair81750622023-03-08 15:02:06 -0800112 std::vector<uint32_t /* layerId */> mDestroyedLayers GUARDED_BY(mMainThreadLock);
113 std::vector<uint32_t /* layerId */> mPendingDestroyedLayers; // only accessed by main thread
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800114
115 proto::TransactionTraceFile createTraceFileProto() const;
Vishnu Nair7891e962021-11-11 12:07:21 -0800116 void loop();
Vishnu Nair81750622023-03-08 15:02:06 -0800117 void addEntry(const std::vector<CommittedUpdates>& committedTransactions,
118 const std::vector<uint32_t>& removedLayers) EXCLUDES(mTraceLock);
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800119 int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock);
120 void tryPushToTracingThread() EXCLUDES(mMainThreadLock);
121 void addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) REQUIRES(mTraceLock);
122 void updateStartingStateLocked(const proto::TransactionTraceEntry& entry) REQUIRES(mTraceLock);
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