• 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 <iostream>
16 #include <fstream>
17 #include <sstream>
18 #include <algorithm>
19 #include <unistd.h>
20 #include <dirent.h>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <climits>
24 #include <cctype>
25 #include <climits>
26 #include <sys/utsname.h>
27 #include "sys/time.h"
28 #include "securec.h"
29 #include "include/sp_utils.h"
30 #include "include/sp_log.h"
31 #include "include/common.h"
32 #include "cpu_collector.h"
33 #include "collect_result.h"
34 
35 using namespace OHOS::HiviewDFX;
36 using namespace OHOS::HiviewDFX::UCollectUtil;
37 using namespace OHOS::HiviewDFX::UCollect;
38 
39 namespace OHOS {
40 namespace SmartPerf {
41 const unsigned int INT_MAX_LEN = 10;
42 const unsigned int CHAR_NUM_DIFF = 48;
43 const unsigned int UI_DECIMALISM = 10;
44 const unsigned int UI_INDEX_2 = 2;
FileAccess(const std::string & fileName)45 bool SPUtils::FileAccess(const std::string &fileName)
46 {
47     return (access(fileName.c_str(), F_OK) == 0);
48 }
HasNumber(const std::string & str)49 bool SPUtils::HasNumber(const std::string &str)
50 {
51     return std::any_of(str.begin(), str.end(), [](char c) { return std::isdigit(c); });
52 }
Cmp(const std::string & a,const std::string & b)53 bool SPUtils::Cmp(const std::string &a, const std::string &b)
54 {
55     if (HasNumber(a) && HasNumber(b)) {
56         std::string str1 = a.substr(0, a.find_first_of("0123456789"));
57         std::string str2 = b.substr(0, b.find_first_of("0123456789"));
58         if (str1 != str2) {
59             return str1 < str2;
60         }
61         int num1 = std::stoi(a.substr(str1.length()));
62         int num2 = std::stoi(b.substr(str2.length()));
63         return num1 < num2;
64     }
65     return false;
66 }
67 
LoadFile(const std::string & filePath,std::string & content)68 bool SPUtils::LoadFile(const std::string &filePath, std::string &content)
69 {
70     char realPath[PATH_MAX] = {0x00};
71     if ((realpath(filePath.c_str(), realPath) == nullptr)) {
72         std::cout << "" << std::endl;
73     }
74     std::ifstream file(realPath);
75     if (!file.is_open()) {
76         return false;
77     }
78 
79     file.seekg(0, std::ios::end);
80     file.tellg();
81 
82     content.clear();
83     file.seekg(0, std::ios::beg);
84     copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), std::back_inserter(content));
85     // remove '' \n\r
86     ReplaceString(content);
87     return true;
88 }
89 
LoadCmdWithLinkBreak(const std::string & cmd,bool isClearLinkBreak,std::string & result)90 bool SPUtils::LoadCmdWithLinkBreak(const std::string &cmd, bool isClearLinkBreak, std::string &result)
91 {
92     const std::string cmdExc = cmd;
93     FILE *fd = popen(cmdExc.c_str(), "r");
94     if (fd == nullptr) {
95         return false;
96     }
97     char buf[4096] = {'\0'};
98     size_t ret = fread(buf, sizeof(buf), 1, fd);
99     if (ret >= 0) {
100         result = buf;
101     }
102     if (pclose(fd) == -1) {
103         std::cout << "" << std::endl;
104     }
105 
106     if (isClearLinkBreak) {
107         // remove '' \n\r
108         ReplaceString(result);
109     }
110 
111     return ret >= 0 ? true : false;
112 }
113 
LoadCmd(const std::string & cmd,std::string & result)114 bool SPUtils::LoadCmd(const std::string &cmd, std::string &result)
115 {
116     return LoadCmdWithLinkBreak(cmd, true, result);
117 }
118 
IncludePathDelimiter(const std::string & path)119 std::string SPUtils::IncludePathDelimiter(const std::string &path)
120 {
121     if (!path.empty() && path.back() != '/') {
122         return path + "/";
123     } else {
124         return path;
125     }
126 }
127 
ForDirFiles(const std::string & path,std::vector<std::string> & files)128 void SPUtils::ForDirFiles(const std::string &path, std::vector<std::string> &files)
129 {
130     std::string pathStringWithDelimiter;
131     DIR *dir = opendir(path.c_str());
132     if (dir == nullptr) {
133         return;
134     }
135 
136     while (true) {
137         struct dirent *ptr = readdir(dir);
138         if (ptr == nullptr) {
139             break;
140         }
141 
142         // current dir OR parent dir
143         if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0)) {
144             continue;
145         } else if (ptr->d_type == DT_DIR) {
146             pathStringWithDelimiter = IncludePathDelimiter(path) + std::string(ptr->d_name);
147             ForDirFiles(pathStringWithDelimiter, files);
148         } else {
149             files.push_back(IncludePathDelimiter(path) + std::string(ptr->d_name));
150         }
151     }
152     closedir(dir);
153 }
154 
IsSubString(const std::string & str,const std::string & sub)155 bool SPUtils::IsSubString(const std::string &str, const std::string &sub)
156 {
157     if (sub.empty() || str.empty()) {
158         return false;
159     }
160 
161     return str.find(sub) != std::string::npos;
162 }
163 
StrSplit(const std::string & content,const std::string & sp,std::vector<std::string> & out)164 void SPUtils::StrSplit(const std::string &content, const std::string &sp, std::vector<std::string> &out)
165 {
166     size_t index = 0;
167     while (index != std::string::npos) {
168         size_t tEnd = content.find_first_of(sp, index);
169         std::string tmp = content.substr(index, tEnd - index);
170         if (tmp != "" && tmp != " ") {
171             out.push_back(tmp);
172         }
173         if (tEnd == std::string::npos) {
174             break;
175         }
176         index = tEnd + 1;
177     }
178 }
179 
ExtractNumber(const std::string & str)180 std::string SPUtils::ExtractNumber(const std::string &str)
181 {
182     int cntInt = 0;
183     const int shift = 10;
184     for (int i = 0; str[i] != '\0'; ++i) {
185         if (str[i] >= '0' && str[i] <= '9') {
186             cntInt *= shift;
187             cntInt += str[i] - '0';
188         }
189     }
190     return std::to_string(cntInt);
191 }
192 
ReplaceString(std::string & res)193 void SPUtils::ReplaceString(std::string &res)
194 {
195     std::string flagOne = "\r";
196     std::string flagTwo = "\n";
197     std::string::size_type ret = res.find(flagOne);
198     while (ret != res.npos) {
199         res.replace(ret, 1, "");
200         ret = res.find(flagOne);
201     }
202     ret = res.find(flagTwo);
203     while (ret != res.npos) {
204         res.replace(ret, 1, "");
205         ret = res.find(flagTwo);
206     }
207 }
208 
GetCurTime()209 long long SPUtils::GetCurTime()
210 {
211     struct timeval tv;
212     gettimeofday(&tv, nullptr);
213     long long timestamp = tv.tv_sec * 1000 + tv.tv_usec / 1000;
214     return timestamp;
215 }
216 
GetTopPkgName()217 std::string SPUtils::GetTopPkgName()
218 {
219     std::string cmd = HIDUMPER_CMD_MAP.at(HidumperCmd::DUMPER_HEAD);
220     std::string curTopPkgStr = "";
221     LoadCmd(cmd, curTopPkgStr);
222     uint64_t left = curTopPkgStr.find_first_of("[");
223     uint64_t right = curTopPkgStr.find_first_of("]");
224     std::string topPkg = curTopPkgStr.substr(left + 1, static_cast<int64_t>(right) - static_cast<int64_t>(left) - 1);
225     return topPkg;
226 }
227 
GetRadar()228 std::string SPUtils::GetRadar()
229 {
230     std::string cmd = HISYSEVENT_CMD_MAP.at(HisyseventCmd::HISYS_APP_START);
231     std::string curRadar = "";
232     LoadCmd(cmd, curRadar);
233     return curRadar;
234 }
GetScreen()235 std::string SPUtils::GetScreen()
236 {
237     std::string cmd = HIDUMPER_CMD_MAP.at(HidumperCmd::DUMPER_SCREEN);
238     std::string screenStr = "";
239     LoadCmd(cmd, screenStr);
240     uint64_t left = screenStr.find("activeMode");
241     uint64_t right = screenStr.find("capability");
242     std::string screen = screenStr.substr(left, right - left);
243     return screen;
244 }
GetRadarFrame()245 std::string SPUtils::GetRadarFrame()
246 {
247     std::string cmd = HISYSEVENT_CMD_MAP.at(HisyseventCmd::HISYS_JANK);
248     std::string curRadar = "";
249     LoadCmd(cmd, curRadar);
250     return curRadar;
251 }
GetRadarResponse()252 std::string SPUtils::GetRadarResponse()
253 {
254     std::string cmd = HISYSEVENT_CMD_MAP.at(HisyseventCmd::HISYS_RESPONSE);
255     std::string curRadar = "";
256     LoadCmd(cmd, curRadar);
257     return curRadar;
258 }
GetRadarComplete()259 std::string SPUtils::GetRadarComplete()
260 {
261     std::string cmd = HISYSEVENT_CMD_MAP.at(HisyseventCmd::HISYS_COMPLETED);
262     std::string curRadar = "";
263     LoadCmd(cmd, curRadar);
264     return curRadar;
265 }
GetSplitOne(std::string cmd)266 static std::string GetSplitOne(std::string cmd)
267 {
268     std::string result;
269     SPUtils::LoadCmd(cmd, result);
270     return result;
271 }
272 
GetDeviceInfoMap()273 std::string SPUtils::GetDeviceInfoMap()
274 {
275     size_t len = 2;
276     bool isTcpMessage = false;
277     std::map<std::string, std::string> deviceInfoMap;
278     std::map<std::string, std::string> cpuInfo = GetCpuInfo(isTcpMessage);
279     std::map<std::string, std::string> gpuInfo = GetGpuInfo(isTcpMessage);
280     std::map<std::string, std::string> deviceInfo = GetDeviceInfo();
281     std::string screenInfos = GetScreen();
282     size_t pos = screenInfos.find(": ");
283     size_t pos1 = screenInfos.find(",");
284     std::string screenSize = screenInfos.substr(pos + len, pos1 - pos - len);
285     deviceInfoMap.insert(cpuInfo.begin(), cpuInfo.end());
286     deviceInfoMap.insert(gpuInfo.begin(), gpuInfo.end());
287     deviceInfoMap.insert(deviceInfo.begin(), deviceInfo.end());
288     deviceInfoMap["activeMode"] = screenSize;
289     if (deviceInfoMap.empty()) {
290         LOGI("Failed to obtain device information");
291     }
292     for (auto iter = deviceInfoMap.cbegin(); iter != deviceInfoMap.cend(); ++iter) {
293         printf("%s: %s\n", iter->first.c_str(), iter->second.c_str());
294     }
295     std::cout << "" << std::endl;
296     return std::string("command exec finished!");
297 }
298 
GetDeviceInfo()299 std::map<std::string, std::string> SPUtils::GetDeviceInfo()
300 {
301     std::map<std::string, std::string> resultMap;
302     std::string sn = GetSplitOne(DEVICE_CMD_MAP.at(DeviceCmd::SN));
303     std::string deviceTypeName = GetSplitOne(DEVICE_CMD_MAP.at(DeviceCmd::DEVICET_NAME));
304     std::string brand = GetSplitOne(DEVICE_CMD_MAP.at(DeviceCmd::BRAND));
305     std::string version = GetSplitOne(DEVICE_CMD_MAP.at(DeviceCmd::VERSION));
306     std::string abilist = GetSplitOne(DEVICE_CMD_MAP.at(DeviceCmd::ABILIST));
307     std::string name = GetSplitOne(DEVICE_CMD_MAP.at(DeviceCmd::NAME));
308     std::string model = GetSplitOne(DEVICE_CMD_MAP.at(DeviceCmd::MODEL));
309     std::string fullname = GetSplitOne(DEVICE_CMD_MAP.at(DeviceCmd::FULL_NAME));
310     resultMap["sn"] = sn;
311     resultMap["deviceTypeName"] = deviceTypeName;
312     resultMap["brand"] = brand;
313     resultMap["board"] = "hw";
314     resultMap["version"] = version;
315     resultMap["abilist"] = abilist;
316     resultMap["name"] = name;
317     resultMap["model"] = model;
318     resultMap["fullname"] = fullname;
319     return resultMap;
320 }
GetCpuInfo(bool isTcpMessage)321 std::map<std::string, std::string> SPUtils::GetCpuInfo(bool isTcpMessage)
322 {
323     std::string clusterNames;
324     std::vector<std::string> policyFiles;
325     std::map<std::string, std::string> resultMap;
326     std::string basePath = "/sys/devices/system/cpu/cpufreq/";
327     DIR *dir = opendir(basePath.c_str());
328     if (dir == nullptr) {
329         return resultMap;
330     }
331     while (true) {
332         struct dirent *ptr = readdir(dir);
333         if (ptr == nullptr) {
334             break;
335         }
336         if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0)) {
337             continue;
338         }
339         std::string clusterName = std::string(ptr->d_name);
340         if (!isTcpMessage) {
341             clusterNames += clusterName + " ";
342             resultMap["cpu_cluster_name"] = clusterNames;
343         }
344         policyFiles.push_back(IncludePathDelimiter(basePath) + clusterName);
345     }
346     closedir(dir);
347     for (size_t i = 0; i < policyFiles.size(); i++) {
348         std::string cpus;
349         LoadFile(policyFiles[i] + "/affected_cpus", cpus);
350         std::string max;
351         LoadFile(policyFiles[i] + "/cpuinfo_max_freq", max);
352         std::string min;
353         LoadFile(policyFiles[i] + "/cpuinfo_min_freq", min);
354         std::string nameBase;
355         if (!isTcpMessage) {
356             nameBase = "cpu_c" + std::to_string(i + 1) + "_";
357         } else {
358             nameBase = "cpu-c" + std::to_string(i + 1) + "-";
359         }
360         resultMap[nameBase + "cluster"] = cpus;
361         resultMap[nameBase + "max"] = max;
362         resultMap[nameBase + "min"] = min;
363     }
364     return resultMap;
365 }
GetGpuInfo(bool isTcpMessage)366 std::map<std::string, std::string> SPUtils::GetGpuInfo(bool isTcpMessage)
367 {
368     const std::vector<std::string> gpuCurFreqPaths = {
369         "/sys/class/devfreq/fde60000.gpu/",
370         "/sys/class/devfreq/gpufreq/",
371     };
372     std::map<std::string, std::string> resultMap;
373     for (auto path : gpuCurFreqPaths) {
374         if (FileAccess(path)) {
375             std::string max;
376             SPUtils::LoadFile(path + "/max_freq", max);
377             std::string min;
378             SPUtils::LoadFile(path + "/min_freq", min);
379             resultMap["gpu_max_freq"] = max;
380             resultMap["gpu_min_freq"] = min;
381         }
382     }
383     return resultMap;
384 }
385 
RemoveSpace(std::string & str)386 void SPUtils::RemoveSpace(std::string &str)
387 {
388     int len = 0;
389 
390     for (size_t i = 0; i < str.length(); i++) {
391         if (str[i] != ' ') {
392             break;
393         }
394 
395         ++len;
396     }
397 
398     if (len > 0) {
399         str = str.substr(len);
400     }
401 
402     len = 0;
403     for (size_t i = str.length(); i > 0; --i) {
404         if (str[i - 1] != ' ') {
405             break;
406         }
407 
408         ++len;
409     }
410 
411     if (len > 0) {
412         for (int i = 0; i < len; i++) {
413             str.pop_back();
414         }
415     }
416 }
417 
418 
IntegerVerification(std::string str,std::string errorInfo)419 bool SPUtils::IntegerVerification(std::string str, std::string errorInfo)
420 {
421     uint64_t dest = 0;
422     bool isValid = false;
423 
424     if (str.empty()) {
425         errorInfo = "option requires an argument";
426         LOGE("sour(%s) error(%s)", str.c_str(), errorInfo.c_str());
427         return false;
428     }
429     if (str.length() > INT_MAX_LEN) {
430         errorInfo = "invalid option parameters";
431         LOGE("sour(%s) error(%s)", str.c_str(), errorInfo.c_str());
432         return false;
433     }
434 
435     for (size_t i = 0; i < str.length(); i++) {
436         if (str[i] < '0' || str[i] > '9') {
437             errorInfo = "invalid option parameters";
438             LOGE("sour(%s) error(%s)", str.c_str(), errorInfo.c_str());
439             return false;
440         }
441 
442         if (!isValid && (str[i] == '0')) {
443             continue;
444         }
445 
446         isValid = true;
447         dest *= UI_DECIMALISM;
448         dest += (str[i] - CHAR_NUM_DIFF);
449     }
450 
451     if (dest == 0 || dest > INT_MAX) {
452         errorInfo = "option parameter out of range";
453         LOGE("sour(%s) dest(%u) error(%s)", str.c_str(), dest, errorInfo.c_str());
454         return false;
455     }
456 
457     return true;
458 }
459 
VeriyParameter(std::set<std::string> & keys,std::string param,std::string & errorInfo)460 bool SPUtils::VeriyParameter(std::set<std::string> &keys, std::string param, std::string &errorInfo)
461 {
462     std::string keyParam;
463     std::string valueParm;
464     std::vector<std::string> out;
465     std::vector<std::string> subOut;
466     std::map<std::string, std::string> mapInfo;
467 
468     if (param.empty()) {
469         errorInfo = "The parameter cannot be empty";
470         return false;
471     }
472 
473     SPUtils::StrSplit(param, "-", out);
474 
475     for (auto it = out.begin(); it != out.end(); ++it) { // Parsing keys and values
476         subOut.clear();
477         SPUtils::StrSplit(*it, " ", subOut);
478         if (mapInfo.end() != mapInfo.find(subOut[0])) {
479             errorInfo = "duplicate parameters -- '" + subOut[0] + "'";
480             return false;
481         }
482 
483         if (subOut.size() >= UI_INDEX_2) {
484             keyParam = subOut[0];
485             valueParm = subOut[1];
486             SPUtils::RemoveSpace(keyParam);
487             SPUtils::RemoveSpace(valueParm);
488             mapInfo[keyParam] = valueParm;
489         } else if (subOut.size() >= 1) {
490             keyParam = subOut[0];
491             SPUtils::RemoveSpace(keyParam);
492             mapInfo[keyParam] = "";
493         }
494     }
495 
496     if (!VeriyKey(keys, mapInfo, errorInfo)) {
497         LOGE("%s", errorInfo.c_str());
498         return false;
499     }
500 
501     if (!VerifyValueStr(mapInfo, errorInfo)) {
502         LOGE("%s", errorInfo.c_str());
503         return false;
504     }
505 
506     if (!IntegerValueVerification(keys, mapInfo, errorInfo)) {
507         LOGE("%s", errorInfo.c_str());
508         return false;
509     }
510     return true;
511 }
512 
VeriyKey(std::set<std::string> & keys,std::map<std::string,std::string> & mapInfo,std::string & errorInfo)513 bool SPUtils::VeriyKey(std::set<std::string> &keys, std::map<std::string, std::string> &mapInfo,
514     std::string &errorInfo)
515 {
516     for (auto it = mapInfo.begin(); it != mapInfo.end(); ++it) {
517         if (keys.end() == keys.find(it->first)) {
518             errorInfo = "invalid parameter -- '" + it->first + "'";
519             return false;
520         }
521     }
522 
523     return true;
524 }
525 
VerifyValueStr(std::map<std::string,std::string> & mapInfo,std::string & errorInfo)526 bool SPUtils::VerifyValueStr(std::map<std::string, std::string> &mapInfo, std::string &errorInfo)
527 {
528     auto a = mapInfo.find("VIEW");
529     if (mapInfo.end() != a && a->second.empty()) { // Cannot be null
530         errorInfo += "option requires an argument -- '" + a->first + "'";
531         return false;
532     }
533     a = mapInfo.find("PKG");
534     if (mapInfo.end() != a && a->second.empty()) { // Cannot be null
535         errorInfo += "option requires an argument -- '" + a->first + "'";
536         return false;
537     }
538     a = mapInfo.find("OUT");
539     if (mapInfo.end() != a) {
540         if (a->second.empty()) {
541             errorInfo += "option requires an argument -- '" + a->first + "'";
542             return false;
543         }
544         // The total length of file path and name cannot exceed PATH_MAX
545         if (a->second.length() >= PATH_MAX) {
546             errorInfo +=
547                 "invalid parameter, file path cannot exceed " + std::to_string(PATH_MAX) + " -- '" + a->first + "'";
548             return false;
549         }
550         size_t pos = a->second.rfind('/');
551         if (pos == a->second.length()) { // not file name
552             errorInfo += "invalid parameter,not file name -- '" + a->first + "'";
553             return false;
554         }
555         if (std::string::npos != pos &&
556             (!SPUtils::FileAccess(a->second.substr(0, pos)))) { // determine if the directory exists
557             errorInfo += "invalid parameter,file path not found -- '" + a->first + "'";
558             return false;
559         }
560         std::string outStr = a->second;
561         std::vector<std::string> outList;
562         SPUtils::StrSplit(outStr, "/", outList);
563         for (auto it = outList.begin(); outList.end() != it; ++it) {
564             if ((*it).length() >= NAME_MAX) {
565                 errorInfo += "invalid parameter, file directory or name cannot exceed 255 -- '" + a->first + "'";
566                 return false;
567             }
568         }
569     }
570     return true;
571 }
572 
IntegerValueVerification(std::set<std::string> & keys,std::map<std::string,std::string> & mapInfo,std::string & errorInfo)573 bool SPUtils::IntegerValueVerification(std::set<std::string> &keys, std::map<std::string, std::string> &mapInfo,
574     std::string &errorInfo)
575 {
576     std::vector<std::string> integerCheck; // Number of integers to be detected
577 
578     if (keys.end() != keys.find("N")) {
579         integerCheck.push_back("N");
580     }
581     if (keys.end() != keys.find("fl")) {
582         integerCheck.push_back("fl");
583     }
584     if (keys.end() != keys.find("ftl")) {
585         integerCheck.push_back("ftl");
586     }
587 
588     for (auto it = integerCheck.begin(); it != integerCheck.end(); ++it) {
589         auto a = mapInfo.find(*it);
590         if (mapInfo.end() != a) {
591             if (a->second.empty()) {
592                 errorInfo += "option requires an argument -- '" + a->first + "'";
593                 return false;
594             }
595             if (!SPUtils::IntegerVerification(a->second, errorInfo)) {
596                 errorInfo += "option parameter out of range -- '" + a->first + "'";
597                 return false;
598             }
599         }
600     }
601 
602     return true;
603 }
604 
IsHmKernel()605 bool SPUtils::IsHmKernel()
606 {
607     bool isHM = false;
608     utsname unameBuf;
609     if ((uname(&unameBuf)) == 0) {
610         std::string osRelease = unameBuf.release;
611         isHM = osRelease.find("HongMeng") != std::string::npos;
612     }
613     return isHM;
614 }
615 
GetCpuNum()616 std::string SPUtils::GetCpuNum()
617 {
618     std::string cpuCores = "cpuCores||";
619     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
620     CollectResult<std::vector<CpuFreq>> result = collector->CollectCpuFrequency();
621     std::vector<CpuFreq> &cpufreq = result.data;
622     size_t cpuNum = cpufreq.size();
623     cpuCores += std::to_string(cpuNum);
624     if (cpuNum == 0) {
625         std::cout << "CPU frequency collection failed." << std::endl;
626         LOGI("CPU frequency collection failed.");
627     }
628     return cpuCores;
629 }
GetCurrentTime(int num,int prevTime)630 void SPUtils::GetCurrentTime(int num, int prevTime)
631 {
632     unsigned long sleepNowTime = 10000;
633     for (int i = 0; i < num; i++) {
634         struct timespec time1 = { 0 };
635         clock_gettime(CLOCK_MONOTONIC, &time1);
636         int curTimeNow = static_cast<int>(time1.tv_sec - 1);
637         if (curTimeNow == prevTime) {
638             usleep(sleepNowTime);
639         } else {
640             break;
641         }
642     }
643 }
IsForeGround(std::string & pkg)644 bool SPUtils::IsForeGround(std::string &pkg)
645 {
646     bool isFoundAppName = false;
647     bool isFoundBundleName = false;
648     const std::string cmd = "aa dump -l";
649     char buf[1024] = {'\0'};
650     std::string appLine = "app name [" + pkg;
651     std::string bundleLine = "bundle name [" + pkg;
652     FILE *fd = popen(cmd.c_str(), "r");
653     if (fd == nullptr) {
654         return false;
655     }
656     bool tag = false;
657     while (fgets(buf, sizeof(buf), fd) != nullptr) {
658         std::string line = buf;
659         if (line.find(appLine) != std::string::npos) {
660             isFoundAppName = true;
661         }
662         if (line.find(bundleLine) != std::string::npos) {
663             isFoundBundleName = true;
664         }
665         if (isFoundAppName || isFoundBundleName) {
666             if (line.find("app state") != std::string::npos) {
667                 tag = IsFindForeGround(line);
668                 isFoundAppName = false;
669                 isFoundBundleName = false;
670             }
671         }
672     }
673     pclose(fd);
674     return tag;
675 }
IsFindForeGround(std::string line)676 bool SPUtils::IsFindForeGround(std::string line)
677 {
678     std::string foreGroundTag = line.substr(line.find("#") + 1);
679     if (foreGroundTag.find("FOREGROUND") != std::string::npos) {
680         return true;
681     } else {
682         return false;
683     }
684 }
IsFindAbilist()685 bool SPUtils::IsFindAbilist()
686 {
687     std::string abilist = GetSplitOne(DEVICE_CMD_MAP.at(DeviceCmd::ABILIST));
688     if (abilist.find("arm") != std::string::npos) {
689         return true;
690     } else {
691         return false;
692     }
693 }
694 }
695 }
696