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 PROCESS_DATA_PLUGIN_H 17#define PROCESS_DATA_PLUGIN_H 18 19#include <algorithm> 20#include <dirent.h> 21#include <fcntl.h> 22#include <inttypes.h> 23#include <iomanip> 24#include <string> 25#include <sys/stat.h> 26#include <sys/types.h> 27#include <unistd.h> 28#include <unordered_map> 29#include <utility> 30 31#include "logging.h" 32#include "process_plugin_config.pb.h" 33#include "process_plugin_result.pb.h" 34#include "plugin_module_api.h" 35 36enum ErrorType { 37 RET_NULL_ADDR, 38 RET_IVALID_PID, 39 RET_TGID_VALUE_NULL, 40 RET_FAIL = -1, 41 RET_SUCC = 0, 42}; 43 44class ProcessDataPlugin { 45public: 46 ProcessDataPlugin(); 47 ~ProcessDataPlugin(); 48 int Start(const uint8_t* configData, uint32_t configSize); 49 int Report(uint8_t* data, uint32_t dataSize); 50 int ReportOptimize(RandomWriteCtx* randomWrite); 51 int Stop(); 52 53 int64_t GetUserHz(); 54 55 // for UT 56 void SetPath(std::string path) 57 { 58 path_ = path; 59 }; 60 61private: 62 template <typename T> bool WriteProcesseList(T& processData); 63 64 DIR* OpenDestDir(const char* dirPath); 65 int32_t GetValidPid(DIR* dirp); 66 std::vector<int> OpenProcPidFiles(int32_t pid); 67 int32_t ReadProcPidFile(int32_t pid, const char* pFileName); 68 69 template <typename T> void WriteProcessInfo(T& processData, int32_t pid); 70 71 template <typename T> void WriteProcess(T& processinfo, const char* pFile, uint32_t fileLen, int32_t pid); 72 73 template <typename T> void SetProcessInfo(T& processinfo, int key, const char* word); 74 75 bool BufnCmp(const char* src, int srcLen, const char* key, int keyLen); 76 bool addPidBySort(int32_t pid); 77 int GetProcStatusId(const char* src, int srcLen); 78 79 template <typename T> bool WriteCpuUsageData(int pid, T& cpuInfo); 80 81 bool ReadCpuUsage(int pid, uint64_t& cpuTime); 82 uint32_t GetCpuUsageData(const std::string& line, uint64_t& cpuTime); 83 bool ReadBootTime(int pid, uint64_t& bootTime); 84 uint32_t GetBootData(const std::string& line, uint64_t& bootTime); 85 86 template <typename T> bool WriteThreadData(int pid, T& cpuInfo); 87 88 bool FindFirstNum(char** p); 89 bool GetValidValue(char* p, uint64_t& num); 90 bool FindFirstSpace(char** p); 91 92 template <typename T> bool GetDiskioData(std::string& line, T& diskioInfo); 93 94 template <typename T> bool WriteDiskioData(int pid, T& processinfo); 95 96 template <typename T> bool WritePssData(int pid, T& processinfo); 97 98 ProcessConfig protoConfig_; 99 std::unique_ptr<uint8_t[]> buffer_; 100 std::vector<int32_t> pids_; 101 std::string path_; 102 int32_t err_; 103 std::unordered_map<int, uint64_t> cpuTime_ = {}; 104 std::unordered_map<int, uint64_t> bootTime_ = {}; 105}; 106 107#endif 108