blob: 3cf35f0132849814e08ce8d165b92d72910b704d [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 Mouri7d436ec2021-01-27 20:40:50 -080019#include <../Fps.h>
Alec Mouri9a29e672020-09-14 12:39:14 -070020#include <../TimeStats/TimeStats.h>
Ady Abraham22c7b5c2020-09-22 19:33:40 -070021#include <gui/ISurfaceComposer.h>
Jorim Jaggi5814ab82020-12-03 20:45:58 +010022#include <gui/JankInfo.h>
Adithya Srinivasan01189672020-10-20 14:23:05 -070023#include <perfetto/trace/android/frame_timeline_event.pbzero.h>
24#include <perfetto/tracing.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070025#include <ui/FenceTime.h>
26#include <utils/RefBase.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070027#include <utils/String16.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070028#include <utils/Timers.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070029#include <utils/Vector.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070030
Alec Mouri9a29e672020-09-14 12:39:14 -070031#include <deque>
32#include <mutex>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070033
Alec Mouri9a29e672020-09-14 12:39:14 -070034namespace android::frametimeline {
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070035
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080036class FrameTimelineTest;
37
38using namespace std::chrono_literals;
39
40// Metadata indicating how the frame was presented w.r.t expected present time.
41enum class FramePresentMetadata : int8_t {
42 // Frame was presented on time
43 OnTimePresent,
44 // Frame was presented late
45 LatePresent,
46 // Frame was presented early
47 EarlyPresent,
48 // Unknown/initial state
49 UnknownPresent,
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070050};
51
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080052// Metadata comparing the frame's actual finish time to the expected deadline.
53enum class FrameReadyMetadata : int8_t {
54 // App/SF finished on time. Early finish is treated as on time since the goal of any component
55 // is to finish before the deadline.
56 OnTimeFinish,
57 // App/SF finished work later than expected
58 LateFinish,
59 // Unknown/initial state
60 UnknownFinish,
61};
62
63// Metadata comparing the frame's actual start time to the expected start time.
64enum class FrameStartMetadata : int8_t {
65 // App/SF started on time
66 OnTimeStart,
67 // App/SF started later than expected
68 LateStart,
69 // App/SF started earlier than expected
70 EarlyStart,
71 // Unknown/initial state
72 UnknownStart,
73};
Adithya Srinivasanf279e042020-08-17 14:56:27 -070074
75/*
76 * Collection of timestamps that can be used for both predictions and actual times.
77 */
78struct TimelineItem {
79 TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0,
80 const nsecs_t presentTime = 0)
81 : startTime(startTime), endTime(endTime), presentTime(presentTime) {}
82
83 nsecs_t startTime;
84 nsecs_t endTime;
85 nsecs_t presentTime;
Ady Abraham55fa7272020-09-30 19:19:27 -070086
87 bool operator==(const TimelineItem& other) const {
88 return startTime == other.startTime && endTime == other.endTime &&
89 presentTime == other.presentTime;
90 }
91
92 bool operator!=(const TimelineItem& other) const { return !(*this == other); }
Adithya Srinivasanf279e042020-08-17 14:56:27 -070093};
94
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080095struct TokenManagerPrediction {
96 nsecs_t timestamp = 0;
97 TimelineItem predictions;
98};
99
100struct JankClassificationThresholds {
101 // The various thresholds for App and SF. If the actual timestamp falls within the threshold
102 // compared to prediction, we treat it as on time.
103 nsecs_t presentThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
104 nsecs_t deadlineThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
105 nsecs_t startThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
106};
107
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700108/*
109 * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It
110 * saves these predictions for a short period of time and returns the predictions for a given token,
111 * if it hasn't expired.
112 */
113class TokenManager {
114public:
115 virtual ~TokenManager() = default;
116
117 // Generates a token for the given set of predictions. Stores the predictions for 120ms and
118 // destroys it later.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700119 virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800120
121 // Returns the stored predictions for a given token, if the predictions haven't expired.
122 virtual std::optional<TimelineItem> getPredictionsForToken(int64_t token) const = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700123};
124
125enum class PredictionState {
126 Valid, // Predictions obtained successfully from the TokenManager
127 Expired, // TokenManager no longer has the predictions
128 None, // Predictions are either not present or didn't come from TokenManager
129};
130
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000131/*
132 * Trace cookie is used to send start and end timestamps of <Surface/Display>Frames separately
133 * without needing to resend all the other information. We send all info to perfetto, along with a
134 * new cookie, in the start of a frame. For the corresponding end, we just send the same cookie.
135 * This helps in reducing the amount of data emitted by the producer.
136 */
137class TraceCookieCounter {
138public:
139 int64_t getCookieForTracing();
140
141private:
142 // Friend class for testing
143 friend class android::frametimeline::FrameTimelineTest;
144
145 std::atomic<int64_t> mTraceCookie = 0;
146};
147
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700148class SurfaceFrame {
149public:
150 enum class PresentState {
151 Presented, // Buffer was latched and presented by SurfaceFlinger
152 Dropped, // Buffer was dropped by SurfaceFlinger
153 Unknown, // Initial state, SurfaceFlinger hasn't seen this buffer yet
154 };
155
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800156 // Only FrameTimeline can construct a SurfaceFrame as it provides Predictions(through
157 // TokenManager), Thresholds and TimeStats pointer.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000158 SurfaceFrame(const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
Alec Mouriadebf5c2021-01-05 12:57:36 -0800159 int32_t layerId, std::string layerName, std::string debugName,
160 PredictionState predictionState, TimelineItem&& predictions,
161 std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds,
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000162 TraceCookieCounter* traceCookieCounter, bool isBuffer);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800163 ~SurfaceFrame() = default;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700164
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800165 // Returns std::nullopt if the frame hasn't been classified yet.
166 // Used by both SF and FrameTimeline.
167 std::optional<int32_t> getJankType() const;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700168
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800169 // Functions called by SF
170 int64_t getToken() const { return mToken; };
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000171 int32_t getInputEventId() const { return mInputEventId; };
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800172 TimelineItem getPredictions() const { return mPredictions; };
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700173 // Actual timestamps of the app are set individually at different functions.
174 // Start time (if the app provides) and Queue time are accessible after queueing the frame,
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000175 // whereas Acquire Fence time is available only during latch. Drop time is available at the time
176 // the buffer was dropped.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800177 void setActualStartTime(nsecs_t actualStartTime);
178 void setActualQueueTime(nsecs_t actualQueueTime);
179 void setAcquireFenceTime(nsecs_t acquireFenceTime);
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000180 void setDropTime(nsecs_t dropTime);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800181 void setPresentState(PresentState presentState, nsecs_t lastLatchTime = 0);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800182 void setRenderRate(Fps renderRate);
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100183
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000184 // When a bufferless SurfaceFrame is promoted to a buffer SurfaceFrame, we also have to update
185 // isBuffer.
186 void promoteToBuffer();
187
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800188 // Functions called by FrameTimeline
189 // BaseTime is the smallest timestamp in this SurfaceFrame.
190 // Used for dumping all timestamps relative to the oldest, making it easy to read.
191 nsecs_t getBaseTime() const;
192 // Sets the actual present time, appropriate metadata and classifies the jank.
Alec Mouri363faf02021-01-29 16:34:55 -0800193 // displayRefreshRate, displayDeadlineDelta, and displayPresentDelta are propagated from the
194 // display frame.
195 void onPresent(nsecs_t presentTime, int32_t displayFrameJankType, Fps refreshRate,
196 nsecs_t displayDeadlineDelta, nsecs_t displayPresentDelta);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800197 // All the timestamps are dumped relative to the baseTime
198 void dump(std::string& result, const std::string& indent, nsecs_t baseTime) const;
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000199 // Dumps only the layer, token, is buffer, jank metadata, prediction and present states.
200 std::string miniDump() const;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800201 // Emits a packet for perfetto tracing. The function body will be executed only if tracing is
202 // enabled. The displayFrameToken is needed to link the SurfaceFrame to the corresponding
203 // DisplayFrame at the trace processor side.
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000204 void trace(int64_t displayFrameToken) const;
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100205
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000206 // Getter functions used only by FrameTimelineTests and SurfaceFrame internally
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800207 TimelineItem getActuals() const;
208 pid_t getOwnerPid() const { return mOwnerPid; };
Alec Mouriadebf5c2021-01-05 12:57:36 -0800209 int32_t getLayerId() const { return mLayerId; };
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000210 PredictionState getPredictionState() const;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800211 PresentState getPresentState() const;
212 FrameReadyMetadata getFrameReadyMetadata() const;
213 FramePresentMetadata getFramePresentMetadata() const;
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000214 nsecs_t getDropTime() const;
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000215 bool getIsBuffer() const;
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000216
217 // For prediction expired frames, this delta is subtracted from the actual end time to get a
218 // start time decent enough to see in traces.
219 // TODO(b/172587309): Remove this when we have actual start times.
220 static constexpr nsecs_t kPredictionExpiredStartTimeDelta =
221 std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800222
223private:
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000224 void tracePredictions(int64_t displayFrameToken) const;
225 void traceActuals(int64_t displayFrameToken) const;
Adithya Srinivasan7c4ac7a2021-03-08 23:48:03 +0000226 void classifyJankLocked(int32_t displayFrameJankType, const Fps& refreshRate,
227 nsecs_t& deadlineDelta) REQUIRES(mMutex);
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000228
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800229 const int64_t mToken;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000230 const int32_t mInputEventId;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800231 const pid_t mOwnerPid;
232 const uid_t mOwnerUid;
233 const std::string mLayerName;
234 const std::string mDebugName;
Alec Mouriadebf5c2021-01-05 12:57:36 -0800235 const int32_t mLayerId;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800236 PresentState mPresentState GUARDED_BY(mMutex);
237 const PredictionState mPredictionState;
238 const TimelineItem mPredictions;
239 TimelineItem mActuals GUARDED_BY(mMutex);
240 std::shared_ptr<TimeStats> mTimeStats;
241 const JankClassificationThresholds mJankClassificationThresholds;
242 nsecs_t mActualQueueTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000243 nsecs_t mDropTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800244 mutable std::mutex mMutex;
245 // Bitmask for the type of jank
246 int32_t mJankType GUARDED_BY(mMutex) = JankType::None;
247 // Indicates if this frame was composited by the GPU or not
248 bool mGpuComposition GUARDED_BY(mMutex) = false;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800249 // Rendering rate for this frame.
250 std::optional<Fps> mRenderRate GUARDED_BY(mMutex);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800251 // Enum for the type of present
252 FramePresentMetadata mFramePresentMetadata GUARDED_BY(mMutex) =
253 FramePresentMetadata::UnknownPresent;
254 // Enum for the type of finish
255 FrameReadyMetadata mFrameReadyMetadata GUARDED_BY(mMutex) = FrameReadyMetadata::UnknownFinish;
256 // Time when the previous buffer from the same layer was latched by SF. This is used in checking
257 // for BufferStuffing where the current buffer is expected to be ready but the previous buffer
258 // was latched instead.
259 nsecs_t mLastLatchTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000260 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto. Using a
261 // reference here because the counter is owned by FrameTimeline, which outlives SurfaceFrame.
262 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000263 // Tells if the SurfaceFrame is representing a buffer or a transaction without a
264 // buffer(animations)
265 bool mIsBuffer;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700266};
267
268/*
269 * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
270 * presented
271 */
272class FrameTimeline {
273public:
274 virtual ~FrameTimeline() = default;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700275 virtual TokenManager* getTokenManager() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700276
Adithya Srinivasan01189672020-10-20 14:23:05 -0700277 // Initializes the Perfetto DataSource that emits DisplayFrame and SurfaceFrame events. Test
278 // classes can avoid double registration by mocking this function.
279 virtual void onBootFinished() = 0;
280
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700281 // 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 -0700282 // Debug name is the human-readable debugging string for dumpsys.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000283 virtual std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
284 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000285 int32_t layerId, std::string layerName, std::string debugName, bool isBuffer) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700286
287 // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
288 // composited into one display frame.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800289 virtual void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700290
291 // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
292 // the token and sets the actualSfWakeTime for the current DisplayFrame.
Alec Mouri7d436ec2021-01-27 20:40:50 -0800293 virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700294
295 // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the given present fence
296 // until it's signaled, and updates the present timestamps of all presented SurfaceFrames in
297 // that vsync.
298 virtual void setSfPresent(nsecs_t sfPresentTime,
299 const std::shared_ptr<FenceTime>& presentFence) = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700300
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700301 // Args:
302 // -jank : Dumps only the Display Frames that are either janky themselves
303 // or contain janky Surface Frames.
304 // -all : Dumps the entire list of DisplayFrames and the SurfaceFrames contained within
305 virtual void parseArgs(const Vector<String16>& args, std::string& result) = 0;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700306
307 // Sets the max number of display frames that can be stored. Called by SF backdoor.
308 virtual void setMaxDisplayFrames(uint32_t size);
309
Alec Mouriadebf5c2021-01-05 12:57:36 -0800310 // Computes the historical fps for the provided set of layer IDs
311 // The fps is compted from the linear timeline of present timestamps for DisplayFrames
312 // containing at least one layer ID.
313 virtual float computeFps(const std::unordered_set<int32_t>& layerIds);
314
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700315 // Restores the max number of display frames to default. Called by SF backdoor.
316 virtual void reset() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700317};
318
319namespace impl {
320
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700321class TokenManager : public android::frametimeline::TokenManager {
322public:
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000323 TokenManager() : mCurrentToken(FrameTimelineInfo::INVALID_VSYNC_ID + 1) {}
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700324 ~TokenManager() = default;
325
326 int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800327 std::optional<TimelineItem> getPredictionsForToken(int64_t token) const override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700328
329private:
330 // Friend class for testing
331 friend class android::frametimeline::FrameTimelineTest;
332
333 void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
334
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800335 std::map<int64_t, TokenManagerPrediction> mPredictions GUARDED_BY(mMutex);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700336 int64_t mCurrentToken GUARDED_BY(mMutex);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800337 mutable std::mutex mMutex;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700338 static constexpr nsecs_t kMaxRetentionTime =
339 std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count();
340};
341
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700342class FrameTimeline : public android::frametimeline::FrameTimeline {
343public:
Adithya Srinivasan01189672020-10-20 14:23:05 -0700344 class FrameTimelineDataSource : public perfetto::DataSource<FrameTimelineDataSource> {
345 void OnSetup(const SetupArgs&) override{};
346 void OnStart(const StartArgs&) override{};
347 void OnStop(const StopArgs&) override{};
348 };
349
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800350 /*
351 * DisplayFrame should be used only internally within FrameTimeline. All members and methods are
352 * guarded by FrameTimeline's mMutex.
353 */
354 class DisplayFrame {
355 public:
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000356 DisplayFrame(std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds,
Adithya Srinivasan939cd4d2021-02-23 06:18:13 +0000357 nsecs_t hwcDuration, TraceCookieCounter* traceCookieCounter);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800358 virtual ~DisplayFrame() = default;
359 // Dumpsys interface - dumps only if the DisplayFrame itself is janky or is at least one
360 // SurfaceFrame is janky.
361 void dumpJank(std::string& result, nsecs_t baseTime, int displayFrameCount) const;
362 // Dumpsys interface - dumps all data irrespective of jank
363 void dumpAll(std::string& result, nsecs_t baseTime) const;
364 // Emits a packet for perfetto tracing. The function body will be executed only if tracing
365 // is enabled.
366 void trace(pid_t surfaceFlingerPid) const;
367 // Sets the token, vsyncPeriod, predictions and SF start time.
Alec Mouri7d436ec2021-01-27 20:40:50 -0800368 void onSfWakeUp(int64_t token, Fps refreshRate, std::optional<TimelineItem> predictions,
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800369 nsecs_t wakeUpTime);
Adithya Srinivasan115ac692021-03-06 01:21:30 +0000370 // Sets the appropriate metadata and classifies the jank.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800371 void onPresent(nsecs_t signalTime);
372 // Adds the provided SurfaceFrame to the current display frame.
373 void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame);
374
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800375 void setPredictions(PredictionState predictionState, TimelineItem predictions);
376 void setActualStartTime(nsecs_t actualStartTime);
377 void setActualEndTime(nsecs_t actualEndTime);
378
379 // BaseTime is the smallest timestamp in a DisplayFrame.
380 // Used for dumping all timestamps relative to the oldest, making it easy to read.
381 nsecs_t getBaseTime() const;
382
383 // Functions to be used only in testing.
384 TimelineItem getActuals() const { return mSurfaceFlingerActuals; };
385 TimelineItem getPredictions() const { return mSurfaceFlingerPredictions; };
Adithya Srinivasan939cd4d2021-02-23 06:18:13 +0000386 FrameStartMetadata getFrameStartMetadata() const { return mFrameStartMetadata; };
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800387 FramePresentMetadata getFramePresentMetadata() const { return mFramePresentMetadata; };
388 FrameReadyMetadata getFrameReadyMetadata() const { return mFrameReadyMetadata; };
389 int32_t getJankType() const { return mJankType; }
390 const std::vector<std::shared_ptr<SurfaceFrame>>& getSurfaceFrames() const {
391 return mSurfaceFrames;
392 }
393
394 private:
395 void dump(std::string& result, nsecs_t baseTime) const;
Adithya Srinivasan061c14c2021-02-11 01:19:47 +0000396 void tracePredictions(pid_t surfaceFlingerPid) const;
397 void traceActuals(pid_t surfaceFlingerPid) const;
Adithya Srinivasan115ac692021-03-06 01:21:30 +0000398 void classifyJank(nsecs_t& deadlineDelta, nsecs_t& deltaToVsync);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800399
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000400 int64_t mToken = FrameTimelineInfo::INVALID_VSYNC_ID;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800401
402 /* Usage of TimelineItem w.r.t SurfaceFlinger
403 * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates
404 * endTime Time when SurfaceFlinger sends a composited frame to Display
405 * presentTime Time when the composited frame was presented on screen
406 */
407 TimelineItem mSurfaceFlingerPredictions;
408 TimelineItem mSurfaceFlingerActuals;
409 std::shared_ptr<TimeStats> mTimeStats;
410 const JankClassificationThresholds mJankClassificationThresholds;
Adithya Srinivasan939cd4d2021-02-23 06:18:13 +0000411 const nsecs_t mHwcDuration;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800412
413 // Collection of predictions and actual values sent over by Layers
414 std::vector<std::shared_ptr<SurfaceFrame>> mSurfaceFrames;
415
416 PredictionState mPredictionState = PredictionState::None;
417 // Bitmask for the type of jank
418 int32_t mJankType = JankType::None;
419 // Indicates if this frame was composited by the GPU or not
420 bool mGpuComposition = false;
421 // Enum for the type of present
422 FramePresentMetadata mFramePresentMetadata = FramePresentMetadata::UnknownPresent;
423 // Enum for the type of finish
424 FrameReadyMetadata mFrameReadyMetadata = FrameReadyMetadata::UnknownFinish;
425 // Enum for the type of start
426 FrameStartMetadata mFrameStartMetadata = FrameStartMetadata::UnknownStart;
427 // The refresh rate (vsync period) in nanoseconds as seen by SF during this DisplayFrame's
428 // timeline
Alec Mouri7d436ec2021-01-27 20:40:50 -0800429 Fps mRefreshRate;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000430 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto.
431 // Using a reference here because the counter is owned by FrameTimeline, which outlives
432 // DisplayFrame.
433 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800434 };
435
436 FrameTimeline(std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid,
Adithya Srinivasan939cd4d2021-02-23 06:18:13 +0000437 JankClassificationThresholds thresholds = {},
438 nsecs_t hwcDuration = kDefaultHwcDuration);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700439 ~FrameTimeline() = default;
440
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700441 frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; }
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000442 std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
443 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +0000444 int32_t layerId, std::string layerName, std::string debugName, bool isBuffer) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800445 void addSurfaceFrame(std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame) override;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800446 void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate) override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700447 void setSfPresent(nsecs_t sfPresentTime,
448 const std::shared_ptr<FenceTime>& presentFence) override;
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700449 void parseArgs(const Vector<String16>& args, std::string& result) override;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700450 void setMaxDisplayFrames(uint32_t size) override;
Alec Mouriadebf5c2021-01-05 12:57:36 -0800451 float computeFps(const std::unordered_set<int32_t>& layerIds) override;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700452 void reset() override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700453
Adithya Srinivasan01189672020-10-20 14:23:05 -0700454 // Sets up the perfetto tracing backend and data source.
455 void onBootFinished() override;
456 // Registers the data source with the perfetto backend. Called as part of onBootFinished()
457 // and should not be called manually outside of tests.
458 void registerDataSource();
459
460 static constexpr char kFrameTimelineDataSource[] = "android.surfaceflinger.frametimeline";
461
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700462private:
463 // Friend class for testing
464 friend class android::frametimeline::FrameTimelineTest;
465
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700466 void flushPendingPresentFences() REQUIRES(mMutex);
467 void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700468 void dumpAll(std::string& result);
469 void dumpJank(std::string& result);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700470
471 // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
472 std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
473 std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
474 mPendingPresentFences GUARDED_BY(mMutex);
475 std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
476 TokenManager mTokenManager;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000477 TraceCookieCounter mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800478 mutable std::mutex mMutex;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700479 uint32_t mMaxDisplayFrames;
Alec Mouri9a29e672020-09-14 12:39:14 -0700480 std::shared_ptr<TimeStats> mTimeStats;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800481 const pid_t mSurfaceFlingerPid;
482 const JankClassificationThresholds mJankClassificationThresholds;
Adithya Srinivasan939cd4d2021-02-23 06:18:13 +0000483 // In SF Predictions, both end & present are the same. The predictions consider the time used by
484 // composer as well, but we have no way to estimate how much time the composer needs. We are
485 // assuming an arbitrary time for the composer work.
486 const nsecs_t mHwcDuration;
487 static constexpr nsecs_t kDefaultHwcDuration = std::chrono::nanoseconds(3ms).count();
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700488 static constexpr uint32_t kDefaultMaxDisplayFrames = 64;
Adithya Srinivasan01189672020-10-20 14:23:05 -0700489 // The initial container size for the vector<SurfaceFrames> inside display frame. Although
490 // this number doesn't represent any bounds on the number of surface frames that can go in a
491 // display frame, this is a good starting size for the vector so that we can avoid the
492 // internal vector resizing that happens with push_back.
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700493 static constexpr uint32_t kNumSurfaceFramesInitial = 10;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700494};
495
496} // namespace impl
497} // namespace android::frametimeline