• 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 #ifndef SYSTEM_INFO_H
17 #define SYSTEM_INFO_H
18 
19 #include <iostream>
20 
21 namespace OHOS {
22 namespace MMI {
23 namespace SYSTEM_INFO {
24 static constexpr double CPU_USAGE_UNKNOWN = -1.0;
25 static constexpr double CPU_USAGE_LOAD = 20.0;
26 static constexpr double CPU_USAGE_MAX = 100.0;
27 class SystemInfo {
28 public:
29     SystemInfo() = default;
30     virtual ~SystemInfo() = default;
31     virtual double GetSystemCpuUsage();
32     virtual double GetProcCpuUsage(const std::string &process_name);
33 };
34 
35 class CpuInfo : public SystemInfo {
36 public:
37     double GetSystemCpuUsage();
38     double GetProcCpuUsage(const std::string &process_name);
39 private:
40     struct Total_Cpu_Occupy {
41         char name[20] { 0 };
42         int32_t guest_nice { 0 };
43         int32_t system { 0 };
44         int32_t idle { 0 };
45         int32_t nice { 0 };
46         int32_t lowait { 0 };
47         int32_t steal { 0 };
48         int32_t softirq { 0 };
49         int32_t user { 0 };
50         int32_t irq { 0 };
51         int32_t guest { 0 };
52     };
53     struct Proc_Cpu_Occupy {
54         int32_t cutime { 0 };
55         int32_t cstime { 0 };
56         int32_t stime { 0 };
57         int32_t utime { 0 };
58     };
59     int32_t GetTaskPidFile(const std::string &process_name);
60     int32_t GetTaskPidCmd(const std::string &process_name, int32_t flag = 0, std::string user = "");
61     int32_t GetProcOccupy(int32_t pid);
62 
63     int32_t GetSystemCpuStatInfo(Total_Cpu_Occupy &info);
64     int64_t GetSystemTotalOccupy();
65     double GetCpuUsage(const Total_Cpu_Occupy &first, const Total_Cpu_Occupy &second);
66 };
67 } // namespace SYSTEM_INFO
68 } // namespace MMI
69 } // namespace OHOS
70 #endif
71