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 "usb_utils.h"
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
20 #include <fstream>
21 #include <sys/time.h>
22
23 using namespace std;
24
HasLog(const string & target,double startTs,const string & file)25 bool HasLog(const string &target, double startTs, const string &file)
26 {
27 bool ret = false;
28 ifstream logFile(file);
29 string::size_type pos;
30 string lineStr;
31 const string flagStr = "[XTSCHECK]";
32 const int32_t tsStartPos = 11;
33 const int32_t tsLength = 17;
34 while (getline(logFile, lineStr)) {
35 double logTs;
36 pos = lineStr.find(flagStr);
37 if (pos == string::npos) {
38 lineStr.clear();
39 continue;
40 }
41 logTs = stod(lineStr.substr(pos + tsStartPos, tsLength));
42 if ((logTs - startTs) >= 0) {
43 if (lineStr.find(target) != string::npos) {
44 ret = true;
45 }
46 }
47 lineStr.clear();
48 }
49 logFile.close();
50 return ret;
51 }
52
GetNowTs(void)53 double GetNowTs(void)
54 {
55 const double transUsecNum = 1000000.0;
56 timeval tv = {0};
57 gettimeofday(&tv, nullptr);
58 return (tv.tv_sec + tv.tv_usec / transUsecNum);
59 }
60
ParseSysCmdResult(FILE & result,int32_t line,int32_t word)61 char *ParseSysCmdResult(FILE &result, int32_t line, int32_t word)
62 {
63 char s[1024];
64 char *pch = nullptr;
65 int32_t lineCnt = 1;
66 int32_t wordCnt = 1;
67 while (1) {
68 if (fgets(s, sizeof(s), &result) == nullptr) {
69 break;
70 }
71 pch = strtok(s, " ");
72 while (pch != nullptr) {
73 if (lineCnt == line && wordCnt == word) {
74 return pch;
75 }
76 pch = strtok(nullptr, " ");
77 wordCnt++;
78 }
79 lineCnt++;
80 wordCnt = 1;
81 }
82 return pch;
83 }
84
ParseFile(char * pch,struct ParseProcInfo & pinfo)85 static void ParseFile(char *pch, struct ParseProcInfo &pinfo)
86 {
87 while (pch != nullptr) {
88 if (strstr(pch, "VmRSS")) {
89 pch = strtok(nullptr, " ");
90 pinfo.ramCur = stod(pch);
91 pinfo.ramCount += 1;
92 pinfo.ramTotal += pinfo.ramCur;
93 if (pinfo.ramCur > pinfo.ramPeak) {
94 pinfo.ramPeak = pinfo.ramCur;
95 }
96 break;
97 }
98 if (strstr(pch, "Cpu")) {
99 pch = strtok(nullptr, " ");
100 pinfo.cpuCur = stod(pch);
101 pinfo.cpuCount += 1;
102 pinfo.cpuTotal += pinfo.cpuCur;
103 if (pinfo.cpuCur > pinfo.cpuPeak) {
104 pinfo.cpuPeak = pinfo.cpuCur;
105 }
106 break;
107 }
108 if (strstr(pch, "Threads")) {
109 pch = strtok(nullptr, " ");
110 pinfo.threadCur = stoi(pch);
111 if (pinfo.threadCur > pinfo.threadPeak) {
112 pinfo.threadPeak = pinfo.threadCur;
113 }
114 break;
115 }
116 pch = strtok(nullptr, " ");
117 }
118 }
119
CalcProcInfoFromFile(struct ProcInfo & info,const string & file)120 void CalcProcInfoFromFile(struct ProcInfo &info, const string &file)
121 {
122 char s[100];
123 struct ParseProcInfo pinfo = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 0, 0};
124
125 FILE *fp = fopen(file.c_str(), "r");
126 if (!fp) {
127 printf("%s:%d file open failed.", __func__, __LINE__);
128 return;
129 }
130 while (1) {
131 if (fgets(s, sizeof(s), fp) == nullptr) {
132 break;
133 }
134 ParseFile(strtok(s, " \t"), pinfo);
135 }
136
137 if (pinfo.ramCount == 0 || pinfo.cpuCount == 0) {
138 (void)fclose(fp);
139 return;
140 }
141 info.ramPeak = pinfo.ramPeak;
142 info.ramAvg = pinfo.ramTotal / pinfo.ramCount;
143 info.cpuPeak = pinfo.cpuPeak;
144 info.cpuAvg = pinfo.cpuTotal / pinfo.cpuCount;
145 info.threadPeak = pinfo.threadPeak;
146
147 (void)fclose(fp);
148 return;
149 }
150
GetTsFromLog(const string & target,double startTs,const string & file)151 double GetTsFromLog(const string &target, double startTs, const string &file)
152 {
153 double logTs;
154 ifstream logFile(file);
155 string lineStr;
156 const int32_t tsStartPos = 11;
157 const int32_t tsLength = 17;
158 while (getline(logFile, lineStr)) {
159 if (lineStr.find(target) != string::npos) {
160 logTs = stod(lineStr.substr(tsStartPos, tsLength));
161 if ((logTs - startTs) >= 0) {
162 return logTs;
163 }
164 }
165 lineStr.clear();
166 }
167 logFile.close();
168 return 0;
169 }
170