106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci 1606f6ba60Sopenharmony_ci#ifndef CPU_DATA_PLUGIN_H 1706f6ba60Sopenharmony_ci#define CPU_DATA_PLUGIN_H 1806f6ba60Sopenharmony_ci 1906f6ba60Sopenharmony_ci#include <dirent.h> 2006f6ba60Sopenharmony_ci#include <fcntl.h> 2106f6ba60Sopenharmony_ci#include <string> 2206f6ba60Sopenharmony_ci#include <unistd.h> 2306f6ba60Sopenharmony_ci#include <fstream> 2406f6ba60Sopenharmony_ci#include <iostream> 2506f6ba60Sopenharmony_ci 2606f6ba60Sopenharmony_ci#include "cpu_plugin_config.pb.h" 2706f6ba60Sopenharmony_ci#include "cpu_plugin_result.pb.h" 2806f6ba60Sopenharmony_ci#include "logging.h" 2906f6ba60Sopenharmony_ci#include "plugin_module_api.h" 3006f6ba60Sopenharmony_ci 3106f6ba60Sopenharmony_cienum ErrorType { 3206f6ba60Sopenharmony_ci RET_NULL_ADDR, 3306f6ba60Sopenharmony_ci RET_IVALID_PID, 3406f6ba60Sopenharmony_ci RET_TGID_VALUE_NULL, 3506f6ba60Sopenharmony_ci RET_FAIL = -1, 3606f6ba60Sopenharmony_ci RET_SUCC = 0, 3706f6ba60Sopenharmony_ci}; 3806f6ba60Sopenharmony_ci 3906f6ba60Sopenharmony_cienum ProcessCpuTimeType { 4006f6ba60Sopenharmony_ci PROCESS_UTIME = 0, 4106f6ba60Sopenharmony_ci PROCESS_STIME, 4206f6ba60Sopenharmony_ci PROCESS_CUTIME, 4306f6ba60Sopenharmony_ci PROCESS_CSTIME, 4406f6ba60Sopenharmony_ci PROCESS_UNSPECIFIED, 4506f6ba60Sopenharmony_ci}; 4606f6ba60Sopenharmony_ci 4706f6ba60Sopenharmony_cienum SystemCpuTimeType { 4806f6ba60Sopenharmony_ci SYSTEM_USER = 1, 4906f6ba60Sopenharmony_ci SYSTEM_NICE, 5006f6ba60Sopenharmony_ci SYSTEM_SYSTEM, 5106f6ba60Sopenharmony_ci SYSTEM_IDLE, 5206f6ba60Sopenharmony_ci SYSTEM_IOWAIT, 5306f6ba60Sopenharmony_ci SYSTEM_IRQ, 5406f6ba60Sopenharmony_ci SYSTEM_SOFTIRQ, 5506f6ba60Sopenharmony_ci SYSTEM_STEAL, 5606f6ba60Sopenharmony_ci SYSTEM_UNSPECIFIED, 5706f6ba60Sopenharmony_ci}; 5806f6ba60Sopenharmony_ci 5906f6ba60Sopenharmony_cistruct CpuTimeData { 6006f6ba60Sopenharmony_ci int64_t userModeUsageTime; 6106f6ba60Sopenharmony_ci int64_t systemModeUsageTime; 6206f6ba60Sopenharmony_ci int64_t systemUsageTime; 6306f6ba60Sopenharmony_ci int64_t systemBootTime; 6406f6ba60Sopenharmony_ci}; 6506f6ba60Sopenharmony_ci 6606f6ba60Sopenharmony_cistruct CpuLoadData { 6706f6ba60Sopenharmony_ci double userLoad; 6806f6ba60Sopenharmony_ci double sysLoad; 6906f6ba60Sopenharmony_ci double totalLoad; 7006f6ba60Sopenharmony_ci}; 7106f6ba60Sopenharmony_ci 7206f6ba60Sopenharmony_ciclass CpuDataPlugin { 7306f6ba60Sopenharmony_cipublic: 7406f6ba60Sopenharmony_ci CpuDataPlugin(); 7506f6ba60Sopenharmony_ci ~CpuDataPlugin(); 7606f6ba60Sopenharmony_ci int Start(const uint8_t* configData, uint32_t configSize); 7706f6ba60Sopenharmony_ci int Report(uint8_t* configData, uint32_t configSize); 7806f6ba60Sopenharmony_ci int ReportOptimize(RandomWriteCtx* randomWrite); 7906f6ba60Sopenharmony_ci int Stop(); 8006f6ba60Sopenharmony_ci 8106f6ba60Sopenharmony_ciprivate: 8206f6ba60Sopenharmony_ci int32_t ReadFile(std::string& fileName); 8306f6ba60Sopenharmony_ci 8406f6ba60Sopenharmony_ci template <typename T> void SetTimestamp(T& sampleTimeStamp); 8506f6ba60Sopenharmony_ci 8606f6ba60Sopenharmony_ci int64_t GetUserHz(); 8706f6ba60Sopenharmony_ci int64_t GetCpuUsageTime(std::vector<std::string>& cpuUsageVec); 8806f6ba60Sopenharmony_ci 8906f6ba60Sopenharmony_ci template <typename T> void WriteProcessCpuUsage(T& cpuUsageInfo, const char* pFile, uint32_t fileLen); 9006f6ba60Sopenharmony_ci 9106f6ba60Sopenharmony_ci bool GetSystemCpuTime(std::vector<std::string>& cpuUsageVec, CpuTimeData& cpuTimeData); 9206f6ba60Sopenharmony_ci 9306f6ba60Sopenharmony_ci template <typename T> void WriteSystemCpuUsage(T& cpuUsageInfo, CpuLoadData& cpuData, 9406f6ba60Sopenharmony_ci const char* pFile, uint32_t fileLen); 9506f6ba60Sopenharmony_ci 9606f6ba60Sopenharmony_ci template <typename T, typename I> void WriteCpuUsageInfo(T& cpuData, I cpuUsageInfo); 9706f6ba60Sopenharmony_ci 9806f6ba60Sopenharmony_ci bool addTidBySort(int32_t tid); 9906f6ba60Sopenharmony_ci DIR* OpenDestDir(std::string& dirPath); 10006f6ba60Sopenharmony_ci int32_t GetValidTid(DIR* dirp); 10106f6ba60Sopenharmony_ci ThreadState GetThreadState(const char threadState); 10206f6ba60Sopenharmony_ci 10306f6ba60Sopenharmony_ci template <typename T> void WriteThread(T& threadInfo, const char* pFile, uint32_t fileLen, int32_t tid); 10406f6ba60Sopenharmony_ci 10506f6ba60Sopenharmony_ci template <typename T> void WriteSingleThreadInfo(T& cpuData, int32_t tid); 10606f6ba60Sopenharmony_ci 10706f6ba60Sopenharmony_ci template <typename T> void WriteThreadInfo(T& cpuData); 10806f6ba60Sopenharmony_ci 10906f6ba60Sopenharmony_ci int32_t GetCpuFrequency(std::string fileName); 11006f6ba60Sopenharmony_ci int GetCpuCoreSize(); 11106f6ba60Sopenharmony_ci int32_t GetMaxCpuFrequencyIndex(); 11206f6ba60Sopenharmony_ci 11306f6ba60Sopenharmony_ci template <typename T> void SetCpuFrequency(T& cpuCoreUsageInfo, int32_t coreNum); 11406f6ba60Sopenharmony_ci 11506f6ba60Sopenharmony_ci template <typename T> bool WriteProcnum(T& cpuData); 11606f6ba60Sopenharmony_ci 11706f6ba60Sopenharmony_ci // for UT 11806f6ba60Sopenharmony_ci void SetPath(std::string path) 11906f6ba60Sopenharmony_ci { 12006f6ba60Sopenharmony_ci path_ = path; 12106f6ba60Sopenharmony_ci } 12206f6ba60Sopenharmony_ci void SetFreqPath(std::string path); 12306f6ba60Sopenharmony_ci 12406f6ba60Sopenharmony_ciprivate: 12506f6ba60Sopenharmony_ci /* data */ 12606f6ba60Sopenharmony_ci CpuConfig protoConfig_; 12706f6ba60Sopenharmony_ci void* buffer_; 12806f6ba60Sopenharmony_ci std::string path_; 12906f6ba60Sopenharmony_ci int32_t err_; 13006f6ba60Sopenharmony_ci 13106f6ba60Sopenharmony_ci int pid_; 13206f6ba60Sopenharmony_ci std::vector<int32_t> tidVec_; 13306f6ba60Sopenharmony_ci int64_t prevProcessCpuTime_; 13406f6ba60Sopenharmony_ci CpuTimeData prevCpuTimeData_; 13506f6ba60Sopenharmony_ci std::map<int32_t, int64_t> prevThreadCpuTimeMap_; 13606f6ba60Sopenharmony_ci std::map<int32_t, int64_t> prevCoreSystemCpuTimeMap_; 13706f6ba60Sopenharmony_ci std::map<int32_t, int64_t> prevCoreSystemBootTimeMap_; 13806f6ba60Sopenharmony_ci std::vector<int32_t> maxFrequencyVec_; 13906f6ba60Sopenharmony_ci std::vector<int32_t> minFrequencyVec_; 14006f6ba60Sopenharmony_ci int32_t maxFreqIndex_; 14106f6ba60Sopenharmony_ci std::string freqPath_; 14206f6ba60Sopenharmony_ci}; 14306f6ba60Sopenharmony_ci 14406f6ba60Sopenharmony_ci#endif 145