1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 CPU_DATA_PLUGIN_H 17 #define CPU_DATA_PLUGIN_H 18 19 #include <dirent.h> 20 #include <fcntl.h> 21 #include <string> 22 #include <unistd.h> 23 #include <fstream> 24 #include <iostream> 25 26 #include "cpu_plugin_config.pb.h" 27 #include "cpu_plugin_result.pb.h" 28 #include "logging.h" 29 #include "plugin_module_api.h" 30 31 enum ErrorType { 32 RET_NULL_ADDR, 33 RET_IVALID_PID, 34 RET_TGID_VALUE_NULL, 35 RET_FAIL = -1, 36 RET_SUCC = 0, 37 }; 38 39 enum ProcessCpuTimeType { 40 PROCESS_UTIME = 0, 41 PROCESS_STIME, 42 PROCESS_CUTIME, 43 PROCESS_CSTIME, 44 PROCESS_UNSPECIFIED, 45 }; 46 47 enum SystemCpuTimeType { 48 SYSTEM_USER = 1, 49 SYSTEM_NICE, 50 SYSTEM_SYSTEM, 51 SYSTEM_IDLE, 52 SYSTEM_IOWAIT, 53 SYSTEM_IRQ, 54 SYSTEM_SOFTIRQ, 55 SYSTEM_STEAL, 56 SYSTEM_UNSPECIFIED, 57 }; 58 59 struct CpuTimeData { 60 int64_t userModeUsageTime; 61 int64_t systemModeUsageTime; 62 int64_t systemUsageTime; 63 int64_t systemBootTime; 64 }; 65 66 struct CpuLoadData { 67 double userLoad; 68 double sysLoad; 69 double totalLoad; 70 }; 71 72 class CpuDataPlugin { 73 public: 74 CpuDataPlugin(); 75 ~CpuDataPlugin(); 76 int Start(const uint8_t* configData, uint32_t configSize); 77 int Report(uint8_t* configData, uint32_t configSize); 78 int ReportOptimize(RandomWriteCtx* randomWrite); 79 int Stop(); 80 81 private: 82 int32_t ReadFile(std::string& fileName); 83 84 template <typename T> void SetTimestamp(T& sampleTimeStamp); 85 86 int64_t GetUserHz(); 87 int64_t GetCpuUsageTime(std::vector<std::string>& cpuUsageVec); 88 89 template <typename T> void WriteProcessCpuUsage(T& cpuUsageInfo, const char* pFile, uint32_t fileLen); 90 91 bool GetSystemCpuTime(std::vector<std::string>& cpuUsageVec, CpuTimeData& cpuTimeData); 92 93 template <typename T> void WriteSystemCpuUsage(T& cpuUsageInfo, CpuLoadData& cpuData, 94 const char* pFile, uint32_t fileLen); 95 96 template <typename T, typename I> void WriteCpuUsageInfo(T& cpuData, I cpuUsageInfo); 97 98 bool addTidBySort(int32_t tid); 99 DIR* OpenDestDir(std::string& dirPath); 100 int32_t GetValidTid(DIR* dirp); 101 ThreadState GetThreadState(const char threadState); 102 103 template <typename T> void WriteThread(T& threadInfo, const char* pFile, uint32_t fileLen, int32_t tid); 104 105 template <typename T> void WriteSingleThreadInfo(T& cpuData, int32_t tid); 106 107 template <typename T> void WriteThreadInfo(T& cpuData); 108 109 int32_t GetCpuFrequency(std::string fileName); 110 int GetCpuCoreSize(); 111 int32_t GetMaxCpuFrequencyIndex(); 112 113 template <typename T> void SetCpuFrequency(T& cpuCoreUsageInfo, int32_t coreNum); 114 115 template <typename T> bool WriteProcnum(T& cpuData); 116 117 // for UT SetPath(std::string path)118 void SetPath(std::string path) 119 { 120 path_ = path; 121 } 122 void SetFreqPath(std::string path); 123 124 private: 125 /* data */ 126 CpuConfig protoConfig_; 127 void* buffer_; 128 std::string path_; 129 int32_t err_; 130 131 int pid_; 132 std::vector<int32_t> tidVec_; 133 int64_t prevProcessCpuTime_; 134 CpuTimeData prevCpuTimeData_; 135 std::map<int32_t, int64_t> prevThreadCpuTimeMap_; 136 std::map<int32_t, int64_t> prevCoreSystemCpuTimeMap_; 137 std::map<int32_t, int64_t> prevCoreSystemBootTimeMap_; 138 std::vector<int32_t> maxFrequencyVec_; 139 std::vector<int32_t> minFrequencyVec_; 140 int32_t maxFreqIndex_; 141 std::string freqPath_; 142 }; 143 144 #endif 145