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