Coverage Report

Created: 2025-03-10 22:58

/root/doris/be/src/util/mem_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
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/mem-info.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <atomic>
24
#include <cstddef>
25
#include <cstdint>
26
#include <string>
27
28
#if !defined(__APPLE__) || !defined(_POSIX_C_SOURCE)
29
#include <unistd.h>
30
#else
31
#include <mach/vm_page_size.h>
32
#endif
33
34
#include "common/config.h"
35
#include "common/logging.h"
36
#include "util/perf_counters.h"
37
#include "util/pretty_printer.h"
38
39
namespace doris {
40
41
// Provides the amount of physical memory available and memory limit.
42
// Populated from /proc/meminfo and other system attributes.
43
// Note: The memory values ​​in MemInfo are original values,
44
// usually you should use the memory values ​​in GlobalMemoryArbitrator,
45
// which are corrected values ​​that better meet your needs.
46
class MemInfo {
47
public:
48
    // Initialize MemInfo.
49
    static void init();
50
51
0
    static inline bool initialized() { return _s_initialized; }
52
53
0
    static int get_page_size() {
54
0
#if !defined(__APPLE__) || !defined(_POSIX_C_SOURCE)
55
0
        return getpagesize();
56
#else
57
        return vm_page_size;
58
#endif
59
0
    }
60
61
    // Get total physical memory in bytes (if has cgroups memory limits, return the limits).
62
1.01k
    static inline int64_t physical_mem() {
63
1.01k
        DCHECK(_s_initialized);
64
1.01k
        return _s_physical_mem.load(std::memory_order_relaxed);
65
1.01k
    }
66
67
    static void refresh_proc_meminfo();
68
69
594
    static inline int64_t sys_mem_available_low_water_mark() {
70
594
        return _s_sys_mem_available_low_water_mark;
71
594
    }
72
24
    static inline int64_t sys_mem_available_warning_water_mark() {
73
24
        return _s_sys_mem_available_warning_water_mark;
74
24
    }
75
0
    static inline int64_t process_minor_gc_size() {
76
0
        return _s_process_minor_gc_size.load(std::memory_order_relaxed);
77
0
    }
78
0
    static inline int64_t process_full_gc_size() {
79
0
        return _s_process_full_gc_size.load(std::memory_order_relaxed);
80
0
    }
81
82
1.89k
    static inline int64_t mem_limit() {
83
1.89k
        DCHECK(_s_initialized);
84
1.89k
        return _s_mem_limit.load(std::memory_order_relaxed);
85
1.89k
    }
86
2
    static inline std::string mem_limit_str() {
87
2
        DCHECK(_s_initialized);
88
2
        return PrettyPrinter::print(_s_mem_limit.load(std::memory_order_relaxed), TUnit::BYTES);
89
2
    }
90
228
    static inline int64_t soft_mem_limit() {
91
228
        DCHECK(_s_initialized);
92
228
        return _s_soft_mem_limit.load(std::memory_order_relaxed);
93
228
    }
94
2
    static inline std::string soft_mem_limit_str() {
95
2
        DCHECK(_s_initialized);
96
2
        return PrettyPrinter::print(_s_soft_mem_limit.load(std::memory_order_relaxed),
97
2
                                    TUnit::BYTES);
98
2
    }
99
0
    static inline int64_t cgroup_mem_limit() {
100
0
        DCHECK(_s_initialized);
101
0
        return _s_cgroup_mem_limit.load(std::memory_order_relaxed);
102
0
    }
103
0
    static inline int64_t cgroup_mem_usage() {
104
0
        DCHECK(_s_initialized);
105
0
        return _s_cgroup_mem_usage.load(std::memory_order_relaxed);
106
0
    }
107
0
    static inline int64_t cgroup_mem_refresh_state() {
108
0
        DCHECK(_s_initialized);
109
0
        return _s_cgroup_mem_refresh_state.load(std::memory_order_relaxed);
110
0
    }
111
112
    static std::string debug_string();
113
114
private:
115
    friend class GlobalMemoryArbitrator;
116
117
    static bool _s_initialized;
118
    static std::atomic<int64_t> _s_physical_mem;
119
    static std::atomic<int64_t> _s_mem_limit;
120
    static std::atomic<int64_t> _s_soft_mem_limit;
121
122
    static std::atomic<int64_t> _s_cgroup_mem_limit;
123
    static std::atomic<int64_t> _s_cgroup_mem_usage;
124
    static std::atomic<bool> _s_cgroup_mem_refresh_state;
125
    static int64_t _s_cgroup_mem_refresh_wait_times;
126
127
    // If you need use system available memory size, use GlobalMemoryArbitrator::sys_mem_available(),
128
    // this value in MemInfo is the original value.
129
    static std::atomic<int64_t> _s_sys_mem_available;
130
    static int64_t _s_sys_mem_available_low_water_mark;
131
    static int64_t _s_sys_mem_available_warning_water_mark;
132
    static std::atomic<int64_t> _s_process_minor_gc_size;
133
    static std::atomic<int64_t> _s_process_full_gc_size;
134
};
135
136
} // namespace doris