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 "include/sp_task.h"
16 #include "include/sp_profiler_factory.h"
17 #include "include/sp_utils.h"
18 namespace OHOS {
19 namespace SmartPerf {
20
21 // init::-SESSIONID 12345678 -INTERVAL 1000 -PKG ohos.samples.ecg -c -g -t -p -f -r
ParseToTask(std::string command,TaskInfo & taskInfo)22 static ExceptionMsg ParseToTask(std::string command, TaskInfo &taskInfo)
23 {
24 std::vector<std::string> args;
25 size_t pos = 0;
26 while ((pos = command.find(" ")) != std::string::npos) {
27 args.push_back(command.substr(0, pos));
28 command.erase(0, pos + 1);
29 }
30 args.push_back(command);
31
32 for (std::string arg:args) {
33 std::cout << "arg:" << arg << std::endl;
34 }
35
36 std::string sessionId;
37 long long interval = 1000;
38 std::string pkg;
39 std::vector<std::string> configs;
40 for (size_t i = 0; i < args.size(); i++) {
41 if (args[i] == COMMAND_MAP_REVERSE.at(CommandType::CT_SESSIONID)) {
42 sessionId = args[++i];
43 } else if (args[i] == COMMAND_MAP_REVERSE.at(CommandType::CT_INTERVAL)) {
44 interval = std::stoll(args[++i]);
45 } else if (args[i] == COMMAND_MAP_REVERSE.at(CommandType::CT_PKG)) {
46 pkg = args[++i];
47 } else if (args[i] == COMMAND_MAP_REVERSE.at(CommandType::CT_NET)) {
48 std::cout << "arg:net:" << std::endl;
49 } else {
50 configs.push_back(args[i]);
51 }
52 }
53 if (sessionId.empty()) {
54 return ExceptionMsg::SESSION_ID_NULL;
55 } else if (pkg.empty()) {
56 return ExceptionMsg::PACKAGE_NULL;
57 } else if (configs.size() == 0) {
58 return ExceptionMsg::TASK_CONFIG_NULL;
59 }
60 taskInfo = { sessionId, pkg, configs, interval };
61 return ExceptionMsg::NO_ERR;
62 }
63
MapToString(std::map<std::string,std::string> myMap)64 static std::string MapToString(std::map<std::string, std::string> myMap)
65 {
66 // 将Map转换为字符串
67 std::string str = "{ ";
68 for (auto it = myMap.begin(); it != myMap.end(); ++it) {
69 str += "\"" + it->first + "\": " + it->second + ", ";
70 }
71 const int subLen = 2;
72 str.erase(str.end() - subLen, str.end());
73 str += " }";
74 return str;
75 }
76
InitTask(std::string recvStr)77 ErrCode SPTask::InitTask(std::string recvStr)
78 {
79 ExceptionMsg exMsg = ParseToTask(recvStr, curTaskInfo);
80 if (exMsg == ExceptionMsg::NO_ERR) {
81 isInit = true;
82 return ErrCode::OK;
83 }
84 if (recvStr.find("-net") != std::string::npos) {
85 isInitNet = true;
86 }
87 std::cout << "ExceptionMsg:" << ExceptionMsgMap.at(exMsg) << std::endl;
88 return ErrCode::FAILED;
89 }
StartTask(std::function<void (std::string data)> msgTask)90 ErrCode SPTask::StartTask(std::function<void(std::string data)> msgTask)
91 {
92 if (!isInit) {
93 return ErrCode::FAILED;
94 }
95 isRunning = true;
96 startTime = SPUtils::GetCurTime();
97 SpProfilerFactory::SetProfilerPkg(curTaskInfo.packageName);
98 std::string pidCmd = "pidof " + curTaskInfo.packageName;
99 std::string pidResult;
100 if (SPUtils::LoadCmd(pidCmd, pidResult)) {
101 SpProfilerFactory::SetProfilerPid(pidResult);
102 }
103 thread = std::thread([this, msgTask]() {
104 std::cout << "Task " << curTaskInfo.sessionId << ": collecting data loop..." << std::endl;
105 while (isRunning) {
106 // 执行采集任务
107 long long lastTime = SPUtils::GetCurTime();
108 std::lock_guard<std::mutex> lock(mtx);
109 std::map<std::string, std::string> dataMap;
110 for (std::string itConfig:curTaskInfo.taskConfig) {
111 SpProfiler *profiler = SpProfilerFactory::GetCmdProfilerItem(commandMap.at(itConfig));
112 std::map<std::string, std::string> itemMap = profiler->ItemData();
113 dataMap.insert(itemMap.begin(), itemMap.end());
114 }
115 if (isInitNet) {
116 std::map<std::string, std::string> itemMap = SPUtils::GetNetwork();
117 dataMap.insert(itemMap.begin(), itemMap.end());
118 }
119 SPData spdata;
120 spdata.values = dataMap;
121 vmap.push_back(spdata);
122 long long nextTime = SPUtils::GetCurTime();
123 long long costTime = nextTime - lastTime;
124 if (costTime < curTaskInfo.freq) {
125 std::this_thread::sleep_for(std::chrono::milliseconds(curTaskInfo.freq - costTime));
126 }
127 if (isRunning) {
128 msgTask(MapToString(dataMap));
129 }
130 }
131 });
132 return ErrCode::OK;
133 }
StopTask()134 void SPTask::StopTask()
135 {
136 if (isInit) {
137 std::string thisBasePath = baseOutPath + "/" + curTaskInfo.sessionId;
138 if (!SPUtils::FileAccess(thisBasePath)) {
139 std::string cmdResult;
140 SPUtils::LoadCmd("mkdir -p " + thisBasePath, cmdResult);
141 }
142 std::string outGeneralPath = thisBasePath + "/t_general_info.csv";
143 std::string outIndexpath = thisBasePath + "/t_index_info.csv";
144 long long endTime = SPUtils::GetCurTime();
145 long long testDuration = (endTime - startTime) / 1000;
146 std::map<std::string, std::string> taskInfoMap = {
147 {"sessionId", curTaskInfo.sessionId},
148 {"taskId", curTaskInfo.sessionId},
149 {"appName", curTaskInfo.packageName},
150 {"packageName", curTaskInfo.packageName},
151 {"startTime", std::to_string(startTime)},
152 {"endTime", std::to_string(endTime)},
153 {"testDuration", std::to_string(testDuration)},
154 {"taskName", "testtask"},
155 {"board", "hw"},
156 };
157 std::map<std::string, std::string> deviceInfo = SPUtils::GetDeviceInfo();
158 std::map<std::string, std::string> cpuInfo = SPUtils::GetCpuInfo();
159 std::map<std::string, std::string> gpuInfo = SPUtils::GetGpuInfo();
160 std::map<std::string, std::string> destMap;
161 destMap.insert(taskInfoMap.begin(), taskInfoMap.end());
162 destMap.insert(deviceInfo.begin(), deviceInfo.end());
163 destMap.insert(cpuInfo.begin(), cpuInfo.end());
164 destMap.insert(gpuInfo.begin(), gpuInfo.end());
165 OHOS::SmartPerf::SpCsvUtil::WriteCsvH(outGeneralPath, destMap);
166 OHOS::SmartPerf::SpCsvUtil::WriteCsv(outIndexpath, vmap);
167 }
168 isRunning = false;
169 isInit = false;
170 isInitNet = false;
171 vmap.clear();
172 if (thread.joinable()) {
173 thread.join();
174 }
175 }
176 }
177 }