• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include "nocopyable.h"
22 
23 namespace OHOS {
24 namespace MMI {
25 inline constexpr int64_t MAX_INPUT_EVENT_TIME = 1000;
26 inline constexpr int64_t MAX_OVER_TIME = 300;
27 template<class T>
28 class TimeCostChk {
29     static inline constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "TimeCostChk" };
30 public:
31     TimeCostChk(const std::string& strReason, const std::string& strOutputStr, int64_t tmChk, T llParam1,
32                 int64_t llParam2 = 0)
beginTime_(std::chrono::high_resolution_clock::now ())33         : beginTime_(std::chrono::high_resolution_clock::now()),
34           strOutput_(strOutputStr),
35           strReason_(strReason),
36           uiTime_(tmChk),
37           llParam1_(static_cast<int64_t>(llParam1)),
38           llParam2_(llParam2) {}
39 
~TimeCostChk(void)40     ~TimeCostChk(void)
41     {
42         int64_t ullCost = GetElapsed_micro();
43         if ((ullCost > uiTime_) && strReason_.size() > 0 && strOutput_.size() > 0) {
44             if (llParam1_ != 0 || llParam2_ != 0) {
45                 MMI_HILOGW("Time cost overtime (%{public}" PRId64 ",(us)>%{public}" PRId64
46                          "(us)) when Reason:%{public}s,chk:%{public}s,"
47                          "param1:%{public}" PRId64 ",param2:%{public}" PRId64 "",
48                          ullCost, uiTime_, strReason_.c_str(), strOutput_.c_str(), llParam1_, llParam2_);
49             } else {
50                 MMI_HILOGW("Overtime(%{public}" PRId64 ",(us)>%{public}" PRId64
51                          "(us)) when Reason:%{public}s,chk:%{public}s",
52                          ullCost, uiTime_, strReason_.c_str(), strOutput_.c_str());
53             }
54         }
55     }
56 
57     DISALLOW_COPY_AND_MOVE(TimeCostChk);
58 
GetElapsed_micro()59     int64_t GetElapsed_micro() const
60     {
61         int64_t tm64Cost = std::chrono::duration_cast<std::chrono::microseconds>(
62             std::chrono::high_resolution_clock::now() - beginTime_).count();
63         return tm64Cost;
64     }
65 
66 private:
67     const std::chrono::time_point<std::chrono::high_resolution_clock> beginTime_;
68     const std::string strOutput_ = "";
69     const std::string strReason_ = "";
70     const int64_t uiTime_ { 0 };
71     const int64_t llParam1_ { 0 };
72     const int64_t llParam2_ { 0 };
73 };
74 } // namespace MMI
75 } // namespace OHOS
76 #endif // TIME_COST_CHK_H