• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef HIDUMPER_UTILS_DUMP_CPU_INFO_H
16 #define HIDUMPER_UTILS_DUMP_CPU_INFO_H
17 #include <string>
18 #include <vector>
19 #include "singleton.h"
20 namespace OHOS {
21 namespace HiviewDFX {
22 struct CPUInfo {
23     long unsigned uTime; // user space cpu time
24     long unsigned nTime; // adjust process priority cpu time
25     long unsigned sTime; // kernel space cpu time
26     long unsigned iTime; // idle cpu time
27     long unsigned iowTime; // io wait cpu time
28     long unsigned irqTime; // hard interrupt cpu time
29     long unsigned sirqTime; // soft interrupt cpu time
30 };
31 
32 struct ProcInfo {
33     long unsigned uTime;
34     long unsigned sTime;
35     long unsigned userSpaceUsage;
36     long unsigned sysSpaceUsage;
37     long unsigned totalUsage;
38     std::string pid;
39     std::string comm;
40     std::string minflt;
41     std::string majflt;
42 };
43 
44 class DumpCpuInfoUtil : public Singleton<DumpCpuInfoUtil> {
45 public:
46     DumpCpuInfoUtil();
47     ~DumpCpuInfoUtil();
48     void UpdateCpuInfo();
49     float GetCpuUsage(const int pid);
50     bool GetCurCPUInfo(std::shared_ptr<CPUInfo> &cpuInfo);
51     bool GetCurProcInfo(std::vector<std::shared_ptr<ProcInfo>> &procInfos);
52     bool GetCurSpecProcInfo(int pid, std::shared_ptr<ProcInfo> &specProc);
53     bool GetOldCPUInfo(std::shared_ptr<CPUInfo> &cpuInfo);
54     bool GetOldProcInfo(std::vector<std::shared_ptr<ProcInfo>> &procInfos);
55     bool GetOldSpecProcInfo(int pid, std::shared_ptr<ProcInfo> &specProc);
56     bool IsNeedRefreshCpu();
57     void CopyCpuInfo(std::shared_ptr<CPUInfo> &tar, const std::shared_ptr<CPUInfo> &source);
58     void GetUpdateCpuStartTime(std::string& dateTime);
59     bool GetDateAndTime(std::string& dateTime);
60 
61 private:
62     void SetCPUInfo(long unsigned& info, const std::string& strInfo);
63     void GetProcessDirFiles(const std::string& path, const std::string& file,
64         std::vector<std::string>& files);
65     void CopyProcInfo(std::shared_ptr<ProcInfo> &tar, const std::shared_ptr<ProcInfo> &source);
66     bool CheckFrequentDumpping();
67 
68 private:
69     static const std::string PROC_STAT_PATH;
70     static const std::string SPACE;
71     static const int TM_START_YEAR;
72     static const int DEC_SYSTEM_VALUE;
73 
74     static const int CPU_STAT_USER_TIME_INDEX = 1;
75     static const int CPU_STAT_NICE_TIME_INDEX = 2;
76     static const int CPU_STAT_SYS_TIME_INDEX = 3;
77     static const int CPU_STAT_IDLE_TIME_INDEX = 4;
78     static const int CPU_STAT_IOW_TIME_INDEX = 5;
79     static const int CPU_STAT_IRQ_TIME_INDEX = 6;
80     static const int CPU_STAT_SIRQ_TIME_INDEX = 7;
81     static const int PROC_INFO_USER_TIME_INDEX = 13;
82     static const int PROC_INFO_SYS_TIME_INDEX = 14;
83     static const int PROC_INFO_MINOR_FAULT_INDEX = 9;
84     static const int PROC_INFO_MAJOR_FAULT_INDEX = 11;
85     static const int CONSTANT_NUM_10 = 10;
86     static const int DUMP_TIME_INTERVAL = 5;
87     static const int CUP_REFRESH_MIN_TIME = 5 * 1000;
88 
89     std::shared_ptr<CPUInfo> curCPUInfo_;
90     std::shared_ptr<CPUInfo> oldCPUInfo_;
91     std::vector<std::shared_ptr<ProcInfo>> curProcs_;
92     std::vector<std::shared_ptr<ProcInfo>> oldProcs_;
93     int dumpTimeSec_{0};
94     long long lastCpuTimeMs_{0};
95     std::string startTime_;
96 };
97 } // namespace HiviewDFX
98 } // namespace OHOS
99 #endif // HIDUMPER_UTILS_DUMP_CPU_INFO_H
100