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 CPU_DUMPER_H 16 #define CPU_DUMPER_H 17 #include "util/dump_cpu_info_util.h" 18 #include "hidumper_executor.h" 19 20 namespace OHOS { 21 namespace HiviewDFX { 22 class CPUDumper : public HidumperExecutor { 23 public: 24 CPUDumper(); 25 ~CPUDumper(); 26 DumpStatus PreExecute(const std::shared_ptr<DumperParameter>& parameter, 27 StringMatrix dumpDatas) override; 28 DumpStatus Execute() override; 29 DumpStatus AfterExecute() override; 30 31 private: 32 DumpStatus DumpCpuUsageData(); 33 DumpStatus ReadLoadAvgInfo(const std::string& filePath, std::string& info); 34 bool GetDateAndTime(std::string& dateTime); 35 void CreateDumpTimeString(const std::string& startTime, const std::string& endTime, 36 std::string& timeStr); 37 void AddStrLineToDumpInfo(const std::string& strLine); 38 void CreateCPUStatString(std::string& str); 39 std::shared_ptr<ProcInfo> GetOldProc(const std::string& pid); 40 void DumpProcInfo(); 41 static bool SortProcInfo(std::shared_ptr<ProcInfo> &left, std::shared_ptr<ProcInfo> &right); 42 void GetInitOldCPUInfo(std::shared_ptr<CPUInfo> tar, const std::shared_ptr<CPUInfo> source); 43 bool GetProcCPUInfo(); 44 45 private: 46 static const std::string LOAD_AVG_FILE_PATH; 47 static const size_t LOAD_AVG_INFO_COUNT; 48 static const int TM_START_YEAR; 49 static const int DEC_SYSTEM_VALUE; 50 static const int PROC_CPU_LENGTH; 51 static const long unsigned HUNDRED_PERCENT_VALUE; 52 static const long unsigned DELAY_VALUE = 500000; 53 54 StringMatrix dumpCPUDatas_; 55 bool isDumpCpuUsage_ = false; 56 int cpuUsagePid_ = -1; 57 std::shared_ptr<CPUInfo> curCPUInfo_; 58 std::shared_ptr<CPUInfo> oldCPUInfo_; 59 std::vector<std::shared_ptr<ProcInfo>> curProcs_; 60 std::vector<std::shared_ptr<ProcInfo>> oldProcs_; 61 std::shared_ptr<ProcInfo> curSpecProc_; 62 std::shared_ptr<ProcInfo> oldSpecProc_; 63 std::string startTime_; 64 std::string endTime_; 65 }; 66 } // namespace HiviewDFX 67 } // namespace OHOS 68 #endif // CPU_DUMPER_H 69