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