diff --git a/services/distributeddataservice/service/rdb/BUILD.gn b/services/distributeddataservice/service/rdb/BUILD.gn index 2f176e11a257d5fc7ed871742ad89b07062ca14a..9c126c795e02cd1ab24ec380b516a6804708d2cf 100644 --- a/services/distributeddataservice/service/rdb/BUILD.gn +++ b/services/distributeddataservice/service/rdb/BUILD.gn @@ -46,6 +46,7 @@ ohos_source_set("distributeddata_rdb") { "rdb_cloud.cpp", "rdb_cursor.cpp", "rdb_general_store.cpp", + "rdb_hiview_adapter.cpp", "rdb_notifier_proxy.cpp", "rdb_query.cpp", "rdb_result_set_impl.cpp", diff --git a/services/distributeddataservice/service/rdb/rdb_hiview_adapter.cpp b/services/distributeddataservice/service/rdb/rdb_hiview_adapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67b2991a5b89766cbd4cc4e42b3834a379c1564a --- /dev/null +++ b/services/distributeddataservice/service/rdb/rdb_hiview_adapter.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define LOG_TAG "RdbHiviewAdapter" + +#include "rdb_hiview_adapter.h" +#include "log_print.h" +#include +#include + +namespace OHOS::DistributedRdb { + +static constexpr const char *DISTRIBUTED_DATAMGR = "DISTDATAMGR"; +static constexpr const char *STATISTIC_EVENT = "RDB_STATISTIC"; + +RdbHiViewAdapter& RdbHiViewAdapter::GetInstance() +{ + static RdbHiViewAdapter instance; + return instance; +} + +void RdbHiViewAdapter::ReportStatistic(const RdbStatEvent &stat) +{ + if (executors_ == nullptr) { + return; + } + statEvents_.Compute(stat, [&stat](const RdbStatEvent &key, uint32_t &value) { + value++; + return true; + }); +} + +void RdbHiViewAdapter::InvokeSync() +{ + auto statEvents = std::move(statEvents_); + uint32_t count = statEvents.Size(); + if (count == 0) { + return; + } + + uint32_t statTypes[count]; + std::string bundleNames[count]; + std::string storeNames[count]; + uint32_t subTypes[count]; + uint32_t costTimes[count]; + uint32_t occurTimes[count]; + uint32_t index = 0; + + statEvents.EraseIf([&statTypes, &bundleNames, &storeNames, &subTypes, &costTimes, &occurTimes, &index]( + const RdbStatEvent &key, uint32_t &value) { + statTypes[index] = key.statType; + bundleNames[index] = key.bundleName; + storeNames[index] = key.storeName; + subTypes[index] = key.subType; + costTimes[index] = key.costTime; + occurTimes[index] = value; + index++; + return true; + }); + + const char* bundleNameArray[count]; + const char* storeNameArray[count]; + for (uint32_t i = 0; i < count; ++i) { + bundleNameArray[i] = bundleNames[i].c_str(); + storeNameArray[i] = storeNames[i].c_str(); + } + HiSysEventParam params[] = { + { .name = "TYPE", .t = HISYSEVENT_UINT32_ARRAY, .v = { .array = statTypes }, .arraySize = count }, + { .name = "BUNDLE_NAME", .t = HISYSEVENT_STRING_ARRAY, .v = { .array = bundleNameArray }, .arraySize = count }, + { .name = "STORE_NAME", .t = HISYSEVENT_STRING_ARRAY, .v = { .array = storeNameArray }, .arraySize = count }, + { .name = "PARAM_TYPE1", .t = HISYSEVENT_UINT32_ARRAY, .v = { .array = subTypes }, .arraySize = count }, + { .name = "PARAM_TYPE2", .t = HISYSEVENT_UINT32_ARRAY, .v = { .array = costTimes }, .arraySize = count }, + { .name = "TIMES", .t = HISYSEVENT_UINT32_ARRAY, .v = { .array = occurTimes }, .arraySize = count }, + }; + auto size = sizeof(params) / sizeof(params[0]); + OH_HiSysEvent_Write(DISTRIBUTED_DATAMGR, STATISTIC_EVENT, HISYSEVENT_STATISTIC, params, size); +} + +void RdbHiViewAdapter::StartTimerThread() +{ + if (executors_ == nullptr) { + return; + } + if (running_.exchange(true)) { + return; + } + auto interval = std::chrono::seconds(WAIT_TIME); + auto fun = [this]() { + InvokeSync(); + }; + executors_->Schedule(fun, interval); +} + +void RdbHiViewAdapter::SetThreadPool(std::shared_ptr executors) +{ + executors_ = executors; + StartTimerThread(); +} + +} // namespace OHOS::DistributedRdb \ No newline at end of file diff --git a/services/distributeddataservice/service/rdb/rdb_hiview_adapter.h b/services/distributeddataservice/service/rdb/rdb_hiview_adapter.h new file mode 100644 index 0000000000000000000000000000000000000000..7e3eee660237d32157ed02dc59c7ce6f75ed6ac3 --- /dev/null +++ b/services/distributeddataservice/service/rdb/rdb_hiview_adapter.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DATAMGR_SERVICE_RDB_HIVIEW_ADAPTER_H +#define DATAMGR_SERVICE_RDB_HIVIEW_ADAPTER_H + +#include "concurrent_map.h" +#include "executor_pool.h" +#include "hisysevent.h" +#include "rdb_types.h" + +namespace OHOS::DistributedRdb { + +class RdbHiViewAdapter { +public: + static RdbHiViewAdapter &GetInstance(); + void ReportStatistic(const RdbStatEvent &stat); + void SetThreadPool(std::shared_ptr executors); + +private: + std::shared_ptr executors_ = nullptr; + ConcurrentMap statEvents_; + void InvokeSync(); + void StartTimerThread(); + std::atomic running_ = false; + static constexpr int32_t WAIT_TIME = 60 * 60 * 24; // 24h +}; +} // namespace OHOS::DistributedRdb +#endif // DATAMGR_SERVICE_RDB_HIVIEW_ADAPTER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 27d8a627d8750d739e7390787cf09c5dd52cbc7d..3f5663b0ad4ccd1cd8956ecde82fd93daa4646f7 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -55,6 +55,7 @@ #include "xcollie.h" #include "permit_delegate.h" #include "bootstrap.h" +#include "rdb_hiview_adapter.h" using OHOS::DistributedData::Anonymous; using OHOS::DistributedData::CheckerManager; using OHOS::DistributedData::MetaDataManager; @@ -864,6 +865,17 @@ int32_t RdbServiceImpl::AfterOpen(const RdbSyncerParam ¶m) return RDB_OK; } +int32_t RdbServiceImpl::ReportStatistic(const RdbSyncerParam& param, const RdbStatEvent &statEvent) +{ + if (!CheckAccess(param.bundleName_, param.storeName_)) { + ZLOGE("bundleName:%{public}s, storeName:%{public}s. Permission error", param.bundleName_.c_str(), + Anonymous::Change(param.storeName_).c_str()); + return RDB_ERROR; + } + RdbHiViewAdapter::GetInstance().ReportStatistic(statEvent); + return RDB_OK; +} + void RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) { if (executors_ != nullptr) { @@ -1021,6 +1033,7 @@ std::pair RdbServiceImpl::GetInstIndexAndUser(uint32_t tokenId int32_t RdbServiceImpl::OnBind(const BindInfo &bindInfo) { executors_ = bindInfo.executors; + RdbHiViewAdapter::GetInstance().SetThreadPool(executors_); return 0; } diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index 3a667fac51979447bf6aa6f2a852da98d1e7dc8c..3bda423c26fa99c91ddce01b83c61bb83e22b2b2 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -102,6 +102,8 @@ public: int32_t AfterOpen(const RdbSyncerParam ¶m) override; + int32_t ReportStatistic(const RdbSyncerParam ¶m, const RdbStatEvent &statEvent) override; + int32_t GetPassword(const RdbSyncerParam ¶m, std::vector> &password) override; std::pair LockCloudContainer(const RdbSyncerParam ¶m) override; diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index 6579bbe82f4868ee4665d7c60aa74f5898bb5c7a..063e73a4ced4231b0e5171706b9cd150a196bb37 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -75,6 +75,23 @@ int32_t RdbServiceStub::OnAfterOpen(MessageParcel &data, MessageParcel &reply) return RDB_OK; } +int32_t RdbServiceStub::OnReportStatistic(MessageParcel& data, MessageParcel& reply) +{ + RdbSyncerParam param; + RdbStatEvent statEvent; + if (!ITypesUtil::Unmarshal(data, param, statEvent)) { + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), + Anonymous::Change(param.storeName_).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + auto status = ReportStatistic(param, statEvent); + if (!ITypesUtil::Marshal(reply, status)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return RDB_OK; +} + int32_t RdbServiceStub::OnDelete(MessageParcel &data, MessageParcel &reply) { RdbSyncerParam param; diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.h b/services/distributeddataservice/service/rdb/rdb_service_stub.h index dfb770d5c41d00fe8543195c2de95951e4a4b7e3..a42862645ad8c21d5fc223bec6716f0e61a14bde 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.h +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.h @@ -65,6 +65,8 @@ private: int32_t OnAfterOpen(MessageParcel& data, MessageParcel& reply); + int32_t OnReportStatistic(MessageParcel& data, MessageParcel& reply); + int32_t OnDisable(MessageParcel& data, MessageParcel& reply); int32_t OnEnable(MessageParcel& data, MessageParcel& reply); @@ -115,7 +117,9 @@ RDB_UTILS_DISABLE_WARNING("-Wc99-designator") &RdbServiceStub::OnUnlockCloudContainer, [static_cast(RdbServiceCode::RDB_SERVICE_CMD_GET_DEBUG_INFO)] = &RdbServiceStub::OnGetDebugInfo, [static_cast(RdbServiceCode::RDB_SERVICE_CMD_VERIFY_PROMISE_INFO)] = - &RdbServiceStub::OnVerifyPromiseInfo + &RdbServiceStub::OnVerifyPromiseInfo, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_REPORT_STAT)] = + &RdbServiceStub::OnReportStatistic, }; RDB_UTILS_POP_WARNING }; diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index c709fe51ce69ce87115da13a612f8c229b9701d4..4579d0711828b0a691f01fa5309ac38f9680c721 100755 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -101,6 +101,7 @@ ohos_unittest("CloudDataTest") { "${data_service_path}/service/rdb/rdb_cloud.cpp", "${data_service_path}/service/rdb/rdb_cursor.cpp", "${data_service_path}/service/rdb/rdb_general_store.cpp", + "${data_service_path}/service/rdb/rdb_hiview_adapter.cpp", "${data_service_path}/service/rdb/rdb_notifier_proxy.cpp", "${data_service_path}/service/rdb/rdb_query.cpp", "${data_service_path}/service/rdb/rdb_result_set_impl.cpp", @@ -195,6 +196,7 @@ ohos_unittest("CloudServiceImplTest") { "${data_service_path}/service/rdb/rdb_cloud.cpp", "${data_service_path}/service/rdb/rdb_cursor.cpp", "${data_service_path}/service/rdb/rdb_general_store.cpp", + "${data_service_path}/service/rdb/rdb_hiview_adapter.cpp", "${data_service_path}/service/rdb/rdb_notifier_proxy.cpp", "${data_service_path}/service/rdb/rdb_query.cpp", "${data_service_path}/service/rdb/rdb_result_set_impl.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/cloudservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/cloudservicestub_fuzzer/BUILD.gn index d3d4fa0675675babce0e2c79b8c255ce92f8305a..30cc77cc748c64e3ef674068fafafea32640ed01 100644 --- a/services/distributeddataservice/service/test/fuzztest/cloudservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/cloudservicestub_fuzzer/BUILD.gn @@ -96,6 +96,7 @@ ohos_fuzztest("CloudServiceStubFuzzTest") { "${data_service_path}/service/rdb/rdb_cloud.cpp", "${data_service_path}/service/rdb/rdb_cursor.cpp", "${data_service_path}/service/rdb/rdb_general_store.cpp", + "${data_service_path}/service/rdb/rdb_hiview_adapter.cpp", "${data_service_path}/service/rdb/rdb_notifier_proxy.cpp", "${data_service_path}/service/rdb/rdb_query.cpp", "${data_service_path}/service/rdb/rdb_result_set_impl.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/rdbservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/rdbservicestub_fuzzer/BUILD.gn index 9264aa3e73a36b2bffb16611612ca68d239ab5a8..43309645c3d16e6b79f0699cab22f4d3f8c1cb21 100644 --- a/services/distributeddataservice/service/test/fuzztest/rdbservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/rdbservicestub_fuzzer/BUILD.gn @@ -85,6 +85,7 @@ ohos_fuzztest("RdbServiceStubFuzzTest") { "${data_service_path}/service/rdb/rdb_cloud.cpp", "${data_service_path}/service/rdb/rdb_cursor.cpp", "${data_service_path}/service/rdb/rdb_general_store.cpp", + "${data_service_path}/service/rdb/rdb_hiview_adapter.cpp", "${data_service_path}/service/rdb/rdb_notifier_proxy.cpp", "${data_service_path}/service/rdb/rdb_query.cpp", "${data_service_path}/service/rdb/rdb_result_set_impl.cpp",