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