• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ROSEN_JANK_STATS_H
17 #define ROSEN_JANK_STATS_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <mutex>
22 #include <queue>
23 #include <string>
24 #include <tuple>
25 #include <utility>
26 #include <vector>
27 
28 #include "nocopyable.h"
29 #include "transaction/rs_render_service_client.h"
30 #include "platform/common/rs_system_properties.h"
31 
32 namespace OHOS {
33 namespace Rosen {
34 constexpr uint32_t STANDARD_REFRESH_RATE = 60;
35 constexpr int64_t TIMESTAMP_INITIAL = -1;
36 constexpr int32_t TRACE_ID_INITIAL = -1;
37 constexpr float TIMESTAMP_INITIAL_FLOAT = -1.f;
38 constexpr float MS_TO_US = 1000.f; // ms to us
39 
40 struct JankFrames {
41     bool isSetReportEventResponse_ = false;
42     bool isSetReportEventResponseTemp_ = false;
43     bool isSetReportEventComplete_ = false;
44     bool isSetReportEventJankFrame_ = false;
45     bool isReportEventResponse_ = false;
46     bool isReportEventComplete_ = false;
47     bool isReportEventJankFrame_ = false;
48     bool isUpdateJankFrame_ = false;
49     bool isFirstFrame_ = false;
50     bool isFirstFrameTemp_ = false;
51     bool isFrameRateRecorded_ = false;
52     bool isAnimationEnded_ = false;
53     bool isDisplayAnimator_ = false;
54     bool isAnimationInterrupted_ = false;
55     int64_t setTimeSteady_ = TIMESTAMP_INITIAL;
56     int64_t startTime_ = TIMESTAMP_INITIAL;
57     int64_t startTimeSteady_ = TIMESTAMP_INITIAL;
58     int64_t endTimeSteady_ = TIMESTAMP_INITIAL;
59     int64_t lastEndTimeSteady_ = TIMESTAMP_INITIAL;
60     int64_t traceCreateTimeSteady_ = TIMESTAMP_INITIAL;
61     int64_t traceTerminateTimeSteady_ = TIMESTAMP_INITIAL;
62     int64_t maxFrameOccurenceTimeSteady_ = TIMESTAMP_INITIAL;
63     int64_t lastMaxFrameOccurenceTimeSteady_ = TIMESTAMP_INITIAL;
64     int64_t maxHitchOccurenceTimeSteady_ = TIMESTAMP_INITIAL;
65     int64_t lastMaxHitchOccurenceTimeSteady_ = TIMESTAMP_INITIAL;
66     int32_t seqMissedFrames_ = 0;
67     int32_t totalFrames_ = 0;
68     int32_t lastTotalFrames_ = 0;
69     int32_t totalMissedFrames_ = 0;
70     int32_t lastTotalMissedFrames_ = 0;
71     int64_t maxFrameTimeSteady_ = 0;
72     int64_t lastMaxFrameTimeSteady_ = 0;
73     int64_t maxTechFrameTimeSteady_ = 0;
74     int64_t lastMaxTechFrameTimeSteady_ = 0;
75     int64_t maxRealFrameTimeSteady_ = 0;
76     int64_t lastMaxRealFrameTimeSteady_ = 0;
77     int32_t maxSeqMissedFrames_ = 0;
78     int32_t lastMaxSeqMissedFrames_ = 0;
79     int64_t totalFrameTimeSteady_ = 0;
80     int64_t lastTotalFrameTimeSteady_ = 0;
81     int32_t traceId_ = TRACE_ID_INITIAL;
82     int64_t totalFrameTimeSteadyForHTR_ = 0;
83     int64_t lastTotalFrameTimeSteadyForHTR_ = 0;
84     int32_t lastMaxFrameRefreshRate_ = 0;
85     int32_t maxFrameRefreshRate_ = 0;
86     float totalHitchTimeSteady_ = 0;
87     float lastTotalHitchTimeSteady_ = 0;
88     float maxHitchTime_ = 0;
89     float lastMaxHitchTime_ = 0;
90     Rosen::DataBaseRs info_;
91 };
92 
93 struct JankFrameRecordStats {
94     const std::string countTraceName_;
95     const int64_t recordThreshold_;
96     bool isRecorded_ = false;
JankFrameRecordStatsJankFrameRecordStats97     JankFrameRecordStats(const std::string& countTraceName, int64_t recordThreshold)
98         : countTraceName_(countTraceName), recordThreshold_(recordThreshold) {}
99 };
100 
101 struct AnimationTraceStats {
102     std::pair<int64_t, std::string> animationId_ = { -1, "" };
103     std::string traceName_;
104     int64_t traceCreateTimeSteady_ = TIMESTAMP_INITIAL;
105     bool isDisplayAnimator_ = false;
106 };
107 
108 struct TraceIdRemainderStats {
109     int64_t remainder_ = 0;
110     int64_t setTimeSteady_ = TIMESTAMP_INITIAL;
111 };
112 
113 struct JankDurationParams {
114     int64_t timeStart_ = TIMESTAMP_INITIAL;
115     int64_t timeStartSteady_ = TIMESTAMP_INITIAL;
116     float timeStartSteadyFloat_ = TIMESTAMP_INITIAL_FLOAT;
117     int64_t timeEnd_ = TIMESTAMP_INITIAL;
118     int64_t timeEndSteady_ = TIMESTAMP_INITIAL;
119     float timeEndSteadyFloat_ = TIMESTAMP_INITIAL_FLOAT;
120     uint32_t refreshRate_ = 0;
121     bool discardJankFrames_ = false;
122     bool skipJankAnimatorFrame_ = false;
123 };
124 
125 class RSJankStats {
126 public:
127     static RSJankStats& GetInstance();
128     void SetOnVsyncStartTime(int64_t onVsyncStartTime, int64_t onVsyncStartTimeSteady,
129                              float onVsyncStartTimeSteadyFloat);
130     void SetStartTime(bool doDirectComposition = false);
131     void SetEndTime(bool skipJankAnimatorFrame = false, bool discardJankFrames = false,
132                     uint32_t dynamicRefreshRate = STANDARD_REFRESH_RATE,
133                     bool doDirectComposition = false, bool isReportTaskDelayed = false);
134     void HandleDirectComposition(const JankDurationParams& rsParams, bool isReportTaskDelayed);
135     void ReportJankStats();
136     void ReportSceneJankStats(const AppInfo& appInfo);
137     void ReportSceneJankFrame(uint32_t dynamicRefreshRate);
138     void SetReportEventResponse(const DataBaseRs& info);
139     void SetReportEventComplete(const DataBaseRs& info);
140     void SetReportEventJankFrame(const DataBaseRs& info, bool isReportTaskDelayed);
141     void SetReportRsSceneJankStart(const AppInfo& info);
142     void SetReportRsSceneJankEnd(const AppInfo& info);
143     void SetAppFirstFrame(pid_t appPid);
144     void SetImplicitAnimationEnd(bool isImplicitAnimationEnd);
145     void SetAccumulatedBufferCount(int accumulatedBufferCount);
146     bool IsAnimationEmpty();
147 
148 private:
149     RSJankStats() = default;
150     ~RSJankStats() = default;
151     DISALLOW_COPY_AND_MOVE(RSJankStats);
152 
153     void UpdateEndTime();
154     void SetRSJankStats(bool skipJankStats, uint32_t dynamicRefreshRate);
155     size_t GetJankRangeType(int64_t missedVsync) const;
156     void UpdateJankFrame(JankFrames& jankFrames, bool skipJankStats, uint32_t dynamicRefreshRate);
157     void UpdateHitchTime(JankFrames& jankFrames, float standardFrameTime);
158     void ReportEventResponse(const JankFrames& jankFrames) const;
159     void ReportEventComplete(const JankFrames& jankFrames) const;
160     void ReportEventJankFrame(const JankFrames& jankFrames, bool isReportTaskDelayed) const;
161     void ReportEventJankFrameWithoutDelay(const JankFrames& jankFrames) const;
162     void ReportEventJankFrameWithDelay(const JankFrames& jankFrames) const;
163     void GetMaxJankInfo(const JankFrames& jankFrames, bool isReportTaskDelayed,
164         int64_t& maxFrameTimeFromStart, int64_t& maxHitchTimeFromStart, int64_t& duration) const;
165     void ReportEventHitchTimeRatio(const JankFrames& jankFrames, bool isReportTaskDelayed) const;
166     void ReportEventHitchTimeRatioWithoutDelay(const JankFrames& jankFrames) const;
167     void ReportEventHitchTimeRatioWithDelay(const JankFrames& jankFrames) const;
168     void ReportEventFirstFrame();
169     void ReportEventFirstFrameByPid(pid_t appPid) const;
170     void HandleImplicitAnimationEndInAdvance(JankFrames& jankFrames, bool isReportTaskDelayed);
171     void RecordJankFrame(uint32_t dynamicRefreshRate);
172     void RecordJankFrameSingle(int64_t missedFrames, JankFrameRecordStats& recordStats);
173     void RecordAnimationDynamicFrameRate(JankFrames& jankFrames, bool isReportTaskDelayed);
174     void SetAnimationTraceBegin(std::pair<int64_t, std::string> animationId, JankFrames& jankFrames);
175     void SetAnimationTraceEnd(JankFrames& jankFrames);
176     void CheckAnimationTraceTimeout();
177     void ClearAllAnimation();
178     std::string GetSceneDescription(const DataBaseRs& info) const;
179     std::pair<int64_t, std::string> GetAnimationId(const DataBaseRs& info) const;
180     int32_t GetTraceIdInit(const DataBaseRs& info, int64_t setTimeSteady);
181     int64_t ConvertTimeToSystime(int64_t time) const;
182     int64_t GetEffectiveFrameTime(bool isConsiderRsStartTime) const;
183     float GetEffectiveFrameTimeFloat(bool isConsiderRsStartTime) const;
184     int64_t GetCurrentSystimeMs() const;
185     int64_t GetCurrentSteadyTimeMs() const;
186     float GetCurrentSteadyTimeMsFloat() const;
187 
188     static constexpr uint16_t ANIMATION_TRACE_CHECK_FREQ = 20;
189     static constexpr uint32_t JANK_RANGE_VERSION = 1;
190     static constexpr uint32_t JANK_SCENE_RANGE_VERSION = 2;
191     static constexpr size_t JANK_STATS_SIZE = 8;
192     static constexpr int64_t TRACE_ID_SCALE_PARAM = 10;
193     static constexpr int64_t MIN_FRAME_SHOW_TIME = 16;
194     static constexpr int32_t DEFAULT_INT_VALUE = 0;
195     static inline const std::string DEFAULT_STRING_VALUE = "";
196     const int64_t SCENE_JANK_FRAME_THRESHOLD = RSSystemProperties::GetSceneJankFrameThreshold();
197     static constexpr int32_t FILTER_TYPE = 0;
198     static constexpr bool IS_FOLD_DISP = false;
199     static constexpr bool IS_CALCULATE_PRECISE_HITCH_TIME = true;
200     static inline const std::string ACCUMULATED_BUFFER_COUNT_TRACE_NAME = "ACCUMULATED_BUFFER_COUNT";
201     static inline const std::string JANK_FRAME_1_TO_5F_COUNT_TRACE_NAME = "JANK_FRAME_1-5F";
202     static inline const std::string JANK_FRAME_6F_COUNT_TRACE_NAME = "JANK_FRAME_6F";
203     std::vector<JankFrameRecordStats> jankExplicitAnimatorFrameRecorder_{ {"JANK_EXPLICIT_ANIMATOR_FRAME_1F", 1},
204                                                                           {"JANK_EXPLICIT_ANIMATOR_FRAME_2F", 2} };
205     std::vector<JankFrameRecordStats> jankImplicitAnimatorFrameRecorder_{ {"JANK_IMPLICIT_ANIMATOR_FRAME_1F", 1},
206                                                                           {"JANK_IMPLICIT_ANIMATOR_FRAME_2F", 2} };
207     bool isFirstSetStart_ = true;
208     bool isFirstSetEnd_ = true;
209     bool isNeedReportJankStats_ = false;
210     bool isNeedReportSceneJankStats_ = false;
211     bool isLastReportSceneDone_ = true;
212     bool isLastFrameDoDirectComposition_ = false;
213     bool isCurrentFrameSwitchToNotDoDirectComposition_ = false;
214     float rsStartTimeSteadyFloat_ = TIMESTAMP_INITIAL_FLOAT;
215     float rtEndTimeSteadyFloat_ = TIMESTAMP_INITIAL_FLOAT;
216     float rtLastEndTimeSteadyFloat_ = TIMESTAMP_INITIAL_FLOAT;
217     int64_t rsStartTime_ = TIMESTAMP_INITIAL;
218     int64_t rsStartTimeSteady_ = TIMESTAMP_INITIAL;
219     int64_t rtStartTime_ = TIMESTAMP_INITIAL;
220     int64_t rtStartTimeSteady_ = TIMESTAMP_INITIAL;
221     int64_t rtEndTime_ = TIMESTAMP_INITIAL;
222     int64_t rtEndTimeSteady_ = TIMESTAMP_INITIAL;
223     int64_t rtLastEndTime_ = TIMESTAMP_INITIAL;
224     int64_t rtLastEndTimeSteady_ = TIMESTAMP_INITIAL;
225     int64_t lastReportTime_ = TIMESTAMP_INITIAL;
226     int64_t lastReportTimeSteady_ = TIMESTAMP_INITIAL;
227     int64_t lastJankFrame6FreqTimeSteady_ = TIMESTAMP_INITIAL;
228     int64_t lastSceneReportTime_ = TIMESTAMP_INITIAL;
229     int64_t lastSceneReportTimeSteady_ = TIMESTAMP_INITIAL;
230     int64_t lastSceneJankFrame6FreqTimeSteady_ = TIMESTAMP_INITIAL;
231     int32_t explicitAnimationTotal_ = 0;
232     int32_t implicitAnimationTotal_ = 0;
233     uint16_t animationTraceCheckCnt_ = 0;
234     int accumulatedBufferCount_ = 0;
235     std::vector<uint16_t> rsJankStats_ = std::vector<uint16_t>(JANK_STATS_SIZE, 0);
236     std::vector<uint16_t> rsSceneJankStats_ = std::vector<uint16_t>(JANK_STATS_SIZE, 0);
237     std::queue<pid_t> firstFrameAppPids_;
238     std::map<int32_t, AnimationTraceStats> animationAsyncTraces_;
239     std::map<int64_t, TraceIdRemainderStats> traceIdRemainder_;
240     std::map<std::pair<int64_t, std::string>, JankFrames> animateJankFrames_;
241     std::mutex mutex_;
242     Rosen::AppInfo appInfo_;
243 
244     enum JankRangeType : size_t {
245         JANK_FRAME_6_FREQ = 0,
246         JANK_FRAME_15_FREQ,
247         JANK_FRAME_20_FREQ,
248         JANK_FRAME_36_FREQ,
249         JANK_FRAME_48_FREQ,
250         JANK_FRAME_60_FREQ,
251         JANK_FRAME_120_FREQ,
252         JANK_FRAME_180_FREQ,
253         JANK_FRAME_INVALID,
254     };
255 };
256 
257 } // namespace Rosen
258 } // namespace OHOS
259 
260 #endif // ROSEN_JANK_STATS_H
261