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