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 
16 #ifndef DATASHARESERVICE_HIVIEW_ADAPTER_H
17 #define DATASHARESERVICE_HIVIEW_ADAPTER_H
18 
19 
20 #include "hiview_base_adapter.h"
21 #include "concurrent_map.h"
22 #include "data_share_service_stub.h"
23 namespace OHOS {
24 namespace DataShare {
25 struct CallerInfo {
26     uint64_t callerTokenId = 0;
27     uint64_t callerUid = 0;
28     uint64_t callerPid = 0;
29     uint64_t costTime = 0;
30     bool isSlowRequest = false;
31     uint32_t funcId = 0;
32 
CallerInfoCallerInfo33     CallerInfo(uint64_t tokenId, uint64_t uid, uint64_t pid, uint64_t time, uint32_t id)
34         : callerTokenId(tokenId), callerUid(uid), callerPid(pid), costTime(time), funcId(id)
35     {}
36 };
37 
38 struct CallerTotalInfo {
39     uint64_t callerUid = 0;
40     uint64_t totalCount = 0;
41     uint64_t slowRequestCount = 0;
42     uint64_t maxCostTime = 0;
43     uint64_t totalCostTime = 0;
44     std::string callerName;
45     std::map<std::string, uint64_t> funcCounts;
46 };
47 
48 class HiViewAdapter : public HiViewBaseAdapter {
49 public:
50     static HiViewAdapter &GetInstance();
51     void InvokeData() override;
52     void ReportDataStatistic(const CallerInfo &callerInfo);
53 
54 private:
55     ConcurrentMap<uint64_t, CallerTotalInfo> callers_;
56     std::map<uint32_t, std::string> funcIdMap_ = {
57         {static_cast<uint32_t>(IDataShareService::DATA_SHARE_SERVICE_CMD_QUERY), "OnQuery"},
58         {static_cast<uint32_t>(IDataShareService::DATA_SHARE_SERVICE_CMD_INSERTEX), "OnInsertEx"},
59         {static_cast<uint32_t>(IDataShareService::DATA_SHARE_SERVICE_CMD_DELETEEX), "OnDeleteEx"},
60         {static_cast<uint32_t>(IDataShareService::DATA_SHARE_SERVICE_CMD_UPDATEEX), "OnUpdateEx"},
61         {static_cast<uint32_t>(IDataShareService::DATA_SHARE_SERVICE_CMD_NOTIFY_OBSERVERS), "OnNotifyObserver"},
62         {static_cast<uint32_t>(IDataShareService::DATA_SHARE_SERVICE_CMD_GET_SILENT_PROXY_STATUS),
63             "OnGetSilentProxyStatus"}
64     };
65     void GetCallerTotalInfoVec(std::vector<CallerTotalInfo> &callerTotalInfos);
66 };
67 } // namespace DataShare
68 } // namespace OHOS
69 #endif // DATASHARESERVICE_HIVIEW_ADAPTER_H