blob: f27e7a9663046f900c17c90e196bab6565ac03b0 [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 "Display/DisplayMap.h"
29#include "FrontEnd/DisplayInfo.h"
30#include "FrontEnd/LayerCreationArgs.h"
31#include "FrontEnd/Update.h"
Robert Carra63d52a2022-03-03 08:03:37 -080032#include "LocklessStack.h"
Vishnu Nair81750622023-03-08 15:02:06 -080033#include "RingBuffer.h"
Vishnu Nair7891e962021-11-11 12:07:21 -080034#include "TransactionProtoParser.h"
35
36using namespace android::surfaceflinger;
37
38namespace android {
39
Vishnu Nair7891e962021-11-11 12:07:21 -080040class SurfaceFlinger;
41class TransactionTracingTest;
Dominik Laskowski46471e62022-01-14 15:34:03 -080042
Vishnu Nair7891e962021-11-11 12:07:21 -080043/*
44 * Records all committed transactions into a ring bufffer.
45 *
46 * Transactions come in via the binder thread. They are serialized to proto
47 * and stored in a map using the transaction id as key. Main thread will
48 * pass the list of transaction ids that are committed every vsync and notify
49 * the tracing thread. The tracing thread will then wake up and add the
50 * committed transactions to the ring buffer.
51 *
52 * When generating SF dump state, we will flush the buffer to a file which
53 * will then be included in the bugreport.
54 *
55 */
56class TransactionTracing {
57public:
58 TransactionTracing();
59 ~TransactionTracing();
60
Vishnu Nair7891e962021-11-11 12:07:21 -080061 void addQueuedTransaction(const TransactionState&);
Vishnu Nair81750622023-03-08 15:02:06 -080062 void addCommittedTransactions(
63 int64_t vsyncId, nsecs_t commitTime, frontend::Update& update,
64 const display::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displayInfos,
65 bool displayInfoChanged);
Vishnu Naird8f5e9f2022-02-03 10:23:28 -080066 status_t writeToFile(std::string filename = FILE_NAME);
Vishnu Nair7891e962021-11-11 12:07:21 -080067 void setBufferSize(size_t bufferSizeInBytes);
Vishnu Nair0cc69e12021-11-18 09:05:49 -080068 void onLayerRemoved(int layerId);
Vishnu Nair7891e962021-11-11 12:07:21 -080069 void dump(std::string&) const;
70 static constexpr auto CONTINUOUS_TRACING_BUFFER_SIZE = 512 * 1024;
71 static constexpr auto ACTIVE_TRACING_BUFFER_SIZE = 100 * 1024 * 1024;
Vishnu Nair81750622023-03-08 15:02:06 -080072 // version 1 - switching to support new frontend
73 static constexpr auto TRACING_VERSION = 1;
Vishnu Nair7891e962021-11-11 12:07:21 -080074
75private:
76 friend class TransactionTracingTest;
Vishnu Nair81750622023-03-08 15:02:06 -080077 friend class SurfaceFlinger;
Vishnu Nair7891e962021-11-11 12:07:21 -080078
79 static constexpr auto FILE_NAME = "/data/misc/wmtrace/transactions_trace.winscope";
80
81 mutable std::mutex mTraceLock;
Dominik Laskowski46471e62022-01-14 15:34:03 -080082 RingBuffer<proto::TransactionTraceFile, proto::TransactionTraceEntry> mBuffer
Vishnu Nair7891e962021-11-11 12:07:21 -080083 GUARDED_BY(mTraceLock);
84 size_t mBufferSizeInBytes GUARDED_BY(mTraceLock) = CONTINUOUS_TRACING_BUFFER_SIZE;
85 std::unordered_map<uint64_t, proto::TransactionState> mQueuedTransactions
86 GUARDED_BY(mTraceLock);
Robert Carra63d52a2022-03-03 08:03:37 -080087 LocklessStack<proto::TransactionState> mTransactionQueue;
Vishnu Nair0cc69e12021-11-18 09:05:49 -080088 nsecs_t mStartingTimestamp GUARDED_BY(mTraceLock);
Vishnu Nair286f4f92022-06-08 16:37:39 -070089 std::unordered_map<int, proto::LayerCreationArgs> mCreatedLayers GUARDED_BY(mTraceLock);
Vishnu Nair81750622023-03-08 15:02:06 -080090 std::map<uint32_t /* layerId */, TracingLayerState> mStartingStates GUARDED_BY(mTraceLock);
91 display::DisplayMap<ui::LayerStack, frontend::DisplayInfo> mStartingDisplayInfos
Vishnu Nair0cc69e12021-11-18 09:05:49 -080092 GUARDED_BY(mTraceLock);
Vishnu Nair81750622023-03-08 15:02:06 -080093
94 std::set<uint32_t /* layerId */> mRemovedLayerHandlesAtStart GUARDED_BY(mTraceLock);
95 TransactionProtoParser mProtoParser;
Vishnu Nair7891e962021-11-11 12:07:21 -080096
97 // We do not want main thread to block so main thread will try to acquire mMainThreadLock,
98 // otherwise will push data to temporary container.
99 std::mutex mMainThreadLock;
100 std::thread mThread GUARDED_BY(mMainThreadLock);
101 bool mDone GUARDED_BY(mMainThreadLock) = false;
102 std::condition_variable mTransactionsAvailableCv;
103 std::condition_variable mTransactionsAddedToBufferCv;
Vishnu Nair81750622023-03-08 15:02:06 -0800104 struct CommittedUpdates {
Vishnu Nair7891e962021-11-11 12:07:21 -0800105 std::vector<uint64_t> transactionIds;
Vishnu Nair81750622023-03-08 15:02:06 -0800106 std::vector<LayerCreationArgs> createdLayers;
107 std::vector<uint32_t> destroyedLayerHandles;
108 bool displayInfoChanged;
109 display::DisplayMap<ui::LayerStack, frontend::DisplayInfo> displayInfos;
Vishnu Nair7891e962021-11-11 12:07:21 -0800110 int64_t vsyncId;
111 int64_t timestamp;
112 };
Vishnu Nair81750622023-03-08 15:02:06 -0800113 std::vector<CommittedUpdates> mUpdates GUARDED_BY(mMainThreadLock);
114 std::vector<CommittedUpdates> mPendingUpdates; // only accessed by main thread
Vishnu Nair7891e962021-11-11 12:07:21 -0800115
Vishnu Nair81750622023-03-08 15:02:06 -0800116 std::vector<uint32_t /* layerId */> mDestroyedLayers GUARDED_BY(mMainThreadLock);
117 std::vector<uint32_t /* layerId */> mPendingDestroyedLayers; // only accessed by main thread
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800118
119 proto::TransactionTraceFile createTraceFileProto() const;
Vishnu Nair7891e962021-11-11 12:07:21 -0800120 void loop();
Vishnu Nair81750622023-03-08 15:02:06 -0800121 void addEntry(const std::vector<CommittedUpdates>& committedTransactions,
122 const std::vector<uint32_t>& removedLayers) EXCLUDES(mTraceLock);
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800123 int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock);
124 void tryPushToTracingThread() EXCLUDES(mMainThreadLock);
125 void addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) REQUIRES(mTraceLock);
126 void updateStartingStateLocked(const proto::TransactionTraceEntry& entry) REQUIRES(mTraceLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800127 // TEST
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800128 // Wait until all the committed transactions for the specified vsync id are added to the buffer.
129 void flush(int64_t vsyncId) EXCLUDES(mMainThreadLock);
130 // Return buffer contents as trace file proto
131 proto::TransactionTraceFile writeToProto() EXCLUDES(mMainThreadLock);
Vishnu Nair7891e962021-11-11 12:07:21 -0800132};
133
134} // namespace android