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 maxFrameTime {0}; 67 int32_t maxSuccessiveFrames {0}; 68 int32_t totalMissed {0}; 69 int32_t totalFrames {0}; 70 int64_t inputTime {0}; 71 int64_t beginVsyncTime {0}; 72 int64_t endVsyncTime {0}; 73 bool needReportToRS {false}; 74 PerfSourceType sourceType {UNKNOWN_SOURCE}; 75 PerfActionType actionType {UNKNOWN_ACTION}; 76 PerfEventType eventType {UNKNOWN_EVENT}; 77 BaseInfo baseInfo; 78 }; 79 80 void ConvertRealtimeToSystime(int64_t realTime, int64_t& sysTime); 81 std::string GetSourceTypeName(PerfSourceType sourceType); 82 83 class SceneRecord { 84 public: 85 void InitRecord(const std::string& sId, PerfActionType aType, PerfSourceType sType, const std::string& nt); 86 void RecordFrame(int64_t vsyncTime, int64_t duration, int32_t skippedFrames); 87 void Report(const std::string& sceneId, int64_t vsyncTime); 88 bool IsTimeOut(int64_t nowTime); 89 bool IsFirstFrame(); 90 void Reset(); 91 public: 92 int64_t beginVsyncTime {0}; 93 int64_t endVsyncTime {0}; 94 int32_t maxFrameTime {0}; 95 int32_t maxSuccessiveFrames {0}; 96 int32_t totalMissed {0}; 97 int32_t totalFrames {0}; 98 int32_t seqMissFrames {0}; 99 bool isSuccessive {false}; 100 bool isFirstFrame {false}; 101 std::string sceneId {""}; 102 PerfActionType actionType {UNKNOWN_ACTION}; 103 PerfSourceType sourceType {UNKNOWN_SOURCE}; 104 std::string note {""}; 105 }; 106 107 class ACE_FORCE_EXPORT PerfMonitor { 108 public: 109 void Start(const std::string& sceneId, PerfActionType type, const std::string& note); 110 void End(const std::string& sceneId, bool isJsApi); 111 void RecordInputEvent(PerfActionType type, PerfSourceType sourceType, int64_t time); 112 int64_t GetInputTime(PerfActionType type); 113 void SetFrameTime(int64_t vsyncTime, int64_t duration, double jank); 114 void SetPageUrl(const std::string& pageUrl); 115 std::string GetPageUrl(); 116 static PerfMonitor* GetPerfMonitor(); 117 static PerfMonitor* pMonitor; 118 119 private: 120 SceneRecord* GetRecord(const std::string& sceneId); 121 void RemoveRecord(const std::string& sceneId); 122 void ReportAnimateStart(const std::string& sceneId, SceneRecord* record); 123 void ReportAnimateEnd(const std::string& sceneId, SceneRecord* record, bool needCompleteTime); 124 void FlushDataBase(SceneRecord* record, DataBase& data, bool needCompleteTime); 125 void ReportPerfEvent(PerfEventType type, DataBase& data); 126 void RecordBaseInfo(SceneRecord* record); 127 private: 128 std::map<PerfActionType, int64_t> mInputTime; 129 int64_t mVsyncTime {0}; 130 PerfSourceType mSourceType {UNKNOWN_SOURCE}; 131 BaseInfo baseInfo; 132 mutable std::mutex mMutex; 133 std::map<std::string, SceneRecord*> mRecords; 134 }; 135 } // namespace OHOS::Ace 136 #endif // ARKUI_PERF_MONITOR_H 137