1 /* 2 * Copyright (c) 2021-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 TIME_COST_CHK_H 17 #define TIME_COST_CHK_H 18 19 #include <cinttypes> 20 #include <map> 21 22 #include "nocopyable.h" 23 24 namespace OHOS { 25 namespace MMI { 26 inline constexpr int64_t MAX_INPUT_EVENT_TIME = 1000; 27 inline constexpr int64_t MAX_OVER_TIME = 300; 28 static std::map<int32_t, std::string> paramType = { 29 { 1, "device_added" }, 30 { 2, "device_removed" }, 31 { 300, "keyboard_key" }, 32 { 400, "pointer_monitor" }, 33 { 401, "pointer_monitor_absolute" }, 34 { 402, "pointer_button" }, 35 { 403, "pointer_axis" }, 36 { 500, "touch_down" }, 37 { 501, "touch_up" }, 38 { 502, "touch_monitor" }, 39 { 600, "tablet_tool_axis" }, 40 { 800, "gesture_swipe_begin" }, 41 { 801, "gesture_swipe_update" }, 42 { 802, "gesturn_swipe_end" }, 43 { 803, "gesturn_pinch_begin" }, 44 { 804, "gesturn_pinch_update" }, 45 { 805, "gesturn_pinch_end" }, 46 }; 47 template <class T> class TimeCostChk { 48 static inline constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "TimeCostChk" }; 49 50 public: 51 TimeCostChk(const std::string &strReason, const std::string &strOutputStr, int64_t tmChk, T llParam1, 52 int64_t llParam2 = 0) beginTime_(std::chrono::high_resolution_clock::now ())53 : beginTime_(std::chrono::high_resolution_clock::now()), 54 strOutput_(strOutputStr), 55 strReason_(strReason), 56 uiTime_(tmChk), 57 llParam1_(static_cast<int64_t>(llParam1)), 58 llParam2_(llParam2) 59 {} 60 ~TimeCostChk(void)61 ~TimeCostChk(void) 62 { 63 int64_t ullCost = GetElapsed_micro(); 64 if ((ullCost > uiTime_) && strReason_.size() > 0 && strOutput_.size() > 0) { 65 if (llParam1_ != 0 || llParam2_ != 0) { 66 MMI_HILOGD("Time cost overtime (%{public}" PRId64 ",(us)>%{public}" PRId64 67 "(us)) when Reason:%{public}s,chk:%{public}s," 68 "paramType:%{public}s, param2:%{public}" PRId64 "", 69 ullCost, uiTime_, strReason_.c_str(), strOutput_.c_str(), paramType[llParam1_].data(), llParam2_); 70 } else { 71 MMI_HILOGD("Overtime(%{public}" PRId64 ",(us)>%{public}" PRId64 72 "(us)) when Reason:%{public}s,chk:%{public}s", 73 ullCost, uiTime_, strReason_.c_str(), strOutput_.c_str()); 74 } 75 } 76 } 77 78 DISALLOW_COPY_AND_MOVE(TimeCostChk); 79 GetElapsed_micro()80 int64_t GetElapsed_micro() const 81 { 82 int64_t tm64Cost = std::chrono::duration_cast<std::chrono::microseconds>( 83 std::chrono::high_resolution_clock::now() - beginTime_ 84 ).count(); 85 return tm64Cost; 86 } 87 88 private: 89 const std::chrono::time_point<std::chrono::high_resolution_clock> beginTime_; 90 const std::string strOutput_ = ""; 91 const std::string strReason_ = ""; 92 const int64_t uiTime_{ 0 }; 93 const int64_t llParam1_{ 0 }; 94 const int64_t llParam2_{ 0 }; 95 }; 96 } // namespace MMI 97 } // namespace OHOS 98 #endif // TIME_COST_CHK_H