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