• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "test_utilities.h"
16 
17 #include <securec.h>
18 #include <cinttypes>
19 #include <cstdio>
20 #include <iostream>
21 
22 namespace OHOS {
23 namespace Developtools {
24 namespace HiPerf {
25 const int CMD_OUTPUT_BUF = 1024;
26 
CheckTestApp(const std::string & appName)27 bool CheckTestApp(const std::string& appName)
28 {
29     FILE *fp = nullptr;
30     char buf[128] = {0}; // 128: buf size
31     std::string cmd = "pidof " + appName;
32     if ((fp = popen(cmd.c_str(), "r")) != nullptr) {
33         if (fgets(buf, sizeof(buf), fp) == nullptr) {
34             pclose(fp);
35             return false;
36         }
37         pclose(fp);
38     }
39     return true;
40 }
41 
GetMemMapOffset(pid_t devhostPid,uint64_t & mapOffset,std::vector<std::shared_ptr<OHOS::HiviewDFX::DfxMap>> & memMaps,std::string & line)42 bool GetMemMapOffset(pid_t devhostPid, uint64_t &mapOffset,
43                      std::vector<std::shared_ptr<OHOS::HiviewDFX::DfxMap>> &memMaps, std::string &line)
44 {
45     pid_t pid = 0;
46     pid_t tid = 0;
47     uint64_t addr = 0;
48     uint64_t len = 0;
49     int ret = sscanf_s(line.c_str(), "  %*s %d, tid %d, addr 0x%" PRIx64 ", len 0x%" PRIx64 "",
50                        &pid, &tid, &addr, &len);
51     constexpr int numSlices {4};
52     if (ret != numSlices) {
53         printf("unknown line %d: '%s' \n", ret, line.c_str());
54         return false;
55     }
56     if (devhostPid != pid || devhostPid != tid) {
57         return false;
58     }
59     for (auto& map: memMaps) {
60         if (map->begin == addr && map->end - map->begin == len) {
61             mapOffset = map->offset;
62             return true;
63         }
64     }
65     return false;
66 }
67 
RunCmd(const std::string & cmdstr)68 bool RunCmd(const std::string& cmdstr)
69 {
70     if (cmdstr.empty()) {
71         return false;
72     }
73     FILE *fp = popen(cmdstr.c_str(), "r");
74     if (fp == nullptr) {
75         return false;
76     }
77     char res[CMD_OUTPUT_BUF] = { '\0' };
78     while (fgets(res, sizeof(res), fp) != nullptr) {
79         std::cout << res;
80     }
81     pclose(fp);
82     return true;
83 }
84 
CheckTraceCommandOutput(const std::string & cmd,const std::vector<std::string> & keywords)85 bool CheckTraceCommandOutput(const std::string& cmd, const std::vector<std::string>& keywords)
86 {
87     if (cmd.empty()) {
88         return false;
89     }
90     FILE* fp = popen(cmd.c_str(), "r");
91     if (fp == nullptr) {
92         return false;
93     }
94 
95     char buffer[CMD_OUTPUT_BUF];
96     int checkIdx = 0;
97     while (fgets(buffer, sizeof(buffer), fp) != nullptr) {
98         while (checkIdx < keywords.size() && strstr(buffer, keywords[checkIdx].c_str()) != nullptr) {
99             GTEST_LOG_(INFO) << "match keyword :" << keywords[checkIdx];
100             checkIdx++;
101             if (checkIdx == keywords.size()) {
102                 break;
103             }
104         }
105     }
106 
107     pclose(fp);
108     if (checkIdx < keywords.size()) {
109         GTEST_LOG_(ERROR) << "Failed to match keyword : " << keywords[checkIdx];
110     }
111     return checkIdx == keywords.size();
112 }
113 } // namespace HiPerf
114 } // namespace Developtools
115 } // namespace OHOS
116