Coverage Report

Created: 2025-07-27 23:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/disk_info.h
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
#pragma once
19
20
#include <sys/types.h>
21
22
#include <map>
23
#include <set>
24
#include <string>
25
#include <vector>
26
27
#include "common/cast_set.h"
28
#include "common/logging.h"
29
#include "common/status.h"
30
31
namespace doris {
32
33
#include "common/compile_check_begin.h"
34
35
// DiskInfo is an interface to query for the disk information at runtime.  This
36
// contains information about the system as well as the specific data node
37
// configuration.
38
// This information is pulled from /proc/partitions.
39
// TODO: datanode information not implemented
40
class DiskInfo {
41
public:
42
    // Initialize DiskInfo.  Just be called before any other functions.
43
    static void init();
44
45
    // Returns the number of (logical) disks on the system
46
0
    static int num_disks() {
47
0
        DCHECK(_s_initialized);
48
0
        return cast_set<int>(_s_disks.size());
49
0
    }
50
51
    // Returns the 0-based disk index for 'path' (path must be a FS path, not
52
    // hdfs path).
53
    static int disk_id(const char* path);
54
55
    // Returns the device name (e.g. sda) for disk_id
56
0
    static const std::string& device_name(int disk_id) {
57
0
        DCHECK_GE(disk_id, 0);
58
0
        DCHECK_LT(disk_id, _s_disks.size());
59
0
        return _s_disks[disk_id].name;
60
0
    }
61
62
0
    static bool is_rotational(int disk_id) {
63
0
        DCHECK_GE(disk_id, 0);
64
0
        DCHECK_LT(disk_id, _s_disks.size());
65
0
        return _s_disks[disk_id].is_rotational;
66
0
    }
67
68
    static std::string debug_string();
69
70
    // get disk devices of given path
71
    static Status get_disk_devices(const std::vector<std::string>& paths,
72
                                   std::set<std::string>* devices);
73
74
private:
75
    static bool _s_initialized;
76
77
    struct Disk {
78
        // Name of the disk (e.g. sda)
79
        std::string name;
80
81
        // 0 based index.  Does not map to anything in the system, useful to index into
82
        // our structures
83
        int id;
84
85
        bool is_rotational;
86
87
0
        Disk() : name(""), id(0) {}
88
0
        Disk(const std::string& name) : name(name), id(0), is_rotational(true) {}
89
2
        Disk(const std::string& name, int id) : name(name), id(id), is_rotational(true) {}
90
        Disk(const std::string& name, int id, bool is_rotational)
91
0
                : name(name), id(id), is_rotational(is_rotational) {}
92
    };
93
94
    // All disks
95
    static std::vector<Disk> _s_disks;
96
97
    // mapping of dev_ts to disk ids
98
    static std::map<dev_t, int> _s_device_id_to_disk_id;
99
100
    // mapping of devices names to disk ids
101
    static std::map<std::string, int> _s_disk_name_to_disk_id;
102
103
    static int _s_num_datanode_dirs;
104
105
    static void get_device_names();
106
};
107
108
#include "common/compile_check_end.h"
109
110
} // namespace doris