Coverage Report

Created: 2026-03-15 18:01

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