blob: d08344ef6ea13dc3de37be60a62cc4b931dda8c3 [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>
Adithya Srinivasan01189672020-10-20 14:23:05 -070029#include <perfetto/trace/android/frame_timeline_event.pbzero.h>
30#include <perfetto/tracing.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070031#include <ui/FenceTime.h>
32#include <utils/RefBase.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070033#include <utils/String16.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070034#include <utils/Timers.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070035#include <utils/Vector.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070036
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080037#include <scheduler/Fps.h>
38
39#include "../TimeStats/TimeStats.h"
Adithya Srinivasanf279e042020-08-17 14:56:27 -070040
Alec Mouri9a29e672020-09-14 12:39:14 -070041namespace android::frametimeline {
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070042
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080043class FrameTimelineTest;
44
45using namespace std::chrono_literals;
46
47// Metadata indicating how the frame was presented w.r.t expected present time.
48enum class FramePresentMetadata : int8_t {
49 // Frame was presented on time
50 OnTimePresent,
51 // Frame was presented late
52 LatePresent,
53 // Frame was presented early
54 EarlyPresent,
55 // Unknown/initial state
56 UnknownPresent,
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070057};
58
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080059// Metadata comparing the frame's actual finish time to the expected deadline.
60enum class FrameReadyMetadata : int8_t {
61 // App/SF finished on time. Early finish is treated as on time since the goal of any component
62 // is to finish before the deadline.
63 OnTimeFinish,
64 // App/SF finished work later than expected
65 LateFinish,
66 // Unknown/initial state
67 UnknownFinish,
68};
69
70// Metadata comparing the frame's actual start time to the expected start time.
71enum class FrameStartMetadata : int8_t {
72 // App/SF started on time
73 OnTimeStart,
74 // App/SF started later than expected
75 LateStart,
76 // App/SF started earlier than expected
77 EarlyStart,
78 // Unknown/initial state
79 UnknownStart,
80};
Adithya Srinivasanf279e042020-08-17 14:56:27 -070081
82/*
83 * Collection of timestamps that can be used for both predictions and actual times.
84 */
85struct TimelineItem {
86 TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0,
87 const nsecs_t presentTime = 0)
88 : startTime(startTime), endTime(endTime), presentTime(presentTime) {}
89
90 nsecs_t startTime;
91 nsecs_t endTime;
92 nsecs_t presentTime;
Ady Abraham55fa7272020-09-30 19:19:27 -070093
94 bool operator==(const TimelineItem& other) const {
95 return startTime == other.startTime && endTime == other.endTime &&
96 presentTime == other.presentTime;
97 }
98
99 bool operator!=(const TimelineItem& other) const { return !(*this == other); }
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700100};
101
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800102struct JankClassificationThresholds {
103 // The various thresholds for App and SF. If the actual timestamp falls within the threshold
104 // compared to prediction, we treat it as on time.
105 nsecs_t presentThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
Adithya Srinivasan54996e22021-06-25 22:26:45 +0000106 nsecs_t deadlineThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(0ms).count();
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800107 nsecs_t startThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
108};
109
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700110/*
111 * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It
112 * saves these predictions for a short period of time and returns the predictions for a given token,
113 * if it hasn't expired.
114 */
115class TokenManager {
116public:
117 virtual ~TokenManager() = default;
118
119 // Generates a token for the given set of predictions. Stores the predictions for 120ms and
120 // destroys it later.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700121 virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800122
123 // Returns the stored predictions for a given token, if the predictions haven't expired.
124 virtual std::optional<TimelineItem> getPredictionsForToken(int64_t token) const = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700125};
126
127enum class PredictionState {
128 Valid, // Predictions obtained successfully from the TokenManager
129 Expired, // TokenManager no longer has the predictions
130 None, // Predictions are either not present or didn't come from TokenManager
131};
132
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000133/*
134 * Trace cookie is used to send start and end timestamps of <Surface/Display>Frames separately
135 * without needing to resend all the other information. We send all info to perfetto, along with a
136 * new cookie, in the start of a frame. For the corresponding end, we just send the same cookie.
137 * This helps in reducing the amount of data emitted by the producer.
138 */
139class TraceCookieCounter {
140public:
141 int64_t getCookieForTracing();
142
143private:
144 // Friend class for testing
145 friend class android::frametimeline::FrameTimelineTest;
146
147 std::atomic<int64_t> mTraceCookie = 0;
148};
149
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700150class SurfaceFrame {
151public:
152 enum class PresentState {
153 Presented, // Buffer was latched and presented by SurfaceFlinger
154 Dropped, // Buffer was dropped by SurfaceFlinger
155 Unknown, // Initial state, SurfaceFlinger hasn't seen this buffer yet
156 };
157
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800158 // Only FrameTimeline can construct a SurfaceFrame as it provides Predictions(through
159 // TokenManager), Thresholds and TimeStats pointer.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000160 SurfaceFrame(const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
Alec Mouriadebf5c2021-01-05 12:57:36 -0800161 int32_t layerId, std::string layerName, std::string debugName,
162 PredictionState predictionState, TimelineItem&& predictions,
163 std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000164 TraceCookieCounter* traceCookieCounter, bool isBuffer, int32_t gameMode);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800165 ~SurfaceFrame() = default;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700166
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800167 // Returns std::nullopt if the frame hasn't been classified yet.
168 // Used by both SF and FrameTimeline.
169 std::optional<int32_t> getJankType() const;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700170
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800171 // Functions called by SF
172 int64_t getToken() const { return mToken; };
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000173 int32_t getInputEventId() const { return mInputEventId; };
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800174 TimelineItem getPredictions() const { return mPredictions; };
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700175 // Actual timestamps of the app are set individually at different functions.
176 // Start time (if the app provides) and Queue time are accessible after queueing the frame,
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000177 // whereas Acquire Fence time is available only during latch. Drop time is available at the time
178 // the buffer was dropped.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800179 void setActualStartTime(nsecs_t actualStartTime);
180 void setActualQueueTime(nsecs_t actualQueueTime);
181 void setAcquireFenceTime(nsecs_t acquireFenceTime);
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000182 void setDropTime(nsecs_t dropTime);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800183 void setPresentState(PresentState presentState, nsecs_t lastLatchTime = 0);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800184 void setRenderRate(Fps renderRate);
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000185 void setGpuComposition();
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100186
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000187 // When a bufferless SurfaceFrame is promoted to a buffer SurfaceFrame, we also have to update
188 // isBuffer.
189 void promoteToBuffer();
190
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800191 // Functions called by FrameTimeline
192 // BaseTime is the smallest timestamp in this SurfaceFrame.
193 // Used for dumping all timestamps relative to the oldest, making it easy to read.
194 nsecs_t getBaseTime() const;
195 // Sets the actual present time, appropriate metadata and classifies the jank.
Alec Mouri363faf02021-01-29 16:34:55 -0800196 // displayRefreshRate, displayDeadlineDelta, and displayPresentDelta are propagated from the
197 // display frame.
198 void onPresent(nsecs_t presentTime, int32_t displayFrameJankType, Fps refreshRate,
199 nsecs_t displayDeadlineDelta, nsecs_t displayPresentDelta);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800200 // All the timestamps are dumped relative to the baseTime
201 void dump(std::string& result, const std::string& indent, nsecs_t baseTime) const;
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000202 // Dumps only the layer, token, is buffer, jank metadata, prediction and present states.
203 std::string miniDump() const;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800204 // Emits a packet for perfetto tracing. The function body will be executed only if tracing is
205 // enabled. The displayFrameToken is needed to link the SurfaceFrame to the corresponding
206 // DisplayFrame at the trace processor side.
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000207 void trace(int64_t displayFrameToken) const;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100208
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000209 // Getter functions used only by FrameTimelineTests and SurfaceFrame internally
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800210 TimelineItem getActuals() const;
211 pid_t getOwnerPid() const { return mOwnerPid; };
Alec Mouriadebf5c2021-01-05 12:57:36 -0800212 int32_t getLayerId() const { return mLayerId; };
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000213 PredictionState getPredictionState() const;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800214 PresentState getPresentState() const;
215 FrameReadyMetadata getFrameReadyMetadata() const;
216 FramePresentMetadata getFramePresentMetadata() const;
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000217 nsecs_t getDropTime() const;
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000218 bool getIsBuffer() const;
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000219
220 // For prediction expired frames, this delta is subtracted from the actual end time to get a
221 // start time decent enough to see in traces.
222 // TODO(b/172587309): Remove this when we have actual start times.
223 static constexpr nsecs_t kPredictionExpiredStartTimeDelta =
224 std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800225
226private:
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000227 void tracePredictions(int64_t displayFrameToken) const;
228 void traceActuals(int64_t displayFrameToken) const;
Adithya Srinivasan7c4ac7a2021-03-08 23:48:03 +0000229 void classifyJankLocked(int32_t displayFrameJankType, const Fps& refreshRate,
230 nsecs_t& deadlineDelta) REQUIRES(mMutex);
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000231
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800232 const int64_t mToken;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000233 const int32_t mInputEventId;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800234 const pid_t mOwnerPid;
235 const uid_t mOwnerUid;
236 const std::string mLayerName;
237 const std::string mDebugName;
Alec Mouriadebf5c2021-01-05 12:57:36 -0800238 const int32_t mLayerId;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800239 PresentState mPresentState GUARDED_BY(mMutex);
240 const PredictionState mPredictionState;
241 const TimelineItem mPredictions;
242 TimelineItem mActuals GUARDED_BY(mMutex);
243 std::shared_ptr<TimeStats> mTimeStats;
244 const JankClassificationThresholds mJankClassificationThresholds;
245 nsecs_t mActualQueueTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000246 nsecs_t mDropTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800247 mutable std::mutex mMutex;
248 // Bitmask for the type of jank
249 int32_t mJankType GUARDED_BY(mMutex) = JankType::None;
250 // Indicates if this frame was composited by the GPU or not
251 bool mGpuComposition GUARDED_BY(mMutex) = false;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800252 // Rendering rate for this frame.
253 std::optional<Fps> mRenderRate GUARDED_BY(mMutex);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800254 // Enum for the type of present
255 FramePresentMetadata mFramePresentMetadata GUARDED_BY(mMutex) =
256 FramePresentMetadata::UnknownPresent;
257 // Enum for the type of finish
258 FrameReadyMetadata mFrameReadyMetadata GUARDED_BY(mMutex) = FrameReadyMetadata::UnknownFinish;
259 // Time when the previous buffer from the same layer was latched by SF. This is used in checking
260 // for BufferStuffing where the current buffer is expected to be ready but the previous buffer
261 // was latched instead.
262 nsecs_t mLastLatchTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000263 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto. Using a
264 // reference here because the counter is owned by FrameTimeline, which outlives SurfaceFrame.
265 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000266 // Tells if the SurfaceFrame is representing a buffer or a transaction without a
267 // buffer(animations)
268 bool mIsBuffer;
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000269 // GameMode from the layer. Used in metrics.
270 int32_t mGameMode = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700271};
272
273/*
274 * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
275 * presented
276 */
277class FrameTimeline {
278public:
279 virtual ~FrameTimeline() = default;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700280 virtual TokenManager* getTokenManager() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700281
Adithya Srinivasan01189672020-10-20 14:23:05 -0700282 // Initializes the Perfetto DataSource that emits DisplayFrame and SurfaceFrame events. Test
283 // classes can avoid double registration by mocking this function.
284 virtual void onBootFinished() = 0;
285
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700286 // 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 -0700287 // Debug name is the human-readable debugging string for dumpsys.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000288 virtual std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
289 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000290 int32_t layerId, std::string layerName, std::string debugName, bool isBuffer,
291 int32_t gameMode) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700292
293 // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
294 // composited into one display frame.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800295 virtual void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700296
297 // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
298 // the token and sets the actualSfWakeTime for the current DisplayFrame.
Alec Mouri7d436ec2021-01-27 20:40:50 -0800299 virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700300
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000301 // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000302 // given present fence until it's signaled, and updates the present timestamps of all presented
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000303 // SurfaceFrames in that vsync. If a gpuFence was also provided, its tracked in the
304 // corresponding DisplayFrame.
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000305 virtual void setSfPresent(nsecs_t sfPresentTime, const std::shared_ptr<FenceTime>& presentFence,
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000306 const std::shared_ptr<FenceTime>& gpuFence) = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700307
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700308 // Args:
309 // -jank : Dumps only the Display Frames that are either janky themselves
310 // or contain janky Surface Frames.
311 // -all : Dumps the entire list of DisplayFrames and the SurfaceFrames contained within
312 virtual void parseArgs(const Vector<String16>& args, std::string& result) = 0;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700313
314 // Sets the max number of display frames that can be stored. Called by SF backdoor.
315 virtual void setMaxDisplayFrames(uint32_t size);
316
Alec Mouriadebf5c2021-01-05 12:57:36 -0800317 // Computes the historical fps for the provided set of layer IDs
318 // The fps is compted from the linear timeline of present timestamps for DisplayFrames
319 // containing at least one layer ID.
320 virtual float computeFps(const std::unordered_set<int32_t>& layerIds);
321
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700322 // Restores the max number of display frames to default. Called by SF backdoor.
323 virtual void reset() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700324};
325
326namespace impl {
327
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700328class TokenManager : public android::frametimeline::TokenManager {
329public:
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000330 TokenManager() : mCurrentToken(FrameTimelineInfo::INVALID_VSYNC_ID + 1) {}
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700331 ~TokenManager() = default;
332
333 int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800334 std::optional<TimelineItem> getPredictionsForToken(int64_t token) const override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700335
336private:
337 // Friend class for testing
338 friend class android::frametimeline::FrameTimelineTest;
339
340 void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
341
Adithya Srinivasanbed4c4f2021-05-03 20:24:46 +0000342 std::map<int64_t, TimelineItem> mPredictions GUARDED_BY(mMutex);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700343 int64_t mCurrentToken GUARDED_BY(mMutex);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800344 mutable std::mutex mMutex;
Adithya Srinivasanbed4c4f2021-05-03 20:24:46 +0000345 static constexpr size_t kMaxTokens = 500;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700346};
347
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700348class FrameTimeline : public android::frametimeline::FrameTimeline {
349public:
Adithya Srinivasan01189672020-10-20 14:23:05 -0700350 class FrameTimelineDataSource : public perfetto::DataSource<FrameTimelineDataSource> {
351 void OnSetup(const SetupArgs&) override{};
352 void OnStart(const StartArgs&) override{};
353 void OnStop(const StopArgs&) override{};
354 };
355
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800356 /*
357 * DisplayFrame should be used only internally within FrameTimeline. All members and methods are
358 * guarded by FrameTimeline's mMutex.
359 */
360 class DisplayFrame {
361 public:
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000362 DisplayFrame(std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds,
Adithya Srinivasan82eef322021-04-10 00:06:04 +0000363 TraceCookieCounter* traceCookieCounter);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800364 virtual ~DisplayFrame() = default;
365 // Dumpsys interface - dumps only if the DisplayFrame itself is janky or is at least one
366 // SurfaceFrame is janky.
367 void dumpJank(std::string& result, nsecs_t baseTime, int displayFrameCount) const;
368 // Dumpsys interface - dumps all data irrespective of jank
369 void dumpAll(std::string& result, nsecs_t baseTime) const;
370 // Emits a packet for perfetto tracing. The function body will be executed only if tracing
371 // is enabled.
372 void trace(pid_t surfaceFlingerPid) const;
373 // Sets the token, vsyncPeriod, predictions and SF start time.
Alec Mouri7d436ec2021-01-27 20:40:50 -0800374 void onSfWakeUp(int64_t token, Fps refreshRate, std::optional<TimelineItem> predictions,
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800375 nsecs_t wakeUpTime);
Adithya Srinivasan115ac692021-03-06 01:21:30 +0000376 // Sets the appropriate metadata and classifies the jank.
Adithya Srinivasan57dc81d2021-04-14 17:31:41 +0000377 void onPresent(nsecs_t signalTime, nsecs_t previousPresentTime);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800378 // Adds the provided SurfaceFrame to the current display frame.
379 void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame);
380
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800381 void setPredictions(PredictionState predictionState, TimelineItem predictions);
382 void setActualStartTime(nsecs_t actualStartTime);
383 void setActualEndTime(nsecs_t actualEndTime);
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000384 void setGpuFence(const std::shared_ptr<FenceTime>& gpuFence);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800385
386 // BaseTime is the smallest timestamp in a DisplayFrame.
387 // Used for dumping all timestamps relative to the oldest, making it easy to read.
388 nsecs_t getBaseTime() const;
389
390 // Functions to be used only in testing.
391 TimelineItem getActuals() const { return mSurfaceFlingerActuals; };
392 TimelineItem getPredictions() const { return mSurfaceFlingerPredictions; };
Adithya Srinivasan939cd4d2021-02-23 06:18:13 +0000393 FrameStartMetadata getFrameStartMetadata() const { return mFrameStartMetadata; };
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800394 FramePresentMetadata getFramePresentMetadata() const { return mFramePresentMetadata; };
395 FrameReadyMetadata getFrameReadyMetadata() const { return mFrameReadyMetadata; };
396 int32_t getJankType() const { return mJankType; }
397 const std::vector<std::shared_ptr<SurfaceFrame>>& getSurfaceFrames() const {
398 return mSurfaceFrames;
399 }
400
401 private:
402 void dump(std::string& result, nsecs_t baseTime) const;
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000403 void tracePredictions(pid_t surfaceFlingerPid) const;
404 void traceActuals(pid_t surfaceFlingerPid) const;
Adithya Srinivasan57dc81d2021-04-14 17:31:41 +0000405 void classifyJank(nsecs_t& deadlineDelta, nsecs_t& deltaToVsync,
406 nsecs_t previousPresentTime);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800407
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000408 int64_t mToken = FrameTimelineInfo::INVALID_VSYNC_ID;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800409
410 /* Usage of TimelineItem w.r.t SurfaceFlinger
411 * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates
412 * endTime Time when SurfaceFlinger sends a composited frame to Display
413 * presentTime Time when the composited frame was presented on screen
414 */
415 TimelineItem mSurfaceFlingerPredictions;
416 TimelineItem mSurfaceFlingerActuals;
417 std::shared_ptr<TimeStats> mTimeStats;
418 const JankClassificationThresholds mJankClassificationThresholds;
419
420 // Collection of predictions and actual values sent over by Layers
421 std::vector<std::shared_ptr<SurfaceFrame>> mSurfaceFrames;
422
423 PredictionState mPredictionState = PredictionState::None;
424 // Bitmask for the type of jank
425 int32_t mJankType = JankType::None;
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000426 // A valid gpu fence indicates that the DisplayFrame was composited by the GPU
427 std::shared_ptr<FenceTime> mGpuFence = FenceTime::NO_FENCE;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800428 // Enum for the type of present
429 FramePresentMetadata mFramePresentMetadata = FramePresentMetadata::UnknownPresent;
430 // Enum for the type of finish
431 FrameReadyMetadata mFrameReadyMetadata = FrameReadyMetadata::UnknownFinish;
432 // Enum for the type of start
433 FrameStartMetadata mFrameStartMetadata = FrameStartMetadata::UnknownStart;
434 // The refresh rate (vsync period) in nanoseconds as seen by SF during this DisplayFrame's
435 // timeline
Alec Mouri7d436ec2021-01-27 20:40:50 -0800436 Fps mRefreshRate;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000437 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto.
438 // Using a reference here because the counter is owned by FrameTimeline, which outlives
439 // DisplayFrame.
440 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800441 };
442
443 FrameTimeline(std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid,
Adithya Srinivasan82eef322021-04-10 00:06:04 +0000444 JankClassificationThresholds thresholds = {});
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700445 ~FrameTimeline() = default;
446
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700447 frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; }
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000448 std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
449 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000450 int32_t layerId, std::string layerName, std::string debugName, bool isBuffer,
451 int32_t gameMode) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800452 void addSurfaceFrame(std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame) override;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800453 void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate) override;
Adithya Srinivasanb6a2fa12021-03-13 00:23:09 +0000454 void setSfPresent(nsecs_t sfPresentTime, const std::shared_ptr<FenceTime>& presentFence,
Adithya Srinivasan36b01af2021-04-07 22:29:47 +0000455 const std::shared_ptr<FenceTime>& gpuFence = FenceTime::NO_FENCE) override;
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700456 void parseArgs(const Vector<String16>& args, std::string& result) override;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700457 void setMaxDisplayFrames(uint32_t size) override;
Alec Mouriadebf5c2021-01-05 12:57:36 -0800458 float computeFps(const std::unordered_set<int32_t>& layerIds) override;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700459 void reset() override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700460
Adithya Srinivasan01189672020-10-20 14:23:05 -0700461 // Sets up the perfetto tracing backend and data source.
462 void onBootFinished() override;
463 // Registers the data source with the perfetto backend. Called as part of onBootFinished()
464 // and should not be called manually outside of tests.
465 void registerDataSource();
466
467 static constexpr char kFrameTimelineDataSource[] = "android.surfaceflinger.frametimeline";
468
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700469private:
470 // Friend class for testing
471 friend class android::frametimeline::FrameTimelineTest;
472
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700473 void flushPendingPresentFences() REQUIRES(mMutex);
474 void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700475 void dumpAll(std::string& result);
476 void dumpJank(std::string& result);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700477
478 // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
479 std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
480 std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
481 mPendingPresentFences GUARDED_BY(mMutex);
482 std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
483 TokenManager mTokenManager;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000484 TraceCookieCounter mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800485 mutable std::mutex mMutex;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700486 uint32_t mMaxDisplayFrames;
Alec Mouri9a29e672020-09-14 12:39:14 -0700487 std::shared_ptr<TimeStats> mTimeStats;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800488 const pid_t mSurfaceFlingerPid;
Adithya Srinivasan57dc81d2021-04-14 17:31:41 +0000489 nsecs_t mPreviousPresentTime = 0;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800490 const JankClassificationThresholds mJankClassificationThresholds;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700491 static constexpr uint32_t kDefaultMaxDisplayFrames = 64;
Adithya Srinivasan01189672020-10-20 14:23:05 -0700492 // The initial container size for the vector<SurfaceFrames> inside display frame. Although
493 // this number doesn't represent any bounds on the number of surface frames that can go in a
494 // display frame, this is a good starting size for the vector so that we can avoid the
495 // internal vector resizing that happens with push_back.
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700496 static constexpr uint32_t kNumSurfaceFramesInitial = 10;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700497};
498
499} // namespace impl
500} // namespace android::frametimeline