1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#ifndef TIME_COST_CHK_H
17c29fa5a6Sopenharmony_ci#define TIME_COST_CHK_H
18c29fa5a6Sopenharmony_ci
19c29fa5a6Sopenharmony_ci#include <cinttypes>
20c29fa5a6Sopenharmony_ci#include <map>
21c29fa5a6Sopenharmony_ci
22c29fa5a6Sopenharmony_ci#include "nocopyable.h"
23c29fa5a6Sopenharmony_ci
24c29fa5a6Sopenharmony_ci#undef MMI_LOG_TAG
25c29fa5a6Sopenharmony_ci#define MMI_LOG_TAG "TimeCostChk"
26c29fa5a6Sopenharmony_ci
27c29fa5a6Sopenharmony_cinamespace OHOS {
28c29fa5a6Sopenharmony_cinamespace MMI {
29c29fa5a6Sopenharmony_ciinline constexpr int64_t MAX_INPUT_EVENT_TIME { 1000 };
30c29fa5a6Sopenharmony_ciinline constexpr int64_t MAX_OVER_TIME { 300 };
31c29fa5a6Sopenharmony_cistatic std::map<int32_t, std::string> paramType = {
32c29fa5a6Sopenharmony_ci    { 1, "device_added" },
33c29fa5a6Sopenharmony_ci    { 2, "device_removed" },
34c29fa5a6Sopenharmony_ci    { 300, "keyboard_key" },
35c29fa5a6Sopenharmony_ci    { 400, "pointer_monitor" },
36c29fa5a6Sopenharmony_ci    { 401, "pointer_monitor_absolute" },
37c29fa5a6Sopenharmony_ci    { 402, "pointer_button" },
38c29fa5a6Sopenharmony_ci    { 403, "pointer_axis" },
39c29fa5a6Sopenharmony_ci    { 500, "touch_down" },
40c29fa5a6Sopenharmony_ci    { 501, "touch_up" },
41c29fa5a6Sopenharmony_ci    { 502, "touch_monitor" },
42c29fa5a6Sopenharmony_ci    { 600, "tablet_tool_axis" },
43c29fa5a6Sopenharmony_ci    { 800, "gesture_swipe_begin" },
44c29fa5a6Sopenharmony_ci    { 801, "gesture_swipe_update" },
45c29fa5a6Sopenharmony_ci    { 802, "gesturn_swipe_end" },
46c29fa5a6Sopenharmony_ci    { 803, "gesturn_pinch_begin" },
47c29fa5a6Sopenharmony_ci    { 804, "gesturn_pinch_update" },
48c29fa5a6Sopenharmony_ci    { 805, "gesturn_pinch_end" },
49c29fa5a6Sopenharmony_ci};
50c29fa5a6Sopenharmony_citemplate <class T> class TimeCostChk {
51c29fa5a6Sopenharmony_cipublic:
52c29fa5a6Sopenharmony_ci    TimeCostChk(const std::string &strReason, const std::string &strOutputStr, int64_t tmChk, T llParam1,
53c29fa5a6Sopenharmony_ci        int64_t llParam2 = 0)
54c29fa5a6Sopenharmony_ci        : beginTime_(std::chrono::high_resolution_clock::now()),
55c29fa5a6Sopenharmony_ci          strOutput_(strOutputStr),
56c29fa5a6Sopenharmony_ci          strReason_(strReason),
57c29fa5a6Sopenharmony_ci          uiTime_(tmChk),
58c29fa5a6Sopenharmony_ci          llParam1_(static_cast<int64_t>(llParam1)),
59c29fa5a6Sopenharmony_ci          llParam2_(llParam2)
60c29fa5a6Sopenharmony_ci    {}
61c29fa5a6Sopenharmony_ci
62c29fa5a6Sopenharmony_ci    ~TimeCostChk(void)
63c29fa5a6Sopenharmony_ci    {
64c29fa5a6Sopenharmony_ci        int64_t ullCost = GetElapsed_micro();
65c29fa5a6Sopenharmony_ci        if ((ullCost > uiTime_) && strReason_.size() > 0 && strOutput_.size() > 0) {
66c29fa5a6Sopenharmony_ci            if ((llParam1_ != 0 || llParam2_ != 0) && (paramType.find(llParam1_) != paramType.end())) {
67c29fa5a6Sopenharmony_ci                MMI_HILOGD("Time cost overtime (%{public}" PRId64 ",(us)>%{public}" PRId64
68c29fa5a6Sopenharmony_ci                    "(us)) when Reason:%{public}s,chk:%{public}s,"
69c29fa5a6Sopenharmony_ci                    "paramType:%{public}s, param2:%{public}" PRId64 "",
70c29fa5a6Sopenharmony_ci                    ullCost, uiTime_, strReason_.c_str(), strOutput_.c_str(), paramType[llParam1_].data(), llParam2_);
71c29fa5a6Sopenharmony_ci            } else {
72c29fa5a6Sopenharmony_ci                MMI_HILOGD("Overtime(%{public}" PRId64 ",(us)>%{public}" PRId64
73c29fa5a6Sopenharmony_ci                    "(us)) when Reason:%{public}s,chk:%{public}s",
74c29fa5a6Sopenharmony_ci                    ullCost, uiTime_, strReason_.c_str(), strOutput_.c_str());
75c29fa5a6Sopenharmony_ci            }
76c29fa5a6Sopenharmony_ci        }
77c29fa5a6Sopenharmony_ci    }
78c29fa5a6Sopenharmony_ci
79c29fa5a6Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(TimeCostChk);
80c29fa5a6Sopenharmony_ci
81c29fa5a6Sopenharmony_ci    int64_t GetElapsed_micro() const
82c29fa5a6Sopenharmony_ci    {
83c29fa5a6Sopenharmony_ci        int64_t tm64Cost = std::chrono::duration_cast<std::chrono::microseconds>(
84c29fa5a6Sopenharmony_ci                            std::chrono::high_resolution_clock::now() - beginTime_
85c29fa5a6Sopenharmony_ci                            ).count();
86c29fa5a6Sopenharmony_ci        return tm64Cost;
87c29fa5a6Sopenharmony_ci    }
88c29fa5a6Sopenharmony_ci
89c29fa5a6Sopenharmony_ciprivate:
90c29fa5a6Sopenharmony_ci    const std::chrono::time_point<std::chrono::high_resolution_clock> beginTime_;
91c29fa5a6Sopenharmony_ci    const std::string strOutput_ = "";
92c29fa5a6Sopenharmony_ci    const std::string strReason_ = "";
93c29fa5a6Sopenharmony_ci    const int64_t uiTime_ { 0 };
94c29fa5a6Sopenharmony_ci    const int64_t llParam1_ { 0 };
95c29fa5a6Sopenharmony_ci    const int64_t llParam2_ { 0 };
96c29fa5a6Sopenharmony_ci};
97c29fa5a6Sopenharmony_ci} // namespace MMI
98c29fa5a6Sopenharmony_ci} // namespace OHOS
99c29fa5a6Sopenharmony_ci#endif // TIME_COST_CHK_H
100