1 /*
2 * Copyright (C) 2021 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 <cstdio>
16 #include <thread>
17 #include <cstring>
18 #include "unistd.h"
19 #include "include/sp_utils.h"
20 #include "include/sp_csv_util.h"
21 #include "include/sp_profiler_factory.h"
22 #include "include/sp_thread_socket.h"
23 #include "include/ByTrace.h"
24 #include "include/smartperf_command.h"
25 namespace OHOS {
26 namespace SmartPerf {
SmartPerfCommand(int argc,char * argv[])27 SmartPerfCommand::SmartPerfCommand(int argc, char *argv[])
28 {
29 if (argc == oneParam) {
30 daemon(0, 0);
31 InitSomething();
32 SpThreadSocket spThreadSocket;
33 std::thread tSocket(&SpThreadSocket::Process, spThreadSocket);
34 tSocket.join();
35 }
36 if (argc == twoParam) {
37 auto iterator = commandHelpMap.begin();
38 while (iterator != commandHelpMap.end()) {
39 if (strcmp(argv[1], iterator->second.c_str()) == 0) {
40 HelpCommand(iterator->first);
41 break;
42 }
43 ++iterator;
44 }
45 }
46 if (argc >= threeParamMore) {
47 for (int i = 1; i <= argc - 1; i++) {
48 std::string argStr = argv[i];
49 std::string argStr1;
50 if (i < argc - 1) {
51 argStr1 = argv[i + 1];
52 }
53 if (commandMap.count(argStr) > 0) {
54 HandleCommand(argStr, argStr1);
55 }
56 }
57 }
58 }
HelpCommand(CommandHelp type) const59 void SmartPerfCommand::HelpCommand(CommandHelp type) const
60 {
61 if (type == CommandHelp::HELP) {
62 std::cout << smartPerfMsg << std::endl;
63 }
64 if (type == CommandHelp::VERSION) {
65 std::cout << smartPerfVersion << std::endl;
66 }
67 }
HandleCommand(std::string argStr,std::string argStr1)68 void SmartPerfCommand::HandleCommand(std::string argStr, std::string argStr1)
69 {
70 switch (commandMap.at(argStr)) {
71 case CommandType::CT_N:
72 num = atoi(argStr1.c_str());
73 break;
74 case CommandType::CT_PKG:
75 pkgName = argStr1;
76 break;
77 case CommandType::CT_PID:
78 pid = argStr1;
79 break;
80 case CommandType::CT_OUT:
81 outPathParam = argStr1;
82 if (strcmp(outPathParam.c_str(), "") != 0) {
83 outPath = outPathParam + std::string(".csv");
84 }
85 break;
86 case CommandType::CT_C:
87 case CommandType::CT_G:
88 case CommandType::CT_D:
89 case CommandType::CT_F:
90 case CommandType::CT_T:
91 case CommandType::CT_P:
92 case CommandType::CT_R:
93 case CommandType::CT_TTRACE:
94 case CommandType::CT_SNAPSHOT:
95 case CommandType::CT_HW:
96 configs.push_back(argStr);
97 break;
98 default:
99 std::cout << "other unknown args:" << argStr << std::endl;
100 break;
101 }
102 SpProfilerFactory::SetProfilerPid(pid);
103 SpProfilerFactory::SetProfilerPkg(pkgName);
104 }
105
ExecCommand()106 std::string SmartPerfCommand::ExecCommand()
107 {
108 int index = 0;
109 std::vector<SPData> vmap;
110 while (index < num) {
111 std::map<std::string, std::string> spMap;
112 long long timestamp = SPUtils::GetCurTime();
113 spMap.insert(std::pair<std::string, std::string>(std::string("timestamp"), std::to_string(timestamp)));
114
115 for (size_t j = 0; j < configs.size(); j++) {
116 std::string curParam = configs[j];
117 SpProfiler *profiler = SpProfilerFactory::GetCmdProfilerItem(commandMap.at(curParam));
118 if (profiler != nullptr) {
119 std::map<std::string, std::string> data = profiler->ItemData();
120 std::map<std::string, std::string> tempData = spMap;
121 tempData.insert(data.cbegin(), data.cend());
122 spMap = tempData;
123 }
124 }
125
126 std::cout << std::endl;
127 int i = 0;
128 for (auto iter = spMap.cbegin(); iter != spMap.cend(); ++iter) {
129 printf("order:%d %s=%s\n", i, iter->first.c_str(), iter->second.c_str());
130 i++;
131 }
132 std::cout << std::endl;
133
134 SPData spdata;
135 spdata.values = spMap;
136 vmap.push_back(spdata);
137 sleep(1);
138 index++;
139 }
140 SpCsvUtil::WriteCsv(std::string(outPath.c_str()), vmap);
141 return std::string("command exec finished!");
142 }
InitSomething()143 void SmartPerfCommand::InitSomething()
144 {
145 std::string cmdResult;
146 if (SPUtils::LoadCmd("chmod o+r /proc/stat", cmdResult)) {
147 printf("Privilege escalation! \n");
148 };
149 if (!SPUtils::FileAccess("/data/local/tmp/capture")) {
150 SPUtils::LoadCmd("mkdir /data/local/tmp/capture", cmdResult);
151 printf("/data/local/tmp/capture created! \n");
152 };
153 }
154 }
155 }
156