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