1 /*
2 * Copyright (c) 2025 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 #define LOG_TAG "RdbHiviewAdapter"
16
17 #include "rdb_hiview_adapter.h"
18 #include "log_print.h"
19 #include <string>
20 #include <vector>
21
22 namespace OHOS::DistributedRdb {
23
24 static constexpr const char *DISTRIBUTED_DATAMGR = "DISTDATAMGR";
25 static constexpr const char *STATISTIC_EVENT = "RDB_STATISTIC";
26 constexpr int32_t WAIT_TIME = 60 * 60 * 24; // 24h
27
GetInstance()28 RdbHiViewAdapter& RdbHiViewAdapter::GetInstance()
29 {
30 static RdbHiViewAdapter instance;
31 return instance;
32 }
33
ReportStatistic(const RdbStatEvent & stat)34 void RdbHiViewAdapter::ReportStatistic(const RdbStatEvent &stat)
35 {
36 if (executors_ == nullptr) {
37 return;
38 }
39 statEvents_.Compute(stat, [&stat](const RdbStatEvent &key, uint32_t &value) {
40 value++;
41 return true;
42 });
43 }
44
InvokeSync()45 void RdbHiViewAdapter::InvokeSync()
46 {
47 auto statEvents = std::move(statEvents_);
48 uint32_t count = statEvents.Size();
49 if (count == 0) {
50 return;
51 }
52
53 uint32_t statTypes[count];
54 const char* bundleNames[count];
55 const char* storeNames[count];
56 uint32_t subTypes[count];
57 uint32_t costTimes[count];
58 uint32_t occurTimes[count];
59 uint32_t index = 0;
60
61 statEvents.ForEach([&statTypes, &bundleNames, &storeNames, &subTypes, &costTimes, &occurTimes, &index](
62 const RdbStatEvent &key, uint32_t &value) {
63 statTypes[index] = key.statType;
64 bundleNames[index] = key.bundleName.c_str();
65 storeNames[index] = key.storeName.c_str();
66 subTypes[index] = key.subType;
67 costTimes[index] = key.costTime;
68 occurTimes[index] = value;
69 index++;
70 return false;
71 });
72
73 HiSysEventParam params[] = {
74 { .name = "TYPE", .t = HISYSEVENT_UINT32_ARRAY, .v = { .array = statTypes }, .arraySize = count },
75 { .name = "BUNDLE_NAME", .t = HISYSEVENT_STRING_ARRAY, .v = { .array = bundleNames }, .arraySize = count },
76 { .name = "STORE_NAME", .t = HISYSEVENT_STRING_ARRAY, .v = { .array = storeNames }, .arraySize = count },
77 { .name = "PARAM_TYPE1", .t = HISYSEVENT_UINT32_ARRAY, .v = { .array = subTypes }, .arraySize = count },
78 { .name = "PARAM_TYPE2", .t = HISYSEVENT_UINT32_ARRAY, .v = { .array = costTimes }, .arraySize = count },
79 { .name = "TIMES", .t = HISYSEVENT_UINT32_ARRAY, .v = { .array = occurTimes }, .arraySize = count },
80 };
81 auto size = sizeof(params) / sizeof(params[0]);
82 OH_HiSysEvent_Write(DISTRIBUTED_DATAMGR, STATISTIC_EVENT, HISYSEVENT_STATISTIC, params, size);
83 }
84
StartTimerThread()85 void RdbHiViewAdapter::StartTimerThread()
86 {
87 if (executors_ == nullptr) {
88 return;
89 }
90 if (running_.exchange(true)) {
91 return;
92 }
93 auto interval = std::chrono::seconds(WAIT_TIME);
94 auto fun = [this]() {
95 InvokeSync();
96 };
97 executors_->Schedule(fun, interval);
98 }
99
SetThreadPool(std::shared_ptr<ExecutorPool> executors)100 void RdbHiViewAdapter::SetThreadPool(std::shared_ptr<ExecutorPool> executors)
101 {
102 executors_ = executors;
103 StartTimerThread();
104 }
105
106 } // namespace OHOS::DistributedRdb