Coverage Report

Created: 2026-04-10 18:35

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