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