• 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 ARKUI_PERF_MONITOR_H
17 #define ARKUI_PERF_MONITOR_H
18 
19 #include <mutex>
20 #include <string>
21 #include <map>
22 #include <vector>
23 #include <algorithm>
24 #include <memory>
25 
26 #include "base/utils/macros.h"
27 #include "base/utils/aps_monitor.h"
28 #include "core/common/window_animation_config.h"
29 
30 namespace OHOS::Ace {
31 constexpr int32_t US_TO_MS = 1000;
32 constexpr int32_t NS_TO_MS = 1000000;
33 constexpr int32_t NS_TO_S = 1000000000;
34 constexpr uint32_t JANK_STATS_VERSION = 2;
35 constexpr char DEFAULT_SCENE_ID[] = "NONE_ANIMATION";
36 
37 enum PerfActionType {
38     UNKNOWN_ACTION = -1,
39     LAST_DOWN = 0,
40     LAST_UP = 1,
41     FIRST_MOVE = 2
42 };
43 
44 enum PerfSourceType {
45     UNKNOWN_SOURCE = -1,
46     PERF_TOUCH_EVENT = 0,
47     PERF_MOUSE_EVENT = 1,
48     PERF_TOUCH_PAD = 2,
49     PERF_JOY_STICK = 3,
50     PERF_KEY_EVENT = 4
51 };
52 
53 enum PerfEventType {
54     UNKNOWN_EVENT = -1,
55     EVENT_RESPONSE = 0,
56     EVENT_COMPLETE = 1,
57     EVENT_JANK_FRAME = 2
58 };
59 
60 struct BaseInfo {
61     int32_t pid {-1};
62     int32_t versionCode {0};
63     std::string versionName {""};
64     std::string bundleName {""};
65     std::string processName {""};
66     std::string abilityName {""};
67     std::string pageUrl {""};
68     std::string pageName {""};
69     std::string note {""};
70 };
71 
72 struct DataBase {
73     std::string sceneId {""};
74     int32_t maxSuccessiveFrames {0};
75     int32_t totalMissed {0};
76     int32_t totalFrames {0};
77     int64_t inputTime {0};
78     int64_t beginVsyncTime {0};
79     int64_t endVsyncTime {0};
80     int64_t maxFrameTime {0};
81     int64_t maxFrameTimeSinceStart {0};
82     int64_t maxHitchTime {0};
83     int64_t maxHitchTimeSinceStart {0};
84     bool needReportRs {false};
85     bool isDisplayAnimator {false};
86     PerfSourceType sourceType {UNKNOWN_SOURCE};
87     PerfActionType actionType {UNKNOWN_ACTION};
88     PerfEventType eventType {UNKNOWN_EVENT};
89     BaseInfo baseInfo;
90 };
91 
92 struct JankInfo {
93     int64_t skippedFrameTime {0};
94     std::string windowName {""};
95     std::string sceneId {""};
96     int32_t filterType {0};
97     int64_t realSkippedFrameTime {0};
98     BaseInfo baseInfo;
99 };
100 
101 void ConvertRealtimeToSystime(int64_t realTime, int64_t& sysTime);
102 std::string GetSourceTypeName(PerfSourceType sourceType);
103 std::string ParsePageUrl(const std::string& pagePath);
104 
105 class SceneRecord {
106 public:
107     void InitRecord(const std::string& sId, PerfActionType aType, PerfSourceType sType, const std::string& nt,
108         int64_t time);
109     void RecordFrame(int64_t vsyncTime, int64_t duration, int32_t skippedFrames);
110     void Report(const std::string& sceneId, int64_t vsyncTime, bool isRsRender);
111     bool IsTimeOut(int64_t nowTime);
112     bool IsFirstFrame();
113     bool IsDisplayAnimator(const std::string& sceneId);
114     void Reset();
115 public:
116     int64_t inputTime {0};
117     int64_t beginVsyncTime {0};
118     int64_t endVsyncTime {0};
119     int64_t  maxFrameTime {0};
120     int64_t maxFrameTimeSinceStart {0};
121     int64_t maxHitchTime {0};
122     int64_t maxHitchTimeSinceStart {0};
123     int32_t maxSuccessiveFrames {0};
124     int32_t totalMissed {0};
125     int32_t totalFrames {0};
126     int32_t seqMissFrames {0};
127     bool isSuccessive {false};
128     bool isFirstFrame {false};
129     bool needReportRs {false};
130     bool isDisplayAnimator {false};
131     std::string sceneId {""};
132     PerfActionType actionType {UNKNOWN_ACTION};
133     PerfSourceType sourceType {UNKNOWN_SOURCE};
134     std::string note {""};
135 };
136 
137 class ACE_FORCE_EXPORT PerfMonitor {
138 public:
139     void Start(const std::string& sceneId, PerfActionType type, const std::string& note);
140     void End(const std::string& sceneId, bool isRsRender);
141     void StartCommercial(const std::string& sceneId, PerfActionType type, const std::string& note);
142     void EndCommercial(const std::string& sceneId, bool isRsRender);
143     void RecordInputEvent(PerfActionType type, PerfSourceType sourceType, int64_t time);
144     int64_t GetInputTime(const std::string& sceneId, PerfActionType type, const std::string& note);
145     void SetFrameTime(int64_t vsyncTime, int64_t duration, double jank, const std::string& windowName);
146     void ReportJankFrameApp(double jank);
147     void SetPageUrl(const std::string& pageUrl);
148     std::string GetPageUrl();
149     void SetPageName(const std::string& pageName);
150     std::string GetPageName();
151     void SetAppForeground(bool isShow);
152     void SetAppStartStatus();
153     static PerfMonitor* GetPerfMonitor();
154     static PerfMonitor* pMonitor;
155     void SetApsMonitor(const std::shared_ptr<ApsMonitor>& apsMonitor);
156     void ReportPageShowMsg(const std::string& pageUrl, const std::string& bundleName,
157                            const std::string& pageName);
158     void RecordWindowRectResize(OHOS::Ace::WindowSizeChangeReason reason,
159                            const std::string& bundleName);
160     void NotifyAppJankStatsBegin();
161     void NotifyAppJankStatsEnd();
162 
163 private:
164     SceneRecord* GetRecord(const std::string& sceneId);
165     void RemoveRecord(const std::string& sceneId);
166     void ReportAnimateStart(const std::string& sceneId, SceneRecord* record);
167     void ReportAnimateEnd(const std::string& sceneId, SceneRecord* record);
168     void FlushDataBase(SceneRecord* record, DataBase& data);
169     void ReportPerfEvent(PerfEventType type, DataBase& data);
170     void RecordBaseInfo(SceneRecord* record);
171     bool IsExceptResponseTime(int64_t time, const std::string& sceneId);
172     int32_t GetFilterType() const;
173     void ClearJankFrameRecord();
174     void JankFrameStatsRecord(double jank);
175     void NotifySbdJankStatsBegin(const std::string& sceneId);
176     void NotifySdbJankStatsEnd(const std::string& sceneId);
177     void ReportJankStatsApp(int64_t duration);
178     void NotifyRsJankStatsBegin();
179     void NotifyRsJankStatsEnd(int64_t endTime);
180 private:
181     std::shared_ptr<ApsMonitor> apsMonitor_ = nullptr;
182     std::map<PerfActionType, int64_t> mInputTime;
183     int64_t mVsyncTime {0};
184     PerfSourceType mSourceType {UNKNOWN_SOURCE};
185     BaseInfo baseInfo;
186     mutable std::mutex mMutex;
187     std::map<std::string, SceneRecord*> mRecords;
188 
189     // for jank frame app
190     bool isResponseExclusion {false};
191     bool isStartAppFrame {false};
192     bool isBackgroundApp {false};
193     bool isExclusionWindow {false};
194     int64_t startAppTime {0};
195     std::string currentSceneId {""};
196     std::vector<uint16_t> jankFrameRecord;
197     int64_t jankFrameRecordBeginTime;
198     bool isStats = {false};
199     int32_t jankFrameTotalCount {0};
200     // filter common discarded frames in white list
201     bool isExceptAnimator {false};
202     bool IsSceneIdInSceneWhiteList(const std::string& sceneId);
203     bool IsScrollJank(const std::string& sceneId);
204     void CheckTimeOutOfExceptAnimatorStatus(const std::string& sceneId);
205     bool IsExclusionFrame();
206     void SetVsyncLazyMode();
207     void CheckInStartAppStatus();
208     void CheckExclusionWindow(const std::string& windowName);
209     void CheckResponseStatus();
210     void ProcessJank(double jank, const std::string& windowName);
211     void ReportJankFrame(double jank, const std::string& windowName);
212 };
213 } // namespace OHOS::Ace
214 #endif // ARKUI_PERF_MONITOR_H
215