| 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 | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 19 | #include <../TimeStats/TimeStats.h> | 
| Ady Abraham | 22c7b5c | 2020-09-22 19:33:40 -0700 | [diff] [blame] | 20 | #include <gui/ISurfaceComposer.h> | 
| Jorim Jaggi | 5814ab8 | 2020-12-03 20:45:58 +0100 | [diff] [blame] | 21 | #include <gui/JankInfo.h> | 
| Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 22 | #include <perfetto/trace/android/frame_timeline_event.pbzero.h> | 
|  | 23 | #include <perfetto/tracing.h> | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 24 | #include <ui/FenceTime.h> | 
|  | 25 | #include <utils/RefBase.h> | 
| Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 26 | #include <utils/String16.h> | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 27 | #include <utils/Timers.h> | 
| Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 28 | #include <utils/Vector.h> | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 29 |  | 
| Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 30 | #include <deque> | 
|  | 31 | #include <mutex> | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 32 |  | 
| Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 33 | namespace android::frametimeline { | 
| Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 34 |  | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 35 | class FrameTimelineTest; | 
|  | 36 |  | 
|  | 37 | using namespace std::chrono_literals; | 
|  | 38 |  | 
|  | 39 | // Metadata indicating how the frame was presented w.r.t expected present time. | 
|  | 40 | enum class FramePresentMetadata : int8_t { | 
|  | 41 | // Frame was presented on time | 
|  | 42 | OnTimePresent, | 
|  | 43 | // Frame was presented late | 
|  | 44 | LatePresent, | 
|  | 45 | // Frame was presented early | 
|  | 46 | EarlyPresent, | 
|  | 47 | // Unknown/initial state | 
|  | 48 | UnknownPresent, | 
| Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 49 | }; | 
|  | 50 |  | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 51 | // Metadata comparing the frame's actual finish time to the expected deadline. | 
|  | 52 | enum class FrameReadyMetadata : int8_t { | 
|  | 53 | // App/SF finished on time. Early finish is treated as on time since the goal of any component | 
|  | 54 | // is to finish before the deadline. | 
|  | 55 | OnTimeFinish, | 
|  | 56 | // App/SF finished work later than expected | 
|  | 57 | LateFinish, | 
|  | 58 | // Unknown/initial state | 
|  | 59 | UnknownFinish, | 
|  | 60 | }; | 
|  | 61 |  | 
|  | 62 | // Metadata comparing the frame's actual start time to the expected start time. | 
|  | 63 | enum class FrameStartMetadata : int8_t { | 
|  | 64 | // App/SF started on time | 
|  | 65 | OnTimeStart, | 
|  | 66 | // App/SF started later than expected | 
|  | 67 | LateStart, | 
|  | 68 | // App/SF started earlier than expected | 
|  | 69 | EarlyStart, | 
|  | 70 | // Unknown/initial state | 
|  | 71 | UnknownStart, | 
|  | 72 | }; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 73 |  | 
|  | 74 | /* | 
|  | 75 | * Collection of timestamps that can be used for both predictions and actual times. | 
|  | 76 | */ | 
|  | 77 | struct TimelineItem { | 
|  | 78 | TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0, | 
|  | 79 | const nsecs_t presentTime = 0) | 
|  | 80 | : startTime(startTime), endTime(endTime), presentTime(presentTime) {} | 
|  | 81 |  | 
|  | 82 | nsecs_t startTime; | 
|  | 83 | nsecs_t endTime; | 
|  | 84 | nsecs_t presentTime; | 
| Ady Abraham | 55fa727 | 2020-09-30 19:19:27 -0700 | [diff] [blame] | 85 |  | 
|  | 86 | bool operator==(const TimelineItem& other) const { | 
|  | 87 | return startTime == other.startTime && endTime == other.endTime && | 
|  | 88 | presentTime == other.presentTime; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | bool operator!=(const TimelineItem& other) const { return !(*this == other); } | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 92 | }; | 
|  | 93 |  | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 94 | struct TokenManagerPrediction { | 
|  | 95 | nsecs_t timestamp = 0; | 
|  | 96 | TimelineItem predictions; | 
|  | 97 | }; | 
|  | 98 |  | 
|  | 99 | struct JankClassificationThresholds { | 
|  | 100 | // The various thresholds for App and SF. If the actual timestamp falls within the threshold | 
|  | 101 | // compared to prediction, we treat it as on time. | 
|  | 102 | nsecs_t presentThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count(); | 
|  | 103 | nsecs_t deadlineThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count(); | 
|  | 104 | nsecs_t startThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count(); | 
|  | 105 | }; | 
|  | 106 |  | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 107 | /* | 
|  | 108 | * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It | 
|  | 109 | * saves these predictions for a short period of time and returns the predictions for a given token, | 
|  | 110 | * if it hasn't expired. | 
|  | 111 | */ | 
|  | 112 | class TokenManager { | 
|  | 113 | public: | 
|  | 114 | virtual ~TokenManager() = default; | 
|  | 115 |  | 
|  | 116 | // Generates a token for the given set of predictions. Stores the predictions for 120ms and | 
|  | 117 | // destroys it later. | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 118 | virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0; | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 119 |  | 
|  | 120 | // Returns the stored predictions for a given token, if the predictions haven't expired. | 
|  | 121 | virtual std::optional<TimelineItem> getPredictionsForToken(int64_t token) const = 0; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 122 | }; | 
|  | 123 |  | 
|  | 124 | enum class PredictionState { | 
|  | 125 | Valid,   // Predictions obtained successfully from the TokenManager | 
|  | 126 | Expired, // TokenManager no longer has the predictions | 
|  | 127 | None,    // Predictions are either not present or didn't come from TokenManager | 
|  | 128 | }; | 
|  | 129 |  | 
| Adithya Srinivasan | 05bd2d1 | 2021-01-11 18:49:58 +0000 | [diff] [blame] | 130 | /* | 
|  | 131 | * Trace cookie is used to send start and end timestamps of <Surface/Display>Frames separately | 
|  | 132 | * without needing to resend all the other information. We send all info to perfetto, along with a | 
|  | 133 | * new cookie, in the start of a frame. For the corresponding end, we just send the same cookie. | 
|  | 134 | * This helps in reducing the amount of data emitted by the producer. | 
|  | 135 | */ | 
|  | 136 | class TraceCookieCounter { | 
|  | 137 | public: | 
|  | 138 | int64_t getCookieForTracing(); | 
|  | 139 |  | 
|  | 140 | private: | 
|  | 141 | // Friend class for testing | 
|  | 142 | friend class android::frametimeline::FrameTimelineTest; | 
|  | 143 |  | 
|  | 144 | std::atomic<int64_t> mTraceCookie = 0; | 
|  | 145 | }; | 
|  | 146 |  | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 147 | class SurfaceFrame { | 
|  | 148 | public: | 
|  | 149 | enum class PresentState { | 
|  | 150 | Presented, // Buffer was latched and presented by SurfaceFlinger | 
|  | 151 | Dropped,   // Buffer was dropped by SurfaceFlinger | 
|  | 152 | Unknown,   // Initial state, SurfaceFlinger hasn't seen this buffer yet | 
|  | 153 | }; | 
|  | 154 |  | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 155 | // Only FrameTimeline can construct a SurfaceFrame as it provides Predictions(through | 
|  | 156 | // TokenManager), Thresholds and TimeStats pointer. | 
|  | 157 | SurfaceFrame(int64_t token, pid_t ownerPid, uid_t ownerUid, std::string layerName, | 
|  | 158 | std::string debugName, PredictionState predictionState, TimelineItem&& predictions, | 
| Adithya Srinivasan | 05bd2d1 | 2021-01-11 18:49:58 +0000 | [diff] [blame] | 159 | std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds, | 
|  | 160 | TraceCookieCounter* traceCookieCounter); | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 161 | ~SurfaceFrame() = default; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 162 |  | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 163 | // Returns std::nullopt if the frame hasn't been classified yet. | 
|  | 164 | // Used by both SF and FrameTimeline. | 
|  | 165 | std::optional<int32_t> getJankType() const; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 166 |  | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 167 | // Functions called by SF | 
|  | 168 | int64_t getToken() const { return mToken; }; | 
|  | 169 | TimelineItem getPredictions() const { return mPredictions; }; | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 170 | // Actual timestamps of the app are set individually at different functions. | 
|  | 171 | // Start time (if the app provides) and Queue time are accessible after queueing the frame, | 
| Ady Abraham | 7f8a1e6 | 2020-09-28 16:09:35 -0700 | [diff] [blame] | 172 | // whereas Acquire Fence time is available only during latch. | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 173 | void setActualStartTime(nsecs_t actualStartTime); | 
|  | 174 | void setActualQueueTime(nsecs_t actualQueueTime); | 
|  | 175 | void setAcquireFenceTime(nsecs_t acquireFenceTime); | 
|  | 176 | void setPresentState(PresentState presentState, nsecs_t lastLatchTime = 0); | 
| Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 177 |  | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 178 | // Functions called by FrameTimeline | 
|  | 179 | // BaseTime is the smallest timestamp in this SurfaceFrame. | 
|  | 180 | // Used for dumping all timestamps relative to the oldest, making it easy to read. | 
|  | 181 | nsecs_t getBaseTime() const; | 
|  | 182 | // Sets the actual present time, appropriate metadata and classifies the jank. | 
|  | 183 | void onPresent(nsecs_t presentTime, int32_t displayFrameJankType, nsecs_t vsyncPeriod); | 
|  | 184 | // All the timestamps are dumped relative to the baseTime | 
|  | 185 | void dump(std::string& result, const std::string& indent, nsecs_t baseTime) const; | 
|  | 186 | // Emits a packet for perfetto tracing. The function body will be executed only if tracing is | 
|  | 187 | // enabled. The displayFrameToken is needed to link the SurfaceFrame to the corresponding | 
|  | 188 | // DisplayFrame at the trace processor side. | 
|  | 189 | void trace(int64_t displayFrameToken); | 
| Jorim Jaggi | 9c03b50 | 2020-11-24 23:51:31 +0100 | [diff] [blame] | 190 |  | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 191 | // Getter functions used only by FrameTimelineTests | 
|  | 192 | TimelineItem getActuals() const; | 
|  | 193 | pid_t getOwnerPid() const { return mOwnerPid; }; | 
|  | 194 | PredictionState getPredictionState() const { return mPredictionState; }; | 
|  | 195 | PresentState getPresentState() const; | 
|  | 196 | FrameReadyMetadata getFrameReadyMetadata() const; | 
|  | 197 | FramePresentMetadata getFramePresentMetadata() const; | 
|  | 198 |  | 
|  | 199 | private: | 
|  | 200 | const int64_t mToken; | 
|  | 201 | const pid_t mOwnerPid; | 
|  | 202 | const uid_t mOwnerUid; | 
|  | 203 | const std::string mLayerName; | 
|  | 204 | const std::string mDebugName; | 
|  | 205 | PresentState mPresentState GUARDED_BY(mMutex); | 
|  | 206 | const PredictionState mPredictionState; | 
|  | 207 | const TimelineItem mPredictions; | 
|  | 208 | TimelineItem mActuals GUARDED_BY(mMutex); | 
|  | 209 | std::shared_ptr<TimeStats> mTimeStats; | 
|  | 210 | const JankClassificationThresholds mJankClassificationThresholds; | 
|  | 211 | nsecs_t mActualQueueTime GUARDED_BY(mMutex) = 0; | 
|  | 212 | mutable std::mutex mMutex; | 
|  | 213 | // Bitmask for the type of jank | 
|  | 214 | int32_t mJankType GUARDED_BY(mMutex) = JankType::None; | 
|  | 215 | // Indicates if this frame was composited by the GPU or not | 
|  | 216 | bool mGpuComposition GUARDED_BY(mMutex) = false; | 
|  | 217 | // Enum for the type of present | 
|  | 218 | FramePresentMetadata mFramePresentMetadata GUARDED_BY(mMutex) = | 
|  | 219 | FramePresentMetadata::UnknownPresent; | 
|  | 220 | // Enum for the type of finish | 
|  | 221 | FrameReadyMetadata mFrameReadyMetadata GUARDED_BY(mMutex) = FrameReadyMetadata::UnknownFinish; | 
|  | 222 | // Time when the previous buffer from the same layer was latched by SF. This is used in checking | 
|  | 223 | // for BufferStuffing where the current buffer is expected to be ready but the previous buffer | 
|  | 224 | // was latched instead. | 
|  | 225 | nsecs_t mLastLatchTime GUARDED_BY(mMutex) = 0; | 
| Adithya Srinivasan | 05bd2d1 | 2021-01-11 18:49:58 +0000 | [diff] [blame] | 226 | // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto. Using a | 
|  | 227 | // reference here because the counter is owned by FrameTimeline, which outlives SurfaceFrame. | 
|  | 228 | TraceCookieCounter& mTraceCookieCounter; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 229 | }; | 
|  | 230 |  | 
|  | 231 | /* | 
|  | 232 | * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were | 
|  | 233 | * presented | 
|  | 234 | */ | 
|  | 235 | class FrameTimeline { | 
|  | 236 | public: | 
|  | 237 | virtual ~FrameTimeline() = default; | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 238 | virtual TokenManager* getTokenManager() = 0; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 239 |  | 
| Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 240 | // Initializes the Perfetto DataSource that emits DisplayFrame and SurfaceFrame events. Test | 
|  | 241 | // classes can avoid double registration by mocking this function. | 
|  | 242 | virtual void onBootFinished() = 0; | 
|  | 243 |  | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 244 | // 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] | 245 | // Debug name is the human-readable debugging string for dumpsys. | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 246 | virtual std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(std::optional<int64_t> token, | 
|  | 247 | pid_t ownerPid, uid_t ownerUid, | 
|  | 248 | std::string layerName, | 
|  | 249 | std::string debugName) = 0; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 250 |  | 
|  | 251 | // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be | 
|  | 252 | // composited into one display frame. | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 253 | virtual void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame) = 0; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 254 |  | 
|  | 255 | // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on | 
|  | 256 | // the token and sets the actualSfWakeTime for the current DisplayFrame. | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 257 | virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime, nsecs_t vsyncPeriod) = 0; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 258 |  | 
|  | 259 | // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the given present fence | 
|  | 260 | // until it's signaled, and updates the present timestamps of all presented SurfaceFrames in | 
|  | 261 | // that vsync. | 
|  | 262 | virtual void setSfPresent(nsecs_t sfPresentTime, | 
|  | 263 | const std::shared_ptr<FenceTime>& presentFence) = 0; | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 264 |  | 
| Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 265 | // Args: | 
|  | 266 | // -jank : Dumps only the Display Frames that are either janky themselves | 
|  | 267 | //         or contain janky Surface Frames. | 
|  | 268 | // -all : Dumps the entire list of DisplayFrames and the SurfaceFrames contained within | 
|  | 269 | virtual void parseArgs(const Vector<String16>& args, std::string& result) = 0; | 
| Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 270 |  | 
|  | 271 | // Sets the max number of display frames that can be stored. Called by SF backdoor. | 
|  | 272 | virtual void setMaxDisplayFrames(uint32_t size); | 
|  | 273 |  | 
|  | 274 | // Restores the max number of display frames to default. Called by SF backdoor. | 
|  | 275 | virtual void reset() = 0; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 276 | }; | 
|  | 277 |  | 
|  | 278 | namespace impl { | 
|  | 279 |  | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 280 | class TokenManager : public android::frametimeline::TokenManager { | 
|  | 281 | public: | 
| Ady Abraham | 22c7b5c | 2020-09-22 19:33:40 -0700 | [diff] [blame] | 282 | TokenManager() : mCurrentToken(ISurfaceComposer::INVALID_VSYNC_ID + 1) {} | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 283 | ~TokenManager() = default; | 
|  | 284 |  | 
|  | 285 | int64_t generateTokenForPredictions(TimelineItem&& predictions) override; | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 286 | std::optional<TimelineItem> getPredictionsForToken(int64_t token) const override; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 287 |  | 
|  | 288 | private: | 
|  | 289 | // Friend class for testing | 
|  | 290 | friend class android::frametimeline::FrameTimelineTest; | 
|  | 291 |  | 
|  | 292 | void flushTokens(nsecs_t flushTime) REQUIRES(mMutex); | 
|  | 293 |  | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 294 | std::map<int64_t, TokenManagerPrediction> mPredictions GUARDED_BY(mMutex); | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 295 | int64_t mCurrentToken GUARDED_BY(mMutex); | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 296 | mutable std::mutex mMutex; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 297 | static constexpr nsecs_t kMaxRetentionTime = | 
|  | 298 | std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count(); | 
|  | 299 | }; | 
|  | 300 |  | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 301 | class FrameTimeline : public android::frametimeline::FrameTimeline { | 
|  | 302 | public: | 
| Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 303 | class FrameTimelineDataSource : public perfetto::DataSource<FrameTimelineDataSource> { | 
|  | 304 | void OnSetup(const SetupArgs&) override{}; | 
|  | 305 | void OnStart(const StartArgs&) override{}; | 
|  | 306 | void OnStop(const StopArgs&) override{}; | 
|  | 307 | }; | 
|  | 308 |  | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 309 | /* | 
|  | 310 | * DisplayFrame should be used only internally within FrameTimeline. All members and methods are | 
|  | 311 | * guarded by FrameTimeline's mMutex. | 
|  | 312 | */ | 
|  | 313 | class DisplayFrame { | 
|  | 314 | public: | 
| Adithya Srinivasan | 05bd2d1 | 2021-01-11 18:49:58 +0000 | [diff] [blame] | 315 | DisplayFrame(std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds, | 
|  | 316 | TraceCookieCounter* traceCookieCounter); | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 317 | virtual ~DisplayFrame() = default; | 
|  | 318 | // Dumpsys interface - dumps only if the DisplayFrame itself is janky or is at least one | 
|  | 319 | // SurfaceFrame is janky. | 
|  | 320 | void dumpJank(std::string& result, nsecs_t baseTime, int displayFrameCount) const; | 
|  | 321 | // Dumpsys interface - dumps all data irrespective of jank | 
|  | 322 | void dumpAll(std::string& result, nsecs_t baseTime) const; | 
|  | 323 | // Emits a packet for perfetto tracing. The function body will be executed only if tracing | 
|  | 324 | // is enabled. | 
|  | 325 | void trace(pid_t surfaceFlingerPid) const; | 
|  | 326 | // Sets the token, vsyncPeriod, predictions and SF start time. | 
|  | 327 | void onSfWakeUp(int64_t token, nsecs_t vsyncPeriod, std::optional<TimelineItem> predictions, | 
|  | 328 | nsecs_t wakeUpTime); | 
|  | 329 | // Sets the appropriate metadata, classifies the jank and returns the classified jankType. | 
|  | 330 | void onPresent(nsecs_t signalTime); | 
|  | 331 | // Adds the provided SurfaceFrame to the current display frame. | 
|  | 332 | void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame); | 
|  | 333 |  | 
|  | 334 | void setTokenAndVsyncPeriod(int64_t token, nsecs_t vsyncPeriod); | 
|  | 335 | void setPredictions(PredictionState predictionState, TimelineItem predictions); | 
|  | 336 | void setActualStartTime(nsecs_t actualStartTime); | 
|  | 337 | void setActualEndTime(nsecs_t actualEndTime); | 
|  | 338 |  | 
|  | 339 | // BaseTime is the smallest timestamp in a DisplayFrame. | 
|  | 340 | // Used for dumping all timestamps relative to the oldest, making it easy to read. | 
|  | 341 | nsecs_t getBaseTime() const; | 
|  | 342 |  | 
|  | 343 | // Functions to be used only in testing. | 
|  | 344 | TimelineItem getActuals() const { return mSurfaceFlingerActuals; }; | 
|  | 345 | TimelineItem getPredictions() const { return mSurfaceFlingerPredictions; }; | 
|  | 346 | FramePresentMetadata getFramePresentMetadata() const { return mFramePresentMetadata; }; | 
|  | 347 | FrameReadyMetadata getFrameReadyMetadata() const { return mFrameReadyMetadata; }; | 
|  | 348 | int32_t getJankType() const { return mJankType; } | 
|  | 349 | const std::vector<std::shared_ptr<SurfaceFrame>>& getSurfaceFrames() const { | 
|  | 350 | return mSurfaceFrames; | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | private: | 
|  | 354 | void dump(std::string& result, nsecs_t baseTime) const; | 
|  | 355 |  | 
|  | 356 | int64_t mToken = ISurfaceComposer::INVALID_VSYNC_ID; | 
|  | 357 |  | 
|  | 358 | /* Usage of TimelineItem w.r.t SurfaceFlinger | 
|  | 359 | * startTime    Time when SurfaceFlinger wakes up to handle transactions and buffer updates | 
|  | 360 | * endTime      Time when SurfaceFlinger sends a composited frame to Display | 
|  | 361 | * presentTime  Time when the composited frame was presented on screen | 
|  | 362 | */ | 
|  | 363 | TimelineItem mSurfaceFlingerPredictions; | 
|  | 364 | TimelineItem mSurfaceFlingerActuals; | 
|  | 365 | std::shared_ptr<TimeStats> mTimeStats; | 
|  | 366 | const JankClassificationThresholds mJankClassificationThresholds; | 
|  | 367 |  | 
|  | 368 | // Collection of predictions and actual values sent over by Layers | 
|  | 369 | std::vector<std::shared_ptr<SurfaceFrame>> mSurfaceFrames; | 
|  | 370 |  | 
|  | 371 | PredictionState mPredictionState = PredictionState::None; | 
|  | 372 | // Bitmask for the type of jank | 
|  | 373 | int32_t mJankType = JankType::None; | 
|  | 374 | // Indicates if this frame was composited by the GPU or not | 
|  | 375 | bool mGpuComposition = false; | 
|  | 376 | // Enum for the type of present | 
|  | 377 | FramePresentMetadata mFramePresentMetadata = FramePresentMetadata::UnknownPresent; | 
|  | 378 | // Enum for the type of finish | 
|  | 379 | FrameReadyMetadata mFrameReadyMetadata = FrameReadyMetadata::UnknownFinish; | 
|  | 380 | // Enum for the type of start | 
|  | 381 | FrameStartMetadata mFrameStartMetadata = FrameStartMetadata::UnknownStart; | 
|  | 382 | // The refresh rate (vsync period) in nanoseconds as seen by SF during this DisplayFrame's | 
|  | 383 | // timeline | 
|  | 384 | nsecs_t mVsyncPeriod = 0; | 
| Adithya Srinivasan | 05bd2d1 | 2021-01-11 18:49:58 +0000 | [diff] [blame] | 385 | // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto. | 
|  | 386 | // Using a reference here because the counter is owned by FrameTimeline, which outlives | 
|  | 387 | // DisplayFrame. | 
|  | 388 | TraceCookieCounter& mTraceCookieCounter; | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 389 | }; | 
|  | 390 |  | 
|  | 391 | FrameTimeline(std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid, | 
|  | 392 | JankClassificationThresholds thresholds = {}); | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 393 | ~FrameTimeline() = default; | 
|  | 394 |  | 
| Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 395 | frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; } | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 396 | std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(std::optional<int64_t> token, | 
|  | 397 | pid_t ownerPid, uid_t ownerUid, | 
|  | 398 | std::string layerName, | 
|  | 399 | std::string debugName) override; | 
|  | 400 | void addSurfaceFrame(std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame) override; | 
|  | 401 | void setSfWakeUp(int64_t token, nsecs_t wakeupTime, nsecs_t vsyncPeriod) override; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 402 | void setSfPresent(nsecs_t sfPresentTime, | 
|  | 403 | const std::shared_ptr<FenceTime>& presentFence) override; | 
| Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 404 | void parseArgs(const Vector<String16>& args, std::string& result) override; | 
| Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 405 | void setMaxDisplayFrames(uint32_t size) override; | 
|  | 406 | void reset() override; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 407 |  | 
| Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 408 | // Sets up the perfetto tracing backend and data source. | 
|  | 409 | void onBootFinished() override; | 
|  | 410 | // Registers the data source with the perfetto backend. Called as part of onBootFinished() | 
|  | 411 | // and should not be called manually outside of tests. | 
|  | 412 | void registerDataSource(); | 
|  | 413 |  | 
|  | 414 | static constexpr char kFrameTimelineDataSource[] = "android.surfaceflinger.frametimeline"; | 
|  | 415 |  | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 416 | private: | 
|  | 417 | // Friend class for testing | 
|  | 418 | friend class android::frametimeline::FrameTimelineTest; | 
|  | 419 |  | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 420 | void flushPendingPresentFences() REQUIRES(mMutex); | 
|  | 421 | void finalizeCurrentDisplayFrame() REQUIRES(mMutex); | 
| Adithya Srinivasan | 8fc601d | 2020-09-25 13:51:09 -0700 | [diff] [blame] | 422 | void dumpAll(std::string& result); | 
|  | 423 | void dumpJank(std::string& result); | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 424 |  | 
|  | 425 | // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array | 
|  | 426 | std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex); | 
|  | 427 | std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>> | 
|  | 428 | mPendingPresentFences GUARDED_BY(mMutex); | 
|  | 429 | std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex); | 
|  | 430 | TokenManager mTokenManager; | 
| Adithya Srinivasan | 05bd2d1 | 2021-01-11 18:49:58 +0000 | [diff] [blame] | 431 | TraceCookieCounter mTraceCookieCounter; | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 432 | mutable std::mutex mMutex; | 
| Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 433 | uint32_t mMaxDisplayFrames; | 
| Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 434 | std::shared_ptr<TimeStats> mTimeStats; | 
| Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 435 | const pid_t mSurfaceFlingerPid; | 
|  | 436 | const JankClassificationThresholds mJankClassificationThresholds; | 
| Adithya Srinivasan | 2d73632 | 2020-10-01 16:53:48 -0700 | [diff] [blame] | 437 | static constexpr uint32_t kDefaultMaxDisplayFrames = 64; | 
| Adithya Srinivasan | 0118967 | 2020-10-20 14:23:05 -0700 | [diff] [blame] | 438 | // The initial container size for the vector<SurfaceFrames> inside display frame. Although | 
|  | 439 | // this number doesn't represent any bounds on the number of surface frames that can go in a | 
|  | 440 | // display frame, this is a good starting size for the vector so that we can avoid the | 
|  | 441 | // internal vector resizing that happens with push_back. | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 442 | static constexpr uint32_t kNumSurfaceFramesInitial = 10; | 
| Adithya Srinivasan | f279e04 | 2020-08-17 14:56:27 -0700 | [diff] [blame] | 443 | }; | 
|  | 444 |  | 
|  | 445 | } // namespace impl | 
|  | 446 | } // namespace android::frametimeline |