• 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 #include "diskio_data_plugin.h"
17 
18 #include <ctime>
19 
20 #include "buffer_splitter.h"
21 
22 namespace {
23 constexpr size_t READ_BUFFER_SIZE = 1024 * 16;
24 } // namespace
25 
DiskioDataPlugin()26 DiskioDataPlugin::DiskioDataPlugin()
27 {
28     ioEntry_ = nullptr;
29     buffer_ = nullptr;
30     path_ = "/proc/vmstat";
31     err_ = -1;
32     prevRdSectorsKb_ = 0;
33     prevWrSectorsKb_ = 0;
34     prevTimestamp_.set_tv_sec(0);
35     prevTimestamp_.set_tv_nsec(0);
36 }
37 
~DiskioDataPlugin()38 DiskioDataPlugin::~DiskioDataPlugin()
39 {
40     HILOG_INFO(LOG_CORE, "%s:~DiskioDataPlugin!", __func__);
41     if (buffer_ != nullptr) {
42         free(buffer_);
43         buffer_ = nullptr;
44     }
45 }
46 
Start(const uint8_t * configData,uint32_t configSize)47 int DiskioDataPlugin::Start(const uint8_t* configData, uint32_t configSize)
48 {
49     buffer_ = malloc(READ_BUFFER_SIZE);
50     if (buffer_ == nullptr) {
51         HILOG_ERROR(LOG_CORE, "%s:malloc buffer_ failed!", __func__);
52         return RET_FAIL;
53     }
54 
55     if (protoConfig_.ParseFromArray(configData, configSize) <= 0) {
56         HILOG_ERROR(LOG_CORE, "%s:parseFromArray failed!", __func__);
57         return RET_FAIL;
58     }
59 
60     if (protoConfig_.report_io_stats()) {
61         ioEntry_ = std::make_shared<IoStats>(protoConfig_.report_io_stats());
62     }
63     HILOG_INFO(LOG_CORE, "%s:start success!", __func__);
64     return RET_SUCC;
65 }
66 
Report(uint8_t * data,uint32_t dataSize)67 int DiskioDataPlugin::Report(uint8_t* data, uint32_t dataSize)
68 {
69     DiskioData dataProto;
70     uint32_t length;
71 
72     WriteDiskioData(dataProto);
73 
74     if (protoConfig_.report_io_stats() && ioEntry_ != nullptr) {
75         ioEntry_->GetIoData();
76         ioEntry_->PutPluginStatsData(dataProto.mutable_statsdata());
77     }
78 
79     length = dataProto.ByteSizeLong();
80     if (length > dataSize) {
81         return -length;
82     }
83     if (dataProto.SerializeToArray(data, length) > 0) {
84         return length;
85     }
86     return 0;
87 }
88 
Stop()89 int DiskioDataPlugin::Stop()
90 {
91     if (buffer_ != nullptr) {
92         free(buffer_);
93         buffer_ = nullptr;
94     }
95     if (ioEntry_ != nullptr) {
96         ioEntry_.reset();
97         ioEntry_ = nullptr;
98     }
99     HILOG_INFO(LOG_CORE, "%s:plugin:stop success!", __func__);
100     return 0;
101 }
102 
ReadFile(std::string & fileName)103 int32_t DiskioDataPlugin::ReadFile(std::string& fileName)
104 {
105     int fd = -1;
106     ssize_t bytesRead = 0;
107     char realPath[PATH_MAX + 1] = {0};
108 
109     if ((fileName.length() >= PATH_MAX) || (realpath(fileName.c_str(), realPath) == nullptr)) {
110         HILOG_ERROR(LOG_CORE, "%s:path is invalid: %s, errno=%d", __func__, fileName.c_str(), errno);
111         return RET_FAIL;
112     }
113     fd = open(realPath, O_RDONLY | O_CLOEXEC);
114     if (fd == -1) {
115         HILOG_ERROR(LOG_CORE, "%s:failed to open(%s), errno=%d", __func__, fileName.c_str(), errno);
116         err_ = errno;
117         return RET_FAIL;
118     }
119     if (buffer_ == nullptr) {
120         HILOG_ERROR(LOG_CORE, "%s:empty address, buffer_ is NULL", __func__);
121         err_ = RET_NULL_ADDR;
122         close(fd);
123         return RET_FAIL;
124     }
125     bytesRead = read(fd, buffer_, READ_BUFFER_SIZE - 1);
126     if (bytesRead <= 0) {
127         close(fd);
128         HILOG_ERROR(LOG_CORE, "%s:failed to read(%s), errno=%d", __func__, fileName.c_str(), errno);
129         err_ = errno;
130         return RET_FAIL;
131     }
132     close(fd);
133 
134     return bytesRead;
135 }
136 
SetTimestamp(CollectTimeStamp & prevTimestamp,CollectTimeStamp & timestamp)137 void DiskioDataPlugin::SetTimestamp(CollectTimeStamp& prevTimestamp, CollectTimeStamp& timestamp)
138 {
139     timespec time;
140     clock_gettime(CLOCK_MONOTONIC, &time);
141     timestamp.set_tv_sec(time.tv_sec);
142     timestamp.set_tv_nsec(time.tv_nsec);
143     prevTimestamp.set_tv_sec(prevTimestamp_.tv_sec());
144     prevTimestamp.set_tv_nsec(prevTimestamp_.tv_nsec());
145     prevTimestamp_.set_tv_sec(time.tv_sec);
146     prevTimestamp_.set_tv_nsec(time.tv_nsec);
147 }
148 
SetDiskioData(DiskioData & diskioData,const char * pFile,uint32_t fileLen)149 void DiskioDataPlugin::SetDiskioData(DiskioData& diskioData, const char* pFile, uint32_t fileLen)
150 {
151     BufferSplitter totalbuffer(const_cast<char*>(pFile), fileLen + 1);
152     int64_t rd_sectors_kb = 0;
153     int64_t wr_sectors_kb = 0;
154 
155     do {
156         totalbuffer.NextWord(' ');
157         std::string curWord = std::string(totalbuffer.CurWord(), totalbuffer.CurWordSize());
158         if (strcmp(curWord.c_str(), "pgpgin") == 0) {
159             if (!totalbuffer.NextWord('\n')) {
160                 HILOG_ERROR(LOG_CORE, "%s:failed to get pgpgin, CurWord() = %s", __func__, totalbuffer.CurWord());
161                 break;
162             }
163             curWord = std::string(totalbuffer.CurWord(), totalbuffer.CurWordSize());
164             rd_sectors_kb = atoi(curWord.c_str());
165         } else if (strcmp(curWord.c_str(), "pgpgout") == 0) {
166             if (!totalbuffer.NextWord('\n')) {
167                 HILOG_ERROR(LOG_CORE, "%s:failed to get pgpgout, CurWord() = %s", __func__, totalbuffer.CurWord());
168                 break;
169             }
170             curWord = std::string(totalbuffer.CurWord(), totalbuffer.CurWordSize());
171             wr_sectors_kb = atoi(curWord.c_str());
172         }
173     } while (totalbuffer.NextLine());
174 
175     // 前一次系统从磁盘调入的总KB数rd1,通过时间间隔t内KB增量计算磁盘读取速率(rd2-rd1)/t
176     diskioData.set_prev_rd_sectors_kb(prevRdSectorsKb_);
177     // 前一次系统调出到磁盘的总KB数wr1,通过时间间隔t内KB增量计算磁盘写入速率(wr2-wr1)/t
178     diskioData.set_prev_wr_sectors_kb(prevWrSectorsKb_);
179     diskioData.set_rd_sectors_kb(rd_sectors_kb); // 当前系统从磁盘调入的总KB数rd2
180     diskioData.set_wr_sectors_kb(wr_sectors_kb); // 当前系统调出到磁盘的总KB数wr2
181     prevRdSectorsKb_ = rd_sectors_kb;
182     prevWrSectorsKb_ = wr_sectors_kb;
183     auto* prevTimestamp = diskioData.mutable_prev_timestamp();
184     auto* timestamp = diskioData.mutable_timestamp();
185     SetTimestamp(*prevTimestamp, *timestamp); // 设置前一次时间戳和当前时间戳,以便计算两次获取数据的时间间隔t
186 }
187 
WriteDiskioData(DiskioData & data)188 void DiskioDataPlugin::WriteDiskioData(DiskioData& data)
189 {
190     int32_t ret = ReadFile(path_);
191     if (ret == RET_FAIL) {
192         return;
193     }
194     if ((buffer_ == nullptr) || (ret == 0)) {
195         return;
196     }
197 
198     SetDiskioData(data, (char*)buffer_, ret);
199 }