1 /*
2  * Copyright (c) 2022 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 "system_info.h"
17 
18 #include <dirent.h>
19 #include <unistd.h>
20 
21 #include <chrono>
22 #include <fstream>
23 #include <string>
24 
25 #include "securec.h"
26 
27 #include "error_multimodal.h"
28 #include "mmi_log.h"
29 
30 #undef MMI_LOG_TAG
31 #define MMI_LOG_TAG "SYSTEM_INFO"
32 
33 namespace OHOS {
34 namespace MMI {
35 namespace SYSTEM_INFO {
36 namespace {
37 constexpr int32_t LOCATION { 14 };
38 constexpr int32_t TIME_WAIT_FOR_OP { 1000 };
39 constexpr int32_t DEFAULT_PID { -1 };
40 } // namespace
41 
CHK_RATE(double rate)42 inline double CHK_RATE(double rate)
43 {
44     return (rate > CPU_USAGE_MAX ? CPU_USAGE_MAX : rate);
45 }
46 
GetTaskPidFile(const std::string & process_name)47 int32_t CpuInfo::GetTaskPidFile(const std::string &process_name)
48 {
49     int32_t pid = DEFAULT_PID;
50     static const std::string procPath = "/proc";
51     DIR* dir = ::opendir(procPath.c_str());
52     CHKPR(dir, DEFAULT_PID);
53     struct dirent* pidFile;
54     while ((pidFile = ::readdir(dir)) != nullptr) {
55         if ((::strcmp(pidFile->d_name, ".") == 0) || (::strcmp(pidFile->d_name, "..") == 0)) {
56             continue;
57         }
58         if (pidFile->d_type != DT_DIR) {
59             continue;
60         }
61         const std::string path = procPath + "/" + pidFile->d_name + "/status";
62         std::ifstream file(path);
63         if (!file.is_open()) {
64             continue;
65         }
66         std::string strLine;
67         if (!std::getline(file, strLine)) {
68             MMI_HILOGE("getline failed");
69             file.close();
70             return DEFAULT_PID;
71         }
72         if (strLine.empty()) {
73             file.close();
74             continue;
75         }
76         if ((strLine.find(process_name)) == std::string::npos) {
77             file.close();
78             continue;
79         }
80         while (std::getline(file, strLine)) {
81             if ((strLine.find("Pid")) != std::string::npos) {
82                 if (::sscanf_s(strLine.c_str(), "%*s%d", &pid, sizeof(pid)) != 1) {
83                     MMI_HILOGE("Failed to delete the pid");
84                 }
85                 break;
86             }
87         }
88         file.close();
89         break;
90     }
91     if (::closedir(dir) != 0) {
92         MMI_HILOGE("Failed to close dir");
93     }
94     return pid;
95 }
96 
GetTaskPidCmd(const std::string & process_name,int32_t flag,std::string user)97 int32_t CpuInfo::GetTaskPidCmd(const std::string &process_name, int32_t flag, std::string user)
98 {
99     std::string command;
100     if (flag) {
101         if (user.empty()) {
102             user = ::getlogin();
103         }
104         command = "pgrep " + process_name + " -u " + user;
105     } else {
106         command = "pidof -s " + process_name;
107     }
108     ::FILE *fp = nullptr;
109     if ((fp = ::popen(command.c_str(), "r")) == nullptr) {
110         MMI_HILOGE("Failed to open, cmd:%{public}s", command.c_str());
111         fp = nullptr;
112         return DEFAULT_PID;
113     }
114     char buf[100] = { 0 };
115     if (::fgets(buf, sizeof(buf), fp) == nullptr) {
116         MMI_HILOGE("Failed to read content");
117         ::pclose(fp);
118         fp = nullptr;
119         return DEFAULT_PID;
120     }
121     ::pclose(fp);
122     return std::stoi(buf);
123 }
124 
GetProcOccupy(int32_t pid)125 int32_t CpuInfo::GetProcOccupy(int32_t pid)
126 {
127     Proc_Cpu_Occupy info;
128     static const std::string procPath = "/proc/" + std::to_string(pid) + "/stat";
129     std::ifstream file(procPath);
130     if (!file.is_open()) {
131         MMI_HILOGE("Failed to open path:%{private}s", procPath.c_str());
132         return RET_ERR;
133     }
134 
135     std::string strLine;
136     std::getline(file, strLine);
137     if (strLine.empty()) {
138         MMI_HILOGE("Failed to read content");
139         file.close();
140         return RET_ERR;
141     }
142     file.close();
143 
144     int position = 1;
145     std::istringstream ss(strLine);
146     while (ss >> strLine) {
147         position++;
148         if (position >= LOCATION) {
149             break;
150         }
151     }
152     ss >> info.utime >> info.stime >> info.cutime >> info.cstime;
153     return (info.utime + info.stime + info.cutime + info.cstime);
154 }
155 
GetCpuUsage(const Total_Cpu_Occupy & first,const Total_Cpu_Occupy & second)156 double CpuInfo::GetCpuUsage(const Total_Cpu_Occupy &first, const Total_Cpu_Occupy &second)
157 {
158     unsigned long cpuTime2 = static_cast<unsigned long>(second.user + second.nice + second.system +
159                                                         second.idle + second.lowait + second.irq + second.softirq);
160     unsigned long cpuTime1 = static_cast<unsigned long>(first.user + first.nice + first.system +
161                                                         first.idle + first.lowait + first.irq + first.softirq);
162 
163     double cpu_use = (second.user - first.user) * CPU_USAGE_MAX / (cpuTime2 - cpuTime1);
164     double cpu_sys = (second.system - first.system) * CPU_USAGE_MAX / (cpuTime2 - cpuTime1);
165 
166     return CHK_RATE(cpu_use + cpu_sys);
167 }
168 
GetSystemCpuStatInfo(Total_Cpu_Occupy & info)169 int32_t CpuInfo::GetSystemCpuStatInfo(Total_Cpu_Occupy &info)
170 {
171     std::ifstream statFile("/proc/stat");
172     if (!statFile.is_open()) {
173         MMI_HILOGE("Failed to open config file");
174         return FILE_OPEN_FAIL;
175     }
176     std::string strLine;
177     std::getline(statFile, strLine);
178     if (strLine.empty()) {
179         MMI_HILOGE("No valid content was read");
180         statFile.close();
181         return STREAM_BUF_READ_FAIL;
182     }
183     if ((strLine.find("cpu")) == std::string::npos) {
184         MMI_HILOGE("The keyword was not matched. Procedure");
185         statFile.close();
186         return RET_ERR;
187     }
188     std::istringstream ss(strLine);
189     ss >> info.name >> info.user >> info.nice >> info.system >> info.idle >> info.lowait \
190         >> info.irq >> info.softirq >> info.steal >> info.guest >> info.guest_nice;
191 
192     statFile.close();
193     return RET_OK;
194 }
195 
GetSystemCpuUsage()196 double CpuInfo::GetSystemCpuUsage()
197 {
198     Total_Cpu_Occupy first {};
199     int32_t ret = GetSystemCpuStatInfo(first);
200     if (ret != RET_OK) {
201         MMI_HILOGE("Failed to obtain CPU information, errcode:%{public}d", ret);
202         return CPU_USAGE_UNKNOWN;
203     }
204     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
205     Total_Cpu_Occupy second {};
206     ret = GetSystemCpuStatInfo(second);
207     if (ret != RET_OK) {
208         MMI_HILOGE("Failed to obtain CPU information, errcode:%{public}d", ret);
209         return CPU_USAGE_UNKNOWN;
210     }
211 
212     return GetCpuUsage(first, second);
213 }
214 
GetSystemTotalOccupy()215 int64_t CpuInfo::GetSystemTotalOccupy()
216 {
217     int ret = -1;
218     Total_Cpu_Occupy occupy {};
219     if ((ret = GetSystemCpuStatInfo(occupy)) != RET_OK) {
220         MMI_HILOGE("Failed to obtain CPU information, errcode:%{public}d", ret);
221         return RET_ERR;
222     }
223     return (occupy.user + occupy.nice + occupy.system + occupy.idle);
224 }
225 
GetProcCpuUsage(const std::string & process_name)226 double CpuInfo::GetProcCpuUsage(const std::string &process_name)
227 {
228     int64_t totalTime1 = 0;
229     int64_t totalTime2 = 0;
230     int64_t procTime1 = 0;
231     int64_t procTime2 = 0;
232     int32_t pid = GetTaskPidFile(process_name);
233 
234     if ((totalTime1 = GetSystemTotalOccupy()) == RET_ERR) {
235         MMI_HILOGE("Failed to obtain CPU occupy");
236         return CPU_USAGE_UNKNOWN;
237     }
238     if ((procTime1 = GetProcOccupy(pid)) == RET_ERR) {
239         MMI_HILOGE("Failed to obtain process CPU information");
240         return CPU_USAGE_UNKNOWN;
241     }
242 
243     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
244 
245     if ((totalTime2 = GetSystemTotalOccupy()) == RET_ERR) {
246         MMI_HILOGE("Failed to obtain CPU occupy");
247         return CPU_USAGE_UNKNOWN;
248     }
249     if ((procTime2 = GetProcOccupy(pid)) == RET_ERR) {
250         MMI_HILOGE("Failed to obtain process CPU information");
251         return CPU_USAGE_UNKNOWN;
252     }
253 
254     return CHK_RATE(CPU_USAGE_MAX * (procTime2 - procTime1) / (totalTime2 - totalTime1));
255 }
256 } // namespace SYSTEM_INFO
257 } // namespace MMI
258 } // namespace OHOS