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 IO_STATS_H 17 #define IO_STATS_H 18 19 #include <array> 20 #include <cinttypes> 21 #include <cstdio> 22 #include <fstream> 23 #include <inttypes.h> 24 #include <iostream> 25 #include <memory> 26 #include <string> 27 #include <sys/mman.h> 28 29 #include "diskio_plugin_config.pb.h" 30 #include "diskio_plugin_result.pb.h" 31 #include "logging.h" 32 33 class ProcStats { 34 public: 35 std::string name_; 36 uint64_t user_; 37 uint64_t nice_; 38 uint64_t system_; 39 uint64_t idle_; 40 uint64_t iowait_; 41 uint64_t steal_; 42 uint64_t hardirq_; 43 uint64_t softirq_; 44 uint64_t guest_; 45 uint64_t guestNice_; 46 ProcStats()47 ProcStats() 48 : name_(""), 49 user_(0), 50 nice_(0), 51 system_(0), 52 idle_(0), 53 iowait_(0), 54 steal_(0), 55 hardirq_(0), 56 softirq_(0), 57 guest_(0), 58 guestNice_(0) 59 { 60 } 61 ~ProcStats()62 ~ProcStats() {} 63 GetTotalTime()64 uint64_t GetTotalTime() 65 { 66 return user_ + nice_ + system_ + idle_ + iowait_ + steal_ + hardirq_ + softirq_; 67 } 68 }; 69 70 class DiskStats { 71 public: 72 std::string deviceName_; 73 uint64_t major_; 74 uint64_t minor_; 75 76 uint64_t rSucc_; // 成功完成读的总次数 rd_ios 77 uint64_t rMerged_; // 合并读次数 rd_merges 78 uint64_t rSectors_; // 读扇区的次数 rd_sectors 79 uint64_t timeOfRead_; // 读花的时间(ms) rd_ticks 80 81 uint64_t wSucc_; // 成功完成写的总次数 wr_ios 82 uint64_t wMerged_; // 合并写次数 wr_merges 83 uint64_t wSectors_; // 写扇区的次数 wr_sectors 84 uint64_t timeOfWrite_; // 写花的时间(ms) wr_ticks 85 86 uint64_t dSucc_; 87 uint64_t dMerged_; 88 uint64_t dSectors_; 89 uint64_t timeOfd_; 90 91 uint64_t flushSucc_; 92 uint64_t timeOfFlush_; 93 94 uint64_t ios_; // I/O的当前进度 ios_pgr 95 uint64_t timeOfIo_; // I/O操作上的毫秒数 tot_ticks ? 96 uint64_t weighted_; // 输入/输出操作花费的加权毫秒数 rq_ticks ? 97 DiskStats()98 DiskStats() 99 : deviceName_(""), 100 major_(0), 101 minor_(0), 102 rSucc_(0), 103 rMerged_(0), 104 rSectors_(0), 105 timeOfRead_(0), 106 wSucc_(0), 107 wMerged_(0), 108 wSectors_(0), 109 timeOfWrite_(0), 110 dSucc_(0), 111 dMerged_(0), 112 dSectors_(0), 113 timeOfd_(0), 114 flushSucc_(0), 115 timeOfFlush_(0), 116 ios_(0), 117 timeOfIo_(0), 118 weighted_(0) 119 { 120 } 121 ~DiskStats()122 ~DiskStats() {} 123 }; 124 125 using CpuDatasPtr = std::shared_ptr<ProcStats>; 126 using DiskDatasPtr = std::shared_ptr<DiskStats>; 127 class IoStats { 128 public: 129 IoStats(DiskioConfig::IoReportType type = DiskioConfig::UNSPECIFIED); ~IoStats()130 ~IoStats() {} 131 bool GetIoData(); 132 bool PutPluginStatsData(StatsData* pluginStats); 133 private: 134 bool ParseCpuStats(); 135 bool GetCpuStats(std::string& line); 136 bool ParseIoStats(); 137 bool GetIoStats(std::string& line); 138 uint32_t PutCpuStatsData(StatsData* pluginStats); 139 void CalcCpuStats(const CpuDatasPtr& cpuData, CpuStats* cpuInfo); 140 double KeepTowDigits(const uint64_t& data, uint64_t div); 141 uint32_t PutIoStatsData(StatsData* pluginStats); 142 void CalcIoStats(const DiskDatasPtr& ioData, IoStatData* ioInfo); 143 uint32_t OutputCpuData(); 144 bool OutputIoData(); 145 uint64_t GetSystime(); 146 bool ParseIoStatsEx(); 147 bool StringToL(const char* word, long& value); 148 bool FindFirstNum(char** p); 149 bool RemoveSpaces(char** p); 150 bool FindFirstSpace(char** p); 151 uint32_t ParseLineFields(const std::string& line); 152 uint32_t ParseLineFields(const std::string& line, std::string& name); 153 154 private: 155 std::mutex mutex_; 156 DiskioConfig::IoReportType type_; 157 uint64_t sysTime_; 158 std::deque<CpuDatasPtr> cpuDatas_; 159 std::deque<DiskDatasPtr> ioDatas_; 160 std::vector<uint64_t> fields_; 161 }; 162 163 #endif // #ifndef IO_STATS_H