• 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 <utility>
25 #include <vector>
26 
27 #include "nocopyable.h"
28 #include "transaction/rs_render_service_client.h"
29 
30 namespace OHOS {
31 namespace Rosen {
32 namespace {
33 constexpr int64_t TIMESTAMP_INITIAL = -1;
34 constexpr int32_t TRACE_ID_INITIAL = -1;
35 
36 struct JankFrames {
37     bool isSetReportEventResponse_ = false;
38     bool isSetReportEventComplete_ = false;
39     bool isSetReportEventJankFrame_ = false;
40     bool isReportEventResponse_ = false;
41     bool isReportEventComplete_ = false;
42     bool isReportEventJankFrame_ = false;
43     bool isUpdateJankFrame_ = false;
44     bool isFirstFrame_ = false;
45     bool isFrameRateRecorded_ = false;
46     bool isAnimationEnded_ = false;
47     bool isDisplayAnimator_ = false;
48     int64_t setTimeSteady_ = TIMESTAMP_INITIAL;
49     int64_t startTimeSteady_ = TIMESTAMP_INITIAL;
50     int64_t endTimeSteady_ = TIMESTAMP_INITIAL;
51     int64_t lastEndTimeSteady_ = TIMESTAMP_INITIAL;
52     int32_t seqMissedFrames_ = 0;
53     int32_t totalFrames_ = 0;
54     int32_t lastTotalFrames_ = 0;
55     int32_t totalMissedFrames_ = 0;
56     int32_t lastTotalMissedFrames_ = 0;
57     int64_t maxFrameTimeSteady_ = 0;
58     int64_t lastMaxFrameTimeSteady_ = 0;
59     int32_t maxSeqMissedFrames_ = 0;
60     int32_t lastMaxSeqMissedFrames_ = 0;
61     int64_t totalFrameTimeSteady_ = 0;
62     int64_t lastTotalFrameTimeSteady_ = 0;
63     int32_t traceId_ = TRACE_ID_INITIAL;
64     Rosen::DataBaseRs info_;
65 };
66 
67 struct JankFrameRecordStats {
68     const std::string countTraceName_;
69     const int64_t recordThreshold_;
70     bool isRecorded_ = false;
JankFrameRecordStatsJankFrameRecordStats71     JankFrameRecordStats(const std::string& countTraceName, int64_t recordThreshold)
72         : countTraceName_(countTraceName), recordThreshold_(recordThreshold) {}
73 };
74 
75 struct AnimationTraceStats {
76     std::pair<int64_t, std::string> animationId_ = { -1, "" };
77     std::string traceName_;
78     int64_t traceCreateTimeSteady_ = TIMESTAMP_INITIAL;
79     bool isDisplayAnimator_ = false;
80 };
81 
82 struct TraceIdRemainderStats {
83     int64_t remainder_ = 0;
84     int64_t setTimeSteady_ = TIMESTAMP_INITIAL;
85 };
86 } // namespace
87 
88 class RSJankStats {
89 public:
90     static RSJankStats& GetInstance();
91     void SetStartTime();
92     void SetEndTime(bool discardJankFrames);
93     void ReportJankStats();
94     void SetReportEventResponse(const DataBaseRs& info);
95     void SetReportEventComplete(const DataBaseRs& info);
96     void SetReportEventJankFrame(const DataBaseRs& info, bool isReportTaskDelayed);
97     void SetAppFirstFrame(pid_t appPid);
98     void SetSkipDisplayNode();
99 
100 private:
101     RSJankStats() = default;
102     ~RSJankStats() = default;
103     DISALLOW_COPY_AND_MOVE(RSJankStats);
104 
105     void UpdateEndTime();
106     void SetRSJankStats();
107     void UpdateJankFrame(JankFrames& jankFrames);
108     void ReportEventResponse(const JankFrames& jankFrames) const;
109     void ReportEventComplete(const JankFrames& jankFrames) const;
110     void ReportEventJankFrame(const JankFrames& jankFrames, bool isReportTaskDelayed) const;
111     void ReportEventFirstFrame();
112     void ReportEventFirstFrameByPid(pid_t appPid) const;
113     void HandleImplicitAnimationEndInAdvance(JankFrames& jankFrames, bool isReportTaskDelayed);
114     void RecordJankFrameInit();
115     void RecordJankFrame();
116     void RecordJankFrameSingle(int64_t missedFrames, JankFrameRecordStats& recordStats);
117     void RecordAnimationDynamicFrameRate(JankFrames& jankFrames, bool isReportTaskDelayed);
118     void SetAnimationTraceBegin(std::pair<int64_t, std::string> animationId, const JankFrames& jankFrames);
119     void SetAnimationTraceEnd(const JankFrames& jankFrames);
120     void CheckAnimationTraceTimeout();
121     void ClearAllAnimation();
122     std::string GetSceneDescription(const DataBaseRs& info) const;
123     std::pair<int64_t, std::string> GetAnimationId(const DataBaseRs& info) const;
124     int32_t GetTraceIdInit(const DataBaseRs& info, int64_t setTimeSteady);
125     int64_t ConvertTimeToSystime(int64_t time) const;
126     int64_t GetCurrentSystimeMs() const;
127     int64_t GetCurrentSteadyTimeMs() const;
128 
129     static constexpr uint16_t ANIMATION_TRACE_CHECK_FREQ = 20;
130     static constexpr uint32_t JANK_RANGE_VERSION = 1;
131     static constexpr size_t JANK_STATS_SIZE = 8;
132     static constexpr int64_t TRACE_ID_SCALE_PARAM = 10;
133     static constexpr bool IS_FOLD_DISP = false;
134     static inline const std::string JANK_FRAME_6F_COUNT_TRACE_NAME = "JANK_FRAME_6F";
135     std::vector<JankFrameRecordStats> jankExplicitAnimatorFrameRecorder_{ {"JANK_EXPLICIT_ANIMATOR_FRAME_2F", 2} };
136     std::vector<JankFrameRecordStats> jankImplicitAnimatorFrameRecorder_{ {"JANK_IMPLICIT_ANIMATOR_FRAME_2F", 2} };
137     bool isFirstSetStart_ = true;
138     bool isFirstSetEnd_ = true;
139     bool isNeedReportJankStats_ = false;
140     bool isSkipDisplayNode_ = false;
141     int64_t startTime_ = TIMESTAMP_INITIAL;
142     int64_t startTimeSteady_ = TIMESTAMP_INITIAL;
143     int64_t endTime_ = TIMESTAMP_INITIAL;
144     int64_t endTimeSteady_ = TIMESTAMP_INITIAL;
145     int64_t lastEndTime_ = TIMESTAMP_INITIAL;
146     int64_t lastEndTimeSteady_ = TIMESTAMP_INITIAL;
147     int64_t lastReportTime_ = TIMESTAMP_INITIAL;
148     int64_t lastReportTimeSteady_ = TIMESTAMP_INITIAL;
149     int64_t lastJankFrame6FreqTimeSteady_ = TIMESTAMP_INITIAL;
150     int32_t explicitAnimationTotal_ = 0;
151     int32_t implicitAnimationTotal_ = 0;
152     uint16_t animationTraceCheckCnt_ = 0;
153     std::vector<uint16_t> rsJankStats_ = std::vector<uint16_t>(JANK_STATS_SIZE, 0);
154     std::queue<pid_t> firstFrameAppPids_;
155     std::map<int32_t, AnimationTraceStats> animationAsyncTraces_;
156     std::map<int64_t, TraceIdRemainderStats> traceIdRemainder_;
157     std::map<std::pair<int64_t, std::string>, JankFrames> animateJankFrames_;
158     std::mutex mutex_;
159 
160     enum JankRangeType : size_t {
161         JANK_FRAME_6_FREQ = 0,
162         JANK_FRAME_15_FREQ,
163         JANK_FRAME_20_FREQ,
164         JANK_FRAME_36_FREQ,
165         JANK_FRAME_48_FREQ,
166         JANK_FRAME_60_FREQ,
167         JANK_FRAME_120_FREQ,
168         JANK_FRAME_180_FREQ,
169         JANK_FRAME_INVALID,
170     };
171 };
172 
173 } // namespace Rosen
174 } // namespace OHOS
175 
176 #endif // ROSEN_JANK_STATS_H
177