• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "HiViewAdapter"
16 
17 #include "hiview_adapter.h"
18 
19 #include <algorithm>
20 #include "hiview_fault_adapter.h"
21 #include "log_print.h"
22 
23 namespace OHOS {
24 namespace DataShare {
25 namespace {
26 constexpr char DOMAIN[] = "DISTDATAMGR";
27 constexpr const char *EVENT_NAME = "DATA_SHARE_STATISTIC";
28 constexpr const uint32_t MAX_COLLECT_COUNT = 20;
29 constexpr const uint32_t MIN_CALL_COUNT = 1000;
30 constexpr const size_t PARAMS_SIZE = 7;
31 }
32 
33 using namespace std::chrono;
34 
GetInstance()35 HiViewAdapter& HiViewAdapter::GetInstance()
36 {
37     static HiViewAdapter instance;
38     return instance;
39 }
40 
ReportDataStatistic(const CallerInfo & callerInfo)41 void HiViewAdapter::ReportDataStatistic(const CallerInfo &callerInfo)
42 {
43     if (executors_ == nullptr) {
44         ZLOGE("executors is nullptr, collect failed");
45         return;
46     }
47 
48     callers_.Compute(callerInfo.callerTokenId, [&callerInfo, this](const uint64_t &key, CallerTotalInfo &value) {
49         if (value.totalCount == 0) {
50             value.callerUid = callerInfo.callerUid;
51             std::string callingName = HiViewFaultAdapter::GetCallingName(key).first;
52             value.callerName = callingName.empty() ? std::to_string(callerInfo.callerPid) : callingName;
53         }
54         value.totalCount += 1;
55         value.maxCostTime = std::max(callerInfo.costTime, value.maxCostTime);
56         value.totalCostTime += callerInfo.costTime;
57         value.slowRequestCount = value.slowRequestCount + (callerInfo.isSlowRequest ? 1 : 0);
58         auto it = funcIdMap_.find(callerInfo.funcId);
59         if (it != funcIdMap_.end()) {
60             auto funcName = it->second;
61             value.funcCounts[funcName] = value.funcCounts[funcName] + 1;
62         }
63         return true;
64     });
65 }
66 
InvokeData()67 void HiViewAdapter::InvokeData()
68 {
69     uint32_t mapSize = callers_.Size();
70     if (mapSize == 0) { return; }
71     std::vector<CallerTotalInfo> callerTotalInfos;
72     GetCallerTotalInfoVec(callerTotalInfos);
73     uint32_t count = std::min(static_cast<uint32_t>(callerTotalInfos.size()), MAX_COLLECT_COUNT);
74     if (count == 0) { return; }
75     int64_t callerUids[count];
76     char* callerNames[count];
77     int64_t totalCounts[count];
78     int64_t slowRequestCounts[count];
79     int64_t maxCostTimes[count];
80     int64_t totalCostTimes[count];
81     char* funcCounts[count];
82     std::vector<std::string> funcCountsHolder(count);
83     for (uint32_t i = 0; i < count; i++) {
84         CallerTotalInfo &callerTotalInfo = callerTotalInfos[i];
85         callerUids[i] = callerTotalInfo.callerUid;
86         callerNames[i] = const_cast<char *>(callerTotalInfo.callerName.c_str());
87         totalCounts[i] = callerTotalInfo.totalCount;
88         slowRequestCounts[i] = callerTotalInfo.slowRequestCount;
89         maxCostTimes[i] = callerTotalInfo.maxCostTime;
90         totalCostTimes[i] = callerTotalInfo.totalCostTime;
91         std::string funcCount = "{";
92         for (const auto &it : callerTotalInfo.funcCounts) {
93             funcCount += it.first + ":" + std::to_string(it.second) + ",";
94         }
95         funcCount = funcCount.substr(0, funcCount.size() - 1) + "}";
96         funcCountsHolder[i] = std::move(funcCount);
97         funcCounts[i] = const_cast<char *>(funcCountsHolder[i].c_str());
98     }
99 
100     HiSysEventParam callerUid = { .name = "COLLIE_UID", .t = HISYSEVENT_INT64_ARRAY,
101         .v = { .array = callerUids }, .arraySize = count };
102     HiSysEventParam callerName = { .name = "CALLER_NAME", .t = HISYSEVENT_STRING_ARRAY,
103         .v = { .array = callerNames }, .arraySize = count };
104     HiSysEventParam totalCount = { .name = "TOTAL_COUNT", .t = HISYSEVENT_INT64_ARRAY,
105         .v = { .array = totalCounts }, .arraySize = count };
106     HiSysEventParam slowRequestCount = { .name = "SLOW_RQUEST_COUNT", .t = HISYSEVENT_INT64_ARRAY,
107         .v = { .array = slowRequestCounts }, .arraySize = count };
108     HiSysEventParam maxCostTime = { .name = "MAX_COST_TIME", .t = HISYSEVENT_INT64_ARRAY,
109         .v = { .array = maxCostTimes }, .arraySize = count };
110     HiSysEventParam totalCostTime = { .name = "TOTAL_COST_TIME", .t = HISYSEVENT_INT64_ARRAY,
111         .v = { .array = totalCostTimes }, .arraySize = count };
112     HiSysEventParam funcCount = { .name = "FUNC_COUNTS", .t = HISYSEVENT_STRING_ARRAY,
113         .v = { .array = funcCounts }, .arraySize = count };
114     HiSysEventParam params[] = {callerUid, callerName, totalCount, slowRequestCount,
115         maxCostTime, totalCostTime, funcCount};
116     int res = OH_HiSysEvent_Write(DOMAIN, EVENT_NAME, HISYSEVENT_STATISTIC, params, PARAMS_SIZE);
117     ZLOGI("OH_HiSysEvent_Write, res = %{public}d", res);
118 }
119 
GetCallerTotalInfoVec(std::vector<CallerTotalInfo> & callerTotalInfos)120 void HiViewAdapter::GetCallerTotalInfoVec(std::vector<CallerTotalInfo> &callerTotalInfos)
121 {
122     callers_.EraseIf([&callerTotalInfos](const uint64_t key, CallerTotalInfo &callerTotalInfo) {
123         if (callerTotalInfo.totalCount >= MIN_CALL_COUNT)
124             callerTotalInfos.push_back(callerTotalInfo);
125         return true;
126     });
127     std::sort(callerTotalInfos.begin(), callerTotalInfos.end(),
128         [](const CallerTotalInfo &first, const CallerTotalInfo &second) {
129         return first.totalCount > second.totalCount;
130     });
131 }
132 } // namespace DataShare
133 } // namespace OHOS