1 /* 2 * Copyright (c) 2022 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 OHOS_ROSEN_WM_UTILS_PERFORM_REPORTER_H 17 #define OHOS_ROSEN_WM_UTILS_PERFORM_REPORTER_H 18 19 #include <atomic> 20 #include <chrono> 21 #include <string> 22 #include <vector> 23 #include <map> 24 #include <mutex> 25 #include <sstream> 26 #include "wm_single_instance.h" 27 28 namespace OHOS { 29 namespace Rosen { 30 31 enum class WindowDFXHelperType : uint32_t { 32 WINDOW_RECT_CHECK = 1, 33 WINDOW_ZORDER_CHECK, 34 WINDOW_FOCUS_CHECK, 35 WINDOW_TRANSPARENT_CHECK, 36 WINDOW_MODAL_UIEXTENSION_UICONTENT_CHECK, 37 WINDOW_MODAL_UIEXTENSION_SUBWINDOW_CHECK, 38 WINDOW_UIEXTENSION_TRANSFER_DATA_FAIL, 39 WINDOW_UIEXTENSION_START_ABILITY_FAIL, 40 WINDOW_FLUSH_EMPTY_DISPLAY_INFO_TO_MMI_EXCEPTION, 41 WINDOW_CREATE_SUB_WINDOW_FAILED, 42 }; 43 44 struct WindowProfileInfo { 45 std::string bundleName = ""; 46 int32_t windowVisibleState = -1; 47 int32_t windowLocatedScreen = -1; 48 int32_t windowSceneMode = -1; 49 std::string rect = ""; 50 int32_t zorder = -1; 51 }; 52 53 enum class KeyboardLifeCycleException { 54 ANIM_SYNC_EXCEPTION, 55 CREATE_EXCEPTION, 56 }; 57 58 const std::map<KeyboardLifeCycleException, std::string> KEYBOARD_LIFE_CYCLE_EXCEPTION_MAP = { 59 {KeyboardLifeCycleException::ANIM_SYNC_EXCEPTION, "ANIM_SYNC_EXCEPTION"}, 60 {KeyboardLifeCycleException::CREATE_EXCEPTION, "CREATE_EXCEPTION"} 61 }; 62 63 struct WindowLifeCycleReportInfo { 64 std::string bundleName; 65 int32_t windowId; 66 int32_t windowType; 67 int32_t windowMode; 68 int32_t windowFlag; 69 std::string timeoutStage; 70 ToStringWindowLifeCycleReportInfo71 inline std::string ToString() const 72 { 73 std::stringstream ss; 74 ss << "[bundleName:" << bundleName << ", id:" << windowId << ", type:" << windowType << ", windowMode:" << 75 windowMode << ", flag:" << windowFlag << "]"; 76 return ss.str(); 77 } 78 }; 79 80 class PerformReporter { 81 public: 82 PerformReporter(const std::string& tag, const std::vector<int64_t>& timeSpiltsMs, uint32_t reportInterval = 50); 83 void start(); 84 void end(); 85 86 private: 87 void count(int64_t costTime); 88 bool report(); 89 void clear(); 90 91 std::string tag_; 92 std::atomic<uint32_t> totalCount_; 93 std::map<int64_t, std::atomic<uint32_t>> timeSplitCount_; 94 std::chrono::steady_clock::time_point startTime_; 95 uint32_t reportInterval_; 96 97 static constexpr auto BARRIER = std::numeric_limits<int64_t>::max(); 98 }; 99 100 // the map form : <bundleName, <abilityName, count>> 101 using FullInfoMap = std::map<std::string, std::map<std::string, uint32_t>>; 102 // the map form : <bundleName, count> 103 using BundleNameMap = std::map<std::string, uint32_t>; 104 class WindowInfoReporter { 105 WM_DECLARE_SINGLE_INSTANCE(WindowInfoReporter); 106 107 public: 108 void InsertCreateReportInfo(const std::string& bundleName); 109 void InsertShowReportInfo(const std::string& bundleName); 110 void InsertHideReportInfo(const std::string& bundleName); 111 void InsertDestroyReportInfo(const std::string& bundleName); 112 113 void InsertRecentReportInfo(const std::string& bundleName, const std::string& packageName); 114 void InsertNavigationBarReportInfo(const std::string& bundleName, const std::string& packageName); 115 116 void ReportBackButtonInfoImmediately(); 117 void ReportZeroOpacityInfoImmediately(const std::string& bundleName, const std::string& packageName); 118 // report when application startup request window 119 void ReportStartWindow(const std::string& bundleName, const std::string& windowName); 120 void ReportRecordedInfos(); 121 void ReportContainerStartBegin(int32_t missionId, const std::string& bundleName, int64_t timestamp); 122 int32_t ReportWindowProfileInfo(const WindowProfileInfo& windowProfileInfo); 123 void ReportWindowException(int32_t detectionType, int32_t pid, const std::string& windowInfo); 124 int32_t ReportUIExtensionException(int32_t exceptionType, int32_t pid, int32_t persistentId, 125 const std::string& uiextInfo); 126 int32_t ReportEventDispatchException(int32_t exceptionType, int32_t pid, const std::string& flushInfo); 127 int32_t ReportKeyboardLifeCycleException(int32_t windowId, KeyboardLifeCycleException subType, 128 const std::string& msg); 129 int32_t ReportSpecWindowLifeCycleChange(WindowLifeCycleReportInfo reportInfo); 130 131 private: 132 void UpdateReportInfo(FullInfoMap& infoMap, const std::string& bundleName, 133 const std::string& packageName); 134 void UpdateReportInfo(BundleNameMap& infoMap, const std::string& bundleName); 135 std::string GetMsgString(const FullInfoMap& infoMap); 136 std::string GetMsgString(const BundleNameMap& infoMap); 137 138 void Report(const std::string& reportTag, const std::string& msg); 139 void ClearRecordedInfos(); 140 141 BundleNameMap windowCreateReportInfos_; 142 BundleNameMap windowShowReportInfos_; 143 BundleNameMap windowHideReportInfos_; 144 BundleNameMap windowDestoryReportInfos_; 145 FullInfoMap windowRecentReportInfos_; 146 FullInfoMap windowNavigationBarReportInfos_; 147 148 std::mutex mtx_; 149 }; 150 } 151 } 152 #endif