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 #include "local_ability_manager_dumper.h"
16 
17 #include "vector"
18 #include "unistd.h"
19 #include "string_ex.h"
20 #include "ipc_payload_statistics.h"
21 
22 namespace OHOS {
23 constexpr const char* DUMP_SUCCESS = " success\n";
24 constexpr const char* DUMP_FAIL = " fail\n";
25 
StartIpcStatistics(std::string & result)26 bool LocalAbilityManagerDumper::StartIpcStatistics(std::string& result)
27 {
28     result = std::string("StartIpcStatistics pid:") + std::to_string(getpid());
29     bool ret = IPCPayloadStatistics::StartStatistics();
30     result += ret ? DUMP_SUCCESS : DUMP_FAIL;
31     return ret;
32 }
33 
StopIpcStatistics(std::string & result)34 bool LocalAbilityManagerDumper::StopIpcStatistics(std::string& result)
35 {
36     result = std::string("StopIpcStatistics pid:") + std::to_string(getpid());
37     bool ret = IPCPayloadStatistics::StopStatistics();
38     result += ret ? DUMP_SUCCESS : DUMP_FAIL;
39     return ret;
40 }
41 
GetIpcStatistics(std::string & result)42 bool LocalAbilityManagerDumper::GetIpcStatistics(std::string& result)
43 {
44     result += "********************************GlobalStatisticsInfo********************************";
45     result += "\nCurrentPid:";
46     result += std::to_string(getpid());
47     result += "\nTotalCount:";
48     result += std::to_string(IPCPayloadStatistics::GetTotalCount());
49     result += "\nTotalTimeCost:";
50     result += std::to_string(IPCPayloadStatistics::GetTotalCost());
51     std::vector<int32_t> pids;
52     pids = IPCPayloadStatistics::GetPids();
53     for (unsigned int i = 0; i < pids.size(); i++) {
54         result += "\n--------------------------------ProcessStatisticsInfo-------------------------------";
55         result += "\nCallingPid:";
56         result += std::to_string(pids[i]);
57         result += "\nCallingPidTotalCount:";
58         result += std::to_string(IPCPayloadStatistics::GetCount(pids[i]));
59         result += "\nCallingPidTotalTimeCost:";
60         result += std::to_string(IPCPayloadStatistics::GetCost(pids[i]));
61         std::vector<IPCInterfaceInfo> intfs;
62         intfs = IPCPayloadStatistics::GetDescriptorCodes(pids[i]);
63         for (unsigned int j = 0; j < intfs.size(); j++) {
64             result += "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~InterfaceStatisticsInfo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
65             result += "\nDescriptorCode:";
66             result += Str16ToStr8(intfs[j].desc) + std::string("_") + std::to_string(intfs[j].code);
67             result += "\nDescriptorCodeCount:";
68             result += std::to_string(
69                 IPCPayloadStatistics::GetDescriptorCodeCount(pids[i], intfs[j].desc, intfs[j].code));
70             result += "\nDescriptorCodeTimeCost:";
71             result += "\nTotal:";
72             result += std::to_string(
73                 IPCPayloadStatistics::GetDescriptorCodeCost(pids[i], intfs[j].desc, intfs[j].code).totalCost);
74             result += " | Max:";
75             result += std::to_string(
76                 IPCPayloadStatistics::GetDescriptorCodeCost(pids[i], intfs[j].desc, intfs[j].code).maxCost);
77             result += " | Min:";
78             result += std::to_string(
79                 IPCPayloadStatistics::GetDescriptorCodeCost(pids[i], intfs[j].desc, intfs[j].code).minCost);
80             result += " | Avg:";
81             result += std::to_string(
82                 IPCPayloadStatistics::GetDescriptorCodeCost(pids[i], intfs[j].desc, intfs[j].code).averCost);
83             result += "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
84         }
85         result += "\n------------------------------------------------------------------------------------";
86     }
87     result += "\n************************************************************************************\n";
88     return true;
89 }
90 } // namespace OHOS