Coverage Report

Created: 2024-11-18 10:37

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