Coverage Report

Created: 2024-11-22 12:06

/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 <condition_variable>
28
#include <string>
29
30
#if !defined(__APPLE__) || !defined(_POSIX_C_SOURCE)
31
#include <unistd.h>
32
#else
33
#include <mach/vm_page_size.h>
34
#endif
35
36
#include "common/logging.h"
37
#ifdef USE_JEMALLOC
38
#include "jemalloc/jemalloc.h"
39
#else
40
#include <gperftools/malloc_extension.h>
41
#endif
42
#include "common/config.h"
43
#include "util/perf_counters.h"
44
#include "util/pretty_printer.h"
45
46
namespace doris {
47
48
class RuntimeProfile;
49
50
// Provides the amount of physical memory available.
51
// Populated from /proc/meminfo.
52
// TODO: Combine mem-info, cpu-info and disk-info into hardware-info/perf_counters ?
53
class MemInfo {
54
public:
55
    // Initialize MemInfo.
56
    static void init();
57
58
0
    static inline bool initialized() { return _s_initialized; }
59
60
0
    static int get_page_size() {
61
0
#if !defined(__APPLE__) || !defined(_POSIX_C_SOURCE)
62
0
        return getpagesize();
63
0
#else
64
0
        return vm_page_size;
65
0
#endif
66
0
    }
67
68
    // Get total physical memory in bytes (if has cgroups memory limits, return the limits).
69
307
    static inline int64_t physical_mem() {
70
307
        DCHECK(_s_initialized);
71
307
        return _s_physical_mem.load(std::memory_order_relaxed);
72
307
    }
73
74
    static void refresh_proc_meminfo();
75
76
    static void refresh_memory_bvar();
77
78
0
    static inline int64_t sys_mem_available_low_water_mark() {
79
0
        return _s_sys_mem_available_low_water_mark;
80
0
    }
81
60
    static inline int64_t sys_mem_available_warning_water_mark() {
82
60
        return _s_sys_mem_available_warning_water_mark;
83
60
    }
84
0
    static inline int64_t process_minor_gc_size() {
85
0
        return _s_process_minor_gc_size.load(std::memory_order_relaxed);
86
0
    }
87
0
    static inline int64_t process_full_gc_size() {
88
0
        return _s_process_full_gc_size.load(std::memory_order_relaxed);
89
0
    }
90
91
0
    static inline int64_t get_tc_metrics(const std::string& name) {
92
0
#ifndef USE_JEMALLOC
93
0
        size_t value = 0;
94
0
        MallocExtension::instance()->GetNumericProperty(name.c_str(), &value);
95
0
        return value;
96
0
#endif
97
0
        return 0;
98
0
    }
99
0
    static inline int64_t get_je_metrics(const std::string& name) {
100
0
#ifdef USE_JEMALLOC
101
0
        size_t value = 0;
102
0
        size_t sz = sizeof(value);
103
0
        if (jemallctl(name.c_str(), &value, &sz, nullptr, 0) == 0) {
104
0
            return value;
105
0
        }
106
0
#endif
107
0
        return 0;
108
0
    }
109
110
0
    static inline unsigned get_je_unsigned_metrics(const std::string& name) {
111
0
#ifdef USE_JEMALLOC
112
0
        unsigned value = 0;
113
0
        size_t sz = sizeof(value);
114
0
        if (jemallctl(name.c_str(), &value, &sz, nullptr, 0) == 0) {
115
0
            return value;
116
0
        }
117
0
#endif
118
0
        return 0;
119
0
    }
120
121
0
    static inline int64_t get_je_all_arena_metrics(const std::string& name) {
122
0
#ifdef USE_JEMALLOC
123
0
        return get_je_metrics(fmt::format("stats.arenas.{}.{}", MALLCTL_ARENAS_ALL, name));
124
0
#endif
125
0
        return 0;
126
0
    }
127
128
    static inline int64_t get_je_all_arena_extents_metrics(int64_t page_size_index,
129
0
                                                           const std::string& extent_type) {
130
0
#ifdef USE_JEMALLOC
131
0
        return get_je_metrics(fmt::format("stats.arenas.{}.extents.{}.{}", MALLCTL_ARENAS_ALL,
132
0
                                          page_size_index, extent_type));
133
0
#endif
134
0
        return 0;
135
0
    }
136
137
0
    static inline void je_purge_all_arena_dirty_pages() {
138
0
#ifdef USE_JEMALLOC
139
0
        // https://github.com/jemalloc/jemalloc/issues/2470
140
0
        // If there is a core dump here, it may cover up the real stack, if stack trace indicates heap corruption
141
0
        // (which led to invalid jemalloc metadata), like double free or use-after-free in the application.
142
0
        // Try sanitizers such as ASAN, or build jemalloc with --enable-debug to investigate further.
143
0
        if (config::enable_je_purge_dirty_pages) {
144
0
            try {
145
0
                // Purge all unused dirty pages for arena <i>, or for all arenas if <i> equals MALLCTL_ARENAS_ALL.
146
0
                int err = jemallctl(fmt::format("arena.{}.purge", MALLCTL_ARENAS_ALL).c_str(),
147
0
                                    nullptr, nullptr, nullptr, 0);
148
0
                if (err) {
149
0
                    LOG(WARNING) << "Jemalloc purge all unused dirty pages failed";
150
0
                }
151
0
            } catch (...) {
152
0
                LOG(WARNING) << "Purge all unused dirty pages for all arenas failed";
153
0
            }
154
0
        }
155
0
#endif
156
0
    }
157
158
    // the limit of `tcache` is the number of pages, not the total number of page bytes.
159
    // `tcache` has two cleaning opportunities: 1. the number of memory alloc and releases reaches a certain number,
160
    // recycle pages that has not been used for a long time; 2. recycle all `tcache` when the thread exits.
161
    // here add a total size limit.
162
0
    static inline void je_thread_tcache_flush() {
163
#ifdef USE_JEMALLOC
164
        constexpr size_t TCACHE_LIMIT = (1ULL << 30); // 1G
165
        if (allocator_cache_mem() - je_dirty_pages_mem() > TCACHE_LIMIT) {
166
            int err = jemallctl("thread.tcache.flush", nullptr, nullptr, nullptr, 0);
167
            if (err) {
168
                LOG(WARNING) << "Jemalloc thread.tcache.flush failed";
169
            }
170
        }
171
#endif
172
0
    }
173
174
    static std::mutex je_purge_dirty_pages_lock;
175
    static std::condition_variable je_purge_dirty_pages_cv;
176
    static std::atomic<bool> je_purge_dirty_pages_notify;
177
0
    static void notify_je_purge_dirty_pages() {
178
0
        je_purge_dirty_pages_notify.store(true, std::memory_order_relaxed);
179
0
        je_purge_dirty_pages_cv.notify_all();
180
0
    }
181
182
0
    static inline size_t allocator_virtual_mem() {
183
0
        return _s_virtual_memory_used.load(std::memory_order_relaxed);
184
0
    }
185
1.80k
    static inline size_t allocator_cache_mem() {
186
1.80k
        return _s_allocator_cache_mem.load(std::memory_order_relaxed);
187
1.80k
    }
188
0
    static inline size_t allocator_metadata_mem() {
189
0
        return _s_allocator_metadata_mem.load(std::memory_order_relaxed);
190
0
    }
191
0
    static inline int64_t je_dirty_pages_mem() {
192
0
        return _s_je_dirty_pages_mem.load(std::memory_order_relaxed);
193
0
    }
194
0
    static inline int64_t je_dirty_pages_mem_limit() {
195
0
        return _s_je_dirty_pages_mem_limit.load(std::memory_order_relaxed);
196
0
    }
197
198
    // Tcmalloc property `generic.total_physical_bytes` records the total length of the virtual memory
199
    // obtained by the process malloc, not the physical memory actually used by the process in the OS.
200
    static void refresh_allocator_mem();
201
202
307
    static inline int64_t mem_limit() {
203
307
        DCHECK(_s_initialized);
204
307
        return _s_mem_limit.load(std::memory_order_relaxed);
205
307
    }
206
0
    static inline std::string mem_limit_str() {
207
0
        DCHECK(_s_initialized);
208
0
        return PrettyPrinter::print(_s_mem_limit.load(std::memory_order_relaxed), TUnit::BYTES);
209
0
    }
210
229
    static inline int64_t soft_mem_limit() {
211
229
        DCHECK(_s_initialized);
212
229
        return _s_soft_mem_limit.load(std::memory_order_relaxed);
213
229
    }
214
0
    static inline std::string soft_mem_limit_str() {
215
0
        DCHECK(_s_initialized);
216
0
        return PrettyPrinter::print(_s_soft_mem_limit.load(std::memory_order_relaxed),
217
0
                                    TUnit::BYTES);
218
0
    }
219
220
    static std::string debug_string();
221
222
private:
223
    friend class GlobalMemoryArbitrator;
224
225
    static bool _s_initialized;
226
    static std::atomic<int64_t> _s_physical_mem;
227
    static std::atomic<int64_t> _s_mem_limit;
228
    static std::atomic<int64_t> _s_soft_mem_limit;
229
230
    static std::atomic<int64_t> _s_allocator_cache_mem;
231
    static std::atomic<int64_t> _s_allocator_metadata_mem;
232
    static std::atomic<int64_t> _s_je_dirty_pages_mem;
233
    static std::atomic<int64_t> _s_je_dirty_pages_mem_limit;
234
    static std::atomic<int64_t> _s_virtual_memory_used;
235
236
    static int64_t _s_cgroup_mem_limit;
237
    static int64_t _s_cgroup_mem_usage;
238
    static bool _s_cgroup_mem_refresh_state;
239
    static int64_t _s_cgroup_mem_refresh_wait_times;
240
241
    static std::atomic<int64_t> _s_sys_mem_available;
242
    static int64_t _s_sys_mem_available_low_water_mark;
243
    static int64_t _s_sys_mem_available_warning_water_mark;
244
    static std::atomic<int64_t> _s_process_minor_gc_size;
245
    static std::atomic<int64_t> _s_process_full_gc_size;
246
};
247
248
} // namespace doris