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 {
48 configs.push_back(args[i]);
49 }
50 }
51 if (sessionId.empty()) {
52 return ExceptionMsg::SESSION_ID_NULL;
53 } else if (configs.size() == 0) {
54 return ExceptionMsg::TASK_CONFIG_NULL;
55 }
56 taskInfo = { sessionId, pkg, configs, interval };
57 return ExceptionMsg::NO_ERR;
58 }
59
MapToString(std::map<std::string,std::string> myMap)60 static std::string MapToString(std::map<std::string, std::string> myMap)
61 {
62 // 将Map转换为字符串
63 std::string str = "{ ";
64 for (auto it = myMap.begin(); it != myMap.end(); ++it) {
65 str += "\"" + it->first + "\": " + it->second + ", ";
66 }
67 const int subLen = 2;
68 str.erase(str.end() - subLen, str.end());
69 str += " }";
70 return str;
71 }
72
InitTask(std::string recvStr)73 ErrCode SPTask::InitTask(std::string recvStr)
74 {
75 ExceptionMsg exMsg = ParseToTask(recvStr, curTaskInfo);
76 if (exMsg == ExceptionMsg::NO_ERR) {
77 isInit = true;
78 return ErrCode::OK;
79 }
80 std::cout << "ExceptionMsg:" << ExceptionMsgMap.at(exMsg) << std::endl;
81 return ErrCode::FAILED;
82 }
StartTask(std::function<void (std::string data)> msgTask)83 ErrCode SPTask::StartTask(std::function<void(std::string data)> msgTask)
84 {
85 if (!isInit) {
86 return ErrCode::FAILED;
87 }
88 isRunning = true;
89 startTime = SPUtils::GetCurTime();
90 if (!curTaskInfo.packageName.empty()) {
91 SpProfilerFactory::SetProfilerPkg(curTaskInfo.packageName);
92 }
93 thread = std::thread([this, msgTask]() {
94 std::cout << "Task " << curTaskInfo.sessionId << ": collecting data loop..." << std::endl;
95 while (isRunning) {
96 // 执行采集任务
97 long long lastTime = SPUtils::GetCurTime();
98 std::lock_guard<std::mutex> lock(mtx);
99 std::map<std::string, std::string> dataMap;
100 dataMap.insert(std::pair<std::string, std::string>(std::string("timestamp"), std::to_string(lastTime)));
101 for (std::string itConfig:curTaskInfo.taskConfig) {
102 SpProfiler *profiler = SpProfilerFactory::GetCmdProfilerItem(commandMap.at(itConfig));
103 std::map<std::string, std::string> itemMap = profiler->ItemData();
104 dataMap.insert(itemMap.begin(), itemMap.end());
105 }
106 SPData spdata;
107 spdata.values.insert(dataMap.begin(), dataMap.end());
108 vmap.push_back(spdata);
109 long long nextTime = SPUtils::GetCurTime();
110 long long costTime = nextTime - lastTime;
111 if (costTime < curTaskInfo.freq) {
112 std::this_thread::sleep_for(std::chrono::milliseconds(curTaskInfo.freq - costTime));
113 }
114 if (isRunning) {
115 msgTask(MapToString(dataMap));
116 }
117 }
118 });
119 return ErrCode::OK;
120 }
StopTask()121 void SPTask::StopTask()
122 {
123 if (isInit) {
124 std::string thisBasePath = baseOutPath + "/" + curTaskInfo.sessionId;
125 if (!SPUtils::FileAccess(thisBasePath)) {
126 std::string cmdResult;
127 SPUtils::LoadCmd("mkdir -p " + thisBasePath, cmdResult);
128 }
129 std::string outGeneralPath = thisBasePath + "/t_general_info.csv";
130 std::string outIndexpath = thisBasePath + "/t_index_info.csv";
131 long long endTime = SPUtils::GetCurTime();
132 long long testDuration = (endTime - startTime) / 1000;
133 std::string screenStr = SPUtils::GetScreen();
134 int pos3 = screenStr.find("=");
135 std::string refreshrate = screenStr.substr(pos3 + 1);
136 std::map<std::string, std::string> taskInfoMap = {
137 {"sessionId", curTaskInfo.sessionId},
138 {"taskId", curTaskInfo.sessionId},
139 {"appName", curTaskInfo.packageName},
140 {"packageName", curTaskInfo.packageName},
141 {"startTime", std::to_string(startTime)},
142 {"endTime", std::to_string(endTime)},
143 {"testDuration", std::to_string(testDuration)},
144 {"taskName", "testtask"},
145 {"board", "hw"},
146 {"target_fps", refreshrate},
147 };
148 std::map<std::string, std::string> deviceInfo = SPUtils::GetDeviceInfo();
149 std::map<std::string, std::string> cpuInfo = SPUtils::GetCpuInfo();
150 std::map<std::string, std::string> gpuInfo = SPUtils::GetGpuInfo();
151 std::map<std::string, std::string> destMap;
152 destMap.insert(taskInfoMap.begin(), taskInfoMap.end());
153 destMap.insert(deviceInfo.begin(), deviceInfo.end());
154 destMap.insert(cpuInfo.begin(), cpuInfo.end());
155 destMap.insert(gpuInfo.begin(), gpuInfo.end());
156 OHOS::SmartPerf::SpCsvUtil::WriteCsvH(outGeneralPath, destMap);
157 OHOS::SmartPerf::SpCsvUtil::WriteCsv(outIndexpath, vmap);
158 }
159 isRunning = false;
160 isInit = false;
161 vmap.clear();
162 if (thread.joinable()) {
163 thread.join();
164 }
165 }
166 }
167 }