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