Coverage Report

Created: 2026-04-16 11:29

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
8
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
8
    std::ifstream partitions("/proc/partitions", std::ios::in);
59
60
57
    while (partitions.good() && !partitions.eof()) {
61
49
        std::string line;
62
49
        getline(partitions, line);
63
49
        boost::trim(line);
64
65
49
        std::vector<std::string> fields = absl::StrSplit(line, " ", absl::SkipWhitespace());
66
67
49
        if (fields.size() != 4) {
68
16
            continue;
69
16
        }
70
71
33
        std::string name = fields[3];
72
73
33
        if (name == "name") {
74
8
            continue;
75
8
        }
76
77
        // Remove the partition# from the name.  e.g. sda2 --> sda
78
25
        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
25
        int major_dev_id = atoi(fields[0].c_str());
82
25
        int minor_dev_id = atoi(fields[1].c_str());
83
25
        dev_t dev = makedev(major_dev_id, minor_dev_id);
84
25
        DCHECK(_s_device_id_to_disk_id.find(dev) == _s_device_id_to_disk_id.end());
85
86
25
        int disk_id = -1;
87
25
        std::map<std::string, int>::iterator it = _s_disk_name_to_disk_id.find(name);
88
89
25
        if (it == _s_disk_name_to_disk_id.end()) {
90
            // First time seeing this disk
91
9
            disk_id = cast_set<int>(_s_disks.size());
92
9
            _s_disks.push_back(Disk(name, disk_id));
93
9
            _s_disk_name_to_disk_id[name] = disk_id;
94
16
        } else {
95
16
            disk_id = it->second;
96
16
        }
97
98
25
        _s_device_id_to_disk_id[dev] = disk_id;
99
25
    }
100
101
8
    if (partitions.is_open()) {
102
8
        partitions.close();
103
8
    }
104
105
8
    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
17
    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
9
        std::stringstream ss;
117
9
        ss << "/sys/block/" << _s_disks[i].name << "/queue/rotational";
118
9
        std::ifstream rotational(ss.str().c_str(), std::ios::in);
119
9
        if (rotational.good()) {
120
9
            std::string line;
121
9
            getline(rotational, line);
122
9
            if (line == "0") {
123
0
                _s_disks[i].is_rotational = false;
124
0
            }
125
9
        }
126
9
        if (rotational.is_open()) {
127
9
            rotational.close();
128
9
        }
129
9
    }
130
8
}
131
132
8
void DiskInfo::init() {
133
8
    get_device_names();
134
8
    _s_initialized = true;
135
8
}
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
8
std::string DiskInfo::debug_string() {
150
8
    DCHECK(_s_initialized);
151
8
    std::stringstream stream;
152
8
    stream << "Disk Info: " << std::endl;
153
8
    stream << "  Num disks " << num_disks() << ": ";
154
155
16
    for (int i = 0; i < _s_disks.size(); ++i) {
156
8
        stream << _s_disks[i].name;
157
158
8
        if (i < num_disks() - 1) {
159
0
            stream << ", ";
160
0
        }
161
8
    }
162
163
8
    stream << std::endl;
164
8
    return stream.str();
165
8
}
166
167
Status DiskInfo::get_disk_devices(const std::vector<std::string>& paths,
168
1
                                  std::set<std::string>* devices) {
169
1
    std::vector<std::string> real_paths;
170
1
    for (auto& path : paths) {
171
1
        std::string p;
172
1
        Status st = io::global_local_filesystem()->canonicalize(path, &p);
173
1
        if (!st.ok()) {
174
0
            LOG(WARNING) << "skip disk monitoring of path. " << st;
175
0
            continue;
176
0
        }
177
1
        real_paths.emplace_back(std::move(p));
178
1
    }
179
180
1
    FILE* fp = fopen("/proc/mounts", "r");
181
1
    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
1
    Status status;
191
1
    char* line_ptr = 0;
192
1
    size_t line_buf_size = 0;
193
1
    for (auto& path : real_paths) {
194
1
        size_t max_mount_size = 0;
195
1
        std::string match_dev;
196
1
        rewind(fp);
197
39
        while (getline(&line_ptr, &line_buf_size, fp) > 0) {
198
38
            char dev_path[4096];
199
38
            char mount_path[4096];
200
38
            int num = sscanf(line_ptr, "%4095s %4095s", dev_path, mount_path);
201
38
            if (num < 2) {
202
0
                continue;
203
0
            }
204
38
            size_t mount_size = strlen(mount_path);
205
38
            if (mount_size < max_mount_size || path.size() < mount_size ||
206
38
                strncmp(path.c_str(), mount_path, mount_size) != 0) {
207
37
                continue;
208
37
            }
209
1
            std::string dev(basename(dev_path));
210
1
            boost::trim_right_if(dev, boost::is_any_of("0123456789"));
211
1
            if (_s_disk_name_to_disk_id.find(dev) != std::end(_s_disk_name_to_disk_id)) {
212
1
                max_mount_size = mount_size;
213
1
                match_dev = dev;
214
1
            }
215
1
        }
216
1
        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
1
        if (max_mount_size > 0) {
226
1
            devices->emplace(match_dev);
227
1
        }
228
1
    }
229
1
    if (line_ptr != nullptr) {
230
1
        free(line_ptr);
231
1
    }
232
1
    fclose(fp);
233
1
    return status;
234
1
}
235
236
} // namespace doris