• 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 int tsStartPos = 11;
33     const int 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,int line,int word)59 char *ParseSysCmdResult(FILE &result, int line, int word)
60 {
61     char s[1024];
62     char *pch = nullptr;
63     int lineCnt = 1;
64     int 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     const char *ramFlag = "VmRSS";
86     const char *cpuFlag = "Cpu";
87     const char *threadFlag = "Threads";
88     static double ramTotal, ramPeak, ramCur, cpuTotal, cpuPeak, cpuCur;
89     static int ramCount, cpuCount, threadPeak, threadCur;
90     char s[100];
91     char *pch;
92     FILE *f = fopen(file.c_str(), "r");
93     while (1) {
94         if (fgets(s, sizeof(s), f) == nullptr) {
95             break;
96         }
97         pch = strtok(s, " \t");
98         while (pch != nullptr) {
99             if (strstr(pch, ramFlag)) {
100                 pch = strtok(nullptr, " ");
101                 ramCur = atof(pch);
102                 ramCount += 1;
103                 ramTotal += ramCur;
104                 if (ramCur > ramPeak) {
105                     ramPeak = ramCur;
106                 }
107                 break;
108             }
109             if (strstr(pch, cpuFlag)) {
110                 pch = strtok(nullptr, " ");
111                 cpuCur = atof(pch);
112                 cpuCount += 1;
113                 cpuTotal += cpuCur;
114                 if (cpuCur > cpuPeak) {
115                     cpuPeak = cpuCur;
116                 }
117                 break;
118             }
119             if (strstr(pch, threadFlag)) {
120                 pch = strtok(nullptr, " ");
121                 threadCur = atoi(pch);
122                 if (threadCur > threadPeak) {
123                     threadPeak = threadCur;
124                 }
125                 break;
126             }
127             pch = strtok(nullptr, " ");
128         }
129     }
130     info.ramPeak = ramPeak;
131     info.ramAvg = ramTotal / ramCount;
132     info.cpuPeak = cpuPeak;
133     info.cpuAvg = cpuTotal / cpuCount;
134     info.threadPeak = threadPeak;
135 }
136 
GetTsFromLog(const string & target,double startTs,const string & file)137 double GetTsFromLog(const string &target, double startTs, const string &file)
138 {
139     double logTs;
140     ifstream logFile(file);
141     string lineStr;
142     const int tsStartPos = 11;
143     const int tsLength = 17;
144     while (getline(logFile, lineStr)) {
145         if (lineStr.find(target) != string::npos) {
146             logTs = atof(lineStr.substr(tsStartPos, tsLength).c_str());
147             if ((logTs - startTs) >= 0) {
148                 return logTs;
149             }
150         }
151         lineStr.clear();
152     }
153     logFile.close();
154     return 0;
155 }
156