• 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 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 
30 enum ErrorType {
31     RET_NULL_ADDR,
32     RET_IVALID_PID,
33     RET_TGID_VALUE_NULL,
34     RET_FAIL = -1,
35     RET_SUCC = 0,
36 };
37 
38 enum ProcessCpuTimeType {
39     PROCESS_UTIME = 0,
40     PROCESS_STIME,
41     PROCESS_CUTIME,
42     PROCESS_CSTIME,
43     PROCESS_UNSPECIFIED,
44 };
45 
46 enum SystemCpuTimeType {
47     SYSTEM_USER = 1,
48     SYSTEM_NICE,
49     SYSTEM_SYSTEM,
50     SYSTEM_IDLE,
51     SYSTEM_IOWAIT,
52     SYSTEM_IRQ,
53     SYSTEM_SOFTIRQ,
54     SYSTEM_STEAL,
55     SYSTEM_UNSPECIFIED,
56 };
57 
58 struct CpuTimeData {
59     int64_t userModeUsageTime;
60     int64_t systemModeUsageTime;
61     int64_t systemUsageTime;
62     int64_t systemBootTime;
63 };
64 
65 class CpuDataPlugin {
66 public:
67     CpuDataPlugin();
68     ~CpuDataPlugin();
69     int Start(const uint8_t* configData, uint32_t configSize);
70     int Report(uint8_t* configData, uint32_t configSize);
71     int Stop();
72 
73 private:
74     int32_t ReadFile(std::string& fileName);
75     void SetTimestamp(SampleTimeStamp& timestamp);
76 
77     int64_t GetUserHz();
78     int64_t GetCpuUsageTime(std::vector<std::string>& cpuUsageVec);
79     void WriteProcessCpuUsage(CpuUsageInfo& cpuUsageInfo, const char* pFile, uint32_t fileLen);
80     bool GetSystemCpuTime(std::vector<std::string>& cpuUsageVec, CpuTimeData& cpuTimeData);
81     void WriteSystemCpuUsage(CpuData& cpuData, const char* pFile, uint32_t fileLen);
82     void WriteCpuUsageInfo(CpuData& data);
83 
84     bool addTidBySort(int32_t tid);
85     DIR* OpenDestDir(std::string& dirPath);
86     int32_t GetValidTid(DIR* dirp);
87     ThreadState GetThreadState(const char threadState);
88     void WriteThread(ThreadInfo& threadInfo, const char* pFile, uint32_t fileLen, int32_t tid);
89     void WriteSingleThreadInfo(CpuData& data, int32_t tid);
90     void WriteThreadInfo(CpuData& data);
91 
92     int32_t GetCpuFrequency(std::string fileName);
93     int GetCpuCoreSize();
94     int32_t GetMaxCpuFrequencyIndex();
95     void SetCpuFrequency(CpuCoreUsageInfo& cpuCore, int32_t coreNum);
96     bool WriteProcnum(CpuData& data);
97 
98     // for UT
SetPath(std::string path)99     void SetPath(std::string path)
100     {
101         path_ = path;
102     }
103     void SetFreqPath(std::string path);
104 
105 private:
106     /* data */
107     CpuConfig protoConfig_;
108     void* buffer_;
109     std::string path_;
110     int32_t err_;
111 
112     int pid_;
113     std::vector<int32_t> tidVec_;
114     int64_t prevProcessCpuTime_;
115     CpuTimeData prevCpuTimeData_;
116     std::map<int32_t, int64_t> prevThreadCpuTimeMap_;
117     std::map<int32_t, int64_t> prevCoreSystemCpuTimeMap_;
118     std::map<int32_t, int64_t> prevCoreSystemBootTimeMap_;
119     std::vector<int32_t> maxFrequencyVec_;
120     std::vector<int32_t> minFrequencyVec_;
121     int32_t maxFreqIndex_;
122     std::string freqPath_;
123 };
124 
125 #endif
126