Apollo 11.0
自动驾驶开放平台
apollo::resource_statistic::DiskResourceStatistic类 参考

#include <disk_resource_statistic.h>

apollo::resource_statistic::DiskResourceStatistic 的协作图:

Public 成员函数

void SetInterval (uint32_t interval)
 Set timer component interval.
 
void LoadDiskConf (const ResourceStatisticConf_DiskConf &disk_conf)
 Load disk conf.
 
bool GetDiskMetric (DiskMetric *disk_metric)
 Get Disk metric and write it into disk_metric.
 

详细描述

在文件 disk_resource_statistic.h 第 33 行定义.

成员函数说明

◆ GetDiskMetric()

bool apollo::resource_statistic::DiskResourceStatistic::GetDiskMetric ( DiskMetric *  disk_metric)

Get Disk metric and write it into disk_metric.

参数
disk_metricDisk metric proto message
返回
If get metric successfully.

在文件 disk_resource_statistic.cc 第 73 行定义.

73 {
74 static const std::string disks_stat_file = "/proc/diskstats";
75 const int device_index = 2, in_out_ms = 12;
76 static constexpr int kDiskInfo = 128;
77
78 auto devices = disk_metric->mutable_devices();
79 const auto stat_lines = GetStatsLines(disks_stat_file, kDiskInfo);
80 uint64_t disk_stats = 0;
81 for (const auto& line : stat_lines) {
82 const std::vector<std::string> stats = absl::StrSplit(line, ' ', absl::SkipWhitespace());
83 auto& device = stats[device_index];
84 if (disk_load_device_set_.find(device) == disk_load_device_set_.end()) {
85 continue;
86 }
87 disk_stats = std::stoll(stats[in_out_ms]);
88 if (disk_load_pre_stat_map_.find(device) == disk_load_pre_stat_map_.end()) {
89 disk_load_pre_stat_map_[device] = disk_stats;
90 continue;
91 }
92
93 auto metric = devices->Add();
94 metric->set_name(device);
95 // add disk load
96 metric->set_load(
97 100.f
98 * (static_cast<float>(disk_stats - disk_load_pre_stat_map_[device]) / static_cast<float>(interval_)));
99 disk_load_pre_stat_map_[device] = disk_stats;
100 // add disk usage
101 const auto space = boost::filesystem::space(disk_usage_path_map_[device]);
102 const auto used_gb = static_cast<uint32_t>((space.capacity - space.available) >> 30);
103 const auto total_gb = static_cast<uint32_t>(space.capacity >> 30);
104 metric->set_used_gb(used_gb);
105 metric->set_available_gb(static_cast<uint32_t>(space.available >> 30));
106 metric->set_total_gb(total_gb);
107 if (total_gb > 0) {
108 metric->set_usage(100.f * used_gb / total_gb);
109 }
110 }
111 return true;
112}
first check imutoantoffset saved in device
Definition readme.txt:2
std::vector< std::string > GetStatsLines(const std::string &stat_file, const int line_count)
Get stats lines from file
Definition util.cc:24

◆ LoadDiskConf()

void apollo::resource_statistic::DiskResourceStatistic::LoadDiskConf ( const ResourceStatisticConf_DiskConf &  disk_conf)

Load disk conf.

参数
disk_confDisk conf message

在文件 disk_resource_statistic.cc 第 30 行定义.

30 {
31 if (disk_conf.device().size() == 0) {
32 return;
33 }
34 // get all load regexes
35 std::vector<std::regex> regex_list;
36 static const std::string device_prefix = "/dev/";
37 for (auto& pattern : disk_conf.device()) {
38 if (pattern.empty()) {
39 continue;
40 }
41 auto pattern_ = pattern;
42 if (pattern[0] != '/') {
43 // add default /dev/
44 pattern_ = device_prefix + pattern;
45 }
46 try {
47 regex_list.emplace_back(std::regex(pattern_));
48 } catch (std::regex_error& e) {
49 AERROR << "error disk conf: " << pattern << ", regex_error caught: " << e.what();
50 }
51 }
52
53 // find all avaliable disks
54 static const std::string mounts_file = "/proc/mounts";
55 static constexpr size_t kMaxMounts = 500, kDevice = 0, kPath = 1, kSize = 6;
56 const auto mount_lines = GetStatsLines(mounts_file, kMaxMounts);
57 for (const auto& line : mount_lines) {
58 const std::vector<std::string> mounts = absl::StrSplit(line, ' ', absl::SkipWhitespace());
59 // must starts with /dev/
60 if (mounts.size() < kSize || mounts[kDevice].substr(0, device_prefix.size()) != device_prefix) {
61 continue;
62 }
63 for (auto& regex_ : regex_list) {
64 if (std::regex_match(mounts[kDevice], regex_)) {
65 auto device = mounts[kDevice].substr(device_prefix.size());
66 disk_load_device_set_.insert(device);
67 disk_usage_path_map_.insert({device, mounts[kPath]});
68 }
69 }
70 }
71}
#define AERROR
Definition log.h:44

◆ SetInterval()

void apollo::resource_statistic::DiskResourceStatistic::SetInterval ( uint32_t  interval)
inline

Set timer component interval.

参数
intervalThe time interval

在文件 disk_resource_statistic.h 第 39 行定义.

39 {
40 interval_ = interval;
41 }

该类的文档由以下文件生成: