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 23 #include "base/utils/macros.h" 24 25 namespace OHOS::Ace { 26 constexpr int32_t US_TO_MS = 1000; 27 constexpr int32_t NS_TO_MS = 1000000; 28 constexpr int32_t NS_TO_S = 1000000000; 29 30 enum PerfActionType { 31 UNKNOWN_ACTION = -1, 32 LAST_DOWN = 0, 33 LAST_UP = 1, 34 FIRST_MOVE = 2 35 }; 36 37 enum PerfSourceType { 38 UNKNOWN_SOURCE = -1, 39 PERF_TOUCH_EVENT = 0, 40 PERF_MOUSE_EVENT = 1, 41 PERF_TOUCH_PAD = 2, 42 PERF_JOY_STICK = 3, 43 PERF_KEY_EVENT = 4 44 }; 45 46 enum PerfEventType { 47 UNKNOWN_EVENT = -1, 48 EVENT_RESPONSE = 0, 49 EVENT_COMPLETE = 1, 50 EVENT_JANK_FRAME = 2 51 }; 52 53 struct BaseInfo { 54 int32_t pid {-1}; 55 int32_t versionCode {0}; 56 std::string versionName {""}; 57 std::string bundleName {""}; 58 std::string processName {""}; 59 std::string abilityName {""}; 60 std::string pageUrl {""}; 61 std::string note {""}; 62 }; 63 64 struct DataBase { 65 std::string sceneId {""}; 66 int32_t maxSuccessiveFrames {0}; 67 int32_t totalMissed {0}; 68 int32_t totalFrames {0}; 69 int64_t inputTime {0}; 70 int64_t beginVsyncTime {0}; 71 int64_t endVsyncTime {0}; 72 int64_t maxFrameTime {0}; 73 bool needReportRs {false}; 74 bool isDisplayAnimator {false}; 75 PerfSourceType sourceType {UNKNOWN_SOURCE}; 76 PerfActionType actionType {UNKNOWN_ACTION}; 77 PerfEventType eventType {UNKNOWN_EVENT}; 78 BaseInfo baseInfo; 79 }; 80 81 struct JankInfo { 82 int64_t skippedFrameTime {0}; 83 std::string windowName {""}; 84 BaseInfo baseInfo; 85 }; 86 87 void ConvertRealtimeToSystime(int64_t realTime, int64_t& sysTime); 88 std::string GetSourceTypeName(PerfSourceType sourceType); 89 90 class SceneRecord { 91 public: 92 void InitRecord(const std::string& sId, PerfActionType aType, PerfSourceType sType, const std::string& nt, 93 int64_t time); 94 void RecordFrame(int64_t vsyncTime, int64_t duration, int32_t skippedFrames); 95 void Report(const std::string& sceneId, int64_t vsyncTime, bool isRsRender); 96 bool IsTimeOut(int64_t nowTime); 97 bool IsFirstFrame(); 98 bool IsDisplayAnimator(const std::string& sceneId); 99 void Reset(); 100 public: 101 int64_t inputTime {0}; 102 int64_t beginVsyncTime {0}; 103 int64_t endVsyncTime {0}; 104 int64_t maxFrameTime {0}; 105 int32_t maxSuccessiveFrames {0}; 106 int32_t totalMissed {0}; 107 int32_t totalFrames {0}; 108 int32_t seqMissFrames {0}; 109 bool isSuccessive {false}; 110 bool isFirstFrame {false}; 111 bool needReportRs {false}; 112 bool isDisplayAnimator {false}; 113 std::string sceneId {""}; 114 PerfActionType actionType {UNKNOWN_ACTION}; 115 PerfSourceType sourceType {UNKNOWN_SOURCE}; 116 std::string note {""}; 117 }; 118 119 class ACE_FORCE_EXPORT PerfMonitor { 120 public: 121 void Start(const std::string& sceneId, PerfActionType type, const std::string& note); 122 void End(const std::string& sceneId, bool isRsRender); 123 void RecordInputEvent(PerfActionType type, PerfSourceType sourceType, int64_t time); 124 int64_t GetInputTime(const std::string& sceneId, PerfActionType type, const std::string& note); 125 void SetFrameTime(int64_t vsyncTime, int64_t duration, double jank, const std::string& windowName); 126 void ReportJankFrameApp(double jank); 127 void SetPageUrl(const std::string& pageUrl); 128 std::string GetPageUrl(); 129 void SetAppForeground(bool isShow); 130 void SetAppStartStatus(); 131 static PerfMonitor* GetPerfMonitor(); 132 static PerfMonitor* pMonitor; 133 134 private: 135 SceneRecord* GetRecord(const std::string& sceneId); 136 void RemoveRecord(const std::string& sceneId); 137 void ReportAnimateStart(const std::string& sceneId, SceneRecord* record); 138 void ReportAnimateEnd(const std::string& sceneId, SceneRecord* record); 139 void FlushDataBase(SceneRecord* record, DataBase& data); 140 void ReportPerfEvent(PerfEventType type, DataBase& data); 141 void RecordBaseInfo(SceneRecord* record); 142 bool IsExceptResponseTime(int64_t time, const std::string& sceneId); 143 private: 144 std::map<PerfActionType, int64_t> mInputTime; 145 int64_t mVsyncTime {0}; 146 PerfSourceType mSourceType {UNKNOWN_SOURCE}; 147 BaseInfo baseInfo; 148 mutable std::mutex mMutex; 149 std::map<std::string, SceneRecord*> mRecords; 150 151 // for jank frame app 152 bool isResponseExclusion {false}; 153 bool isStartAppFrame {false}; 154 bool isBackgroundApp {false}; 155 bool isExclusionWindow {false}; 156 int64_t startAppTime {0}; 157 bool IsExclusionFrame(); 158 void CheckInStartAppStatus(); 159 void CheckExclusionWindow(const std::string& windowName); 160 void CheckResponseStatus(); 161 void ProcessJank(double jank, const std::string& windowName); 162 void ReportJankFrame(double jank, const std::string& windowName); 163 }; 164 } // namespace OHOS::Ace 165 #endif // ARKUI_PERF_MONITOR_H 166