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