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 PROCESS_DATA_PLUGIN_H 1706f6ba60Sopenharmony_ci#define PROCESS_DATA_PLUGIN_H 1806f6ba60Sopenharmony_ci 1906f6ba60Sopenharmony_ci#include <algorithm> 2006f6ba60Sopenharmony_ci#include <dirent.h> 2106f6ba60Sopenharmony_ci#include <fcntl.h> 2206f6ba60Sopenharmony_ci#include <inttypes.h> 2306f6ba60Sopenharmony_ci#include <iomanip> 2406f6ba60Sopenharmony_ci#include <string> 2506f6ba60Sopenharmony_ci#include <sys/stat.h> 2606f6ba60Sopenharmony_ci#include <sys/types.h> 2706f6ba60Sopenharmony_ci#include <unistd.h> 2806f6ba60Sopenharmony_ci#include <unordered_map> 2906f6ba60Sopenharmony_ci#include <utility> 3006f6ba60Sopenharmony_ci 3106f6ba60Sopenharmony_ci#include "logging.h" 3206f6ba60Sopenharmony_ci#include "process_plugin_config.pb.h" 3306f6ba60Sopenharmony_ci#include "process_plugin_result.pb.h" 3406f6ba60Sopenharmony_ci#include "plugin_module_api.h" 3506f6ba60Sopenharmony_ci 3606f6ba60Sopenharmony_cienum ErrorType { 3706f6ba60Sopenharmony_ci RET_NULL_ADDR, 3806f6ba60Sopenharmony_ci RET_IVALID_PID, 3906f6ba60Sopenharmony_ci RET_TGID_VALUE_NULL, 4006f6ba60Sopenharmony_ci RET_FAIL = -1, 4106f6ba60Sopenharmony_ci RET_SUCC = 0, 4206f6ba60Sopenharmony_ci}; 4306f6ba60Sopenharmony_ci 4406f6ba60Sopenharmony_ciclass ProcessDataPlugin { 4506f6ba60Sopenharmony_cipublic: 4606f6ba60Sopenharmony_ci ProcessDataPlugin(); 4706f6ba60Sopenharmony_ci ~ProcessDataPlugin(); 4806f6ba60Sopenharmony_ci int Start(const uint8_t* configData, uint32_t configSize); 4906f6ba60Sopenharmony_ci int Report(uint8_t* data, uint32_t dataSize); 5006f6ba60Sopenharmony_ci int ReportOptimize(RandomWriteCtx* randomWrite); 5106f6ba60Sopenharmony_ci int Stop(); 5206f6ba60Sopenharmony_ci 5306f6ba60Sopenharmony_ci int64_t GetUserHz(); 5406f6ba60Sopenharmony_ci 5506f6ba60Sopenharmony_ci // for UT 5606f6ba60Sopenharmony_ci void SetPath(std::string path) 5706f6ba60Sopenharmony_ci { 5806f6ba60Sopenharmony_ci path_ = path; 5906f6ba60Sopenharmony_ci }; 6006f6ba60Sopenharmony_ci 6106f6ba60Sopenharmony_ciprivate: 6206f6ba60Sopenharmony_ci template <typename T> bool WriteProcesseList(T& processData); 6306f6ba60Sopenharmony_ci 6406f6ba60Sopenharmony_ci DIR* OpenDestDir(const char* dirPath); 6506f6ba60Sopenharmony_ci int32_t GetValidPid(DIR* dirp); 6606f6ba60Sopenharmony_ci std::vector<int> OpenProcPidFiles(int32_t pid); 6706f6ba60Sopenharmony_ci int32_t ReadProcPidFile(int32_t pid, const char* pFileName); 6806f6ba60Sopenharmony_ci 6906f6ba60Sopenharmony_ci template <typename T> void WriteProcessInfo(T& processData, int32_t pid); 7006f6ba60Sopenharmony_ci 7106f6ba60Sopenharmony_ci template <typename T> void WriteProcess(T& processinfo, const char* pFile, uint32_t fileLen, int32_t pid); 7206f6ba60Sopenharmony_ci 7306f6ba60Sopenharmony_ci template <typename T> void SetProcessInfo(T& processinfo, int key, const char* word); 7406f6ba60Sopenharmony_ci 7506f6ba60Sopenharmony_ci bool BufnCmp(const char* src, int srcLen, const char* key, int keyLen); 7606f6ba60Sopenharmony_ci bool addPidBySort(int32_t pid); 7706f6ba60Sopenharmony_ci int GetProcStatusId(const char* src, int srcLen); 7806f6ba60Sopenharmony_ci 7906f6ba60Sopenharmony_ci template <typename T> bool WriteCpuUsageData(int pid, T& cpuInfo); 8006f6ba60Sopenharmony_ci 8106f6ba60Sopenharmony_ci bool ReadCpuUsage(int pid, uint64_t& cpuTime); 8206f6ba60Sopenharmony_ci uint32_t GetCpuUsageData(const std::string& line, uint64_t& cpuTime); 8306f6ba60Sopenharmony_ci bool ReadBootTime(int pid, uint64_t& bootTime); 8406f6ba60Sopenharmony_ci uint32_t GetBootData(const std::string& line, uint64_t& bootTime); 8506f6ba60Sopenharmony_ci 8606f6ba60Sopenharmony_ci template <typename T> bool WriteThreadData(int pid, T& cpuInfo); 8706f6ba60Sopenharmony_ci 8806f6ba60Sopenharmony_ci bool FindFirstNum(char** p); 8906f6ba60Sopenharmony_ci bool GetValidValue(char* p, uint64_t& num); 9006f6ba60Sopenharmony_ci bool FindFirstSpace(char** p); 9106f6ba60Sopenharmony_ci 9206f6ba60Sopenharmony_ci template <typename T> bool GetDiskioData(std::string& line, T& diskioInfo); 9306f6ba60Sopenharmony_ci 9406f6ba60Sopenharmony_ci template <typename T> bool WriteDiskioData(int pid, T& processinfo); 9506f6ba60Sopenharmony_ci 9606f6ba60Sopenharmony_ci template <typename T> bool WritePssData(int pid, T& processinfo); 9706f6ba60Sopenharmony_ci 9806f6ba60Sopenharmony_ci ProcessConfig protoConfig_; 9906f6ba60Sopenharmony_ci std::unique_ptr<uint8_t[]> buffer_; 10006f6ba60Sopenharmony_ci std::vector<int32_t> pids_; 10106f6ba60Sopenharmony_ci std::string path_; 10206f6ba60Sopenharmony_ci int32_t err_; 10306f6ba60Sopenharmony_ci std::unordered_map<int, uint64_t> cpuTime_ = {}; 10406f6ba60Sopenharmony_ci std::unordered_map<int, uint64_t> bootTime_ = {}; 10506f6ba60Sopenharmony_ci}; 10606f6ba60Sopenharmony_ci 10706f6ba60Sopenharmony_ci#endif 108