blob: 6cda3094409e81936981a18af283c6b66c370b05 [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
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080019#include <atomic>
20#include <chrono>
21#include <deque>
22#include <memory>
23#include <mutex>
24#include <optional>
25#include <string>
26
Ady Abraham22c7b5c2020-09-22 19:33:40 -070027#include <gui/ISurfaceComposer.h>
Jorim Jaggi5814ab82020-12-03 20:45:58 +010028#include <gui/JankInfo.h>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070029#include <gui/LayerMetadata.h>
Adithya Srinivasan01189672020-10-20 14:23:05 -070030#include <perfetto/trace/android/frame_timeline_event.pbzero.h>
31#include <perfetto/tracing.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070032#include <ui/FenceTime.h>
33#include <utils/RefBase.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070034#include <utils/String16.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070035#include <utils/Timers.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070036#include <utils/Vector.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070037
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080038#include <scheduler/Fps.h>
39
40#include "../TimeStats/TimeStats.h"
Adithya Srinivasanf279e042020-08-17 14:56:27 -070041
Alec Mouri9a29e672020-09-14 12:39:14 -070042namespace android::frametimeline {
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070043
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080044class FrameTimelineTest;
45
46using namespace std::chrono_literals;
47
48// Metadata indicating how the frame was presented w.r.t expected present time.
49enum class FramePresentMetadata : int8_t {
50 // Frame was presented on time
51 OnTimePresent,
52 // Frame was presented late
53 LatePresent,
54 // Frame was presented early
55 EarlyPresent,
56 // Unknown/initial state
57 UnknownPresent,
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070058};
59
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080060// Metadata comparing the frame's actual finish time to the expected deadline.
61enum class FrameReadyMetadata : int8_t {
62 // App/SF finished on time. Early finish is treated as on time since the goal of any component
63 // is to finish before the deadline.
64 OnTimeFinish,
65 // App/SF finished work later than expected
66 LateFinish,
67 // Unknown/initial state
68 UnknownFinish,
69};
70
71// Metadata comparing the frame's actual start time to the expected start time.
72enum class FrameStartMetadata : int8_t {
73 // App/SF started on time
74 OnTimeStart,
75 // App/SF started later than expected
76 LateStart,
77 // App/SF started earlier than expected
78 EarlyStart,
79 // Unknown/initial state
80 UnknownStart,
81};
Adithya Srinivasanf279e042020-08-17 14:56:27 -070082
83/*
84 * Collection of timestamps that can be used for both predictions and actual times.
85 */
86struct TimelineItem {
87 TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0,
Ben Widawskyebdbead2024-10-24 11:47:50 -070088 const nsecs_t presentTime = 0, const nsecs_t desiredPresentTime = 0)
89 : startTime(startTime),
90 endTime(endTime),
91 presentTime(presentTime),
92 desiredPresentTime(desiredPresentTime) {}
Adithya Srinivasanf279e042020-08-17 14:56:27 -070093
94 nsecs_t startTime;
95 nsecs_t endTime;
96 nsecs_t presentTime;
Ben Widawskyebdbead2024-10-24 11:47:50 -070097 nsecs_t desiredPresentTime;
Ady Abraham55fa7272020-09-30 19:19:27 -070098
99 bool operator==(const TimelineItem& other) const {
100 return startTime == other.startTime && endTime == other.endTime &&
Ben Widawskyebdbead2024-10-24 11:47:50 -0700101 presentTime == other.presentTime && desiredPresentTime != other.desiredPresentTime;
Ady Abraham55fa7272020-09-30 19:19:27 -0700102 }
103
104 bool operator!=(const TimelineItem& other) const { return !(*this == other); }
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700105};
106
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800107struct JankClassificationThresholds {
108 // The various thresholds for App and SF. If the actual timestamp falls within the threshold
109 // compared to prediction, we treat it as on time.
110 nsecs_t presentThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
Adithya Srinivasan54996e22021-06-25 22:26:45 +0000111 nsecs_t deadlineThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(0ms).count();
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800112 nsecs_t startThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
113};
114
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700115/*
116 * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It
117 * saves these predictions for a short period of time and returns the predictions for a given token,
118 * if it hasn't expired.
119 */
120class TokenManager {
121public:
122 virtual ~TokenManager() = default;
123
124 // Generates a token for the given set of predictions. Stores the predictions for 120ms and
125 // destroys it later.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700126 virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800127
128 // Returns the stored predictions for a given token, if the predictions haven't expired.
129 virtual std::optional<TimelineItem> getPredictionsForToken(int64_t token) const = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700130};
131
132enum class PredictionState {
133 Valid, // Predictions obtained successfully from the TokenManager
134 Expired, // TokenManager no longer has the predictions
135 None, // Predictions are either not present or didn't come from TokenManager
136};
137
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000138/*
139 * Trace cookie is used to send start and end timestamps of <Surface/Display>Frames separately
140 * without needing to resend all the other information. We send all info to perfetto, along with a
141 * new cookie, in the start of a frame. For the corresponding end, we just send the same cookie.
142 * This helps in reducing the amount of data emitted by the producer.
143 */
144class TraceCookieCounter {
145public:
146 int64_t getCookieForTracing();
147
148private:
149 // Friend class for testing
150 friend class android::frametimeline::FrameTimelineTest;
151
152 std::atomic<int64_t> mTraceCookie = 0;
153};
154
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700155class SurfaceFrame {
156public:
157 enum class PresentState {
158 Presented, // Buffer was latched and presented by SurfaceFlinger
159 Dropped, // Buffer was dropped by SurfaceFlinger
160 Unknown, // Initial state, SurfaceFlinger hasn't seen this buffer yet
161 };
162
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800163 // Only FrameTimeline can construct a SurfaceFrame as it provides Predictions(through
164 // TokenManager), Thresholds and TimeStats pointer.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000165 SurfaceFrame(const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
Alec Mouriadebf5c2021-01-05 12:57:36 -0800166 int32_t layerId, std::string layerName, std::string debugName,
167 PredictionState predictionState, TimelineItem&& predictions,
168 std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700169 TraceCookieCounter* traceCookieCounter, bool isBuffer, GameMode);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800170 ~SurfaceFrame() = default;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700171
Sally Qi438eb7d2023-12-05 18:59:32 -0800172 bool isSelfJanky() const;
173
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800174 // Returns std::nullopt if the frame hasn't been classified yet.
175 // Used by both SF and FrameTimeline.
176 std::optional<int32_t> getJankType() const;
Ying Wei96eb5352023-11-21 17:37:21 +0000177 std::optional<JankSeverityType> getJankSeverityType() const;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700178
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800179 // Functions called by SF
180 int64_t getToken() const { return mToken; };
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000181 int32_t getInputEventId() const { return mInputEventId; };
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800182 TimelineItem getPredictions() const { return mPredictions; };
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700183 // Actual timestamps of the app are set individually at different functions.
184 // Start time (if the app provides) and Queue time are accessible after queueing the frame,
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000185 // whereas Acquire Fence time is available only during latch. Drop time is available at the time
186 // the buffer was dropped.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800187 void setActualStartTime(nsecs_t actualStartTime);
188 void setActualQueueTime(nsecs_t actualQueueTime);
189 void setAcquireFenceTime(nsecs_t acquireFenceTime);
Ben Widawskyebdbead2024-10-24 11:47:50 -0700190 void setDesiredPresentTime(nsecs_t desiredPresentTime);
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000191 void setDropTime(nsecs_t dropTime);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800192 void setPresentState(PresentState presentState, nsecs_t lastLatchTime = 0);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800193 void setRenderRate(Fps renderRate);
Pascal Muetschardac7bcd92023-10-03 15:05:36 +0200194 // Return the render rate if it exists, otherwise returns the DisplayFrame's render rate.
195 Fps getRenderRate() const;
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000196 void setGpuComposition();
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100197
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000198 // When a bufferless SurfaceFrame is promoted to a buffer SurfaceFrame, we also have to update
199 // isBuffer.
200 void promoteToBuffer();
201
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800202 // Functions called by FrameTimeline
203 // BaseTime is the smallest timestamp in this SurfaceFrame.
204 // Used for dumping all timestamps relative to the oldest, making it easy to read.
205 nsecs_t getBaseTime() const;
206 // Sets the actual present time, appropriate metadata and classifies the jank.
Alec Mouri363faf02021-01-29 16:34:55 -0800207 // displayRefreshRate, displayDeadlineDelta, and displayPresentDelta are propagated from the
208 // display frame.
209 void onPresent(nsecs_t presentTime, int32_t displayFrameJankType, Fps refreshRate,
Pascal Muetschardac7bcd92023-10-03 15:05:36 +0200210 Fps displayFrameRenderRate, nsecs_t displayDeadlineDelta,
211 nsecs_t displayPresentDelta);
Ady Abraham14beed72024-05-15 17:16:45 -0700212 // Sets the frame as none janky as there was no real display frame.
213 void onCommitNotComposited(Fps refreshRate, Fps displayFrameRenderRate);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800214 // All the timestamps are dumped relative to the baseTime
215 void dump(std::string& result, const std::string& indent, nsecs_t baseTime) const;
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000216 // Dumps only the layer, token, is buffer, jank metadata, prediction and present states.
217 std::string miniDump() const;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800218 // Emits a packet for perfetto tracing. The function body will be executed only if tracing is
219 // enabled. The displayFrameToken is needed to link the SurfaceFrame to the corresponding
Ady Abraham57f8e182022-03-08 15:54:33 -0800220 // DisplayFrame at the trace processor side. monoBootOffset is the difference
221 // between SYSTEM_TIME_BOOTTIME and SYSTEM_TIME_MONOTONIC.
Ady Abraham43a68c32024-09-04 19:21:20 -0700222 void trace(int64_t displayFrameToken, nsecs_t monoBootOffset,
223 bool filterFramesBeforeTraceStarts) const;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100224
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000225 // Getter functions used only by FrameTimelineTests and SurfaceFrame internally
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800226 TimelineItem getActuals() const;
227 pid_t getOwnerPid() const { return mOwnerPid; };
Alec Mouriadebf5c2021-01-05 12:57:36 -0800228 int32_t getLayerId() const { return mLayerId; };
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000229 PredictionState getPredictionState() const;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800230 PresentState getPresentState() const;
231 FrameReadyMetadata getFrameReadyMetadata() const;
232 FramePresentMetadata getFramePresentMetadata() const;
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000233 nsecs_t getDropTime() const;
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000234 bool getIsBuffer() const;
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000235
236 // For prediction expired frames, this delta is subtracted from the actual end time to get a
237 // start time decent enough to see in traces.
238 // TODO(b/172587309): Remove this when we have actual start times.
239 static constexpr nsecs_t kPredictionExpiredStartTimeDelta =
240 std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800241
242private:
Ady Abraham43a68c32024-09-04 19:21:20 -0700243 void tracePredictions(int64_t displayFrameToken, nsecs_t monoBootOffset,
244 bool filterFramesBeforeTraceStarts) const;
245 void traceActuals(int64_t displayFrameToken, nsecs_t monoBootOffset,
246 bool filterFramesBeforeTraceStarts) const;
Adithya Srinivasan7c4ac7a2021-03-08 23:48:03 +0000247 void classifyJankLocked(int32_t displayFrameJankType, const Fps& refreshRate,
Ady Abrahame96e79f2024-05-20 18:00:39 +0000248 Fps displayFrameRenderRate, nsecs_t* outDeadlineDelta) REQUIRES(mMutex);
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000249
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800250 const int64_t mToken;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000251 const int32_t mInputEventId;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800252 const pid_t mOwnerPid;
253 const uid_t mOwnerUid;
254 const std::string mLayerName;
255 const std::string mDebugName;
Alec Mouriadebf5c2021-01-05 12:57:36 -0800256 const int32_t mLayerId;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800257 PresentState mPresentState GUARDED_BY(mMutex);
258 const PredictionState mPredictionState;
259 const TimelineItem mPredictions;
260 TimelineItem mActuals GUARDED_BY(mMutex);
261 std::shared_ptr<TimeStats> mTimeStats;
262 const JankClassificationThresholds mJankClassificationThresholds;
263 nsecs_t mActualQueueTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000264 nsecs_t mDropTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800265 mutable std::mutex mMutex;
266 // Bitmask for the type of jank
267 int32_t mJankType GUARDED_BY(mMutex) = JankType::None;
Ying Wei96eb5352023-11-21 17:37:21 +0000268 // Enum for the severity of jank
269 JankSeverityType mJankSeverityType GUARDED_BY(mMutex) = JankSeverityType::None;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800270 // Indicates if this frame was composited by the GPU or not
271 bool mGpuComposition GUARDED_BY(mMutex) = false;
Pascal Muetschardac7bcd92023-10-03 15:05:36 +0200272 // Refresh rate for this frame.
273 Fps mDisplayFrameRenderRate GUARDED_BY(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800274 // Rendering rate for this frame.
275 std::optional<Fps> mRenderRate GUARDED_BY(mMutex);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800276 // Enum for the type of present
277 FramePresentMetadata mFramePresentMetadata GUARDED_BY(mMutex) =
278 FramePresentMetadata::UnknownPresent;
279 // Enum for the type of finish
280 FrameReadyMetadata mFrameReadyMetadata GUARDED_BY(mMutex) = FrameReadyMetadata::UnknownFinish;
281 // Time when the previous buffer from the same layer was latched by SF. This is used in checking
282 // for BufferStuffing where the current buffer is expected to be ready but the previous buffer
283 // was latched instead.
284 nsecs_t mLastLatchTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000285 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto. Using a
286 // reference here because the counter is owned by FrameTimeline, which outlives SurfaceFrame.
287 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000288 // Tells if the SurfaceFrame is representing a buffer or a transaction without a
289 // buffer(animations)
290 bool mIsBuffer;
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000291 // GameMode from the layer. Used in metrics.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700292 GameMode mGameMode = GameMode::Unsupported;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700293};
294
295/*
296 * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
297 * presented
298 */
299class FrameTimeline {
300public:
301 virtual ~FrameTimeline() = default;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700302 virtual TokenManager* getTokenManager() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700303
Adithya Srinivasan01189672020-10-20 14:23:05 -0700304 // Initializes the Perfetto DataSource that emits DisplayFrame and SurfaceFrame events. Test
305 // classes can avoid double registration by mocking this function.
306 virtual void onBootFinished() = 0;
307
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700308 // Create a new surface frame, set the predictions based on a token and return it to the caller.
Alec Mouri9a29e672020-09-14 12:39:14 -0700309 // Debug name is the human-readable debugging string for dumpsys.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000310 virtual std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
311 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000312 int32_t layerId, std::string layerName, std::string debugName, bool isBuffer,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700313 GameMode) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700314
315 // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
316 // composited into one display frame.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800317 virtual void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700318
319 // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
320 // the token and sets the actualSfWakeTime for the current DisplayFrame.
Pascal Muetschardac7bcd92023-10-03 15:05:36 +0200321 virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate,
322 Fps renderRate) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700323
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000324 // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000325 // given present fence until it's signaled, and updates the present timestamps of all presented
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000326 // SurfaceFrames in that vsync. If a gpuFence was also provided, its tracked in the
327 // corresponding DisplayFrame.
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000328 virtual void setSfPresent(nsecs_t sfPresentTime, const std::shared_ptr<FenceTime>& presentFence,
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000329 const std::shared_ptr<FenceTime>& gpuFence) = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700330
Ady Abraham14beed72024-05-15 17:16:45 -0700331 // Tells FrameTimeline that a frame was committed but not composited. This is used to flush
332 // all the associated surface frames.
333 virtual void onCommitNotComposited() = 0;
334
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700335 // Args:
336 // -jank : Dumps only the Display Frames that are either janky themselves
337 // or contain janky Surface Frames.
338 // -all : Dumps the entire list of DisplayFrames and the SurfaceFrames contained within
339 virtual void parseArgs(const Vector<String16>& args, std::string& result) = 0;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700340
341 // Sets the max number of display frames that can be stored. Called by SF backdoor.
Josh Gaoade0f672023-01-17 14:59:04 -0800342 virtual void setMaxDisplayFrames(uint32_t size) = 0;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700343
Alec Mouriadebf5c2021-01-05 12:57:36 -0800344 // Computes the historical fps for the provided set of layer IDs
345 // The fps is compted from the linear timeline of present timestamps for DisplayFrames
346 // containing at least one layer ID.
Josh Gaoade0f672023-01-17 14:59:04 -0800347 virtual float computeFps(const std::unordered_set<int32_t>& layerIds) = 0;
Alec Mouriadebf5c2021-01-05 12:57:36 -0800348
Ben Widawskyebdbead2024-10-24 11:47:50 -0700349 // Supports the legacy FrameStats interface
350 virtual void generateFrameStats(int32_t layer, size_t count, FrameStats* outStats) const = 0;
351
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700352 // Restores the max number of display frames to default. Called by SF backdoor.
353 virtual void reset() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700354};
355
356namespace impl {
357
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700358class TokenManager : public android::frametimeline::TokenManager {
359public:
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000360 TokenManager() : mCurrentToken(FrameTimelineInfo::INVALID_VSYNC_ID + 1) {}
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700361 ~TokenManager() = default;
362
363 int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800364 std::optional<TimelineItem> getPredictionsForToken(int64_t token) const override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700365
366private:
367 // Friend class for testing
368 friend class android::frametimeline::FrameTimelineTest;
369
370 void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
371
Adithya Srinivasanbed4c4f2021-05-03 20:24:46 +0000372 std::map<int64_t, TimelineItem> mPredictions GUARDED_BY(mMutex);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700373 int64_t mCurrentToken GUARDED_BY(mMutex);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800374 mutable std::mutex mMutex;
Adithya Srinivasanbed4c4f2021-05-03 20:24:46 +0000375 static constexpr size_t kMaxTokens = 500;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700376};
377
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700378class FrameTimeline : public android::frametimeline::FrameTimeline {
379public:
Adithya Srinivasan01189672020-10-20 14:23:05 -0700380 class FrameTimelineDataSource : public perfetto::DataSource<FrameTimelineDataSource> {
Ady Abraham43a68c32024-09-04 19:21:20 -0700381 public:
382 nsecs_t getStartTime() const { return mTraceStartTime; }
383
384 private:
385 void OnSetup(const SetupArgs&) override {};
386 void OnStart(const StartArgs&) override { mTraceStartTime = systemTime(); };
387 void OnStop(const StopArgs&) override {};
388
389 nsecs_t mTraceStartTime = 0;
Adithya Srinivasan01189672020-10-20 14:23:05 -0700390 };
391
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800392 /*
393 * DisplayFrame should be used only internally within FrameTimeline. All members and methods are
394 * guarded by FrameTimeline's mMutex.
395 */
396 class DisplayFrame {
397 public:
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000398 DisplayFrame(std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds,
Adithya Srinivasan82eef322021-04-10 00:06:04 +0000399 TraceCookieCounter* traceCookieCounter);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800400 virtual ~DisplayFrame() = default;
401 // Dumpsys interface - dumps only if the DisplayFrame itself is janky or is at least one
402 // SurfaceFrame is janky.
403 void dumpJank(std::string& result, nsecs_t baseTime, int displayFrameCount) const;
404 // Dumpsys interface - dumps all data irrespective of jank
405 void dumpAll(std::string& result, nsecs_t baseTime) const;
406 // Emits a packet for perfetto tracing. The function body will be executed only if tracing
Ady Abraham57f8e182022-03-08 15:54:33 -0800407 // is enabled. monoBootOffset is the difference between SYSTEM_TIME_BOOTTIME
408 // and SYSTEM_TIME_MONOTONIC.
Sally Qi438eb7d2023-12-05 18:59:32 -0800409 nsecs_t trace(pid_t surfaceFlingerPid, nsecs_t monoBootOffset,
Ady Abraham43a68c32024-09-04 19:21:20 -0700410 nsecs_t previousPredictionPresentTime,
411 bool filterFramesBeforeTraceStarts) const;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800412 // Sets the token, vsyncPeriod, predictions and SF start time.
Pascal Muetschardac7bcd92023-10-03 15:05:36 +0200413 void onSfWakeUp(int64_t token, Fps refreshRate, Fps renderRate,
414 std::optional<TimelineItem> predictions, nsecs_t wakeUpTime);
Adithya Srinivasan115ac692021-03-06 01:21:30 +0000415 // Sets the appropriate metadata and classifies the jank.
Adithya Srinivasan57dc81d2021-04-14 17:31:41 +0000416 void onPresent(nsecs_t signalTime, nsecs_t previousPresentTime);
Ady Abraham14beed72024-05-15 17:16:45 -0700417 // Flushes all the surface frames as those were not generating any actual display frames.
418 void onCommitNotComposited();
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800419 // Adds the provided SurfaceFrame to the current display frame.
420 void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame);
421
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800422 void setPredictions(PredictionState predictionState, TimelineItem predictions);
423 void setActualStartTime(nsecs_t actualStartTime);
424 void setActualEndTime(nsecs_t actualEndTime);
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000425 void setGpuFence(const std::shared_ptr<FenceTime>& gpuFence);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800426
427 // BaseTime is the smallest timestamp in a DisplayFrame.
428 // Used for dumping all timestamps relative to the oldest, making it easy to read.
429 nsecs_t getBaseTime() const;
430
431 // Functions to be used only in testing.
432 TimelineItem getActuals() const { return mSurfaceFlingerActuals; };
433 TimelineItem getPredictions() const { return mSurfaceFlingerPredictions; };
Adithya Srinivasan939cd4d2021-02-23 06:18:13 +0000434 FrameStartMetadata getFrameStartMetadata() const { return mFrameStartMetadata; };
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800435 FramePresentMetadata getFramePresentMetadata() const { return mFramePresentMetadata; };
436 FrameReadyMetadata getFrameReadyMetadata() const { return mFrameReadyMetadata; };
437 int32_t getJankType() const { return mJankType; }
Ying Wei96eb5352023-11-21 17:37:21 +0000438 JankSeverityType getJankSeverityType() const { return mJankSeverityType; }
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800439 const std::vector<std::shared_ptr<SurfaceFrame>>& getSurfaceFrames() const {
440 return mSurfaceFrames;
441 }
442
443 private:
444 void dump(std::string& result, nsecs_t baseTime) const;
Ady Abraham43a68c32024-09-04 19:21:20 -0700445 void tracePredictions(pid_t surfaceFlingerPid, nsecs_t monoBootOffset,
446 bool filterFramesBeforeTraceStarts) const;
447 void traceActuals(pid_t surfaceFlingerPid, nsecs_t monoBootOffset,
448 bool filterFramesBeforeTraceStarts) const;
Sally Qiaa107742023-09-29 14:53:14 -0700449 void addSkippedFrame(pid_t surfaceFlingerPid, nsecs_t monoBootOffset,
Ady Abraham43a68c32024-09-04 19:21:20 -0700450 nsecs_t previousActualPresentTime,
451 bool filterFramesBeforeTraceStarts) const;
Adithya Srinivasan57dc81d2021-04-14 17:31:41 +0000452 void classifyJank(nsecs_t& deadlineDelta, nsecs_t& deltaToVsync,
453 nsecs_t previousPresentTime);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800454
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000455 int64_t mToken = FrameTimelineInfo::INVALID_VSYNC_ID;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800456
457 /* Usage of TimelineItem w.r.t SurfaceFlinger
458 * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates
459 * endTime Time when SurfaceFlinger sends a composited frame to Display
460 * presentTime Time when the composited frame was presented on screen
461 */
462 TimelineItem mSurfaceFlingerPredictions;
463 TimelineItem mSurfaceFlingerActuals;
464 std::shared_ptr<TimeStats> mTimeStats;
465 const JankClassificationThresholds mJankClassificationThresholds;
466
467 // Collection of predictions and actual values sent over by Layers
468 std::vector<std::shared_ptr<SurfaceFrame>> mSurfaceFrames;
469
470 PredictionState mPredictionState = PredictionState::None;
471 // Bitmask for the type of jank
472 int32_t mJankType = JankType::None;
Ying Wei96eb5352023-11-21 17:37:21 +0000473 // Enum for the severity of jank
474 JankSeverityType mJankSeverityType = JankSeverityType::None;
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000475 // A valid gpu fence indicates that the DisplayFrame was composited by the GPU
476 std::shared_ptr<FenceTime> mGpuFence = FenceTime::NO_FENCE;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800477 // Enum for the type of present
478 FramePresentMetadata mFramePresentMetadata = FramePresentMetadata::UnknownPresent;
479 // Enum for the type of finish
480 FrameReadyMetadata mFrameReadyMetadata = FrameReadyMetadata::UnknownFinish;
481 // Enum for the type of start
482 FrameStartMetadata mFrameStartMetadata = FrameStartMetadata::UnknownStart;
483 // The refresh rate (vsync period) in nanoseconds as seen by SF during this DisplayFrame's
484 // timeline
Alec Mouri7d436ec2021-01-27 20:40:50 -0800485 Fps mRefreshRate;
Pascal Muetschardac7bcd92023-10-03 15:05:36 +0200486 // The current render rate for this DisplayFrame.
487 Fps mRenderRate;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000488 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto.
489 // Using a reference here because the counter is owned by FrameTimeline, which outlives
490 // DisplayFrame.
491 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800492 };
493
494 FrameTimeline(std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid,
Ady Abraham43a68c32024-09-04 19:21:20 -0700495 JankClassificationThresholds thresholds = {}, bool useBootTimeClock = true,
496 bool filterFramesBeforeTraceStarts = true);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700497 ~FrameTimeline() = default;
498
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700499 frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; }
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000500 std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
501 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000502 int32_t layerId, std::string layerName, std::string debugName, bool isBuffer,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700503 GameMode) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800504 void addSurfaceFrame(std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame) override;
Pascal Muetschardac7bcd92023-10-03 15:05:36 +0200505 void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate, Fps renderRate) override;
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000506 void setSfPresent(nsecs_t sfPresentTime, const std::shared_ptr<FenceTime>& presentFence,
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000507 const std::shared_ptr<FenceTime>& gpuFence = FenceTime::NO_FENCE) override;
Ady Abraham14beed72024-05-15 17:16:45 -0700508 void onCommitNotComposited() override;
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700509 void parseArgs(const Vector<String16>& args, std::string& result) override;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700510 void setMaxDisplayFrames(uint32_t size) override;
Alec Mouriadebf5c2021-01-05 12:57:36 -0800511 float computeFps(const std::unordered_set<int32_t>& layerIds) override;
Ben Widawskyebdbead2024-10-24 11:47:50 -0700512 void generateFrameStats(int32_t layer, size_t count, FrameStats* outStats) const override;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700513 void reset() override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700514
Adithya Srinivasan01189672020-10-20 14:23:05 -0700515 // Sets up the perfetto tracing backend and data source.
516 void onBootFinished() override;
517 // Registers the data source with the perfetto backend. Called as part of onBootFinished()
518 // and should not be called manually outside of tests.
519 void registerDataSource();
520
521 static constexpr char kFrameTimelineDataSource[] = "android.surfaceflinger.frametimeline";
522
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700523private:
524 // Friend class for testing
525 friend class android::frametimeline::FrameTimelineTest;
526
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700527 void flushPendingPresentFences() REQUIRES(mMutex);
Ady Abrahamfcb16862022-10-10 14:35:21 -0700528 std::optional<size_t> getFirstSignalFenceIndex() const REQUIRES(mMutex);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700529 void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700530 void dumpAll(std::string& result);
531 void dumpJank(std::string& result);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700532
533 // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
534 std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
535 std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
536 mPendingPresentFences GUARDED_BY(mMutex);
537 std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
538 TokenManager mTokenManager;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000539 TraceCookieCounter mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800540 mutable std::mutex mMutex;
Ady Abraham57f8e182022-03-08 15:54:33 -0800541 const bool mUseBootTimeClock;
Ady Abraham43a68c32024-09-04 19:21:20 -0700542 const bool mFilterFramesBeforeTraceStarts;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700543 uint32_t mMaxDisplayFrames;
Alec Mouri9a29e672020-09-14 12:39:14 -0700544 std::shared_ptr<TimeStats> mTimeStats;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800545 const pid_t mSurfaceFlingerPid;
Sally Qi438eb7d2023-12-05 18:59:32 -0800546 nsecs_t mPreviousActualPresentTime = 0;
547 nsecs_t mPreviousPredictionPresentTime = 0;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800548 const JankClassificationThresholds mJankClassificationThresholds;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700549 static constexpr uint32_t kDefaultMaxDisplayFrames = 64;
Adithya Srinivasan01189672020-10-20 14:23:05 -0700550 // The initial container size for the vector<SurfaceFrames> inside display frame. Although
551 // this number doesn't represent any bounds on the number of surface frames that can go in a
552 // display frame, this is a good starting size for the vector so that we can avoid the
553 // internal vector resizing that happens with push_back.
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700554 static constexpr uint32_t kNumSurfaceFramesInitial = 10;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700555};
556
557} // namespace impl
558} // namespace android::frametimeline