• 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 #include <map>
21 
22 #include "nocopyable.h"
23 
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "TimeCostChk"
26 
27 namespace OHOS {
28 namespace MMI {
29 inline constexpr int64_t MAX_INPUT_EVENT_TIME { 1000 };
30 inline constexpr int64_t MAX_OVER_TIME { 300 };
31 
32 template <class T> class TimeCostChk {
33 public:
34     TimeCostChk(const std::string &strReason, const std::string &strOutputStr, int64_t tmChk, T llParam1,
35         int64_t llParam2 = 0)
beginTime_(std::chrono::high_resolution_clock::now ())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     {}
43 
~TimeCostChk(void)44     ~TimeCostChk(void)
45     {
46         int64_t ullCost = GetElapsed_micro();
47         const std::string eventType = GetEventTypeString(llParam1_);
48         if ((ullCost > uiTime_) && strReason_.size() > 0 && strOutput_.size() > 0) {
49             if ((llParam1_ != 0 || llParam2_ != 0) && (!eventType.empty())) {
50                 MMI_HILOGD("Time cost overtime (%{public}" PRId64 ",(us)>%{public}" PRId64
51                     "(us)) when Reason:%{public}s,chk:%{public}s,"
52                     "paramType:%{public}s, param2:%{public}" PRId64 "",
53                     ullCost, uiTime_, strReason_.c_str(), strOutput_.c_str(), eventType.c_str(), llParam2_);
54             } else {
55                 MMI_HILOGD("Overtime(%{public}" PRId64 ",(us)>%{public}" PRId64
56                     "(us)) when Reason:%{public}s,chk:%{public}s",
57                     ullCost, uiTime_, strReason_.c_str(), strOutput_.c_str());
58             }
59         }
60     }
61 
62     DISALLOW_COPY_AND_MOVE(TimeCostChk);
63 
GetElapsed_micro()64     int64_t GetElapsed_micro() const
65     {
66         int64_t tm64Cost = std::chrono::duration_cast<std::chrono::microseconds>(
67                             std::chrono::high_resolution_clock::now() - beginTime_
68                             ).count();
69         return tm64Cost;
70     }
71 
72 private:
73 
GetEventTypeString(int64_t key)74     const std::string GetEventTypeString(int64_t key)
75     {
76         switch (key) {
77             case 1: /* 1: device_added */
78                 return "device_added";
79             case 2: /* 2: device_removed */
80                 return "device_removed";
81             case 300: /* 300: keyboard_key */
82                 return "keyboard_key";
83             case 400: /* 400: pointer_monitor */
84                 return "pointer_monitor";
85             case 401: /* 401: pointer_monitor_absolute */
86                 return "pointer_monitor_absolute";
87             case 402: /* 402: pointer_button */
88                 return "pointer_button";
89             case 403: /* 403: pointer_axis */
90                 return "pointer_axis";
91             case 500: /* 500: touch_down */
92                 return "touch_down";
93             case 501: /* 501: touch_up */
94                 return "touch_up";
95             case 502: /* 502: touch_monitor */
96                 return "touch_monitor";
97             case 600: /* 600: tablet_tool_axis */
98                 return "tablet_tool_axis";
99             case 800: /* 800: gesture_swipe_begin */
100                 return "gesture_swipe_begin";
101             case 801: /* 801: gesture_swipe_update */
102                 return "gesture_swipe_update";
103             case 802: /* 802: gesturn_swipe_end */
104                 return "gesturn_swipe_end";
105             case 803: /* 803: gesturn_pinch_begin */
106                 return "gesturn_pinch_begin";
107             case 804: /* 804: gesturn_pinch_update */
108                 return "gesturn_pinch_update";
109             case 805: /* 805: gesturn_pinch_end */
110                 return "gesturn_pinch_end";
111             default:
112                 return "";
113         }
114     }
115 
116     const std::chrono::time_point<std::chrono::high_resolution_clock> beginTime_;
117     const std::string strOutput_ = "";
118     const std::string strReason_ = "";
119     const int64_t uiTime_ { 0 };
120     const int64_t llParam1_ { 0 };
121     const int64_t llParam2_ { 0 };
122 };
123 } // namespace MMI
124 } // namespace OHOS
125 #endif // TIME_COST_CHK_H
126