Coverage Report

Created: 2024-11-20 12:56

/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 <stddef.h>
24
#include <stdint.h>
25
26
#include <atomic>
27
#include <string>
28
29
#if !defined(__APPLE__) || !defined(_POSIX_C_SOURCE)
30
#include <unistd.h>
31
#else
32
#include <mach/vm_page_size.h>
33
#endif
34
35
#include "common/logging.h"
36
#ifdef USE_JEMALLOC
37
#include "jemalloc/jemalloc.h"
38
#else
39
#include <gperftools/malloc_extension.h>
40
#endif
41
#include "common/config.h"
42
#include "util/perf_counters.h"
43
44
namespace doris {
45
46
class RuntimeProfile;
47
48
// Provides the amount of physical memory available.
49
// Populated from /proc/meminfo.
50
// TODO: Combine mem-info, cpu-info and disk-info into hardware-info/perf_counters ?
51
class MemInfo {
52
public:
53
    // Initialize MemInfo.
54
    static void init();
55
56
0
    static inline bool initialized() { return _s_initialized; }
57
58
0
    static int get_page_size() {
59
0
#if !defined(__APPLE__) || !defined(_POSIX_C_SOURCE)
60
0
        return getpagesize();
61
0
#else
62
0
        return vm_page_size;
63
0
#endif
64
0
    }
65
66
    // Get total physical memory in bytes (if has cgroups memory limits, return the limits).
67
219
    static inline int64_t physical_mem() {
68
219
        DCHECK(_s_initialized);
69
219
        return _s_physical_mem;
70
219
    }
71
72
    static void refresh_proc_meminfo();
73
74
163k
    static inline int64_t sys_mem_available() {
75
163k
        return _s_sys_mem_available.load(std::memory_order_relaxed) -
76
163k
               refresh_interval_memory_growth;
77
163k
    }
78
0
    static inline std::string sys_mem_available_str() { return _s_sys_mem_available_str; }
79
163k
    static inline int64_t sys_mem_available_low_water_mark() {
80
163k
        return _s_sys_mem_available_low_water_mark;
81
163k
    }
82
84
    static inline int64_t sys_mem_available_warning_water_mark() {
83
84
        return _s_sys_mem_available_warning_water_mark;
84
84
    }
85
86
0
    static inline int64_t get_tc_metrics(const std::string& name) {
87
0
#ifndef USE_JEMALLOC
88
0
        size_t value = 0;
89
0
        MallocExtension::instance()->GetNumericProperty(name.c_str(), &value);
90
0
        return value;
91
0
#endif
92
0
        return 0;
93
0
    }
94
0
    static inline int64_t get_je_metrics(const std::string& name) {
95
0
#ifdef USE_JEMALLOC
96
0
        size_t value = 0;
97
0
        size_t sz = sizeof(value);
98
0
        if (jemallctl(name.c_str(), &value, &sz, nullptr, 0) == 0) {
99
0
            return value;
100
0
        }
101
0
#endif
102
0
        return 0;
103
0
    }
104
105
0
    static inline int64_t get_je_all_arena_metrics(const std::string& name) {
106
0
#ifdef USE_JEMALLOC
107
0
        return get_je_metrics(fmt::format("stats.arenas.{}.{}", MALLCTL_ARENAS_ALL, name));
108
0
#endif
109
0
        return 0;
110
0
    }
111
112
0
    static inline void je_purge_all_arena_dirty_pages() {
113
#ifdef USE_JEMALLOC
114
        // https://github.com/jemalloc/jemalloc/issues/2470
115
        // If there is a core dump here, it may cover up the real stack, if stack trace indicates heap corruption
116
        // (which led to invalid jemalloc metadata), like double free or use-after-free in the application.
117
        // Try sanitizers such as ASAN, or build jemalloc with --enable-debug to investigate further.
118
        if (config::enable_je_purge_dirty_pages) {
119
            try {
120
                // Purge all unused dirty pages for arena <i>, or for all arenas if <i> equals MALLCTL_ARENAS_ALL.
121
                jemallctl(fmt::format("arena.{}.purge", MALLCTL_ARENAS_ALL).c_str(), nullptr,
122
                          nullptr, nullptr, 0);
123
            } catch (...) {
124
                LOG(WARNING) << "Purge all unused dirty pages for all arenas failed";
125
            }
126
        }
127
#endif
128
0
    }
129
130
0
    static inline size_t allocator_virtual_mem() {
131
0
        return _s_virtual_memory_used.load(std::memory_order_relaxed);
132
0
    }
133
0
    static inline size_t allocator_cache_mem() {
134
0
        return _s_allocator_cache_mem.load(std::memory_order_relaxed);
135
0
    }
136
0
    static inline std::string allocator_cache_mem_str() { return _s_allocator_cache_mem_str; }
137
163k
    static inline int64_t proc_mem_no_allocator_cache() {
138
163k
        return _s_proc_mem_no_allocator_cache.load(std::memory_order_relaxed) +
139
163k
               refresh_interval_memory_growth;
140
163k
    }
141
142
    // Tcmalloc property `generic.total_physical_bytes` records the total length of the virtual memory
143
    // obtained by the process malloc, not the physical memory actually used by the process in the OS.
144
    static void refresh_allocator_mem();
145
146
    /** jemalloc pdirty is number of pages within unused extents that are potentially
147
      * dirty, and for which madvise() or similar has not been called.
148
      *
149
      * So they will be subtracted from RSS to make accounting more
150
      * accurate, since those pages are not really RSS but a memory
151
      * that can be used at anytime via jemalloc.
152
      */
153
0
    static inline void refresh_proc_mem_no_allocator_cache() {
154
0
        _s_proc_mem_no_allocator_cache.store(
155
0
                PerfCounters::get_vm_rss() - static_cast<int64_t>(_s_allocator_cache_mem.load(
156
0
                                                     std::memory_order_relaxed)),
157
0
                std::memory_order_relaxed);
158
0
        refresh_interval_memory_growth = 0;
159
0
    }
160
161
163k
    static inline int64_t mem_limit() {
162
163k
        DCHECK(_s_initialized);
163
163k
        return _s_mem_limit;
164
163k
    }
165
0
    static inline std::string mem_limit_str() {
166
0
        DCHECK(_s_initialized);
167
0
        return _s_mem_limit_str;
168
0
    }
169
141
    static inline int64_t soft_mem_limit() {
170
141
        DCHECK(_s_initialized);
171
141
        return _s_soft_mem_limit;
172
141
    }
173
0
    static inline std::string soft_mem_limit_str() {
174
0
        DCHECK(_s_initialized);
175
0
        return _s_soft_mem_limit_str;
176
0
    }
177
84
    static bool is_exceed_soft_mem_limit(int64_t bytes = 0) {
178
84
        return proc_mem_no_allocator_cache() + bytes >= soft_mem_limit() ||
179
84
               sys_mem_available() < sys_mem_available_warning_water_mark();
180
84
    }
181
182
    static std::string debug_string();
183
184
    static bool process_minor_gc();
185
    static bool process_full_gc();
186
187
    static int64_t tg_not_enable_overcommit_group_gc();
188
    static int64_t tg_enable_overcommit_group_gc(int64_t request_free_memory,
189
                                                 RuntimeProfile* profile);
190
191
    // It is only used after the memory limit is exceeded. When multiple threads are waiting for the available memory of the process,
192
    // avoid multiple threads starting at the same time and causing OOM.
193
    static std::atomic<int64_t> refresh_interval_memory_growth;
194
195
private:
196
    static bool _s_initialized;
197
    static int64_t _s_physical_mem;
198
    static int64_t _s_mem_limit;
199
    static std::string _s_mem_limit_str;
200
    static int64_t _s_soft_mem_limit;
201
    static std::string _s_soft_mem_limit_str;
202
203
    static std::atomic<int64_t> _s_allocator_cache_mem;
204
    static std::string _s_allocator_cache_mem_str;
205
    static std::atomic<int64_t> _s_virtual_memory_used;
206
    static std::atomic<int64_t> _s_proc_mem_no_allocator_cache;
207
208
    static std::atomic<int64_t> _s_sys_mem_available;
209
    static std::string _s_sys_mem_available_str;
210
    static int64_t _s_sys_mem_available_low_water_mark;
211
    static int64_t _s_sys_mem_available_warning_water_mark;
212
    static int64_t _s_process_minor_gc_size;
213
    static int64_t _s_process_full_gc_size;
214
};
215
216
} // namespace doris