Apollo 11.0
自动驾驶开放平台
disk_resource_statistic.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
17
18#include <regex>
19#include <vector>
20
21#include <boost/filesystem.hpp>
22
23#include "absl/strings/str_split.h"
24
27
28namespace apollo {
29namespace resource_statistic {
30void DiskResourceStatistic::LoadDiskConf(const ResourceStatisticConf_DiskConf& disk_conf) {
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}
72
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}
113
114} // namespace resource_statistic
115} // namespace apollo
void LoadDiskConf(const ResourceStatisticConf_DiskConf &disk_conf)
Load disk conf.
bool GetDiskMetric(DiskMetric *disk_metric)
Get Disk metric and write it into disk_metric.
first check imutoantoffset saved in device
Definition readme.txt:2
#define AERROR
Definition log.h:44
Some string util functions.
std::vector< std::string > GetStatsLines(const std::string &stat_file, const int line_count)
Get stats lines from file
Definition util.cc:24
class register implement
Definition arena_queue.h:37