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