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