• 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 
16 #include <fstream>
17 #include <sstream>
18 #include "include/gp_utils.h"
19 
20 namespace OHOS {
21     namespace SmartPerf {
mSplit(const std::string & content,const std::string & sp,std::vector<std::string> & out)22         void GPUtils::mSplit(const std::string &content, const std::string &sp, std::vector<std::string> &out)
23         {
24             int index = 0;
25             while (index != std::string::npos) {
26                 int t_end = content.find_first_of(sp, index);
27                 std::string tmp = content.substr(index, t_end - index);
28                 if (tmp != "" && tmp != " ") {
29                     out.push_back(tmp);
30                 }
31                 if (t_end == std::string::npos) {
32                     break;
33                 }
34                 index = t_end + 1;
35             }
36         }
37 
canOpen(const std::string & path)38         bool GPUtils::canOpen(const std::string &path)
39         {
40             FILE *fp = fopen(path.c_str(), "r");
41             if (fp == nullptr) {
42                 return false;
43             }
44             if (fclose(fp) == EOF) {
45                 return false;
46             }
47             return true;
48         }
49 
50         // popen
readFile(const std::string & cmd)51         std::string GPUtils::readFile(const std::string &cmd)
52         {
53             const int buffLengh = 1024;
54             std::string res = "NA";
55             FILE *fp = popen(cmd.c_str(), "r");
56 
57             char line[buffLengh];
58             line[0] = '\0';
59             while (fgets(line, buffLengh, fp) != nullptr) {
60                 res = std::string(line);
61             }
62 
63             if (fclose(fp) == EOF) {
64                 return "";
65             }
66             return res;
67         }
68 
69         // fopen
freadFile(const std::string & path)70         std::string GPUtils::freadFile(const std::string &path)
71         {
72             std::string res = "NA";
73             const int buffLengh = 1024;
74             FILE *fp;
75             if ((fp = fopen(path.c_str(), "r")) != nullptr) {
76                 char s[buffLengh];
77                 s[0] = '\0';
78                 while (fgets(s, sizeof(s), fp) != nullptr) {
79                     res += std::string(s);
80                 }
81             }
82             if (fp != nullptr) {
83                 fclose(fp);
84             }
85             return res;
86         }
87 
88         // get number_str from str
getNumber(const std::string & str)89         std::string GPUtils::getNumber(const std::string &str)
90         {
91             int cntInt = 0;
92             const int shift = 10;
93 
94             for (int i = 0; str[i] != '\0'; ++i) {
95                 if (str[i] >= '0' && str[i] <= '9') {
96                     cntInt *= shift;
97                     cntInt += str[i] - '0';
98                 }
99             }
100             return std::to_string(cntInt);
101         }
102 
103         // wirte to csv by path
writeCsv(const std::string & path,std::vector<GPData> & vmap)104         void GPUtils::writeCsv(const std::string &path, std::vector<GPData> &vmap)
105         {
106             std::ofstream outFile;
107             outFile.open(path.c_str(), std::ios::out);
108             int i = 0;
109             std::string title = "";
110             for (GPData gpdata : vmap) {
111                 std::map<std::string, std::string>::iterator iter;
112                 std::string line_content = "";
113                 for (iter = gpdata.values.begin(); iter != gpdata.values.end(); ++iter) {
114                     if (i == 0) {
115                         title += iter->first + ",";
116                     }
117                     line_content += iter->second + ",";
118                 }
119                 if (i == 0) {
120                     title.pop_back();
121                     outFile << title << std::endl;
122                 }
123                 line_content.pop_back();
124                 outFile << line_content << std::endl;
125                 ++i;
126             }
127             outFile.close();
128         }
129     }
130 }
131