Coverage Report

Created: 2026-10-09 14:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/mem_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
// 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 int(vm_page_size);
58
#endif
59
0
    }
60
61
    // Get total physical memory in bytes (if has cgroups memory limits, return the limits).
62
51.0k
    static inline int64_t physical_mem() {
63
51.0k
        DCHECK(_s_initialized);
64
51.0k
        return _s_physical_mem.load(std::memory_order_relaxed);
65
51.0k
    }
66
67
    static void refresh_proc_meminfo();
68
69
48.6M
    static inline int64_t sys_mem_available_low_water_mark() {
70
48.6M
        return _s_sys_mem_available_low_water_mark;
71
48.6M
    }
72
6.54M
    static inline int64_t sys_mem_available_warning_water_mark() {
73
6.54M
        return _s_sys_mem_available_warning_water_mark;
74
6.54M
    }
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
49.9M
    static inline int64_t mem_limit() {
83
49.9M
        DCHECK(_s_initialized);
84
49.9M
        return _s_mem_limit.load(std::memory_order_relaxed);
85
49.9M
    }
86
#ifdef BE_TEST
87
    static void set_mem_limit_for_test(int64_t mem_limit) {
88
        _s_mem_limit.store(mem_limit, std::memory_order_relaxed);
89
    }
90
    static void set_soft_mem_limit_for_test(int64_t soft_mem_limit) {
91
        _s_soft_mem_limit.store(soft_mem_limit, std::memory_order_relaxed);
92
    }
93
    // Returns the previous value so that the test can restore it.
94
    static int64_t set_sys_mem_available_for_test(int64_t sys_mem_available) {
95
        return _s_sys_mem_available.exchange(sys_mem_available, std::memory_order_relaxed);
96
    }
97
#endif
98
50.5k
    static inline std::string mem_limit_str() {
99
50.5k
        DCHECK(_s_initialized);
100
50.5k
        return PrettyPrinter::print(_s_mem_limit.load(std::memory_order_relaxed), TUnit::BYTES);
101
50.5k
    }
102
6.85M
    static inline int64_t soft_mem_limit() {
103
6.85M
        DCHECK(_s_initialized);
104
6.85M
        return _s_soft_mem_limit.load(std::memory_order_relaxed);
105
6.85M
    }
106
50.4k
    static inline std::string soft_mem_limit_str() {
107
50.4k
        DCHECK(_s_initialized);
108
50.4k
        return PrettyPrinter::print(_s_soft_mem_limit.load(std::memory_order_relaxed),
109
50.4k
                                    TUnit::BYTES);
110
50.4k
    }
111
0
    static inline int64_t cgroup_mem_limit() {
112
0
        DCHECK(_s_initialized);
113
0
        return _s_cgroup_mem_limit.load(std::memory_order_relaxed);
114
0
    }
115
81.4k
    static inline int64_t cgroup_mem_usage() {
116
81.4k
        DCHECK(_s_initialized);
117
81.4k
        return _s_cgroup_mem_usage.load(std::memory_order_relaxed);
118
81.4k
    }
119
0
    static inline int64_t cgroup_mem_refresh_state() {
120
        DCHECK(_s_initialized);
121
0
        return _s_cgroup_mem_refresh_state.load(std::memory_order_relaxed);
122
0
    }
123
124
    static std::string debug_string();
125
126
private:
127
    friend class GlobalMemoryArbitrator;
128
129
    static bool _s_initialized;
130
    static std::atomic<int64_t> _s_physical_mem;
131
    static std::atomic<int64_t> _s_mem_limit;
132
    static std::atomic<int64_t> _s_soft_mem_limit;
133
134
    static std::atomic<int64_t> _s_cgroup_mem_limit;
135
    static std::atomic<int64_t> _s_cgroup_mem_usage;
136
    static std::atomic<bool> _s_cgroup_mem_refresh_state;
137
    static int64_t _s_cgroup_mem_refresh_wait_times;
138
139
    // If you need use system available memory size, use GlobalMemoryArbitrator::sys_mem_available(),
140
    // this value in MemInfo is the original value.
141
    static std::atomic<int64_t> _s_sys_mem_available;
142
    static int64_t _s_sys_mem_available_low_water_mark;
143
    static int64_t _s_sys_mem_available_warning_water_mark;
144
    static std::atomic<int64_t> _s_process_minor_gc_size;
145
    static std::atomic<int64_t> _s_process_full_gc_size;
146
};
147
148
} // namespace doris