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 #include "dump_ipc_helper.h"
17 
18 #include <vector>
19 #include <sys/types.h>
20 #include <unistd.h>
21 
22 #include "ipc_payload_statistics.h"
23 #include "string_ex.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 const std::string DUMP_IPC_CMD_SUCCESS = "success";
28 const std::string DUMP_IPC_CMD_FAIL = "fail";
29 
DumpIpcStart(std::string & result)30 void DumpIpcHelper::DumpIpcStart(std::string& result)
31 {
32     result += "StartIpcStatistics pid: " + std::to_string(getprocpid()) + "\t";
33     if (IPCPayloadStatistics::StartStatistics()) {
34         result += DUMP_IPC_CMD_SUCCESS;
35         return;
36     }
37     result += DUMP_IPC_CMD_FAIL;
38 }
39 
DumpIpcStop(std::string & result)40 void DumpIpcHelper::DumpIpcStop(std::string& result)
41 {
42     result += "StopIpcStatistics pid: " + std::to_string(getprocpid()) + "\t";
43     if (IPCPayloadStatistics::StopStatistics()) {
44         result += DUMP_IPC_CMD_SUCCESS;
45         return;
46     }
47     result += DUMP_IPC_CMD_FAIL;
48 }
49 
DumpIpcStat(std::string & result)50 void DumpIpcHelper::DumpIpcStat(std::string& result)
51 {
52     result += "********************************GlobalStatisticsInfo********************************";
53     result += "\nCurrentPid:";
54     result += std::to_string(getprocpid());
55     result += "\nTotalCount:";
56     result += std::to_string(IPCPayloadStatistics::GetTotalCount());
57     result += "\nTotalTimeCost:";
58     result += std::to_string(IPCPayloadStatistics::GetTotalCost());
59     std::vector<int32_t> pids = IPCPayloadStatistics::GetPids();
60     for (int32_t pid : pids) {
61         result += "\n--------------------------------ProcessStatisticsInfo-------------------------------";
62         result += "\nCallingPid:";
63         result += std::to_string(pid);
64         result += "\nCallingPidTotalCount:";
65         result += std::to_string(IPCPayloadStatistics::GetCount(pid));
66         result += "\nCallingPidTotalTimeCost:";
67         result += std::to_string(IPCPayloadStatistics::GetCost(pid));
68         std::vector<OHOS::IPCInterfaceInfo> ipcInterfaceInfos = IPCPayloadStatistics::GetDescriptorCodes(pid);
69         for (const auto& ipcInterfaceInfo : ipcInterfaceInfos) {
70             result += "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~InterfaceStatisticsInfo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
71             result += "\nDescriptorCode:";
72             result += Str16ToStr8(ipcInterfaceInfo.desc) + std::string("_") + std::to_string(ipcInterfaceInfo.code);
73             result += "\nDescriptorCodeCount:";
74             result += std::to_string(
75                 IPCPayloadStatistics::GetDescriptorCodeCount(pid, ipcInterfaceInfo.desc, ipcInterfaceInfo.code));
76             result += "\nDescriptorCodeTimeCost:";
77             result += "\nTotal:";
78             OHOS::IPCPayloadCost descriptorCodeCost = IPCPayloadStatistics::GetDescriptorCodeCost(
79                 pid, ipcInterfaceInfo.desc, ipcInterfaceInfo.code);
80             result += std::to_string(descriptorCodeCost.totalCost);
81             result += " | Max:";
82             result += std::to_string(descriptorCodeCost.maxCost);
83             result += " | Min:";
84             result += std::to_string(descriptorCodeCost.minCost);
85             result += " | Avg:";
86             result += std::to_string(descriptorCodeCost.averCost);
87             result += "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
88         }
89         result += "\n------------------------------------------------------------------------------------";
90     }
91     result += "\n************************************************************************************\n";
92 }
93 }  // namespace AppExecFwk
94 }  // namespace OHOS
95