1bc03f14fSopenharmony_ci/*
2bc03f14fSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License.
5bc03f14fSopenharmony_ci * You may obtain a copy of the License at
6bc03f14fSopenharmony_ci *
7bc03f14fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc03f14fSopenharmony_ci *
9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and
13bc03f14fSopenharmony_ci * limitations under the License.
14bc03f14fSopenharmony_ci */
15bc03f14fSopenharmony_ci
16bc03f14fSopenharmony_ci#include <sys/time.h>
17bc03f14fSopenharmony_ci
18bc03f14fSopenharmony_ci#include "calculate_time_consuming.h"
19bc03f14fSopenharmony_ci#include "dfx_types.h"
20bc03f14fSopenharmony_ci#include "pasteboard_hilog.h"
21bc03f14fSopenharmony_ci#include "reporter.h"
22bc03f14fSopenharmony_ci#include "statistic_reporter.h"
23bc03f14fSopenharmony_ci
24bc03f14fSopenharmony_cinamespace OHOS {
25bc03f14fSopenharmony_cinamespace MiscServices {
26bc03f14fSopenharmony_ciuint64_t CalculateTimeConsuming::lastTime_ = 0;
27bc03f14fSopenharmony_civoid CalculateTimeConsuming::SetBeginTime()
28bc03f14fSopenharmony_ci{
29bc03f14fSopenharmony_ci    lastTime_ = GetCurrentTimeMicros();
30bc03f14fSopenharmony_ci}
31bc03f14fSopenharmony_ciCalculateTimeConsuming::CalculateTimeConsuming(const size_t calPasteboardData, const int calPasteboardState)
32bc03f14fSopenharmony_ci    : pasteboardState_(calPasteboardState)
33bc03f14fSopenharmony_ci{
34bc03f14fSopenharmony_ci    pasteboardData_ = CalculateData(calPasteboardData);
35bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "CalculateTimeConsuming()");
36bc03f14fSopenharmony_ci}
37bc03f14fSopenharmony_ci
38bc03f14fSopenharmony_ciCalculateTimeConsuming::~CalculateTimeConsuming()
39bc03f14fSopenharmony_ci{
40bc03f14fSopenharmony_ci    uint64_t delta = GetCurrentTimeMicros() - lastTime_;
41bc03f14fSopenharmony_ci    int calculateTime = CalculateTime(delta);
42bc03f14fSopenharmony_ci    Reporter::GetInstance().TimeConsumingStatistic().Report({ pasteboardState_, pasteboardData_, calculateTime });
43bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "~CalculateTimeConsuming()");
44bc03f14fSopenharmony_ci}
45bc03f14fSopenharmony_ci
46bc03f14fSopenharmony_ciint CalculateTimeConsuming::CalculateData(size_t calPasteboardData) const
47bc03f14fSopenharmony_ci{
48bc03f14fSopenharmony_ci    constexpr int M_BTYE = 1024;
49bc03f14fSopenharmony_ci    constexpr int TC_ZERO_KB = 0;
50bc03f14fSopenharmony_ci    constexpr int TC_HUNDRED_KB = 100;
51bc03f14fSopenharmony_ci    constexpr int TC_FIVE_HUNDRED = 500;
52bc03f14fSopenharmony_ci    constexpr int TC_ONE_MB = 1;
53bc03f14fSopenharmony_ci    constexpr int TC_FIVE_MB = 5;
54bc03f14fSopenharmony_ci    constexpr int TC_TEN_MB = 10;
55bc03f14fSopenharmony_ci    constexpr int TC_FIFTY_MB = 50;
56bc03f14fSopenharmony_ci
57bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "CalculateData() enter");
58bc03f14fSopenharmony_ci    if (calPasteboardData % M_BTYE == 0) {
59bc03f14fSopenharmony_ci        if (calPasteboardData >= TC_ZERO_KB && calPasteboardData < TC_HUNDRED_KB) {
60bc03f14fSopenharmony_ci            return static_cast<int>(DataRange::DR_ZERO_TO_HUNDRED_KB);
61bc03f14fSopenharmony_ci        } else if (calPasteboardData >= TC_HUNDRED_KB && calPasteboardData < TC_FIVE_HUNDRED) {
62bc03f14fSopenharmony_ci            return static_cast<int>(DataRange::DR_HUNDRED_TO_FIVE_HUNDREDS_KB);
63bc03f14fSopenharmony_ci        } else {
64bc03f14fSopenharmony_ci            return static_cast<int>(DataRange::DR_FIVE_HUNDREDS_TO_THOUSAND_KB);
65bc03f14fSopenharmony_ci        }
66bc03f14fSopenharmony_ci    } else {
67bc03f14fSopenharmony_ci        size_t dataSize = calPasteboardData % M_BTYE;
68bc03f14fSopenharmony_ci        if (dataSize >= TC_ONE_MB && dataSize < TC_FIVE_MB) {
69bc03f14fSopenharmony_ci            return static_cast<int>(DataRange::DR_ONE_TO_FIVE_MB);
70bc03f14fSopenharmony_ci        } else if (dataSize >= TC_FIVE_MB && dataSize < TC_TEN_MB) {
71bc03f14fSopenharmony_ci            return static_cast<int>(DataRange::DR_FIVE_TO_TEN_MB);
72bc03f14fSopenharmony_ci        } else if (dataSize >= TC_TEN_MB && dataSize < TC_FIFTY_MB) {
73bc03f14fSopenharmony_ci            return static_cast<int>(DataRange::DR_TEN_TO_FIFTY_MB);
74bc03f14fSopenharmony_ci        } else {
75bc03f14fSopenharmony_ci            return static_cast<int>(DataRange::DR_OVER_FIFTY_MB);
76bc03f14fSopenharmony_ci        }
77bc03f14fSopenharmony_ci    }
78bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "CalculateData()  end");
79bc03f14fSopenharmony_ci}
80bc03f14fSopenharmony_ci
81bc03f14fSopenharmony_ciint CalculateTimeConsuming::CalculateTime(uint64_t time)
82bc03f14fSopenharmony_ci{
83bc03f14fSopenharmony_ci    constexpr int FIVE_HUNDRED_MS = 500;
84bc03f14fSopenharmony_ci    uint64_t timeCosuming = time % FIVE_HUNDRED_MS;
85bc03f14fSopenharmony_ci    switch (timeCosuming) {
86bc03f14fSopenharmony_ci        case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_ZERO):
87bc03f14fSopenharmony_ci            return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_ONE);
88bc03f14fSopenharmony_ci        case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_ONE):
89bc03f14fSopenharmony_ci            return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_TWO);
90bc03f14fSopenharmony_ci        case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_TWO):
91bc03f14fSopenharmony_ci            return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_THREE);
92bc03f14fSopenharmony_ci        case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_THREE):
93bc03f14fSopenharmony_ci            return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_FOUR);
94bc03f14fSopenharmony_ci        case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_FOUR):
95bc03f14fSopenharmony_ci            return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_FIVE);
96bc03f14fSopenharmony_ci        case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_FIVE):
97bc03f14fSopenharmony_ci            return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_SIX);
98bc03f14fSopenharmony_ci        case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_SIX):
99bc03f14fSopenharmony_ci            return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_SEVEN);
100bc03f14fSopenharmony_ci        case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_SEVEN):
101bc03f14fSopenharmony_ci            return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_EIGHT);
102bc03f14fSopenharmony_ci        case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_EIGHT):
103bc03f14fSopenharmony_ci            return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_NINE);
104bc03f14fSopenharmony_ci        case static_cast<uint64_t>(TimeLevel::PER_FIVE_HUNDRED_MS_NINE):
105bc03f14fSopenharmony_ci            return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_TEN);
106bc03f14fSopenharmony_ci        default:
107bc03f14fSopenharmony_ci            return static_cast<int>(TimeConsumingStatistic::TCS_TIME_CONSUMING_LEVEL_ELEVEN);
108bc03f14fSopenharmony_ci    }
109bc03f14fSopenharmony_ci}
110bc03f14fSopenharmony_ci
111bc03f14fSopenharmony_ciuint64_t CalculateTimeConsuming::GetCurrentTimeMicros()
112bc03f14fSopenharmony_ci{
113bc03f14fSopenharmony_ci    constexpr int64_t SEC_TO_MILLISEC = 1000;
114bc03f14fSopenharmony_ci    constexpr int64_t MICROSEC_TO_MILLISEC = 1000;
115bc03f14fSopenharmony_ci
116bc03f14fSopenharmony_ci    PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "GetCurrentTimeMicros()  start");
117bc03f14fSopenharmony_ci    struct timeval tv = { 0, 0 };
118bc03f14fSopenharmony_ci    gettimeofday(&tv, nullptr);
119bc03f14fSopenharmony_ci    return (tv.tv_sec * SEC_TO_MILLISEC + tv.tv_usec / MICROSEC_TO_MILLISEC);
120bc03f14fSopenharmony_ci}
121bc03f14fSopenharmony_ci} // namespace MiscServices
122bc03f14fSopenharmony_ci} // namespace OHOS
123