• 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 "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             logTs = atof(lineStr.substr(pos + tsStartPos, tsLength).c_str());
39             if ((logTs - startTs) >= 0) {
40                 if (lineStr.find(target) != string::npos) {
41                     ret = true;
42                 }
43             }
44         }
45         lineStr.clear();
46     }
47     logFile.close();
48     return ret;
49 }
50 
GetNowTs(void)51 double GetNowTs(void)
52 {
53     const double transUsecNum = 1000000.0;
54     timeval tv = {0};
55     gettimeofday(&tv, nullptr);
56     return (tv.tv_sec + tv.tv_usec / transUsecNum);
57 }
58 
ParseSysCmdResult(FILE & result,int32_t line,int32_t word)59 char *ParseSysCmdResult(FILE &result, int32_t line, int32_t word)
60 {
61     char s[1024];
62     char *pch = nullptr;
63     int32_t lineCnt = 1;
64     int32_t wordCnt = 1;
65     while (1) {
66         if (fgets(s, sizeof(s), &result) == nullptr) {
67             break;
68         }
69         pch = strtok(s, " ");
70         while (pch != nullptr) {
71             if (lineCnt == line && wordCnt == word) {
72                 return pch;
73             }
74             pch = strtok(nullptr, " ");
75             wordCnt++;
76         }
77         lineCnt++;
78         wordCnt = 1;
79     }
80     return pch;
81 }
82 
CalcProcInfoFromFile(struct ProcInfo & info,const string & file)83 void CalcProcInfoFromFile(struct ProcInfo &info, const string &file)
84 {
85     static double ramTotal, ramPeak, ramCur, cpuTotal, cpuPeak, cpuCur;
86     static int32_t ramCount, cpuCount, threadPeak, threadCur;
87     char s[100];
88     char *pch;
89     FILE *f = fopen(file.c_str(), "r");
90     while (1) {
91         if (fgets(s, sizeof(s), f) == nullptr) {
92             break;
93         }
94         pch = strtok(s, " \t");
95         while (pch != nullptr) {
96             if (strstr(pch, "VmRSS")) {
97                 pch = strtok(nullptr, " ");
98                 ramCur = atof(pch);
99                 ramCount += 1;
100                 ramTotal += ramCur;
101                 if (ramCur > ramPeak) {
102                     ramPeak = ramCur;
103                 }
104                 break;
105             }
106             if (strstr(pch, "Cpu")) {
107                 pch = strtok(nullptr, " ");
108                 cpuCur = atof(pch);
109                 cpuCount += 1;
110                 cpuTotal += cpuCur;
111                 if (cpuCur > cpuPeak) {
112                     cpuPeak = cpuCur;
113                 }
114                 break;
115             }
116             if (strstr(pch, "Threads")) {
117                 pch = strtok(nullptr, " ");
118                 threadCur = atoi(pch);
119                 if (threadCur > threadPeak) {
120                     threadPeak = threadCur;
121                 }
122                 break;
123             }
124             pch = strtok(nullptr, " ");
125         }
126     }
127     info.ramPeak = ramPeak;
128     info.ramAvg = ramTotal / ramCount;
129     info.cpuPeak = cpuPeak;
130     info.cpuAvg = cpuTotal / cpuCount;
131     info.threadPeak = threadPeak;
132 
133     if (fclose(f)) {
134         return;
135     }
136 }
137 
GetTsFromLog(const string & target,double startTs,const string & file)138 double GetTsFromLog(const string &target, double startTs, const string &file)
139 {
140     double logTs;
141     ifstream logFile(file);
142     string lineStr;
143     const int32_t tsStartPos = 11;
144     const int32_t tsLength = 17;
145     while (getline(logFile, lineStr)) {
146         if (lineStr.find(target) != string::npos) {
147             logTs = atof(lineStr.substr(tsStartPos, tsLength).c_str());
148             if ((logTs - startTs) >= 0) {
149                 return logTs;
150             }
151         }
152         lineStr.clear();
153     }
154     logFile.close();
155     return 0;
156 }
157