blob: 084935b877a45f6b4131c7f3ae0ee523977722b0 [file] [log] [blame]
Adithya Srinivasanf279e042020-08-17 14:56:27 -07001/*
2 * Copyright 2020 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 Mouri9a29e672020-09-14 12:39:14 -070019#include <../TimeStats/TimeStats.h>
Ady Abraham22c7b5c2020-09-22 19:33:40 -070020#include <gui/ISurfaceComposer.h>
Jorim Jaggi5814ab82020-12-03 20:45:58 +010021#include <gui/JankInfo.h>
Adithya Srinivasan01189672020-10-20 14:23:05 -070022#include <perfetto/trace/android/frame_timeline_event.pbzero.h>
23#include <perfetto/tracing.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070024#include <ui/FenceTime.h>
25#include <utils/RefBase.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070026#include <utils/String16.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070027#include <utils/Timers.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070028#include <utils/Vector.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070029
Alec Mouri9a29e672020-09-14 12:39:14 -070030#include <deque>
31#include <mutex>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070032
Alec Mouri9a29e672020-09-14 12:39:14 -070033namespace android::frametimeline {
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070034
35enum JankMetadata {
36 // Frame was presented earlier than expected
37 EarlyPresent = 0x1,
38 // Frame was presented later than expected
39 LatePresent = 0x2,
40 // App/SF started earlier than expected
41 EarlyStart = 0x4,
42 // App/SF started later than expected
43 LateStart = 0x8,
44 // App/SF finished work earlier than the deadline
45 EarlyFinish = 0x10,
46 // App/SF finished work later than the deadline
47 LateFinish = 0x20,
48 // SF was in GPU composition
49 GpuComposition = 0x40,
50};
51
Adithya Srinivasanf279e042020-08-17 14:56:27 -070052class FrameTimelineTest;
53
54/*
55 * Collection of timestamps that can be used for both predictions and actual times.
56 */
57struct TimelineItem {
58 TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0,
59 const nsecs_t presentTime = 0)
60 : startTime(startTime), endTime(endTime), presentTime(presentTime) {}
61
62 nsecs_t startTime;
63 nsecs_t endTime;
64 nsecs_t presentTime;
Ady Abraham55fa7272020-09-30 19:19:27 -070065
66 bool operator==(const TimelineItem& other) const {
67 return startTime == other.startTime && endTime == other.endTime &&
68 presentTime == other.presentTime;
69 }
70
71 bool operator!=(const TimelineItem& other) const { return !(*this == other); }
Adithya Srinivasanf279e042020-08-17 14:56:27 -070072};
73
74/*
75 * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It
76 * saves these predictions for a short period of time and returns the predictions for a given token,
77 * if it hasn't expired.
78 */
79class TokenManager {
80public:
81 virtual ~TokenManager() = default;
82
83 // Generates a token for the given set of predictions. Stores the predictions for 120ms and
84 // destroys it later.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070085 virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -070086};
87
88enum class PredictionState {
89 Valid, // Predictions obtained successfully from the TokenManager
90 Expired, // TokenManager no longer has the predictions
91 None, // Predictions are either not present or didn't come from TokenManager
92};
93
94/*
95 * Stores a set of predictions and the corresponding actual timestamps pertaining to a single frame
96 * from the app
97 */
98class SurfaceFrame {
99public:
100 enum class PresentState {
101 Presented, // Buffer was latched and presented by SurfaceFlinger
102 Dropped, // Buffer was dropped by SurfaceFlinger
103 Unknown, // Initial state, SurfaceFlinger hasn't seen this buffer yet
104 };
105
106 virtual ~SurfaceFrame() = default;
107
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700108 virtual TimelineItem getPredictions() const = 0;
109 virtual TimelineItem getActuals() const = 0;
110 virtual nsecs_t getActualQueueTime() const = 0;
111 virtual PresentState getPresentState() const = 0;
112 virtual PredictionState getPredictionState() const = 0;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700113 virtual pid_t getOwnerPid() const = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700114
115 virtual void setPresentState(PresentState state) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700116
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700117 // Actual timestamps of the app are set individually at different functions.
118 // Start time (if the app provides) and Queue time are accessible after queueing the frame,
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700119 // whereas Acquire Fence time is available only during latch.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700120 virtual void setActualStartTime(nsecs_t actualStartTime) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700121 virtual void setActualQueueTime(nsecs_t actualQueueTime) = 0;
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700122 virtual void setAcquireFenceTime(nsecs_t acquireFenceTime) = 0;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100123
124 // Retrieves jank classification, if it's already been classified.
125 virtual std::optional<JankType> getJankType() const = 0;
126
127 // Token identifying the frame.
128 virtual int64_t getToken() const = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700129};
130
131/*
132 * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
133 * presented
134 */
135class FrameTimeline {
136public:
137 virtual ~FrameTimeline() = default;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700138 virtual TokenManager* getTokenManager() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700139
Adithya Srinivasan01189672020-10-20 14:23:05 -0700140 // Initializes the Perfetto DataSource that emits DisplayFrame and SurfaceFrame events. Test
141 // classes can avoid double registration by mocking this function.
142 virtual void onBootFinished() = 0;
143
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700144 // Create a new surface frame, set the predictions based on a token and return it to the caller.
145 // Sets the PredictionState of SurfaceFrame.
Alec Mouri9a29e672020-09-14 12:39:14 -0700146 // Debug name is the human-readable debugging string for dumpsys.
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100147 virtual std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700148 pid_t ownerPid, uid_t ownerUid, std::string layerName, std::string debugName,
Alec Mouri9a29e672020-09-14 12:39:14 -0700149 std::optional<int64_t> token) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700150
151 // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
152 // composited into one display frame.
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100153 virtual void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame,
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700154 SurfaceFrame::PresentState state) = 0;
155
156 // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
157 // the token and sets the actualSfWakeTime for the current DisplayFrame.
158 virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime) = 0;
159
160 // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the given present fence
161 // until it's signaled, and updates the present timestamps of all presented SurfaceFrames in
162 // that vsync.
163 virtual void setSfPresent(nsecs_t sfPresentTime,
164 const std::shared_ptr<FenceTime>& presentFence) = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700165
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700166 // Args:
167 // -jank : Dumps only the Display Frames that are either janky themselves
168 // or contain janky Surface Frames.
169 // -all : Dumps the entire list of DisplayFrames and the SurfaceFrames contained within
170 virtual void parseArgs(const Vector<String16>& args, std::string& result) = 0;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700171
172 // Sets the max number of display frames that can be stored. Called by SF backdoor.
173 virtual void setMaxDisplayFrames(uint32_t size);
174
175 // Restores the max number of display frames to default. Called by SF backdoor.
176 virtual void reset() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700177};
178
179namespace impl {
180
181using namespace std::chrono_literals;
182
183class TokenManager : public android::frametimeline::TokenManager {
184public:
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700185 TokenManager() : mCurrentToken(ISurfaceComposer::INVALID_VSYNC_ID + 1) {}
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700186 ~TokenManager() = default;
187
188 int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
189 std::optional<TimelineItem> getPredictionsForToken(int64_t token);
190
191private:
192 // Friend class for testing
193 friend class android::frametimeline::FrameTimelineTest;
194
195 void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
196
197 std::unordered_map<int64_t, TimelineItem> mPredictions GUARDED_BY(mMutex);
198 std::vector<std::pair<int64_t, nsecs_t>> mTokens GUARDED_BY(mMutex);
199 int64_t mCurrentToken GUARDED_BY(mMutex);
200 std::mutex mMutex;
201 static constexpr nsecs_t kMaxRetentionTime =
202 std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count();
203};
204
205class SurfaceFrame : public android::frametimeline::SurfaceFrame {
206public:
Adithya Srinivasan01189672020-10-20 14:23:05 -0700207 SurfaceFrame(int64_t token, pid_t ownerPid, uid_t ownerUid, std::string layerName,
208 std::string debugName, PredictionState predictionState,
209 TimelineItem&& predictions);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700210 ~SurfaceFrame() = default;
211
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700212 TimelineItem getPredictions() const override { return mPredictions; };
213 TimelineItem getActuals() const override;
214 nsecs_t getActualQueueTime() const override;
215 PresentState getPresentState() const override;
216 PredictionState getPredictionState() const override { return mPredictionState; };
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700217 pid_t getOwnerPid() const override { return mOwnerPid; };
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100218 std::optional<JankType> getJankType() const override;
219 int64_t getToken() const override { return mToken; };
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700220 nsecs_t getBaseTime() const;
221 uid_t getOwnerUid() const { return mOwnerUid; };
222 const std::string& getName() const { return mLayerName; };
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700223
224 void setActualStartTime(nsecs_t actualStartTime) override;
225 void setActualQueueTime(nsecs_t actualQueueTime) override;
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700226 void setAcquireFenceTime(nsecs_t acquireFenceTime) override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700227 void setPresentState(PresentState state) override;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700228 void setActualPresentTime(nsecs_t presentTime);
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100229 void setJankInfo(JankType jankType, int32_t jankMetadata);
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700230
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700231 // All the timestamps are dumped relative to the baseTime
232 void dump(std::string& result, const std::string& indent, nsecs_t baseTime);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700233
Adithya Srinivasan01189672020-10-20 14:23:05 -0700234 // Emits a packet for perfetto tracing. The function body will be executed only if tracing is
235 // enabled. The displayFrameToken is needed to link the SurfaceFrame to the corresponding
236 // DisplayFrame at the trace processor side.
237 void traceSurfaceFrame(int64_t displayFrameToken);
238
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700239private:
Adithya Srinivasan01189672020-10-20 14:23:05 -0700240 const int64_t mToken;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700241 const pid_t mOwnerPid;
Alec Mouri9a29e672020-09-14 12:39:14 -0700242 const uid_t mOwnerUid;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700243 const std::string mLayerName;
Alec Mouri9a29e672020-09-14 12:39:14 -0700244 const std::string mDebugName;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700245 PresentState mPresentState GUARDED_BY(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700246 const PredictionState mPredictionState;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700247 const TimelineItem mPredictions;
248 TimelineItem mActuals GUARDED_BY(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700249 nsecs_t mActualQueueTime GUARDED_BY(mMutex);
250 mutable std::mutex mMutex;
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100251 JankType mJankType GUARDED_BY(mMutex); // Enum for the type of jank
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700252 int32_t mJankMetadata GUARDED_BY(mMutex); // Additional details about the jank
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700253};
254
255class FrameTimeline : public android::frametimeline::FrameTimeline {
256public:
Adithya Srinivasan01189672020-10-20 14:23:05 -0700257 class FrameTimelineDataSource : public perfetto::DataSource<FrameTimelineDataSource> {
258 void OnSetup(const SetupArgs&) override{};
259 void OnStart(const StartArgs&) override{};
260 void OnStop(const StopArgs&) override{};
261 };
262
Alec Mouri9a29e672020-09-14 12:39:14 -0700263 FrameTimeline(std::shared_ptr<TimeStats> timeStats);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700264 ~FrameTimeline() = default;
265
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700266 frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; }
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100267 std::shared_ptr<frametimeline::SurfaceFrame> createSurfaceFrameForToken(
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700268 pid_t ownerPid, uid_t ownerUid, std::string layerName, std::string debugName,
Alec Mouri9a29e672020-09-14 12:39:14 -0700269 std::optional<int64_t> token) override;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100270 void addSurfaceFrame(std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame,
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700271 SurfaceFrame::PresentState state) override;
272 void setSfWakeUp(int64_t token, nsecs_t wakeupTime) override;
273 void setSfPresent(nsecs_t sfPresentTime,
274 const std::shared_ptr<FenceTime>& presentFence) override;
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700275 void parseArgs(const Vector<String16>& args, std::string& result) override;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700276 void setMaxDisplayFrames(uint32_t size) override;
277 void reset() override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700278
Adithya Srinivasan01189672020-10-20 14:23:05 -0700279 // Sets up the perfetto tracing backend and data source.
280 void onBootFinished() override;
281 // Registers the data source with the perfetto backend. Called as part of onBootFinished()
282 // and should not be called manually outside of tests.
283 void registerDataSource();
284
285 static constexpr char kFrameTimelineDataSource[] = "android.surfaceflinger.frametimeline";
286
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700287private:
288 // Friend class for testing
289 friend class android::frametimeline::FrameTimelineTest;
290
291 /*
292 * DisplayFrame should be used only internally within FrameTimeline.
293 */
294 struct DisplayFrame {
295 DisplayFrame();
296
Adithya Srinivasan01189672020-10-20 14:23:05 -0700297 int64_t token = ISurfaceComposer::INVALID_VSYNC_ID;
298
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700299 /* Usage of TimelineItem w.r.t SurfaceFlinger
300 * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates
301 * endTime Time when SurfaceFlinger sends a composited frame to Display
302 * presentTime Time when the composited frame was presented on screen
303 */
304 TimelineItem surfaceFlingerPredictions;
305 TimelineItem surfaceFlingerActuals;
306
307 // Collection of predictions and actual values sent over by Layers
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100308 std::vector<std::shared_ptr<SurfaceFrame>> surfaceFrames;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700309
Adithya Srinivasan01189672020-10-20 14:23:05 -0700310 PredictionState predictionState = PredictionState::None;
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100311 JankType jankType = JankType::None; // Enum for the type of jank
Adithya Srinivasan01189672020-10-20 14:23:05 -0700312 int32_t jankMetadata = 0x0; // Additional details about the jank
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700313 };
314
315 void flushPendingPresentFences() REQUIRES(mMutex);
316 void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700317 // BaseTime is the smallest timestamp in a DisplayFrame.
318 // Used for dumping all timestamps relative to the oldest, making it easy to read.
319 nsecs_t findBaseTime(const std::shared_ptr<DisplayFrame>&) REQUIRES(mMutex);
320 void dumpDisplayFrame(std::string& result, const std::shared_ptr<DisplayFrame>&,
321 nsecs_t baseTime) REQUIRES(mMutex);
322 void dumpAll(std::string& result);
323 void dumpJank(std::string& result);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700324
Adithya Srinivasan01189672020-10-20 14:23:05 -0700325 // Emits a packet for perfetto tracing. The function body will be executed only if tracing is
326 // enabled.
327 void traceDisplayFrame(const DisplayFrame& displayFrame) REQUIRES(mMutex);
328
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700329 // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
330 std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
331 std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
332 mPendingPresentFences GUARDED_BY(mMutex);
333 std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
334 TokenManager mTokenManager;
335 std::mutex mMutex;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700336 uint32_t mMaxDisplayFrames;
Alec Mouri9a29e672020-09-14 12:39:14 -0700337 std::shared_ptr<TimeStats> mTimeStats;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700338 static constexpr uint32_t kDefaultMaxDisplayFrames = 64;
Adithya Srinivasan01189672020-10-20 14:23:05 -0700339 // The initial container size for the vector<SurfaceFrames> inside display frame. Although
340 // this number doesn't represent any bounds on the number of surface frames that can go in a
341 // display frame, this is a good starting size for the vector so that we can avoid the
342 // internal vector resizing that happens with push_back.
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700343 static constexpr uint32_t kNumSurfaceFramesInitial = 10;
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700344 // The various thresholds for App and SF. If the actual timestamp falls within the threshold
345 // compared to prediction, we don't treat it as a jank.
346 static constexpr nsecs_t kPresentThreshold =
347 std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
348 static constexpr nsecs_t kDeadlineThreshold =
349 std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
350 static constexpr nsecs_t kSFStartThreshold =
351 std::chrono::duration_cast<std::chrono::nanoseconds>(1ms).count();
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700352};
353
354} // namespace impl
355} // namespace android::frametimeline