• 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 #include "calculate_time_consuming.h"
17 #include <sys/time.h>
18 
19 #include "pasteboard_hilog.h"
20 #include "reporter.h"
21 
22 namespace OHOS {
23 namespace MiscServices {
24 uint64_t CalculateTimeConsuming::lastTime_;
CalculateTimeConsuming(const size_t calPasteboardData,const int calPasteboardState)25 CalculateTimeConsuming::CalculateTimeConsuming(const size_t calPasteboardData, const int calPasteboardState)
26     : pasteboardState_(calPasteboardState)
27 {
28     pasteboardData_ = CalculateData(calPasteboardData);
29     lastTime_ = 0;
30     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "CalculateTimeConsuming()");
31 }
32 
SetBeginTime()33 void CalculateTimeConsuming::SetBeginTime()
34 {
35     lastTime_ = GetCurrentTimeMicros();
36 }
37 
~CalculateTimeConsuming()38 CalculateTimeConsuming::~CalculateTimeConsuming()
39 {
40     uint64_t endTime = GetCurrentTimeMicros();
41     if (endTime < lastTime_) {
42         return;
43     }
44     uint64_t delta = endTime - lastTime_;
45     int calculateTime = CalculateTime(delta);
46     Reporter::GetInstance().TimeConsumingStatistic().Report({ pasteboardState_, pasteboardData_, calculateTime });
47     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "~CalculateTimeConsuming()");
48 }
49 
CalculateData(size_t calPasteboardData) const50 int CalculateTimeConsuming::CalculateData(size_t calPasteboardData) const
51 {
52     constexpr int M_BTYE = 1024;
53     constexpr int TC_ZERO_KB = 0;
54     constexpr int TC_HUNDRED_KB = 100;
55     constexpr int TC_FIVE_HUNDRED = 500;
56     constexpr int TC_ONE_MB = 1;
57     constexpr int TC_FIVE_MB = 5;
58     constexpr int TC_TEN_MB = 10;
59     constexpr int TC_FIFTY_MB = 50;
60 
61     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "CalculateData() enter");
62     if (calPasteboardData % M_BTYE == 0) {
63         if (calPasteboardData >= TC_ZERO_KB && calPasteboardData < TC_HUNDRED_KB) {
64             return static_cast<int>(DataRange::DR_ZERO_TO_HUNDRED_KB);
65         } else if (calPasteboardData >= TC_HUNDRED_KB && calPasteboardData < TC_FIVE_HUNDRED) {
66             return static_cast<int>(DataRange::DR_HUNDRED_TO_FIVE_HUNDREDS_KB);
67         } else {
68             return static_cast<int>(DataRange::DR_FIVE_HUNDREDS_TO_THOUSAND_KB);
69         }
70     } else {
71         size_t dataSize = calPasteboardData % M_BTYE;
72         if (dataSize >= TC_ONE_MB && dataSize < TC_FIVE_MB) {
73             return static_cast<int>(DataRange::DR_ONE_TO_FIVE_MB);
74         } else if (dataSize >= TC_FIVE_MB && dataSize < TC_TEN_MB) {
75             return static_cast<int>(DataRange::DR_FIVE_TO_TEN_MB);
76         } else if (dataSize >= TC_TEN_MB && dataSize < TC_FIFTY_MB) {
77             return static_cast<int>(DataRange::DR_TEN_TO_FIFTY_MB);
78         } else {
79             return static_cast<int>(DataRange::DR_OVER_FIFTY_MB);
80         }
81     }
82     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "CalculateData() end");
83 }
84 
CalculateTime(uint64_t time)85 int CalculateTimeConsuming::CalculateTime(uint64_t time)
86 {
87     constexpr int FIVE_HUNDRED_MS = 500;
88     uint64_t timeConsuming = time % FIVE_HUNDRED_MS;
89     switch (timeConsuming) {
90         case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_ZERO):
91             return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_ONE);
92         case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_ONE):
93             return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_TWO);
94         case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_TWO):
95             return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_THREE);
96         case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_THREE):
97             return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_FOUR);
98         case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_FOUR):
99             return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_FIVE);
100         case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_FIVE):
101             return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_SIX);
102         case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_SIX):
103             return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_SEVEN);
104         case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_SEVEN):
105             return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_EIGHT);
106         case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_EIGHT):
107             return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_NINE);
108         case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_NINE):
109             return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_TEN);
110         default:
111             return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_ELEVEN);
112     }
113 }
114 
GetCurrentTimeMicros()115 uint64_t CalculateTimeConsuming::GetCurrentTimeMicros()
116 {
117     constexpr int64_t SEC_TO_MILLISEC = 1000;
118     constexpr int64_t MICROSEC_TO_MILLISEC = 1000;
119 
120     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "GetCurrentTimeMicros() start");
121     struct timeval tv = { 0, 0 };
122     gettimeofday(&tv, nullptr);
123     return (tv.tv_sec * SEC_TO_MILLISEC + tv.tv_usec / MICROSEC_TO_MILLISEC);
124 }
125 } // namespace MiscServices
126 } // namespace OHOS
127