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