• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
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 
35 enum ErrorType {
36     RET_NULL_ADDR,
37     RET_IVALID_PID,
38     RET_TGID_VALUE_NULL,
39     RET_FAIL = -1,
40     RET_SUCC = 0,
41 };
42 
43 class ProcessDataPlugin {
44 public:
45     ProcessDataPlugin();
46     ~ProcessDataPlugin();
47     int Start(const uint8_t* configData, uint32_t configSize);
48     int Report(uint8_t* configData, uint32_t configSize);
49     int Stop();
50 
51     int64_t GetUserHz();
52 
53     // for UT
SetPath(std::string path)54     void SetPath(std::string path)
55     {
56         path_ = path;
57     };
58 
59 private:
60     bool WriteProcesseList(ProcessData& data);
61     DIR* OpenDestDir(const char* dirPath);
62     int32_t GetValidPid(DIR* dirp);
63     std::vector<int> OpenProcPidFiles(int32_t pid);
64     int32_t ReadProcPidFile(int32_t pid, const char* pFileName);
65     void WriteProcessInfo(ProcessData& data, int32_t pid);
66     void WriteProcess(ProcessInfo* processinfo, const char* pFile, uint32_t fileLen, int32_t pid);
67     void SetProcessInfo(ProcessInfo* processinfo, int key, const char* word);
68     bool BufnCmp(const char* src, int srcLen, const char* key, int keyLen);
69     bool addPidBySort(int32_t pid);
70     int GetProcStatusId(const char* src, int srcLen);
71     bool WriteCpuUsageData(int pid, CpuInfo* protoc);
72     bool ReadCpuUsage(int pid, CpuInfo* protoc, uint64_t& cpuTime);
73     uint32_t GetCpuUsageData(const std::string& line, CpuInfo* protoc, uint64_t& cpuTime);
74     bool ReadBootTime(int pid, CpuInfo* protoc, uint64_t& bootTime);
75     uint32_t GetBootData(const std::string& line, CpuInfo* protoc, uint64_t& bootTime);
76     bool WriteThreadData(int pid, CpuInfo* protoc);
77     bool FindFirstNum(char** p);
78     bool GetValidValue(char* p, uint64_t& num);
79     bool FindFirstSpace(char** p);
80     bool GetDiskioData(std::string& line, DiskioInfo* protoc);
81     bool WriteDiskioData(int pid, DiskioInfo* protoc);
82     bool WritePssData(int pid, PssInfo* protoc);
83 
84     ProcessConfig protoConfig_;
85     std::unique_ptr<uint8_t[]> buffer_;
86     std::vector<int32_t> pids_;
87     std::string path_;
88     int32_t err_;
89     std::unordered_map<int, uint64_t> cpuTime_ = {};
90     std::unordered_map<int, uint64_t> bootTime_ = {};
91 };
92 
93 #endif
94