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