blob: f208eb8803150319e1aa5430a691abf66c0ffc74 [file] [log] [blame]
Adrian Roos1e1a1282017-11-01 19:05:31 +01001/*
2 * Copyright 2017 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
Alec Mouri5793c7d2020-03-10 19:55:50 -070019#include <android-base/thread_annotations.h>
Adrian Roos1e1a1282017-11-01 19:05:31 +010020#include <layerproto/LayerProtoHeader.h>
21#include <utils/Errors.h>
Nataniel Borges2b796da2019-02-15 13:32:18 -080022#include <utils/StrongPointer.h>
Adrian Roos1e1a1282017-11-01 19:05:31 +010023
Nataniel Borges2b796da2019-02-15 13:32:18 -080024#include <condition_variable>
Chia-I Wua3e7ddc2018-09-20 11:42:46 -070025#include <memory>
Adrian Roos1e1a1282017-11-01 19:05:31 +010026#include <mutex>
Yichi Chen9c696ed2018-10-01 22:32:30 +080027#include <queue>
Nataniel Borges2b796da2019-02-15 13:32:18 -080028#include <thread>
Adrian Roos1e1a1282017-11-01 19:05:31 +010029
30using namespace android::surfaceflinger;
31
32namespace android {
33
Nataniel Borges2b796da2019-02-15 13:32:18 -080034class SurfaceFlinger;
35
Yichi Chen9c696ed2018-10-01 22:32:30 +080036constexpr auto operator""_MB(unsigned long long const num) {
37 return num * 1024 * 1024;
38}
Adrian Roos1e1a1282017-11-01 19:05:31 +010039/*
40 * SurfaceTracing records layer states during surface flinging.
41 */
42class SurfaceTracing {
43public:
Dominik Laskowski9dab3432019-03-27 13:21:10 -070044 explicit SurfaceTracing(SurfaceFlinger& flinger);
Dominik Laskowski542c9dc2020-04-10 12:42:02 -070045 bool enable();
Nataniel Borges2b796da2019-02-15 13:32:18 -080046 bool disable();
47 status_t writeToFile();
Yichi Chen9c696ed2018-10-01 22:32:30 +080048 bool isEnabled() const;
Nataniel Borges2b796da2019-02-15 13:32:18 -080049 void notify(const char* where);
Vishnu Nair60db8c02020-04-02 11:55:16 -070050 void notifyLocked(const char* where) NO_THREAD_SAFETY_ANALYSIS /* REQUIRES(mSfLock) */;
Nataniel Borges2b796da2019-02-15 13:32:18 -080051
52 void setBufferSize(size_t bufferSizeInByte);
53 void writeToFileAsync();
Yiwei Zhang5434a782018-12-05 18:06:32 -080054 void dump(std::string& result) const;
Adrian Roos1e1a1282017-11-01 19:05:31 +010055
Vishnu Nair9245d3b2019-03-22 13:38:56 -070056 enum : uint32_t {
57 TRACE_CRITICAL = 1 << 0,
58 TRACE_INPUT = 1 << 1,
Vishnu Nair60db8c02020-04-02 11:55:16 -070059 TRACE_COMPOSITION = 1 << 2,
60 TRACE_EXTRA = 1 << 3,
61 TRACE_HWC = 1 << 4,
Vishnu Nair9245d3b2019-03-22 13:38:56 -070062 TRACE_ALL = 0xffffffff
63 };
64 void setTraceFlags(uint32_t flags);
Vishnu Nair60db8c02020-04-02 11:55:16 -070065 bool flagIsSetLocked(uint32_t flags) NO_THREAD_SAFETY_ANALYSIS /* REQUIRES(mSfLock) */ {
66 return (mTraceFlags & flags) == flags;
67 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -070068
Adrian Roos1e1a1282017-11-01 19:05:31 +010069private:
Robert Delgado626670f2019-06-27 15:55:27 -070070 static constexpr auto kDefaultBufferCapInByte = 5_MB;
Yichi Chen9c696ed2018-10-01 22:32:30 +080071 static constexpr auto kDefaultFileName = "/data/misc/wmtrace/layers_trace.pb";
72
73 class LayersTraceBuffer { // ring buffer
74 public:
75 size_t size() const { return mSizeInBytes; }
76 size_t used() const { return mUsedInBytes; }
77 size_t frameCount() const { return mStorage.size(); }
78
Nataniel Borges2b796da2019-02-15 13:32:18 -080079 void setSize(size_t newSize) { mSizeInBytes = newSize; }
Yichi Chen9c696ed2018-10-01 22:32:30 +080080 void reset(size_t newSize);
81 void emplace(LayersTraceProto&& proto);
82 void flush(LayersTraceFileProto* fileProto);
83
84 private:
85 size_t mUsedInBytes = 0U;
86 size_t mSizeInBytes = 0U;
87 std::queue<LayersTraceProto> mStorage;
88 };
Adrian Roos1e1a1282017-11-01 19:05:31 +010089
Nataniel Borges2b796da2019-02-15 13:32:18 -080090 void mainLoop();
Vishnu Nair73454c32020-04-03 18:56:19 -070091 bool addFirstEntry();
Vishnu Nair9245d3b2019-03-22 13:38:56 -070092 LayersTraceProto traceWhenNotified();
Dominik Laskowski542c9dc2020-04-10 12:42:02 -070093 LayersTraceProto traceLayersLocked(const char* where) REQUIRES(mSfLock);
Vishnu Nair9245d3b2019-03-22 13:38:56 -070094
95 // Returns true if trace is enabled.
96 bool addTraceToBuffer(LayersTraceProto& entry);
Nataniel Borges2b796da2019-02-15 13:32:18 -080097 void writeProtoFileLocked() REQUIRES(mTraceLock);
Adrian Roos1e1a1282017-11-01 19:05:31 +010098
Alec Mouri5793c7d2020-03-10 19:55:50 -070099 SurfaceFlinger& mFlinger;
Nataniel Borges2b796da2019-02-15 13:32:18 -0800100 status_t mLastErr = NO_ERROR;
101 std::thread mThread;
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700102 std::condition_variable mCanStartTrace;
Nataniel Borges2b796da2019-02-15 13:32:18 -0800103
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700104 std::mutex& mSfLock;
chaviwd8fcca92020-03-06 13:07:18 -0800105 uint32_t mTraceFlags GUARDED_BY(mSfLock) = TRACE_CRITICAL | TRACE_INPUT;
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700106 const char* mWhere GUARDED_BY(mSfLock) = "";
Vishnu Nair60db8c02020-04-02 11:55:16 -0700107 uint32_t mMissedTraceEntries GUARDED_BY(mSfLock) = 0;
108 bool mTracingInProgress GUARDED_BY(mSfLock) = false;
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700109
110 mutable std::mutex mTraceLock;
Nataniel Borges2b796da2019-02-15 13:32:18 -0800111 LayersTraceBuffer mBuffer GUARDED_BY(mTraceLock);
112 size_t mBufferSize GUARDED_BY(mTraceLock) = kDefaultBufferCapInByte;
113 bool mEnabled GUARDED_BY(mTraceLock) = false;
114 bool mWriteToFile GUARDED_BY(mTraceLock) = false;
Adrian Roos1e1a1282017-11-01 19:05:31 +0100115};
116
117} // namespace android