Coverage Report

Created: 2024-11-20 16:51

/root/doris/be/src/util/disk_info.cpp
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "util/disk_info.h"
19
20
// IWYU pragma: no_include <bthread/errno.h>
21
#include <errno.h> // IWYU pragma: keep
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <sys/stat.h>
26
#include <sys/sysmacros.h>
27
#include <sys/types.h>
28
29
#include <algorithm>
30
#include <boost/algorithm/string/classification.hpp>
31
#include <boost/algorithm/string/detail/classification.hpp>
32
#include <boost/algorithm/string/trim.hpp>
33
#include <fstream>
34
#include <iterator>
35
#include <memory>
36
#include <utility>
37
38
#include "gutil/strings/split.h"
39
#include "io/fs/local_file_system.h"
40
41
namespace doris {
42
43
bool DiskInfo::_s_initialized;
44
std::vector<DiskInfo::Disk> DiskInfo::_s_disks;
45
std::map<dev_t, int> DiskInfo::_s_device_id_to_disk_id;
46
std::map<std::string, int> DiskInfo::_s_disk_name_to_disk_id;
47
int DiskInfo::_s_num_datanode_dirs;
48
49
// Parses /proc/partitions to get the number of disks.  A bit of looking around
50
// seems to indicate this as the best way to do this.
51
// TODO: is there not something better than this?
52
1
void DiskInfo::get_device_names() {
53
    // Format of this file is:
54
    //    major, minor, #blocks, name
55
    // We are only interesting in name which is formatted as device_name<partition #>
56
    // The same device will show up multiple times for each partition (e.g. sda1, sda2).
57
1
    std::ifstream partitions("/proc/partitions", std::ios::in);
58
59
8
    while (partitions.good() && !partitions.eof()) {
60
7
        std::string line;
61
7
        getline(partitions, line);
62
7
        boost::trim(line);
63
64
7
        std::vector<std::string> fields = strings::Split(line, " ", strings::SkipWhitespace());
65
66
7
        if (fields.size() != 4) {
67
2
            continue;
68
2
        }
69
70
5
        std::string name = fields[3];
71
72
5
        if (name == "name") {
73
1
            continue;
74
1
        }
75
76
        // Remove the partition# from the name.  e.g. sda2 --> sda
77
4
        boost::trim_right_if(name, boost::is_any_of("0123456789"));
78
79
        // Create a mapping of all device ids (one per partition) to the disk id.
80
4
        int major_dev_id = atoi(fields[0].c_str());
81
4
        int minor_dev_id = atoi(fields[1].c_str());
82
4
        dev_t dev = makedev(major_dev_id, minor_dev_id);
83
4
        DCHECK(_s_device_id_to_disk_id.find(dev) == _s_device_id_to_disk_id.end());
84
85
4
        int disk_id = -1;
86
4
        std::map<std::string, int>::iterator it = _s_disk_name_to_disk_id.find(name);
87
88
4
        if (it == _s_disk_name_to_disk_id.end()) {
89
            // First time seeing this disk
90
2
            disk_id = _s_disks.size();
91
2
            _s_disks.push_back(Disk(name, disk_id));
92
2
            _s_disk_name_to_disk_id[name] = disk_id;
93
2
        } else {
94
2
            disk_id = it->second;
95
2
        }
96
97
4
        _s_device_id_to_disk_id[dev] = disk_id;
98
4
    }
99
100
1
    if (partitions.is_open()) {
101
1
        partitions.close();
102
1
    }
103
104
1
    if (_s_disks.empty()) {
105
        // If all else fails, return 1
106
0
        LOG(WARNING) << "Could not determine number of disks on this machine.";
107
0
        _s_disks.push_back(Disk("sda", 0));
108
0
    }
109
110
    // Determine if the disk is rotational or not.
111
3
    for (int i = 0; i < _s_disks.size(); ++i) {
112
        // We can check if it is rotational by reading:
113
        // /sys/block/<device>/queue/rotational
114
        // If the file is missing or has unexpected data, default to rotational.
115
2
        std::stringstream ss;
116
2
        ss << "/sys/block/" << _s_disks[i].name << "/queue/rotational";
117
2
        std::ifstream rotational(ss.str().c_str(), std::ios::in);
118
2
        if (rotational.good()) {
119
2
            std::string line;
120
2
            getline(rotational, line);
121
2
            if (line == "0") {
122
0
                _s_disks[i].is_rotational = false;
123
0
            }
124
2
        }
125
2
        if (rotational.is_open()) {
126
2
            rotational.close();
127
2
        }
128
2
    }
129
1
}
130
131
1
void DiskInfo::init() {
132
1
    get_device_names();
133
1
    _s_initialized = true;
134
1
}
135
136
0
int DiskInfo::disk_id(const char* path) {
137
0
    struct stat s;
138
0
    stat(path, &s);
139
0
    std::map<dev_t, int>::iterator it = _s_device_id_to_disk_id.find(s.st_dev);
140
141
0
    if (it == _s_device_id_to_disk_id.end()) {
142
0
        return -1;
143
0
    }
144
145
0
    return it->second;
146
0
}
147
148
0
std::string DiskInfo::debug_string() {
149
0
    DCHECK(_s_initialized);
150
0
    std::stringstream stream;
151
0
    stream << "Disk Info: " << std::endl;
152
0
    stream << "  Num disks " << num_disks() << ": ";
153
154
0
    for (int i = 0; i < _s_disks.size(); ++i) {
155
0
        stream << _s_disks[i].name;
156
157
0
        if (i < num_disks() - 1) {
158
0
            stream << ", ";
159
0
        }
160
0
    }
161
162
0
    stream << std::endl;
163
0
    return stream.str();
164
0
}
165
166
Status DiskInfo::get_disk_devices(const std::vector<std::string>& paths,
167
0
                                  std::set<std::string>* devices) {
168
0
    std::vector<std::string> real_paths;
169
0
    for (auto& path : paths) {
170
0
        std::string p;
171
0
        Status st = io::global_local_filesystem()->canonicalize(path, &p);
172
0
        if (!st.ok()) {
173
0
            LOG(WARNING) << "skip disk monitoring of path. " << st;
174
0
            continue;
175
0
        }
176
0
        real_paths.emplace_back(std::move(p));
177
0
    }
178
179
0
    FILE* fp = fopen("/proc/mounts", "r");
180
0
    if (fp == nullptr) {
181
0
        std::stringstream ss;
182
0
        char buf[64];
183
0
        ss << "open /proc/mounts failed, errno:" << errno
184
0
           << ", message:" << strerror_r(errno, buf, 64);
185
0
        LOG(WARNING) << ss.str();
186
0
        return Status::InternalError(ss.str());
187
0
    }
188
189
0
    Status status;
190
0
    char* line_ptr = 0;
191
0
    size_t line_buf_size = 0;
192
0
    for (auto& path : real_paths) {
193
0
        size_t max_mount_size = 0;
194
0
        std::string match_dev;
195
0
        rewind(fp);
196
0
        while (getline(&line_ptr, &line_buf_size, fp) > 0) {
197
0
            char dev_path[4096];
198
0
            char mount_path[4096];
199
0
            int num = sscanf(line_ptr, "%4095s %4095s", dev_path, mount_path);
200
0
            if (num < 2) {
201
0
                continue;
202
0
            }
203
0
            size_t mount_size = strlen(mount_path);
204
0
            if (mount_size < max_mount_size || path.size() < mount_size ||
205
0
                strncmp(path.c_str(), mount_path, mount_size) != 0) {
206
0
                continue;
207
0
            }
208
0
            std::string dev(basename(dev_path));
209
0
            boost::trim_right_if(dev, boost::is_any_of("0123456789"));
210
0
            if (_s_disk_name_to_disk_id.find(dev) != std::end(_s_disk_name_to_disk_id)) {
211
0
                max_mount_size = mount_size;
212
0
                match_dev = dev;
213
0
            }
214
0
        }
215
0
        if (ferror(fp) != 0) {
216
0
            std::stringstream ss;
217
0
            char buf[64];
218
0
            ss << "open /proc/mounts failed, errno:" << errno
219
0
               << ", message:" << strerror_r(errno, buf, 64);
220
0
            LOG(WARNING) << ss.str();
221
0
            status = Status::InternalError(ss.str());
222
0
            break;
223
0
        }
224
0
        if (max_mount_size > 0) {
225
0
            devices->emplace(match_dev);
226
0
        }
227
0
    }
228
0
    if (line_ptr != nullptr) {
229
0
        free(line_ptr);
230
0
    }
231
0
    fclose(fp);
232
0
    return status;
233
0
}
234
235
} // namespace doris