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 | | |
18 | | #include "common/daemon.h" |
19 | | |
20 | | // IWYU pragma: no_include <bthread/errno.h> |
21 | | #include <errno.h> // IWYU pragma: keep |
22 | | #include <gflags/gflags.h> |
23 | | |
24 | | #include "runtime/memory/jemalloc_control.h" |
25 | | #if !defined(__SANITIZE_ADDRESS__) && !defined(ADDRESS_SANITIZER) && !defined(LEAK_SANITIZER) && \ |
26 | | !defined(THREAD_SANITIZER) && !defined(USE_JEMALLOC) |
27 | | #include <gperftools/malloc_extension.h> // IWYU pragma: keep |
28 | | #endif |
29 | | // IWYU pragma: no_include <bits/std_abs.h> |
30 | | #include <butil/iobuf.h> |
31 | | #include <math.h> |
32 | | #include <stdint.h> |
33 | | #include <stdlib.h> |
34 | | |
35 | | // IWYU pragma: no_include <bits/chrono.h> |
36 | | #include <chrono> // IWYU pragma: keep |
37 | | #include <map> |
38 | | #include <ostream> |
39 | | #include <string> |
40 | | |
41 | | #include "cloud/config.h" |
42 | | #include "common/config.h" |
43 | | #include "common/logging.h" |
44 | | #include "common/metrics/doris_metrics.h" |
45 | | #include "common/metrics/metrics.h" |
46 | | #include "common/metrics/system_metrics.h" |
47 | | #include "common/status.h" |
48 | | #include "load/memtable/memtable_memory_limiter.h" |
49 | | #include "runtime/be_proc_monitor.h" |
50 | | #include "runtime/exec_env.h" |
51 | | #include "runtime/fragment_mgr.h" |
52 | | #include "runtime/memory/global_memory_arbitrator.h" |
53 | | #include "runtime/memory/mem_tracker_limiter.h" |
54 | | #include "runtime/memory/memory_reclamation.h" |
55 | | #include "runtime/process_profile.h" |
56 | | #include "runtime/runtime_query_statistics_mgr.h" |
57 | | #include "runtime/workload_group/workload_group_manager.h" |
58 | | #include "storage/storage_engine.h" |
59 | | #include "storage/tablet/tablet_manager.h" |
60 | | #include "util/algorithm_util.h" |
61 | | #include "util/mem_info.h" |
62 | | #include "util/perf_counters.h" |
63 | | #include "util/s3_rate_limiter_manager.h" |
64 | | #include "util/time.h" |
65 | | |
66 | | namespace doris { |
67 | | namespace { |
68 | | |
69 | | std::atomic<int64_t> last_print_proc_mem = 0; |
70 | | std::atomic<int32_t> refresh_cache_capacity_sleep_time_ms = 0; |
71 | | std::atomic<int32_t> memory_gc_sleep_time = 0; |
72 | | #ifdef USE_JEMALLOC |
73 | | std::atomic<int32_t> je_reset_dirty_decay_sleep_time_ms = 0; |
74 | | #endif |
75 | | |
76 | 927 | void update_rowsets_and_segments_num_metrics() { |
77 | 927 | if (config::is_cloud_mode()) { |
78 | | // TODO(plat1ko): CloudStorageEngine |
79 | 694 | } else { |
80 | 694 | StorageEngine& engine = ExecEnv::GetInstance()->storage_engine().to_local(); |
81 | 694 | auto* metrics = DorisMetrics::instance(); |
82 | 694 | metrics->all_rowsets_num->set_value(engine.tablet_manager()->get_rowset_nums()); |
83 | 694 | metrics->all_segments_num->set_value(engine.tablet_manager()->get_segment_nums()); |
84 | 694 | } |
85 | 927 | } |
86 | | |
87 | | } // namespace |
88 | | |
89 | 6 | void Daemon::tcmalloc_gc_thread() { |
90 | | // TODO All cache GC wish to be supported |
91 | | #if !defined(ADDRESS_SANITIZER) && !defined(LEAK_SANITIZER) && !defined(THREAD_SANITIZER) && \ |
92 | | !defined(USE_JEMALLOC) |
93 | | |
94 | | // Limit size of tcmalloc cache via release_rate and max_cache_percent. |
95 | | // We adjust release_rate according to memory_pressure, which is usage percent of memory. |
96 | | int64_t max_cache_percent = 60; |
97 | | double release_rates[10] = {1.0, 1.0, 1.0, 5.0, 5.0, 20.0, 50.0, 100.0, 500.0, 2000.0}; |
98 | | int64_t pressure_limit = 90; |
99 | | bool is_performance_mode = false; |
100 | | int64_t physical_limit_bytes = |
101 | | std::min(MemInfo::physical_mem() - MemInfo::sys_mem_available_low_water_mark(), |
102 | | MemInfo::mem_limit()); |
103 | | |
104 | | if (config::memory_mode == std::string("performance")) { |
105 | | max_cache_percent = 100; |
106 | | pressure_limit = 90; |
107 | | is_performance_mode = true; |
108 | | physical_limit_bytes = std::min(MemInfo::mem_limit(), MemInfo::physical_mem()); |
109 | | } else if (config::memory_mode == std::string("compact")) { |
110 | | max_cache_percent = 20; |
111 | | pressure_limit = 80; |
112 | | } |
113 | | |
114 | | int last_ms = 0; |
115 | | const int kMaxLastMs = 30000; |
116 | | const int kIntervalMs = 10; |
117 | | size_t init_aggressive_decommit = 0; |
118 | | size_t current_aggressive_decommit = 0; |
119 | | size_t expected_aggressive_decommit = 0; |
120 | | int64_t last_memory_pressure = 0; |
121 | | |
122 | | MallocExtension::instance()->GetNumericProperty("tcmalloc.aggressive_memory_decommit", |
123 | | &init_aggressive_decommit); |
124 | | current_aggressive_decommit = init_aggressive_decommit; |
125 | | |
126 | | while (!_stop_background_threads_latch.wait_for(std::chrono::milliseconds(kIntervalMs))) { |
127 | | size_t tc_used_bytes = 0; |
128 | | size_t tc_alloc_bytes = 0; |
129 | | size_t rss = PerfCounters::get_vm_rss(); |
130 | | |
131 | | MallocExtension::instance()->GetNumericProperty("generic.total_physical_bytes", |
132 | | &tc_alloc_bytes); |
133 | | MallocExtension::instance()->GetNumericProperty("generic.current_allocated_bytes", |
134 | | &tc_used_bytes); |
135 | | int64_t tc_cached_bytes = (int64_t)tc_alloc_bytes - (int64_t)tc_used_bytes; |
136 | | int64_t to_free_bytes = |
137 | | (int64_t)tc_cached_bytes - ((int64_t)tc_used_bytes * max_cache_percent / 100); |
138 | | to_free_bytes = std::max(to_free_bytes, (int64_t)0); |
139 | | |
140 | | int64_t memory_pressure = 0; |
141 | | int64_t rss_pressure = 0; |
142 | | int64_t alloc_bytes = std::max(rss, tc_alloc_bytes); |
143 | | memory_pressure = alloc_bytes * 100 / physical_limit_bytes; |
144 | | rss_pressure = rss * 100 / physical_limit_bytes; |
145 | | |
146 | | expected_aggressive_decommit = init_aggressive_decommit; |
147 | | if (memory_pressure > pressure_limit) { |
148 | | // We are reaching oom, so release cache aggressively. |
149 | | // Ideally, we should reuse cache and not allocate from system any more, |
150 | | // however, it is hard to set limit on cache of tcmalloc and doris |
151 | | // use mmap in vectorized mode. |
152 | | // Limit cache capactiy is enough. |
153 | | if (rss_pressure > pressure_limit) { |
154 | | int64_t min_free_bytes = alloc_bytes - physical_limit_bytes * 9 / 10; |
155 | | to_free_bytes = std::max(to_free_bytes, min_free_bytes); |
156 | | to_free_bytes = std::max(to_free_bytes, tc_cached_bytes * 30 / 100); |
157 | | // We assure that we have at least 500M bytes in cache. |
158 | | to_free_bytes = std::min(to_free_bytes, tc_cached_bytes - 500 * 1024 * 1024); |
159 | | expected_aggressive_decommit = 1; |
160 | | } |
161 | | last_ms = kMaxLastMs; |
162 | | } else if (memory_pressure > (pressure_limit - 10)) { |
163 | | // In most cases, adjusting release rate is enough, if memory are consumed quickly |
164 | | // we should release manually. |
165 | | if (last_memory_pressure <= (pressure_limit - 10)) { |
166 | | to_free_bytes = std::max(to_free_bytes, tc_cached_bytes * 10 / 100); |
167 | | } |
168 | | } |
169 | | |
170 | | int release_rate_index = int(memory_pressure) / 10; |
171 | | double release_rate = 1.0; |
172 | | if (release_rate_index >= sizeof(release_rates) / sizeof(release_rates[0])) { |
173 | | release_rate = 2000.0; |
174 | | } else { |
175 | | release_rate = release_rates[release_rate_index]; |
176 | | } |
177 | | MallocExtension::instance()->SetMemoryReleaseRate(release_rate); |
178 | | |
179 | | if ((current_aggressive_decommit != expected_aggressive_decommit) && !is_performance_mode) { |
180 | | MallocExtension::instance()->SetNumericProperty("tcmalloc.aggressive_memory_decommit", |
181 | | expected_aggressive_decommit); |
182 | | current_aggressive_decommit = expected_aggressive_decommit; |
183 | | } |
184 | | |
185 | | last_memory_pressure = memory_pressure; |
186 | | // We release at least 2% bytes once, frequent releasing hurts performance. |
187 | | if (to_free_bytes > (physical_limit_bytes * 2 / 100)) { |
188 | | last_ms += kIntervalMs; |
189 | | if (last_ms >= kMaxLastMs) { |
190 | | LOG(INFO) << "generic.current_allocated_bytes " << tc_used_bytes |
191 | | << ", generic.total_physical_bytes " << tc_alloc_bytes << ", rss " << rss |
192 | | << ", max_cache_percent " << max_cache_percent << ", release_rate " |
193 | | << release_rate << ", memory_pressure " << memory_pressure |
194 | | << ", physical_limit_bytes " << physical_limit_bytes << ", to_free_bytes " |
195 | | << to_free_bytes << ", current_aggressive_decommit " |
196 | | << current_aggressive_decommit; |
197 | | MallocExtension::instance()->ReleaseToSystem(to_free_bytes); |
198 | | last_ms = 0; |
199 | | } |
200 | | } else { |
201 | | last_ms = 0; |
202 | | } |
203 | | } |
204 | | #endif |
205 | 6 | } |
206 | | |
207 | 260k | void refresh_process_memory_metrics() { |
208 | 260k | doris::PerfCounters::refresh_proc_status(); |
209 | 260k | doris::MemInfo::refresh_proc_meminfo(); |
210 | 260k | doris::GlobalMemoryArbitrator::reset_refresh_interval_memory_growth(); |
211 | 260k | ExecEnv::GetInstance()->brpc_iobuf_block_memory_tracker()->set_consumption( |
212 | 260k | butil::IOBuf::block_memory()); |
213 | 260k | } |
214 | | |
215 | 260k | void refresh_common_allocator_metrics() { |
216 | | #if !defined(ADDRESS_SANITIZER) && !defined(LEAK_SANITIZER) && !defined(THREAD_SANITIZER) |
217 | | doris::JemallocControl::refresh_allocator_mem(); |
218 | | if (config::enable_system_metrics) { |
219 | | DorisMetrics::instance()->system_metrics()->update_allocator_metrics(); |
220 | | } |
221 | | #endif |
222 | 260k | doris::GlobalMemoryArbitrator::refresh_memory_bvar(); |
223 | 260k | } |
224 | | |
225 | 260k | void refresh_memory_state_after_memory_change() { |
226 | 260k | if (abs(last_print_proc_mem - PerfCounters::get_vm_rss()) > 268435456) { |
227 | 652 | last_print_proc_mem = PerfCounters::get_vm_rss(); |
228 | 652 | doris::MemTrackerLimiter::clean_tracker_limiter_group(); |
229 | 652 | doris::ProcessProfile::instance()->memory_profile()->enable_print_log_process_usage(); |
230 | 652 | doris::ProcessProfile::instance()->memory_profile()->refresh_memory_overview_profile(); |
231 | 652 | doris::JemallocControl::notify_je_purge_dirty_pages(); |
232 | 652 | LOG(INFO) << doris::GlobalMemoryArbitrator:: |
233 | 652 | process_mem_log_str(); // print mem log when memory state by 256M |
234 | 652 | } |
235 | 260k | } |
236 | | |
237 | 260k | void refresh_cache_capacity() { |
238 | 260k | if (doris::GlobalMemoryArbitrator::cache_adjust_capacity_notify.load( |
239 | 260k | std::memory_order_relaxed)) { |
240 | | // the last cache capacity adjustment has not been completed. |
241 | | // if not return, last_periodic_refreshed_cache_capacity_adjust_weighted may be modified, but notify is ignored. |
242 | 0 | return; |
243 | 0 | } |
244 | 260k | if (refresh_cache_capacity_sleep_time_ms <= 0) { |
245 | 250k | auto cache_capacity_reduce_mem_limit = int64_t( |
246 | 250k | doris::MemInfo::soft_mem_limit() * config::cache_capacity_reduce_mem_limit_frac); |
247 | 250k | int64_t process_memory_usage = doris::GlobalMemoryArbitrator::process_memory_usage(); |
248 | | // the rule is like this: |
249 | | // 1. if the process mem usage < soft memlimit * 0.6, then do not need adjust cache capacity. |
250 | | // 2. if the process mem usage > soft memlimit * 0.6 and process mem usage < soft memlimit, then it will be adjusted to a lower value. |
251 | | // 3. if the process mem usage > soft memlimit, then the capacity is adjusted to 0. |
252 | 250k | double new_cache_capacity_adjust_weighted = |
253 | 250k | AlgoUtil::descent_by_step(10, cache_capacity_reduce_mem_limit, |
254 | 250k | doris::MemInfo::soft_mem_limit(), process_memory_usage); |
255 | 250k | if (new_cache_capacity_adjust_weighted != |
256 | 250k | doris::GlobalMemoryArbitrator::last_periodic_refreshed_cache_capacity_adjust_weighted) { |
257 | 1.10k | doris::GlobalMemoryArbitrator::last_periodic_refreshed_cache_capacity_adjust_weighted = |
258 | 1.10k | new_cache_capacity_adjust_weighted; |
259 | 1.10k | doris::GlobalMemoryArbitrator::notify_cache_adjust_capacity(); |
260 | 1.10k | refresh_cache_capacity_sleep_time_ms = config::memory_gc_sleep_time_ms; |
261 | 249k | } else { |
262 | 249k | refresh_cache_capacity_sleep_time_ms = 0; |
263 | 249k | } |
264 | 250k | } |
265 | 260k | refresh_cache_capacity_sleep_time_ms -= config::memory_maintenance_sleep_time_ms; |
266 | 260k | } |
267 | | |
268 | 260k | void je_reset_dirty_decay() { |
269 | | #ifdef USE_JEMALLOC |
270 | | if (doris::JemallocControl::je_reset_dirty_decay_notify.load(std::memory_order_relaxed)) { |
271 | | // if not return, je_enable_dirty_page may be modified, but notify is ignored. |
272 | | return; |
273 | | } |
274 | | |
275 | | if (je_reset_dirty_decay_sleep_time_ms <= 0) { |
276 | | bool new_je_enable_dirty_page = true; |
277 | | if (doris::JemallocControl::je_enable_dirty_page) { |
278 | | // if Jemalloc dirty page is enabled and process memory exceed soft mem limit, |
279 | | // disable Jemalloc dirty page. |
280 | | new_je_enable_dirty_page = !GlobalMemoryArbitrator::is_exceed_soft_mem_limit(); |
281 | | } else { |
282 | | // if Jemalloc dirty page is disabled and 10% free memory left to exceed soft mem limit, |
283 | | // enable Jemalloc dirty page, this is to avoid frequent changes |
284 | | // between enabling and disabling Jemalloc dirty pages, if the process memory does |
285 | | // not exceed the soft mem limit after turning off Jemalloc dirty pages, |
286 | | // but it will exceed soft mem limit after turning it on. |
287 | | new_je_enable_dirty_page = !GlobalMemoryArbitrator::is_exceed_soft_mem_limit( |
288 | | int64_t(doris::MemInfo::soft_mem_limit() * 0.1)); |
289 | | } |
290 | | |
291 | | if (doris::JemallocControl::je_enable_dirty_page != new_je_enable_dirty_page) { |
292 | | // `notify_je_reset_dirty_decay` only if `je_enable_dirty_page` changes. |
293 | | doris::JemallocControl::je_enable_dirty_page = new_je_enable_dirty_page; |
294 | | doris::JemallocControl::notify_je_reset_dirty_decay(); |
295 | | je_reset_dirty_decay_sleep_time_ms = config::memory_gc_sleep_time_ms; |
296 | | } else { |
297 | | je_reset_dirty_decay_sleep_time_ms = 0; |
298 | | } |
299 | | } |
300 | | je_reset_dirty_decay_sleep_time_ms -= config::memory_maintenance_sleep_time_ms; |
301 | | #endif |
302 | 260k | } |
303 | | |
304 | 260k | void memory_gc() { |
305 | 260k | if (config::disable_memory_gc) { |
306 | 0 | return; |
307 | 0 | } |
308 | 260k | if (memory_gc_sleep_time <= 0) { |
309 | 26.0k | auto gc_func = [](const std::string& revoke_reason) { |
310 | 0 | doris::ProcessProfile::instance()->memory_profile()->print_log_process_usage(); |
311 | 0 | if (doris::MemoryReclamation::revoke_process_memory(revoke_reason)) { |
312 | | // If there is not enough memory to be gc, the process memory usage will not be printed in the next continuous gc. |
313 | 0 | doris::ProcessProfile::instance() |
314 | 0 | ->memory_profile() |
315 | 0 | ->enable_print_log_process_usage(); |
316 | 0 | } |
317 | 0 | }; |
318 | | |
319 | 26.0k | if (doris::GlobalMemoryArbitrator::sys_mem_available() < |
320 | 26.0k | doris::MemInfo::sys_mem_available_low_water_mark()) { |
321 | 0 | gc_func("sys available memory less than low water mark"); |
322 | 26.0k | } else if (doris::GlobalMemoryArbitrator::process_memory_usage() > |
323 | 26.0k | doris::MemInfo::mem_limit()) { |
324 | 0 | gc_func("process memory used exceed limit"); |
325 | 0 | } |
326 | 26.0k | memory_gc_sleep_time = config::memory_gc_sleep_time_ms; |
327 | 26.0k | } |
328 | 260k | memory_gc_sleep_time -= config::memory_maintenance_sleep_time_ms; |
329 | 260k | } |
330 | | |
331 | 6 | void Daemon::memory_maintenance_thread() { |
332 | 6 | doris::enable_profile_counter_check = 0; |
333 | 260k | while (!_stop_background_threads_latch.wait_for( |
334 | 260k | std::chrono::milliseconds(config::memory_maintenance_sleep_time_ms))) { |
335 | | // step 1. Refresh process memory metrics. |
336 | 260k | refresh_process_memory_metrics(); |
337 | | |
338 | | // step 2. Refresh jemalloc/tcmalloc metrics. |
339 | 260k | refresh_common_allocator_metrics(); |
340 | | |
341 | | // step 3. Update and print memory stat when the memory changes by 256M. |
342 | 260k | refresh_memory_state_after_memory_change(); |
343 | | |
344 | | // step 4. Asyn Refresh cache capacity |
345 | | // TODO adjust cache capacity based on smoothstep (smooth gradient). |
346 | 260k | refresh_cache_capacity(); |
347 | | |
348 | | // step 5. Cancel top memory task when process memory exceed hard limit. |
349 | 260k | memory_gc(); |
350 | | |
351 | | // step 6. Refresh weighted memory ratio of workload groups. |
352 | 260k | doris::ExecEnv::GetInstance()->workload_group_mgr()->do_sweep(); |
353 | 260k | doris::ExecEnv::GetInstance()->workload_group_mgr()->refresh_workload_group_memory_state(); |
354 | | |
355 | | // step 7: handle paused queries(caused by memory insufficient) |
356 | 260k | doris::ExecEnv::GetInstance()->workload_group_mgr()->handle_paused_queries(); |
357 | | |
358 | | // step 8. Flush memtable |
359 | 260k | doris::GlobalMemoryArbitrator::notify_memtable_memory_refresh(); |
360 | | // TODO notify flush memtable |
361 | | |
362 | | // step 9. Reset Jemalloc dirty page decay. |
363 | 260k | je_reset_dirty_decay(); |
364 | 260k | } |
365 | 6 | } |
366 | | |
367 | 6 | void Daemon::memtable_memory_refresh_thread() { |
368 | | // Refresh the memory statistics of the load channel tracker more frequently, |
369 | | // which helps to accurately control the memory of LoadChannelMgr. |
370 | 260k | do { |
371 | 260k | std::unique_lock<std::mutex> l(doris::GlobalMemoryArbitrator::memtable_memory_refresh_lock); |
372 | 523k | while (_stop_background_threads_latch.count() != 0 && |
373 | 523k | !doris::GlobalMemoryArbitrator::memtable_memory_refresh_notify.load( |
374 | 523k | std::memory_order_relaxed)) { |
375 | 262k | doris::GlobalMemoryArbitrator::memtable_memory_refresh_cv.wait_for( |
376 | 262k | l, std::chrono::milliseconds(100)); |
377 | 262k | } |
378 | 260k | if (_stop_background_threads_latch.count() == 0) { |
379 | 2 | break; |
380 | 2 | } |
381 | | |
382 | 260k | Defer defer {[&]() { |
383 | 260k | doris::GlobalMemoryArbitrator::memtable_memory_refresh_notify.store( |
384 | 260k | false, std::memory_order_relaxed); |
385 | 260k | }}; |
386 | 260k | doris::ExecEnv::GetInstance()->memtable_memory_limiter()->refresh_mem_tracker(); |
387 | 260k | } while (true); |
388 | 6 | } |
389 | | |
390 | | /* |
391 | | * this thread will calculate some metrics at a fix interval(15 sec) |
392 | | * 1. push bytes per second |
393 | | * 2. scan bytes per second |
394 | | * 3. max io util of all disks |
395 | | * 4. max network send bytes rate |
396 | | * 5. max network receive bytes rate |
397 | | */ |
398 | 6 | void Daemon::calculate_metrics_thread() { |
399 | 6 | int64_t last_ts = -1L; |
400 | 6 | int64_t lst_query_bytes = -1; |
401 | | |
402 | 6 | std::map<std::string, int64_t> lst_disks_io_time; |
403 | 6 | std::map<std::string, int64_t> lst_net_send_bytes; |
404 | 6 | std::map<std::string, int64_t> lst_net_receive_bytes; |
405 | | |
406 | 933 | do { |
407 | 933 | DorisMetrics::instance()->metric_registry()->trigger_all_hooks(true); |
408 | | |
409 | 933 | if (last_ts == -1L) { |
410 | 6 | last_ts = GetMonoTimeMicros() / 1000; |
411 | 6 | lst_query_bytes = DorisMetrics::instance()->query_scan_bytes->value(); |
412 | 6 | if (config::enable_system_metrics) { |
413 | 3 | DorisMetrics::instance()->system_metrics()->get_disks_io_time(&lst_disks_io_time); |
414 | 3 | DorisMetrics::instance()->system_metrics()->get_network_traffic( |
415 | 3 | &lst_net_send_bytes, &lst_net_receive_bytes); |
416 | 3 | } |
417 | 927 | } else { |
418 | 927 | int64_t current_ts = GetMonoTimeMicros() / 1000; |
419 | 927 | long interval = (current_ts - last_ts) / 1000; |
420 | 927 | last_ts = current_ts; |
421 | | |
422 | | // 1. query bytes per second |
423 | 927 | int64_t current_query_bytes = DorisMetrics::instance()->query_scan_bytes->value(); |
424 | 927 | int64_t qps = (current_query_bytes - lst_query_bytes) / (interval + 1); |
425 | 927 | DorisMetrics::instance()->query_scan_bytes_per_second->set_value(qps < 0 ? 0 : qps); |
426 | 927 | lst_query_bytes = current_query_bytes; |
427 | | |
428 | 927 | if (config::enable_system_metrics) { |
429 | | // 2. max disk io util |
430 | 263 | DorisMetrics::instance()->system_metrics()->update_max_disk_io_util_percent( |
431 | 263 | lst_disks_io_time, 15); |
432 | | |
433 | | // update lst map |
434 | 263 | DorisMetrics::instance()->system_metrics()->get_disks_io_time(&lst_disks_io_time); |
435 | | |
436 | | // 3. max network traffic |
437 | 263 | int64_t max_send = 0; |
438 | 263 | int64_t max_receive = 0; |
439 | 263 | DorisMetrics::instance()->system_metrics()->get_max_net_traffic( |
440 | 263 | lst_net_send_bytes, lst_net_receive_bytes, 15, &max_send, &max_receive); |
441 | 263 | DorisMetrics::instance()->system_metrics()->update_max_network_send_bytes_rate( |
442 | 263 | max_send); |
443 | 263 | DorisMetrics::instance()->system_metrics()->update_max_network_receive_bytes_rate( |
444 | 263 | max_receive); |
445 | | // update lst map |
446 | 263 | DorisMetrics::instance()->system_metrics()->get_network_traffic( |
447 | 263 | &lst_net_send_bytes, &lst_net_receive_bytes); |
448 | | |
449 | 263 | DorisMetrics::instance()->system_metrics()->update_be_avail_cpu_num(); |
450 | 263 | } |
451 | 927 | update_rowsets_and_segments_num_metrics(); |
452 | 927 | } |
453 | 933 | } while (!_stop_background_threads_latch.wait_for(std::chrono::seconds(15))); |
454 | 6 | } |
455 | | |
456 | 6 | void Daemon::report_runtime_query_statistics_thread() { |
457 | 4.65k | while (!_stop_background_threads_latch.wait_for( |
458 | 4.65k | std::chrono::milliseconds(config::report_query_statistics_interval_ms))) { |
459 | 4.65k | ExecEnv::GetInstance()->runtime_query_statistics_mgr()->report_runtime_query_statistics(); |
460 | 4.65k | } |
461 | 6 | } |
462 | | |
463 | 6 | void Daemon::je_reset_dirty_decay_thread() const { |
464 | 6 | do { |
465 | 6 | std::unique_lock<std::mutex> l(doris::JemallocControl::je_reset_dirty_decay_lock); |
466 | 140k | while (_stop_background_threads_latch.count() != 0 && |
467 | 140k | !doris::JemallocControl::je_reset_dirty_decay_notify.load( |
468 | 140k | std::memory_order_relaxed)) { |
469 | 140k | doris::JemallocControl::je_reset_dirty_decay_cv.wait_for( |
470 | 140k | l, std::chrono::milliseconds(100)); |
471 | 140k | } |
472 | 6 | if (_stop_background_threads_latch.count() == 0) { |
473 | 2 | break; |
474 | 2 | } |
475 | | |
476 | 4 | Defer defer {[&]() { |
477 | 0 | doris::JemallocControl::je_reset_dirty_decay_notify.store(false, |
478 | 0 | std::memory_order_relaxed); |
479 | 0 | }}; |
480 | | #ifdef USE_JEMALLOC |
481 | | if (config::disable_memory_gc || !config::enable_je_purge_dirty_pages) { |
482 | | continue; |
483 | | } |
484 | | |
485 | | // There is a significant difference only when dirty_decay_ms is equal to 0 or not. |
486 | | // |
487 | | // 1. When dirty_decay_ms is not equal to 0, the free memory will be cached in the Jemalloc |
488 | | // dirty page first. even if dirty_decay_ms is equal to 1, the Jemalloc dirty page will not |
489 | | // be released to the system exactly after 1ms, it will be released according to the decay rule. |
490 | | // The Jemalloc document specifies that dirty_decay_ms is an approximate time. |
491 | | // |
492 | | // 2. It has been observed in an actual cluster that even if dirty_decay_ms is changed |
493 | | // from th default 5000 to 1, Jemalloc dirty page will still cache a large amount of memory, everything |
494 | | // seems to be the same as `dirty_decay_ms:5000`. only when dirty_decay_ms is changed to 0, |
495 | | // jemalloc dirty page will stop caching and free memory will be released to the system immediately. |
496 | | // of course, performance will be affected. |
497 | | // |
498 | | // 3. After reducing dirty_decay_ms, manually calling `decay_all_arena_dirty_pages` may release dirty pages |
499 | | // as soon as possible, but no relevant experimental data can be found, so it is simple and safe |
500 | | // to adjust dirty_decay_ms only between zero and non-zero. |
501 | | |
502 | | if (doris::JemallocControl::je_enable_dirty_page) { |
503 | | doris::JemallocControl::je_reset_all_arena_dirty_decay_ms(config::je_dirty_decay_ms); |
504 | | } else { |
505 | | doris::JemallocControl::je_reset_all_arena_dirty_decay_ms(0); |
506 | | } |
507 | | #endif |
508 | 4 | } while (true); |
509 | 6 | } |
510 | | |
511 | 6 | void Daemon::cache_adjust_capacity_thread() { |
512 | 1.10k | do { |
513 | 1.10k | std::unique_lock<std::mutex> l(doris::GlobalMemoryArbitrator::cache_adjust_capacity_lock); |
514 | 141k | while (_stop_background_threads_latch.count() != 0 && |
515 | 141k | !doris::GlobalMemoryArbitrator::cache_adjust_capacity_notify.load( |
516 | 141k | std::memory_order_relaxed)) { |
517 | 140k | doris::GlobalMemoryArbitrator::cache_adjust_capacity_cv.wait_for( |
518 | 140k | l, std::chrono::milliseconds(100)); |
519 | 140k | } |
520 | 1.10k | double adjust_weighted = std::min<double>( |
521 | 1.10k | GlobalMemoryArbitrator::last_periodic_refreshed_cache_capacity_adjust_weighted, |
522 | 1.10k | GlobalMemoryArbitrator::last_memory_exceeded_cache_capacity_adjust_weighted); |
523 | 1.10k | if (_stop_background_threads_latch.count() == 0) { |
524 | 2 | break; |
525 | 2 | } |
526 | | |
527 | 1.10k | Defer defer {[&]() { |
528 | 1.10k | doris::GlobalMemoryArbitrator::cache_adjust_capacity_notify.store( |
529 | 1.10k | false, std::memory_order_relaxed); |
530 | 1.10k | }}; |
531 | 1.10k | if (config::disable_memory_gc) { |
532 | 0 | continue; |
533 | 0 | } |
534 | 1.10k | if (GlobalMemoryArbitrator::last_affected_cache_capacity_adjust_weighted == |
535 | 1.10k | adjust_weighted) { |
536 | 0 | LOG(INFO) << fmt::format( |
537 | 0 | "[MemoryGC] adjust cache capacity end, adjust_weighted {} has not been " |
538 | 0 | "modified.", |
539 | 0 | adjust_weighted); |
540 | 0 | continue; |
541 | 0 | } |
542 | 1.10k | std::unique_ptr<RuntimeProfile> profile = std::make_unique<RuntimeProfile>(""); |
543 | 1.10k | auto freed_mem = CacheManager::instance()->for_each_cache_refresh_capacity(adjust_weighted, |
544 | 1.10k | profile.get()); |
545 | 1.10k | std::stringstream ss; |
546 | 1.10k | profile->pretty_print(&ss); |
547 | 1.10k | LOG(INFO) << fmt::format( |
548 | 1.10k | "[MemoryGC] adjust cache capacity end, free memory {}, details: {}", |
549 | 1.10k | PrettyPrinter::print(freed_mem, TUnit::BYTES), ss.str()); |
550 | 1.10k | GlobalMemoryArbitrator::last_affected_cache_capacity_adjust_weighted = adjust_weighted; |
551 | 1.10k | } while (true); |
552 | 6 | } |
553 | | |
554 | 6 | void Daemon::cache_prune_stale_thread() { |
555 | 6 | int32_t interval = config::cache_periodic_prune_stale_sweep_sec; |
556 | 236 | while (!_stop_background_threads_latch.wait_for(std::chrono::seconds(interval))) { |
557 | 230 | if (config::cache_periodic_prune_stale_sweep_sec <= 0) { |
558 | 0 | LOG(WARNING) << "config of cache clean interval is: [" << interval |
559 | 0 | << "], it means the cache prune stale thread is disabled, will wait 3s " |
560 | 0 | "and check again."; |
561 | 0 | interval = 3; |
562 | 0 | continue; |
563 | 0 | } |
564 | 230 | if (config::disable_memory_gc) { |
565 | 0 | continue; |
566 | 0 | } |
567 | 230 | CacheManager::instance()->for_each_cache_prune_stale(); |
568 | 230 | interval = config::cache_periodic_prune_stale_sweep_sec; |
569 | 230 | } |
570 | 6 | } |
571 | | |
572 | 3 | void Daemon::be_proc_monitor_thread() { |
573 | 27 | while (!_stop_background_threads_latch.wait_for( |
574 | 27 | std::chrono::milliseconds(config::be_proc_monitor_interval_ms))) { |
575 | 24 | LOG(INFO) << "log be thread num, " << BeProcMonitor::get_be_thread_info(); |
576 | 24 | } |
577 | 3 | } |
578 | | |
579 | 6 | void Daemon::calculate_workload_group_metrics_thread() { |
580 | 2.80k | while (!_stop_background_threads_latch.wait_for( |
581 | 2.80k | std::chrono::milliseconds(config::workload_group_metrics_interval_ms))) { |
582 | 2.80k | ExecEnv::GetInstance()->workload_group_mgr()->refresh_workload_group_metrics(); |
583 | 2.80k | } |
584 | 6 | } |
585 | | |
586 | 6 | void Daemon::s3_rate_limiter_refresh_thread() { |
587 | | // Single trigger for dynamic rate limiter changes: picks up both mutable |
588 | | // s3_{get,put}_* config updates and cgroup CPU quota changes (serverless BEs can |
589 | | // be resized in place). refresh() is idempotent and compares against the buckets' |
590 | | // own parameters, so quiet iterations are cheap no-ops. |
591 | 1.40k | while (!_stop_background_threads_latch.wait_for(std::chrono::seconds(10))) { |
592 | 1.39k | if (config::enable_s3_rate_limiter) { |
593 | 0 | S3RateLimiterManager::instance().refresh(); |
594 | 0 | } |
595 | 1.39k | } |
596 | 6 | } |
597 | | |
598 | 6 | void Daemon::start() { |
599 | 6 | Status st; |
600 | 6 | st = Thread::create( |
601 | 6 | "Daemon", "tcmalloc_gc_thread", [this]() { this->tcmalloc_gc_thread(); }, |
602 | 6 | &_threads.emplace_back()); |
603 | 6 | CHECK(st.ok()) << st; |
604 | 6 | st = Thread::create( |
605 | 6 | "Daemon", "memory_maintenance_thread", [this]() { this->memory_maintenance_thread(); }, |
606 | 6 | &_threads.emplace_back()); |
607 | 6 | CHECK(st.ok()) << st; |
608 | 6 | st = Thread::create( |
609 | 6 | "Daemon", "memtable_memory_refresh_thread", |
610 | 6 | [this]() { this->memtable_memory_refresh_thread(); }, &_threads.emplace_back()); |
611 | 6 | CHECK(st.ok()) << st; |
612 | | |
613 | 6 | if (config::enable_metric_calculator) { |
614 | 6 | st = Thread::create( |
615 | 6 | "Daemon", "calculate_metrics_thread", |
616 | 6 | [this]() { this->calculate_metrics_thread(); }, &_threads.emplace_back()); |
617 | 6 | CHECK(st.ok()) << st; |
618 | 6 | } |
619 | 6 | st = Thread::create( |
620 | 6 | "Daemon", "s3_rate_limiter_refresh_thread", |
621 | 6 | [this]() { this->s3_rate_limiter_refresh_thread(); }, &_threads.emplace_back()); |
622 | 6 | CHECK(st.ok()) << st; |
623 | 6 | st = Thread::create( |
624 | 6 | "Daemon", "je_reset_dirty_decay_thread", |
625 | 6 | [this]() { this->je_reset_dirty_decay_thread(); }, &_threads.emplace_back()); |
626 | 6 | CHECK(st.ok()) << st; |
627 | 6 | st = Thread::create( |
628 | 6 | "Daemon", "cache_adjust_capacity_thread", |
629 | 6 | [this]() { this->cache_adjust_capacity_thread(); }, &_threads.emplace_back()); |
630 | 6 | CHECK(st.ok()) << st; |
631 | 6 | st = Thread::create( |
632 | 6 | "Daemon", "cache_prune_stale_thread", [this]() { this->cache_prune_stale_thread(); }, |
633 | 6 | &_threads.emplace_back()); |
634 | 6 | CHECK(st.ok()) << st; |
635 | 6 | st = Thread::create( |
636 | 6 | "Daemon", "query_runtime_statistics_thread", |
637 | 6 | [this]() { this->report_runtime_query_statistics_thread(); }, &_threads.emplace_back()); |
638 | 6 | CHECK(st.ok()) << st; |
639 | | |
640 | 6 | if (config::enable_be_proc_monitor) { |
641 | 3 | st = Thread::create( |
642 | 3 | "Daemon", "be_proc_monitor_thread", [this]() { this->be_proc_monitor_thread(); }, |
643 | 3 | &_threads.emplace_back()); |
644 | 3 | } |
645 | 6 | CHECK(st.ok()) << st; |
646 | | |
647 | 6 | st = Thread::create( |
648 | 6 | "Daemon", "workload_group_metrics", |
649 | 6 | [this]() { this->calculate_workload_group_metrics_thread(); }, |
650 | 6 | &_threads.emplace_back()); |
651 | 6 | CHECK(st.ok()) << st; |
652 | 6 | } |
653 | | |
654 | 2 | void Daemon::stop() { |
655 | 2 | LOG(INFO) << "Doris daemon is stopping."; |
656 | 2 | if (_stop_background_threads_latch.count() == 0) { |
657 | 0 | LOG(INFO) << "Doris daemon stop returned since no bg threads latch."; |
658 | 0 | return; |
659 | 0 | } |
660 | 2 | _stop_background_threads_latch.count_down(); |
661 | 21 | for (auto&& t : _threads) { |
662 | 21 | if (t) { |
663 | 21 | t->join(); |
664 | 21 | } |
665 | 21 | } |
666 | | LOG(INFO) << "Doris daemon stopped after background threads are joined."; |
667 | 2 | } |
668 | | |
669 | | } // namespace doris |