From 334c0ca9f6fcd2a72c729679ab09a7de4068c278 Mon Sep 17 00:00:00 2001 From: shu-xin0115 Date: Tue, 19 Aug 2025 11:58:19 +0800 Subject: [PATCH] add normal bundle hiview Signed-off-by: shu-xin0115 --- .../data_share/common/bundle_mgr_proxy.cpp | 5 + .../service/data_share/dfx/hiview_adapter.cpp | 279 +++++++++--------- .../service/data_share/dfx/hiview_adapter.h | 139 ++++----- 3 files changed, 223 insertions(+), 200 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp index 267a66c32..ed89ae59b 100644 --- a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp @@ -25,6 +25,7 @@ #include "system_ability_definition.h" #include "uri_utils.h" #include "ipc_skeleton.h" +#include "hiview_adapter.h" #include "hiview_fault_adapter.h" namespace OHOS::DataShare { @@ -122,6 +123,10 @@ int BundleMgrProxy::GetBundleInfoFromBMSWithCheck( if (res != E_OK) { return res; } + // report bundleName of normal app + if (!bundleConfig.isSystemApp) { + HiviewAdapter::GetInstance().ReportNormalApp(bundleName); + } // Not allow normal app visit normal app. if (!DataShareThreadLocal::IsFromSystemApp() && !bundleConfig.isSystemApp) { ZLOGE("Not allow normal app visit normal app, bundle:%{public}s, callingPid:%{public}d", diff --git a/services/distributeddataservice/service/data_share/dfx/hiview_adapter.cpp b/services/distributeddataservice/service/data_share/dfx/hiview_adapter.cpp index 21e4eba9d..8fb3b28a4 100644 --- a/services/distributeddataservice/service/data_share/dfx/hiview_adapter.cpp +++ b/services/distributeddataservice/service/data_share/dfx/hiview_adapter.cpp @@ -1,133 +1,148 @@ -/* -* 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 "HiViewAdapter" - -#include "hiview_adapter.h" - -#include -#include "hiview_fault_adapter.h" -#include "log_print.h" - -namespace OHOS { -namespace DataShare { -namespace { -constexpr char DOMAIN[] = "DISTDATAMGR"; -constexpr const char *EVENT_NAME = "DATA_SHARE_STATISTIC"; -constexpr const uint32_t MAX_COLLECT_COUNT = 20; -constexpr const uint32_t MIN_CALL_COUNT = 1000; -constexpr const size_t PARAMS_SIZE = 7; -} - -using namespace std::chrono; - -HiViewAdapter& HiViewAdapter::GetInstance() -{ - static HiViewAdapter instance; - return instance; -} - -void HiViewAdapter::ReportDataStatistic(const CallerInfo &callerInfo) -{ - if (executors_ == nullptr) { - ZLOGE("executors is nullptr, collect failed"); - return; - } - - callers_.Compute(callerInfo.callerTokenId, [&callerInfo, this](const uint64_t &key, CallerTotalInfo &value) { - if (value.totalCount == 0) { - value.callerUid = callerInfo.callerUid; - std::string callingName = HiViewFaultAdapter::GetCallingName(key).first; - value.callerName = callingName.empty() ? std::to_string(callerInfo.callerPid) : callingName; - } - value.totalCount += 1; - value.maxCostTime = std::max(callerInfo.costTime, value.maxCostTime); - value.totalCostTime += callerInfo.costTime; - value.slowRequestCount = value.slowRequestCount + (callerInfo.isSlowRequest ? 1 : 0); - auto it = funcIdMap_.find(callerInfo.funcId); - if (it != funcIdMap_.end()) { - auto funcName = it->second; - value.funcCounts[funcName] = value.funcCounts[funcName] + 1; - } - return true; - }); -} - -void HiViewAdapter::InvokeData() -{ - uint32_t mapSize = callers_.Size(); - if (mapSize == 0) { return; } - std::vector callerTotalInfos; - GetCallerTotalInfoVec(callerTotalInfos); - uint32_t count = std::min(static_cast(callerTotalInfos.size()), MAX_COLLECT_COUNT); - if (count == 0) { return; } - int64_t callerUids[count]; - char* callerNames[count]; - int64_t totalCounts[count]; - int64_t slowRequestCounts[count]; - int64_t maxCostTimes[count]; - int64_t totalCostTimes[count]; - char* funcCounts[count]; - std::vector funcCountsHolder(count); - for (uint32_t i = 0; i < count; i++) { - CallerTotalInfo &callerTotalInfo = callerTotalInfos[i]; - callerUids[i] = callerTotalInfo.callerUid; - callerNames[i] = const_cast(callerTotalInfo.callerName.c_str()); - totalCounts[i] = callerTotalInfo.totalCount; - slowRequestCounts[i] = callerTotalInfo.slowRequestCount; - maxCostTimes[i] = callerTotalInfo.maxCostTime; - totalCostTimes[i] = callerTotalInfo.totalCostTime; - std::string funcCount = "{"; - for (const auto &it : callerTotalInfo.funcCounts) { - funcCount += it.first + ":" + std::to_string(it.second) + ","; - } - funcCount = funcCount.substr(0, funcCount.size() - 1) + "}"; - funcCountsHolder[i] = std::move(funcCount); - funcCounts[i] = const_cast(funcCountsHolder[i].c_str()); - } - - HiSysEventParam callerUid = { .name = "COLLIE_UID", .t = HISYSEVENT_INT64_ARRAY, - .v = { .array = callerUids }, .arraySize = count }; - HiSysEventParam callerName = { .name = "CALLER_NAME", .t = HISYSEVENT_STRING_ARRAY, - .v = { .array = callerNames }, .arraySize = count }; - HiSysEventParam totalCount = { .name = "TOTAL_COUNT", .t = HISYSEVENT_INT64_ARRAY, - .v = { .array = totalCounts }, .arraySize = count }; - HiSysEventParam slowRequestCount = { .name = "SLOW_RQUEST_COUNT", .t = HISYSEVENT_INT64_ARRAY, - .v = { .array = slowRequestCounts }, .arraySize = count }; - HiSysEventParam maxCostTime = { .name = "MAX_COST_TIME", .t = HISYSEVENT_INT64_ARRAY, - .v = { .array = maxCostTimes }, .arraySize = count }; - HiSysEventParam totalCostTime = { .name = "TOTAL_COST_TIME", .t = HISYSEVENT_INT64_ARRAY, - .v = { .array = totalCostTimes }, .arraySize = count }; - HiSysEventParam funcCount = { .name = "FUNC_COUNTS", .t = HISYSEVENT_STRING_ARRAY, - .v = { .array = funcCounts }, .arraySize = count }; - HiSysEventParam params[] = {callerUid, callerName, totalCount, slowRequestCount, - maxCostTime, totalCostTime, funcCount}; - int res = OH_HiSysEvent_Write(DOMAIN, EVENT_NAME, HISYSEVENT_STATISTIC, params, PARAMS_SIZE); - ZLOGI("OH_HiSysEvent_Write, res = %{public}d", res); -} - -void HiViewAdapter::GetCallerTotalInfoVec(std::vector &callerTotalInfos) -{ - callers_.EraseIf([&callerTotalInfos](const uint64_t key, CallerTotalInfo &callerTotalInfo) { - if (callerTotalInfo.totalCount >= MIN_CALL_COUNT) - callerTotalInfos.push_back(callerTotalInfo); - return true; - }); - std::sort(callerTotalInfos.begin(), callerTotalInfos.end(), - [](const CallerTotalInfo &first, const CallerTotalInfo &second) { - return first.totalCount > second.totalCount; - }); -} -} // namespace DataShare +/* +* 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 "HiViewAdapter" + +#include "hiview_adapter.h" + +#include +#include "hiview_fault_adapter.h" +#include "log_print.h" + +namespace OHOS { +namespace DataShare { +namespace { +constexpr char DOMAIN[] = "DISTDATAMGR"; +constexpr const char *EVENT_NAME = "DATA_SHARE_STATISTIC"; +constexpr const uint32_t MAX_COLLECT_COUNT = 20; +constexpr const uint32_t MIN_CALL_COUNT = 1000; +constexpr const size_t PARAMS_SIZE = 8; +} + +using namespace std::chrono; + +HiViewAdapter& HiViewAdapter::GetInstance() +{ + static HiViewAdapter instance; + return instance; +} + +void HiViewAdapter::ReportDataStatistic(const CallerInfo &callerInfo) +{ + if (executors_ == nullptr) { + ZLOGE("executors is nullptr, collect failed"); + return; + } + + callers_.Compute(callerInfo.callerTokenId, [&callerInfo, this](const uint64_t &key, CallerTotalInfo &value) { + if (value.totalCount == 0) { + value.callerUid = callerInfo.callerUid; + std::string callingName = HiViewFaultAdapter::GetCallingName(key).first; + value.callerName = callingName.empty() ? std::to_string(callerInfo.callerPid) : callingName; + } + value.totalCount += 1; + value.maxCostTime = std::max(callerInfo.costTime, value.maxCostTime); + value.totalCostTime += callerInfo.costTime; + value.slowRequestCount = value.slowRequestCount + (callerInfo.isSlowRequest ? 1 : 0); + auto it = funcIdMap_.find(callerInfo.funcId); + if (it != funcIdMap_.end()) { + auto funcName = it->second; + value.funcCounts[funcName] = value.funcCounts[funcName] + 1; + } + return true; + }); +} + +void HiViewAdapter::ReportNormalApp(const std::string &bundleName) +{ + if (normalApps_.find(bundleName) == normalApps_.end()) { + normalApps_.insert(bundleName); + } +} + +void HiViewAdapter::InvokeData() +{ + uint32_t mapSize = callers_.Size(); + if (mapSize == 0) { return; } + std::vector callerTotalInfos; + GetCallerTotalInfoVec(callerTotalInfos); + uint32_t count = std::min(static_cast(callerTotalInfos.size()), MAX_COLLECT_COUNT); + if (count == 0) { return; } + int64_t callerUids[count]; + char* callerNames[count]; + int64_t totalCounts[count]; + int64_t slowRequestCounts[count]; + int64_t maxCostTimes[count]; + int64_t totalCostTimes[count]; + char* funcCounts[count]; + std::vector funcCountsHolder(count); + for (uint32_t i = 0; i < count; i++) { + CallerTotalInfo &callerTotalInfo = callerTotalInfos[i]; + callerUids[i] = callerTotalInfo.callerUid; + callerNames[i] = const_cast(callerTotalInfo.callerName.c_str()); + totalCounts[i] = callerTotalInfo.totalCount; + slowRequestCounts[i] = callerTotalInfo.slowRequestCount; + maxCostTimes[i] = callerTotalInfo.maxCostTime; + totalCostTimes[i] = callerTotalInfo.totalCostTime; + std::string funcCount = "{"; + for (const auto &it : callerTotalInfo.funcCounts) { + funcCount += it.first + ":" + std::to_string(it.second) + ","; + } + funcCount = funcCount.substr(0, funcCount.size() - 1) + "}"; + funcCountsHolder[i] = std::move(funcCount); + funcCounts[i] = const_cast(funcCountsHolder[i].c_str()); + } + size_t appCount = normalApps_.size(); + char* normalAppInfos[appCount]; + size_t index = 0; + for (const auto &it : normalApps_) { + normalAppInfos[index++] = const_cast(it.c_str()); + } + normalApps_.clear(); + HiSysEventParam callerUid = { .name = "COLLIE_UID", .t = HISYSEVENT_INT64_ARRAY, + .v = { .array = callerUids }, .arraySize = count }; + HiSysEventParam callerName = { .name = "CALLER_NAME", .t = HISYSEVENT_STRING_ARRAY, + .v = { .array = callerNames }, .arraySize = count }; + HiSysEventParam totalCount = { .name = "TOTAL_COUNT", .t = HISYSEVENT_INT64_ARRAY, + .v = { .array = totalCounts }, .arraySize = count }; + HiSysEventParam slowRequestCount = { .name = "SLOW_RQUEST_COUNT", .t = HISYSEVENT_INT64_ARRAY, + .v = { .array = slowRequestCounts }, .arraySize = count }; + HiSysEventParam maxCostTime = { .name = "MAX_COST_TIME", .t = HISYSEVENT_INT64_ARRAY, + .v = { .array = maxCostTimes }, .arraySize = count }; + HiSysEventParam totalCostTime = { .name = "TOTAL_COST_TIME", .t = HISYSEVENT_INT64_ARRAY, + .v = { .array = totalCostTimes }, .arraySize = count }; + HiSysEventParam funcCount = { .name = "FUNC_COUNTS", .t = HISYSEVENT_STRING_ARRAY, + .v = { .array = funcCounts }, .arraySize = count }; + HiSysEventParam normalAppInfo = { .name = "NORMAL_APP_INFO", .t = HISYSEVENT_STRING_ARRAY, + .v = { .array = normalAppInfos }, .arraySize = appCount }; + HiSysEventParam params[] = {callerUid, callerName, totalCount, slowRequestCount, + maxCostTime, totalCostTime, funcCount, normalAppInfo}; + int res = OH_HiSysEvent_Write(DOMAIN, EVENT_NAME, HISYSEVENT_STATISTIC, params, PARAMS_SIZE); + ZLOGI("OH_HiSysEvent_Write, res = %{public}d", res); +} + +void HiViewAdapter::GetCallerTotalInfoVec(std::vector &callerTotalInfos) +{ + callers_.EraseIf([&callerTotalInfos](const uint64_t key, CallerTotalInfo &callerTotalInfo) { + if (callerTotalInfo.totalCount >= MIN_CALL_COUNT) + callerTotalInfos.push_back(callerTotalInfo); + return true; + }); + std::sort(callerTotalInfos.begin(), callerTotalInfos.end(), + [](const CallerTotalInfo &first, const CallerTotalInfo &second) { + return first.totalCount > second.totalCount; + }); +} +} // namespace DataShare } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/dfx/hiview_adapter.h b/services/distributeddataservice/service/data_share/dfx/hiview_adapter.h index 0b13a3bb0..4a965375a 100644 --- a/services/distributeddataservice/service/data_share/dfx/hiview_adapter.h +++ b/services/distributeddataservice/service/data_share/dfx/hiview_adapter.h @@ -1,69 +1,72 @@ -/* -* 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 DATASHARESERVICE_HIVIEW_ADAPTER_H -#define DATASHARESERVICE_HIVIEW_ADAPTER_H - - -#include "hiview_base_adapter.h" -#include "concurrent_map.h" -#include "data_share_service_stub.h" -namespace OHOS { -namespace DataShare { -struct CallerInfo { - uint64_t callerTokenId = 0; - uint64_t callerUid = 0; - uint64_t callerPid = 0; - uint64_t costTime = 0; - bool isSlowRequest = false; - uint32_t funcId = 0; - - CallerInfo(uint64_t tokenId, uint64_t uid, uint64_t pid, uint64_t time, uint32_t id) - : callerTokenId(tokenId), callerUid(uid), callerPid(pid), costTime(time), funcId(id) - {} -}; - -struct CallerTotalInfo { - uint64_t callerUid = 0; - uint64_t totalCount = 0; - uint64_t slowRequestCount = 0; - uint64_t maxCostTime = 0; - uint64_t totalCostTime = 0; - std::string callerName; - std::map funcCounts; -}; - -class HiViewAdapter : public HiViewBaseAdapter { -public: - static HiViewAdapter &GetInstance(); - void InvokeData() override; - void ReportDataStatistic(const CallerInfo &callerInfo); - -private: - ConcurrentMap callers_; - std::map funcIdMap_ = { - {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_QUERY), "OnQuery"}, - {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_INSERTEX), "OnInsertEx"}, - {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_DELETEEX), "OnDeleteEx"}, - {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_UPDATEEX), "OnUpdateEx"}, - {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_NOTIFY_OBSERVERS), "OnNotifyObserver"}, - {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_GET_SILENT_PROXY_STATUS), - "OnGetSilentProxyStatus"} - }; - void GetCallerTotalInfoVec(std::vector &callerTotalInfos); -}; -} // namespace DataShare -} // namespace OHOS +/* +* 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 DATASHARESERVICE_HIVIEW_ADAPTER_H +#define DATASHARESERVICE_HIVIEW_ADAPTER_H + + +#include "hiview_base_adapter.h" +#include "concurrent_map.h" +#include "data_share_service_stub.h" +#include +namespace OHOS { +namespace DataShare { +struct CallerInfo { + uint64_t callerTokenId = 0; + uint64_t callerUid = 0; + uint64_t callerPid = 0; + uint64_t costTime = 0; + bool isSlowRequest = false; + uint32_t funcId = 0; + + CallerInfo(uint64_t tokenId, uint64_t uid, uint64_t pid, uint64_t time, uint32_t id) + : callerTokenId(tokenId), callerUid(uid), callerPid(pid), costTime(time), funcId(id) + {} +}; + +struct CallerTotalInfo { + uint64_t callerUid = 0; + uint64_t totalCount = 0; + uint64_t slowRequestCount = 0; + uint64_t maxCostTime = 0; + uint64_t totalCostTime = 0; + std::string callerName; + std::map funcCounts; +}; + +class HiViewAdapter : public HiViewBaseAdapter { +public: + static HiViewAdapter &GetInstance(); + void InvokeData() override; + void ReportDataStatistic(const CallerInfo &callerInfo); + void ReportNormalApp(const std::string &bundleName); + +private: + ConcurrentMap callers_; + std::unordered_set normalApps_; + std::map funcIdMap_ = { + {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_QUERY), "OnQuery"}, + {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_INSERTEX), "OnInsertEx"}, + {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_DELETEEX), "OnDeleteEx"}, + {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_UPDATEEX), "OnUpdateEx"}, + {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_NOTIFY_OBSERVERS), "OnNotifyObserver"}, + {static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_GET_SILENT_PROXY_STATUS), + "OnGetSilentProxyStatus"} + }; + void GetCallerTotalInfoVec(std::vector &callerTotalInfos); +}; +} // namespace DataShare +} // namespace OHOS #endif // DATASHARESERVICE_HIVIEW_ADAPTER_H \ No newline at end of file -- Gitee