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 FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_PERFORMANCE_CHECK_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_PERFORMANCE_CHECK_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "base/json/json_util.h"
27 #include "base/utils/macros.h"
28 #include "base/utils/noncopyable.h"
29 #include "bridge/common/utils/source_map.h"
30 
31 namespace OHOS::Ace {
32 struct PerformanceCheckNode {
33     int32_t pageDepth = 0;      // node depth
34     int32_t childrenSize = 0;   // node children size
35     int32_t codeRow = 0;        // js code row
36     int32_t codeCol = 0;        // js code col
37     int64_t layoutTime = 0;     // node layout time
38     int32_t flexLayouts = 0;    // node flex layout times
39     bool isForEachItem = false; // foreach item
40     int32_t foreachItems = 0;   // foreach item count
41     std::string nodeTag;        // node tag
42 };
43 
44 struct CodeInfo {
45     int32_t row = 0;     // ets code row
46     int32_t col = 0;     // ets code col
47     std::string sources; // ets page
48 };
49 
50 using PerformanceCheckNodeMap = std::unordered_map<int32_t, PerformanceCheckNode>;
51 
52 class ACE_EXPORT AcePerformanceCheck final {
53 public:
54     static void Start();
55     static void Stop();
56 
57 private:
58     AcePerformanceCheck() = default;
59     ~AcePerformanceCheck() = default;
60 
61     static std::unique_ptr<JsonValue> performanceInfo_;
62 
63     friend class AceScopedPerformanceCheck;
64     ACE_DISALLOW_COPY_AND_MOVE(AcePerformanceCheck);
65 };
66 
67 class ACE_EXPORT AceScopedPerformanceCheck final {
68 public:
69     explicit AceScopedPerformanceCheck(const std::string& name);
70     ~AceScopedPerformanceCheck();
71 
72     static CodeInfo GetCodeInfo(int32_t row, int32_t col);
73     static void RecordPerformanceCheckData(
74         const PerformanceCheckNodeMap& nodeMap, int64_t vsyncTimeout, std::string path);
75 
76 private:
77     static std::string GetCurrentTime();
78     static bool CheckIsRuleContainsPage(const std::string& ruleType, const std::string& pagePath);
79     static void RecordPageNodeCountAndDepth(int32_t pageNodeCount, int32_t pageDepth,
80         std::vector<PerformanceCheckNode>& pageNodeList, const CodeInfo& info);
81     static void RecordForEachItemsCount(
82         int32_t count, std::unordered_map<int32_t, PerformanceCheckNode>& foreachNodeMap, const CodeInfo& info);
83     static void RecordFlexLayoutsCount(const std::vector<PerformanceCheckNode>& nodeList, const CodeInfo& info);
84     static void RecordVsyncTimeout(const PerformanceCheckNodeMap& nodeMap, int64_t vsyncTimeout, const CodeInfo& info);
85     static bool CheckPage(const CodeInfo& codeInfo, const std::string& rule);
86     static void RecordFunctionTimeout();
87     static RefPtr<Framework::RevSourceMap> GetCurrentSourceMap();
88     int64_t markTime_ = 0;
89     std::string name_;
90     static std::string currentPath_;
91     static std::vector<std::pair<int64_t, std::string>> records_;
92     ACE_DISALLOW_COPY_AND_MOVE(AceScopedPerformanceCheck);
93 };
94 } // namespace OHOS::Ace
95 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_ACE_PERFORMANCE_CHECK_H
96