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