File: TransactionTracing.h

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (128 lines) | stat: -rw-r--r-- 5,527 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <android-base/thread_annotations.h>
#include <layerproto/TransactionProto.h>
#include <utils/Errors.h>
#include <utils/Timers.h>

#include <memory>
#include <mutex>
#include <thread>

#include "RingBuffer.h"
#include "LocklessStack.h"
#include "TransactionProtoParser.h"

using namespace android::surfaceflinger;

namespace android {

class SurfaceFlinger;
class TransactionTracingTest;

/*
 * Records all committed transactions into a ring bufffer.
 *
 * Transactions come in via the binder thread. They are serialized to proto
 * and stored in a map using the transaction id as key. Main thread will
 * pass the list of transaction ids that are committed every vsync and notify
 * the tracing thread. The tracing thread will then wake up and add the
 * committed transactions to the ring buffer.
 *
 * When generating SF dump state, we will flush the buffer to a file which
 * will then be included in the bugreport.
 *
 */
class TransactionTracing {
public:
    TransactionTracing();
    ~TransactionTracing();

    void addQueuedTransaction(const TransactionState&);
    void addCommittedTransactions(std::vector<TransactionState>& transactions, int64_t vsyncId);
    status_t writeToFile(std::string filename = FILE_NAME);
    void setBufferSize(size_t bufferSizeInBytes);
    void onLayerAdded(BBinder* layerHandle, int layerId, const std::string& name, uint32_t flags,
                      int parentId);
    void onMirrorLayerAdded(BBinder* layerHandle, int layerId, const std::string& name,
                            int mirrorFromId);
    void onLayerRemoved(int layerId);
    void onHandleRemoved(BBinder* layerHandle);
    void dump(std::string&) const;
    static constexpr auto CONTINUOUS_TRACING_BUFFER_SIZE = 512 * 1024;
    static constexpr auto ACTIVE_TRACING_BUFFER_SIZE = 100 * 1024 * 1024;

private:
    friend class TransactionTracingTest;

    static constexpr auto FILE_NAME = "/data/misc/wmtrace/transactions_trace.winscope";

    mutable std::mutex mTraceLock;
    RingBuffer<proto::TransactionTraceFile, proto::TransactionTraceEntry> mBuffer
            GUARDED_BY(mTraceLock);
    size_t mBufferSizeInBytes GUARDED_BY(mTraceLock) = CONTINUOUS_TRACING_BUFFER_SIZE;
    std::unordered_map<uint64_t, proto::TransactionState> mQueuedTransactions
            GUARDED_BY(mTraceLock);
    LocklessStack<proto::TransactionState> mTransactionQueue;
    nsecs_t mStartingTimestamp GUARDED_BY(mTraceLock);
    std::vector<proto::LayerCreationArgs> mCreatedLayers GUARDED_BY(mTraceLock);
    std::unordered_map<BBinder* /* layerHandle */, int32_t /* layerId */> mLayerHandles
            GUARDED_BY(mTraceLock);
    std::vector<int32_t /* layerId */> mRemovedLayerHandles GUARDED_BY(mTraceLock);
    std::map<int32_t /* layerId */, TracingLayerState> mStartingStates GUARDED_BY(mTraceLock);
    TransactionProtoParser mProtoParser GUARDED_BY(mTraceLock);
    // Parses the transaction to proto without holding any tracing locks so we can generate proto
    // in the binder thread without any contention.
    TransactionProtoParser mLockfreeProtoParser;

    // We do not want main thread to block so main thread will try to acquire mMainThreadLock,
    // otherwise will push data to temporary container.
    std::mutex mMainThreadLock;
    std::thread mThread GUARDED_BY(mMainThreadLock);
    bool mDone GUARDED_BY(mMainThreadLock) = false;
    std::condition_variable mTransactionsAvailableCv;
    std::condition_variable mTransactionsAddedToBufferCv;
    struct CommittedTransactions {
        std::vector<uint64_t> transactionIds;
        int64_t vsyncId;
        int64_t timestamp;
    };
    std::vector<CommittedTransactions> mCommittedTransactions GUARDED_BY(mMainThreadLock);
    std::vector<CommittedTransactions> mPendingTransactions; // only accessed by main thread

    std::vector<int32_t /* layerId */> mRemovedLayers GUARDED_BY(mMainThreadLock);
    std::vector<int32_t /* layerId */> mPendingRemovedLayers; // only accessed by main thread

    proto::TransactionTraceFile createTraceFileProto() const;
    void loop();
    void addEntry(const std::vector<CommittedTransactions>& committedTransactions,
                  const std::vector<int32_t>& removedLayers) EXCLUDES(mTraceLock);
    int32_t getLayerIdLocked(const sp<IBinder>& layerHandle) REQUIRES(mTraceLock);
    void tryPushToTracingThread() EXCLUDES(mMainThreadLock);
    void addStartingStateToProtoLocked(proto::TransactionTraceFile& proto) REQUIRES(mTraceLock);
    void updateStartingStateLocked(const proto::TransactionTraceEntry& entry) REQUIRES(mTraceLock);

    // TEST
    // Wait until all the committed transactions for the specified vsync id are added to the buffer.
    void flush(int64_t vsyncId) EXCLUDES(mMainThreadLock);
    // Return buffer contents as trace file proto
    proto::TransactionTraceFile writeToProto() EXCLUDES(mMainThreadLock);
};

} // namespace android