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 "wm_single_instance.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 30 enum class WindowDFXHelperType : uint32_t { 31 WINDOW_RECT_CHECK = 1, 32 WINDOW_ZORDER_CHECK, 33 WINDOW_FOCUS_CHECK, 34 WINDOW_TRANSPARENT_CHECK, 35 WINDOW_MODAL_UIEXTENSION_UICONTENT_CHECK, 36 WINDOW_MODAL_UIEXTENSION_SUBWINDOW_CHECK, 37 WINDOW_UIEXTENSION_TRANSFER_DATA_FAIL, 38 WINDOW_UIEXTENSION_START_ABILITY_FAIL, 39 WINDOW_FLUSH_EMPTY_DISPLAY_INFO_TO_MMI_EXCEPTION, 40 WINDOW_FLUSH_EMPTY_WINDOW_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 }; 50 51 class PerformReporter { 52 public: 53 PerformReporter(const std::string& tag, const std::vector<int64_t>& timeSpiltsMs, uint32_t reportInterval = 50); 54 void start(); 55 void end(); 56 57 private: 58 void count(int64_t costTime); 59 bool report(); 60 void clear(); 61 62 std::string tag_; 63 std::atomic<uint32_t> totalCount_; 64 std::map<int64_t, std::atomic<uint32_t>> timeSplitCount_; 65 std::chrono::steady_clock::time_point startTime_; 66 uint32_t reportInterval_; 67 68 static constexpr auto BARRIER = std::numeric_limits<int64_t>::max(); 69 }; 70 71 // the map form : <bundleName, <abilityName, count>> 72 using FullInfoMap = std::map<std::string, std::map<std::string, uint32_t>>; 73 // the map form : <bundleName, count> 74 using BundleNameMap = std::map<std::string, uint32_t>; 75 class WindowInfoReporter { 76 WM_DECLARE_SINGLE_INSTANCE(WindowInfoReporter); 77 78 public: 79 void InsertCreateReportInfo(const std::string& bundleName); 80 void InsertShowReportInfo(const std::string& bundleName); 81 void InsertHideReportInfo(const std::string& bundleName); 82 void InsertDestroyReportInfo(const std::string& bundleName); 83 84 void InsertRecentReportInfo(const std::string& bundleName, const std::string& packageName); 85 void InsertNavigationBarReportInfo(const std::string& bundleName, const std::string& packageName); 86 87 void ReportBackButtonInfoImmediately(); 88 void ReportZeroOpacityInfoImmediately(const std::string& bundleName, const std::string& packageName); 89 // report when application startup request window 90 void ReportStartWindow(const std::string& bundleName, const std::string& windowName); 91 void ReportRecordedInfos(); 92 void ReportContainerStartBegin(int32_t missionId, const std::string& bundleName, int64_t timestamp); 93 int32_t ReportWindowProfileInfo(const WindowProfileInfo& windowProfileInfo); 94 void ReportWindowException(int32_t detectionType, int32_t pid, const std::string& windowInfo); 95 int32_t ReportUIExtensionException(int32_t exceptionType, int32_t pid, int32_t persistentId, 96 const std::string& uiextInfo); 97 int32_t ReportEventDispatchException(int32_t exceptionType, int32_t pid, const std::string& flushInfo); 98 99 private: 100 void UpdateReportInfo(FullInfoMap& infoMap, const std::string& bundleName, 101 const std::string& packageName); 102 void UpdateReportInfo(BundleNameMap& infoMap, const std::string& bundleName); 103 std::string GetMsgString(const FullInfoMap& infoMap); 104 std::string GetMsgString(const BundleNameMap& infoMap); 105 106 void Report(const std::string& reportTag, const std::string& msg); 107 void ClearRecordedInfos(); 108 109 BundleNameMap windowCreateReportInfos_; 110 BundleNameMap windowShowReportInfos_; 111 BundleNameMap windowHideReportInfos_; 112 BundleNameMap windowDestoryReportInfos_; 113 FullInfoMap windowRecentReportInfos_; 114 FullInfoMap windowNavigationBarReportInfos_; 115 116 std::mutex mtx_; 117 }; 118 } 119 } 120 #endif