1 /*
2  * Copyright (C) 2024 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 OHOS_IPC_PAYLOAD_STATISTICS_IMPL_H
17 #define OHOS_IPC_PAYLOAD_STATISTICS_IMPL_H
18 
19 #include <map>
20 #include <shared_mutex>
21 #include <string>
22 #include <vector>
23 #include <atomic>
24 
25 namespace OHOS {
26 
27 struct IPCInterfaceInfo {
28     std::u16string desc;
29     int32_t code;
30 };
31 
32 struct IPCPayloadCost {
33     uint64_t totalCost;
34     uint32_t maxCost;
35     uint32_t minCost;
36     uint32_t averCost;
37 };
38 
39 struct IPCPayloadInfo {
40     IPCInterfaceInfo interface;
41     IPCPayloadCost cost;
42     uint64_t count;
43 };
44 
45 class IPCPayloadStatisticsImpl {
46 public:
47     static IPCPayloadStatisticsImpl& GetInstance();
48     IPCPayloadStatisticsImpl();
49     ~IPCPayloadStatisticsImpl();
50 
51     uint64_t GetTotalCount();
52     uint64_t GetTotalCost();
53 
54     std::vector<int32_t> GetPids();
55     uint64_t GetCount(const int32_t pid);
56     uint64_t GetCost(const int32_t pid);
57 
58     std::vector<IPCInterfaceInfo> GetDescriptorCodes(const int32_t pid);
59     uint64_t GetDescriptorCodeCount(const int32_t pid, const std::u16string &desc, const int32_t code);
60     IPCPayloadCost GetDescriptorCodeCost(const int32_t pid, const std::u16string &desc, const int32_t code);
61 
62     bool StartStatistics();
63     bool StopStatistics();
64     bool GetStatisticsStatus();
65     bool ClearStatisticsData();
66 
67     bool UpdatePayloadInfo(
68         const int32_t pid, const std::u16string &desc, const int32_t code, const uint32_t currentCost);
69 private:
70     bool GetPayloadInfo(
71         const int32_t pid, const std::u16string &desc, const int32_t code, IPCPayloadInfo &payloadInfo);
72     std::shared_mutex dataMutex_;
73     std::map<int32_t, std::map<std::u16string, IPCPayloadInfo>> payloadStat_;
74     std::atomic<bool> isStatisticsFlag_;
75 };
76 } // namespace OHOS
77 #endif // OHOS_IPC_PAYLOAD_STATISTICS_IMPL_H
78