Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 1 | /* |
| 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 Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 19 | #include <../Fps.h> |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 20 | #include <../TimeStats/TimeStats.h> |
Ady Abraham | 22c7b5c | 2020-09-22 19:33:40 -0700 | [diff] [blame] | 21 | #include <gui/ISurfaceComposer.h> |
Jorim Jaggi | 5814ab8 | 2020-12-03 20:45:58 +0100 | [diff] [blame] | 22 | #include <gui/JankInfo.h> |
Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 23 | #include <perfetto/trace/android/frame_timeline_event.pbzero.h> |
| 24 | #include <perfetto/tracing.h> |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 25 | #include <ui/FenceTime.h> |
| 26 | #include <utils/RefBase.h> |
Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 27 | #include <utils/String16.h> |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 28 | #include <utils/Timers.h> |
Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 29 | #include <utils/Vector.h> |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 30 | |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 31 | #include <deque> |
| 32 | #include <mutex> |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 33 | |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 34 | namespace android::frametimeline { |
Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 35 | |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 36 | class FrameTimelineTest; |
| 37 | |
| 38 | using namespace std::chrono_literals; |
| 39 | |
| 40 | // Metadata indicating how the frame was presented w.r.t expected present time. |
| 41 | enum 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 Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 52 | // Metadata comparing the frame's actual finish time to the expected deadline. |
| 53 | enum 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. |
| 64 | enum 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 Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Collection of timestamps that can be used for both predictions and actual times. |
| 77 | */ |
| 78 | struct 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 Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 86 | |
| 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 Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 95 | struct TokenManagerPrediction { |
| 96 | nsecs_t timestamp = 0; |
| 97 | TimelineItem predictions; |
| 98 | }; |
| 99 | |
| 100 | struct 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 Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 108 | /* |
| 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 | */ |
| 113 | class TokenManager { |
| 114 | public: |
| 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 Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 119 | virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 120 | |
| 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 Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | enum 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 Srinivasan | 05bd2d1 | 2021-01-11 18:49:58 +0000 | [diff] [blame] | 131 | /* |
| 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 | */ |
| 137 | class TraceCookieCounter { |
| 138 | public: |
| 139 | int64_t getCookieForTracing(); |
| 140 | |
| 141 | private: |
| 142 | // Friend class for testing |
| 143 | friend class android::frametimeline::FrameTimelineTest; |
| 144 | |
| 145 | std::atomic<int64_t> mTraceCookie = 0; |
| 146 | }; |
| 147 | |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 148 | class SurfaceFrame { |
| 149 | public: |
| 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 Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 156 | // Only FrameTimeline can construct a SurfaceFrame as it provides Predictions(through |
| 157 | // TokenManager), Thresholds and TimeStats pointer. |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 158 | SurfaceFrame(const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid, |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 159 | int32_t layerId, std::string layerName, std::string debugName, |
| 160 | PredictionState predictionState, TimelineItem&& predictions, |
| 161 | std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds, |
Adithya Srinivasan | 785addd | 2021-03-09 00:38:00 +0000 | [diff] [blame] | 162 | TraceCookieCounter* traceCookieCounter, bool isBuffer); |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 163 | ~SurfaceFrame() = default; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 164 | |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 165 | // 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 Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 168 | |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 169 | // Functions called by SF |
| 170 | int64_t getToken() const { return mToken; }; |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 171 | int32_t getInputEventId() const { return mInputEventId; }; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 172 | TimelineItem getPredictions() const { return mPredictions; }; |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 173 | // 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 Srinivasan | 061c14c | 2021-02-11 01:19:47 +0000 | [diff] [blame] | 175 | // whereas Acquire Fence time is available only during latch. Drop time is available at the time |
| 176 | // the buffer was dropped. |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 177 | void setActualStartTime(nsecs_t actualStartTime); |
| 178 | void setActualQueueTime(nsecs_t actualQueueTime); |
| 179 | void setAcquireFenceTime(nsecs_t acquireFenceTime); |
Adithya Srinivasan | 061c14c | 2021-02-11 01:19:47 +0000 | [diff] [blame] | 180 | void setDropTime(nsecs_t dropTime); |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 181 | void setPresentState(PresentState presentState, nsecs_t lastLatchTime = 0); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 182 | void setRenderRate(Fps renderRate); |
Adithya Srinivasan | b6a2fa1 | 2021-03-13 00:23:09 +0000 | [diff] [blame] | 183 | void setGpuComposition(); |
Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 184 | |
Adithya Srinivasan | 785addd | 2021-03-09 00:38:00 +0000 | [diff] [blame] | 185 | // When a bufferless SurfaceFrame is promoted to a buffer SurfaceFrame, we also have to update |
| 186 | // isBuffer. |
| 187 | void promoteToBuffer(); |
| 188 | |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 189 | // Functions called by FrameTimeline |
| 190 | // BaseTime is the smallest timestamp in this SurfaceFrame. |
| 191 | // Used for dumping all timestamps relative to the oldest, making it easy to read. |
| 192 | nsecs_t getBaseTime() const; |
| 193 | // Sets the actual present time, appropriate metadata and classifies the jank. |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 194 | // displayRefreshRate, displayDeadlineDelta, and displayPresentDelta are propagated from the |
| 195 | // display frame. |
| 196 | void onPresent(nsecs_t presentTime, int32_t displayFrameJankType, Fps refreshRate, |
| 197 | nsecs_t displayDeadlineDelta, nsecs_t displayPresentDelta); |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 198 | // All the timestamps are dumped relative to the baseTime |
| 199 | void dump(std::string& result, const std::string& indent, nsecs_t baseTime) const; |
Adithya Srinivasan | 785addd | 2021-03-09 00:38:00 +0000 | [diff] [blame] | 200 | // Dumps only the layer, token, is buffer, jank metadata, prediction and present states. |
| 201 | std::string miniDump() const; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 202 | // Emits a packet for perfetto tracing. The function body will be executed only if tracing is |
| 203 | // enabled. The displayFrameToken is needed to link the SurfaceFrame to the corresponding |
| 204 | // DisplayFrame at the trace processor side. |
Adithya Srinivasan | 061c14c | 2021-02-11 01:19:47 +0000 | [diff] [blame] | 205 | void trace(int64_t displayFrameToken) const; |
Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 206 | |
Adithya Srinivasan | 061c14c | 2021-02-11 01:19:47 +0000 | [diff] [blame] | 207 | // Getter functions used only by FrameTimelineTests and SurfaceFrame internally |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 208 | TimelineItem getActuals() const; |
| 209 | pid_t getOwnerPid() const { return mOwnerPid; }; |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 210 | int32_t getLayerId() const { return mLayerId; }; |
Adithya Srinivasan | 061c14c | 2021-02-11 01:19:47 +0000 | [diff] [blame] | 211 | PredictionState getPredictionState() const; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 212 | PresentState getPresentState() const; |
| 213 | FrameReadyMetadata getFrameReadyMetadata() const; |
| 214 | FramePresentMetadata getFramePresentMetadata() const; |
Adithya Srinivasan | 061c14c | 2021-02-11 01:19:47 +0000 | [diff] [blame] | 215 | nsecs_t getDropTime() const; |
Adithya Srinivasan | 785addd | 2021-03-09 00:38:00 +0000 | [diff] [blame] | 216 | bool getIsBuffer() const; |
Adithya Srinivasan | 061c14c | 2021-02-11 01:19:47 +0000 | [diff] [blame] | 217 | |
| 218 | // For prediction expired frames, this delta is subtracted from the actual end time to get a |
| 219 | // start time decent enough to see in traces. |
| 220 | // TODO(b/172587309): Remove this when we have actual start times. |
| 221 | static constexpr nsecs_t kPredictionExpiredStartTimeDelta = |
| 222 | std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count(); |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 223 | |
| 224 | private: |
Adithya Srinivasan | 061c14c | 2021-02-11 01:19:47 +0000 | [diff] [blame] | 225 | void tracePredictions(int64_t displayFrameToken) const; |
| 226 | void traceActuals(int64_t displayFrameToken) const; |
Adithya Srinivasan | 7c4ac7a | 2021-03-08 23:48:03 +0000 | [diff] [blame] | 227 | void classifyJankLocked(int32_t displayFrameJankType, const Fps& refreshRate, |
| 228 | nsecs_t& deadlineDelta) REQUIRES(mMutex); |
Adithya Srinivasan | 061c14c | 2021-02-11 01:19:47 +0000 | [diff] [blame] | 229 | |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 230 | const int64_t mToken; |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 231 | const int32_t mInputEventId; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 232 | const pid_t mOwnerPid; |
| 233 | const uid_t mOwnerUid; |
| 234 | const std::string mLayerName; |
| 235 | const std::string mDebugName; |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 236 | const int32_t mLayerId; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 237 | PresentState mPresentState GUARDED_BY(mMutex); |
| 238 | const PredictionState mPredictionState; |
| 239 | const TimelineItem mPredictions; |
| 240 | TimelineItem mActuals GUARDED_BY(mMutex); |
| 241 | std::shared_ptr<TimeStats> mTimeStats; |
| 242 | const JankClassificationThresholds mJankClassificationThresholds; |
| 243 | nsecs_t mActualQueueTime GUARDED_BY(mMutex) = 0; |
Adithya Srinivasan | 061c14c | 2021-02-11 01:19:47 +0000 | [diff] [blame] | 244 | nsecs_t mDropTime GUARDED_BY(mMutex) = 0; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 245 | mutable std::mutex mMutex; |
| 246 | // Bitmask for the type of jank |
| 247 | int32_t mJankType GUARDED_BY(mMutex) = JankType::None; |
| 248 | // Indicates if this frame was composited by the GPU or not |
| 249 | bool mGpuComposition GUARDED_BY(mMutex) = false; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 250 | // Rendering rate for this frame. |
| 251 | std::optional<Fps> mRenderRate GUARDED_BY(mMutex); |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 252 | // Enum for the type of present |
| 253 | FramePresentMetadata mFramePresentMetadata GUARDED_BY(mMutex) = |
| 254 | FramePresentMetadata::UnknownPresent; |
| 255 | // Enum for the type of finish |
| 256 | FrameReadyMetadata mFrameReadyMetadata GUARDED_BY(mMutex) = FrameReadyMetadata::UnknownFinish; |
| 257 | // Time when the previous buffer from the same layer was latched by SF. This is used in checking |
| 258 | // for BufferStuffing where the current buffer is expected to be ready but the previous buffer |
| 259 | // was latched instead. |
| 260 | nsecs_t mLastLatchTime GUARDED_BY(mMutex) = 0; |
Adithya Srinivasan | 05bd2d1 | 2021-01-11 18:49:58 +0000 | [diff] [blame] | 261 | // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto. Using a |
| 262 | // reference here because the counter is owned by FrameTimeline, which outlives SurfaceFrame. |
| 263 | TraceCookieCounter& mTraceCookieCounter; |
Adithya Srinivasan | 785addd | 2021-03-09 00:38:00 +0000 | [diff] [blame] | 264 | // Tells if the SurfaceFrame is representing a buffer or a transaction without a |
| 265 | // buffer(animations) |
| 266 | bool mIsBuffer; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | /* |
| 270 | * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were |
| 271 | * presented |
| 272 | */ |
| 273 | class FrameTimeline { |
| 274 | public: |
| 275 | virtual ~FrameTimeline() = default; |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 276 | virtual TokenManager* getTokenManager() = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 277 | |
Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 278 | // Initializes the Perfetto DataSource that emits DisplayFrame and SurfaceFrame events. Test |
| 279 | // classes can avoid double registration by mocking this function. |
| 280 | virtual void onBootFinished() = 0; |
| 281 | |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 282 | // Create a new surface frame, set the predictions based on a token and return it to the caller. |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 283 | // Debug name is the human-readable debugging string for dumpsys. |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 284 | virtual std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken( |
| 285 | const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid, |
Adithya Srinivasan | 785addd | 2021-03-09 00:38:00 +0000 | [diff] [blame] | 286 | int32_t layerId, std::string layerName, std::string debugName, bool isBuffer) = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 287 | |
| 288 | // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be |
| 289 | // composited into one display frame. |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 290 | virtual void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame) = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 291 | |
| 292 | // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on |
| 293 | // the token and sets the actualSfWakeTime for the current DisplayFrame. |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 294 | virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate) = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 295 | |
Adithya Srinivasan | b6a2fa1 | 2021-03-13 00:23:09 +0000 | [diff] [blame] | 296 | // Sets the sfPresentTime, gpuComposition and finalizes the current DisplayFrame. Tracks the |
| 297 | // given present fence until it's signaled, and updates the present timestamps of all presented |
| 298 | // SurfaceFrames in that vsync. |
| 299 | virtual void setSfPresent(nsecs_t sfPresentTime, const std::shared_ptr<FenceTime>& presentFence, |
| 300 | bool gpuComposition) = 0; |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 301 | |
Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 302 | // Args: |
| 303 | // -jank : Dumps only the Display Frames that are either janky themselves |
| 304 | // or contain janky Surface Frames. |
| 305 | // -all : Dumps the entire list of DisplayFrames and the SurfaceFrames contained within |
| 306 | virtual void parseArgs(const Vector<String16>& args, std::string& result) = 0; |
Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 307 | |
| 308 | // Sets the max number of display frames that can be stored. Called by SF backdoor. |
| 309 | virtual void setMaxDisplayFrames(uint32_t size); |
| 310 | |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 311 | // Computes the historical fps for the provided set of layer IDs |
| 312 | // The fps is compted from the linear timeline of present timestamps for DisplayFrames |
| 313 | // containing at least one layer ID. |
| 314 | virtual float computeFps(const std::unordered_set<int32_t>& layerIds); |
| 315 | |
Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 316 | // Restores the max number of display frames to default. Called by SF backdoor. |
| 317 | virtual void reset() = 0; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | namespace impl { |
| 321 | |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 322 | class TokenManager : public android::frametimeline::TokenManager { |
| 323 | public: |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 324 | TokenManager() : mCurrentToken(FrameTimelineInfo::INVALID_VSYNC_ID + 1) {} |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 325 | ~TokenManager() = default; |
| 326 | |
| 327 | int64_t generateTokenForPredictions(TimelineItem&& predictions) override; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 328 | std::optional<TimelineItem> getPredictionsForToken(int64_t token) const override; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 329 | |
| 330 | private: |
| 331 | // Friend class for testing |
| 332 | friend class android::frametimeline::FrameTimelineTest; |
| 333 | |
| 334 | void flushTokens(nsecs_t flushTime) REQUIRES(mMutex); |
| 335 | |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 336 | std::map<int64_t, TokenManagerPrediction> mPredictions GUARDED_BY(mMutex); |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 337 | int64_t mCurrentToken GUARDED_BY(mMutex); |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 338 | mutable std::mutex mMutex; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 339 | static constexpr nsecs_t kMaxRetentionTime = |
| 340 | std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count(); |
| 341 | }; |
| 342 | |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 343 | class FrameTimeline : public android::frametimeline::FrameTimeline { |
| 344 | public: |
Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 345 | class FrameTimelineDataSource : public perfetto::DataSource<FrameTimelineDataSource> { |
| 346 | void OnSetup(const SetupArgs&) override{}; |
| 347 | void OnStart(const StartArgs&) override{}; |
| 348 | void OnStop(const StopArgs&) override{}; |
| 349 | }; |
| 350 | |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 351 | /* |
| 352 | * DisplayFrame should be used only internally within FrameTimeline. All members and methods are |
| 353 | * guarded by FrameTimeline's mMutex. |
| 354 | */ |
| 355 | class DisplayFrame { |
| 356 | public: |
Adithya Srinivasan | 05bd2d1 | 2021-01-11 18:49:58 +0000 | [diff] [blame] | 357 | DisplayFrame(std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds, |
Adithya Srinivasan | 82eef32 | 2021-04-10 00:06:04 +0000 | [diff] [blame^] | 358 | TraceCookieCounter* traceCookieCounter); |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 359 | virtual ~DisplayFrame() = default; |
| 360 | // Dumpsys interface - dumps only if the DisplayFrame itself is janky or is at least one |
| 361 | // SurfaceFrame is janky. |
| 362 | void dumpJank(std::string& result, nsecs_t baseTime, int displayFrameCount) const; |
| 363 | // Dumpsys interface - dumps all data irrespective of jank |
| 364 | void dumpAll(std::string& result, nsecs_t baseTime) const; |
| 365 | // Emits a packet for perfetto tracing. The function body will be executed only if tracing |
| 366 | // is enabled. |
| 367 | void trace(pid_t surfaceFlingerPid) const; |
| 368 | // Sets the token, vsyncPeriod, predictions and SF start time. |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 369 | void onSfWakeUp(int64_t token, Fps refreshRate, std::optional<TimelineItem> predictions, |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 370 | nsecs_t wakeUpTime); |
Adithya Srinivasan | 115ac69 | 2021-03-06 01:21:30 +0000 | [diff] [blame] | 371 | // Sets the appropriate metadata and classifies the jank. |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 372 | void onPresent(nsecs_t signalTime); |
| 373 | // Adds the provided SurfaceFrame to the current display frame. |
| 374 | void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame); |
| 375 | |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 376 | void setPredictions(PredictionState predictionState, TimelineItem predictions); |
| 377 | void setActualStartTime(nsecs_t actualStartTime); |
| 378 | void setActualEndTime(nsecs_t actualEndTime); |
Adithya Srinivasan | b6a2fa1 | 2021-03-13 00:23:09 +0000 | [diff] [blame] | 379 | void setGpuComposition(); |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 380 | |
| 381 | // BaseTime is the smallest timestamp in a DisplayFrame. |
| 382 | // Used for dumping all timestamps relative to the oldest, making it easy to read. |
| 383 | nsecs_t getBaseTime() const; |
| 384 | |
| 385 | // Functions to be used only in testing. |
| 386 | TimelineItem getActuals() const { return mSurfaceFlingerActuals; }; |
| 387 | TimelineItem getPredictions() const { return mSurfaceFlingerPredictions; }; |
Adithya Srinivasan | 939cd4d | 2021-02-23 06:18:13 +0000 | [diff] [blame] | 388 | FrameStartMetadata getFrameStartMetadata() const { return mFrameStartMetadata; }; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 389 | FramePresentMetadata getFramePresentMetadata() const { return mFramePresentMetadata; }; |
| 390 | FrameReadyMetadata getFrameReadyMetadata() const { return mFrameReadyMetadata; }; |
| 391 | int32_t getJankType() const { return mJankType; } |
| 392 | const std::vector<std::shared_ptr<SurfaceFrame>>& getSurfaceFrames() const { |
| 393 | return mSurfaceFrames; |
| 394 | } |
| 395 | |
| 396 | private: |
| 397 | void dump(std::string& result, nsecs_t baseTime) const; |
Adithya Srinivasan | 061c14c | 2021-02-11 01:19:47 +0000 | [diff] [blame] | 398 | void tracePredictions(pid_t surfaceFlingerPid) const; |
| 399 | void traceActuals(pid_t surfaceFlingerPid) const; |
Adithya Srinivasan | 115ac69 | 2021-03-06 01:21:30 +0000 | [diff] [blame] | 400 | void classifyJank(nsecs_t& deadlineDelta, nsecs_t& deltaToVsync); |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 401 | |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 402 | int64_t mToken = FrameTimelineInfo::INVALID_VSYNC_ID; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 403 | |
| 404 | /* Usage of TimelineItem w.r.t SurfaceFlinger |
| 405 | * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates |
| 406 | * endTime Time when SurfaceFlinger sends a composited frame to Display |
| 407 | * presentTime Time when the composited frame was presented on screen |
| 408 | */ |
| 409 | TimelineItem mSurfaceFlingerPredictions; |
| 410 | TimelineItem mSurfaceFlingerActuals; |
| 411 | std::shared_ptr<TimeStats> mTimeStats; |
| 412 | const JankClassificationThresholds mJankClassificationThresholds; |
| 413 | |
| 414 | // Collection of predictions and actual values sent over by Layers |
| 415 | std::vector<std::shared_ptr<SurfaceFrame>> mSurfaceFrames; |
| 416 | |
| 417 | PredictionState mPredictionState = PredictionState::None; |
| 418 | // Bitmask for the type of jank |
| 419 | int32_t mJankType = JankType::None; |
| 420 | // Indicates if this frame was composited by the GPU or not |
| 421 | bool mGpuComposition = false; |
| 422 | // Enum for the type of present |
| 423 | FramePresentMetadata mFramePresentMetadata = FramePresentMetadata::UnknownPresent; |
| 424 | // Enum for the type of finish |
| 425 | FrameReadyMetadata mFrameReadyMetadata = FrameReadyMetadata::UnknownFinish; |
| 426 | // Enum for the type of start |
| 427 | FrameStartMetadata mFrameStartMetadata = FrameStartMetadata::UnknownStart; |
| 428 | // The refresh rate (vsync period) in nanoseconds as seen by SF during this DisplayFrame's |
| 429 | // timeline |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 430 | Fps mRefreshRate; |
Adithya Srinivasan | 05bd2d1 | 2021-01-11 18:49:58 +0000 | [diff] [blame] | 431 | // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto. |
| 432 | // Using a reference here because the counter is owned by FrameTimeline, which outlives |
| 433 | // DisplayFrame. |
| 434 | TraceCookieCounter& mTraceCookieCounter; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 435 | }; |
| 436 | |
| 437 | FrameTimeline(std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid, |
Adithya Srinivasan | 82eef32 | 2021-04-10 00:06:04 +0000 | [diff] [blame^] | 438 | JankClassificationThresholds thresholds = {}); |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 439 | ~FrameTimeline() = default; |
| 440 | |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 441 | frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; } |
Siarhei Vishniakou | fc434ac | 2021-01-13 10:28:00 -1000 | [diff] [blame] | 442 | std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken( |
| 443 | const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid, |
Adithya Srinivasan | 785addd | 2021-03-09 00:38:00 +0000 | [diff] [blame] | 444 | int32_t layerId, std::string layerName, std::string debugName, bool isBuffer) override; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 445 | void addSurfaceFrame(std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame) override; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 446 | void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate) override; |
Adithya Srinivasan | b6a2fa1 | 2021-03-13 00:23:09 +0000 | [diff] [blame] | 447 | void setSfPresent(nsecs_t sfPresentTime, const std::shared_ptr<FenceTime>& presentFence, |
| 448 | bool gpuComposition = false) override; |
Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 449 | void parseArgs(const Vector<String16>& args, std::string& result) override; |
Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 450 | void setMaxDisplayFrames(uint32_t size) override; |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 451 | float computeFps(const std::unordered_set<int32_t>& layerIds) override; |
Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 452 | void reset() override; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 453 | |
Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 454 | // 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 Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 462 | private: |
| 463 | // Friend class for testing |
| 464 | friend class android::frametimeline::FrameTimelineTest; |
| 465 | |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 466 | void flushPendingPresentFences() REQUIRES(mMutex); |
| 467 | void finalizeCurrentDisplayFrame() REQUIRES(mMutex); |
Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 468 | void dumpAll(std::string& result); |
| 469 | void dumpJank(std::string& result); |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 470 | |
| 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 Srinivasan | 05bd2d1 | 2021-01-11 18:49:58 +0000 | [diff] [blame] | 477 | TraceCookieCounter mTraceCookieCounter; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 478 | mutable std::mutex mMutex; |
Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 479 | uint32_t mMaxDisplayFrames; |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 480 | std::shared_ptr<TimeStats> mTimeStats; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 481 | const pid_t mSurfaceFlingerPid; |
| 482 | const JankClassificationThresholds mJankClassificationThresholds; |
Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 483 | static constexpr uint32_t kDefaultMaxDisplayFrames = 64; |
Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 484 | // The initial container size for the vector<SurfaceFrames> inside display frame. Although |
| 485 | // this number doesn't represent any bounds on the number of surface frames that can go in a |
| 486 | // display frame, this is a good starting size for the vector so that we can avoid the |
| 487 | // internal vector resizing that happens with push_back. |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 488 | static constexpr uint32_t kNumSurfaceFramesInitial = 10; |
Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | } // namespace impl |
| 492 | } // namespace android::frametimeline |