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 57 private: 58 void SetCPUInfo(long unsigned& info, const std::string& strInfo); 59 void GetProcessDirFiles(const std::string& path, const std::string& file, 60 std::vector<std::string>& files); 61 void CopyCpuInfo(std::shared_ptr<CPUInfo> &tar, const std::shared_ptr<CPUInfo> &source); 62 void CopyProcInfo(std::shared_ptr<ProcInfo> &tar, const std::shared_ptr<ProcInfo> &source); 63 bool CheckFrequentDumpping(); 64 65 private: 66 static const std::string LOAD_AVG_FILE_PATH; 67 static const size_t LOAD_AVG_INFO_COUNT = 3; 68 static const std::string PROC_STAT_FILE_PATH; 69 static const std::string SPACE; 70 static const int CPU_STAT_USER_TIME_INDEX = 1; 71 static const int CPU_STAT_NICE_TIME_INDEX = 2; 72 static const int CPU_STAT_SYS_TIME_INDEX = 3; 73 static const int CPU_STAT_IDLE_TIME_INDEX = 4; 74 static const int CPU_STAT_IOW_TIME_INDEX = 5; 75 static const int CPU_STAT_IRQ_TIME_INDEX = 6; 76 static const int CPU_STAT_SIRQ_TIME_INDEX = 7; 77 static const int PROC_INFO_USER_TIME_INDEX = 13; 78 static const int PROC_INFO_SYS_TIME_INDEX = 14; 79 static const int PROC_INFO_MINOR_FAULT_INDEX = 9; 80 static const int PROC_INFO_MAJOR_FAULT_INDEX = 11; 81 static const int CONSTANT_NUM_10 = 10; 82 static const int DUMP_TIME_INTERVAL = 5; 83 84 std::shared_ptr<CPUInfo> curCPUInfo_; 85 std::shared_ptr<CPUInfo> oldCPUInfo_; 86 std::vector<std::shared_ptr<ProcInfo>> curProcs_; 87 std::vector<std::shared_ptr<ProcInfo>> oldProcs_; 88 int dumpTimeSec_ = 0; 89 std::mutex mutex_; 90 }; 91 } // namespace HiviewDFX 92 } // namespace OHOS 93 #endif // HIDUMPER_UTILS_DUMP_CPU_INFO_H 94