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 };
38 
39 struct WindowProfileInfo {
40     std::string bundleName = "";
41     int32_t windowVisibleState = -1;
42     int32_t windowLocatedScreen = -1;
43     int32_t windowSceneMode = -1;
44 };
45 
46 class PerformReporter {
47 public:
48     PerformReporter(const std::string& tag, const std::vector<int64_t>& timeSpiltsMs, uint32_t reportInterval = 50);
49     void start();
50     void end();
51 
52 private:
53     void count(int64_t costTime);
54     bool report();
55     void clear();
56 
57     std::string tag_;
58     std::atomic<uint32_t> totalCount_;
59     std::map<int64_t, std::atomic<uint32_t>> timeSplitCount_;
60     std::chrono::steady_clock::time_point startTime_;
61     uint32_t reportInterval_;
62 
63     static constexpr auto BARRIER = std::numeric_limits<int64_t>::max();
64 };
65 
66 // the map form : <bundleName, <abilityName, count>>
67 using FullInfoMap = std::map<std::string, std::map<std::string, uint32_t>>;
68 // the map form : <bundleName, count>
69 using BundleNameMap = std::map<std::string, uint32_t>;
70 class WindowInfoReporter {
71 WM_DECLARE_SINGLE_INSTANCE(WindowInfoReporter);
72 
73 public:
74     void InsertCreateReportInfo(const std::string& bundleName);
75     void InsertShowReportInfo(const std::string& bundleName);
76     void InsertHideReportInfo(const std::string& bundleName);
77     void InsertDestroyReportInfo(const std::string& bundleName);
78 
79     void InsertRecentReportInfo(const std::string& bundleName, const std::string& packageName);
80     void InsertNavigationBarReportInfo(const std::string& bundleName, const std::string& packageName);
81 
82     void ReportBackButtonInfoImmediately();
83     void ReportZeroOpacityInfoImmediately(const std::string& bundleName, const std::string& packageName);
84     // report when application startup request window
85     void ReportStartWindow(const std::string& bundleName, const std::string& windowName);
86     void ReportRecordedInfos();
87     void ReportContainerStartBegin(int32_t missionId, const std::string& bundleName, int64_t timestamp);
88     int32_t ReportWindowProfileInfo(const WindowProfileInfo& windowProfileInfo);
89     void ReportWindowException(int32_t detectionType, int32_t pid, const std::string& windowInfo);
90 
91 private:
92     void UpdateReportInfo(FullInfoMap& infoMap, const std::string& bundleName,
93         const std::string& packageName);
94     void UpdateReportInfo(BundleNameMap& infoMap, const std::string& bundleName);
95     std::string GetMsgString(const FullInfoMap& infoMap);
96     std::string GetMsgString(const BundleNameMap& infoMap);
97 
98     void Report(const std::string& reportTag, const std::string& msg);
99     void ClearRecordedInfos();
100 
101     BundleNameMap windowCreateReportInfos_;
102     BundleNameMap windowShowReportInfos_;
103     BundleNameMap windowHideReportInfos_;
104     BundleNameMap windowDestoryReportInfos_;
105     FullInfoMap windowRecentReportInfos_;
106     FullInfoMap windowNavigationBarReportInfos_;
107 
108     std::mutex mtx_;
109 };
110 }
111 }
112 #endif