be/src/runtime/memory/jemalloc_control.cpp
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 "runtime/memory/jemalloc_control.h" |
19 | | |
20 | | #include <atomic> |
21 | | #include <condition_variable> |
22 | | |
23 | | namespace doris { |
24 | | #include "common/compile_check_begin.h" |
25 | | |
26 | | std::mutex JemallocControl::je_purge_dirty_pages_lock; |
27 | | std::atomic<bool> JemallocControl::je_purge_dirty_pages_notify {false}; |
28 | | std::mutex JemallocControl::je_reset_dirty_decay_lock; |
29 | | std::atomic<bool> JemallocControl::je_enable_dirty_page {true}; |
30 | | std::condition_variable JemallocControl::je_reset_dirty_decay_cv; |
31 | | std::atomic<bool> JemallocControl::je_reset_dirty_decay_notify {false}; |
32 | | |
33 | | std::atomic<int64_t> JemallocControl::je_cache_bytes_ = 0; |
34 | | std::atomic<int64_t> JemallocControl::je_tcache_mem_ = 0; |
35 | | std::atomic<int64_t> JemallocControl::je_metadata_mem_ = 0; |
36 | | std::atomic<int64_t> JemallocControl::je_dirty_pages_mem_ = std::numeric_limits<int64_t>::min(); |
37 | | std::atomic<int64_t> JemallocControl::je_virtual_memory_used_ = 0; |
38 | | |
39 | 0 | void JemallocControl::refresh_allocator_mem() { |
40 | 0 | #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || defined(THREAD_SANITIZER) |
41 | | #elif defined(USE_JEMALLOC) |
42 | | // jemalloc mallctl refer to : https://jemalloc.net/jemalloc.3.html |
43 | | // https://www.bookstack.cn/read/aliyun-rds-core/4a0cdf677f62feb3.md |
44 | | // Check the Doris BE web page `http://ip:webserver_port/memory` to get the Jemalloc Profile. |
45 | | |
46 | | // 'epoch' is a special mallctl -- it updates the statistics. Without it, all |
47 | | // the following calls will return stale values. It increments and returns |
48 | | // the current epoch number, which might be useful to log as a sanity check. |
49 | | uint64_t epoch = 0; |
50 | | size_t sz = sizeof(epoch); |
51 | | jemallctl("epoch", &epoch, &sz, &epoch, sz); |
52 | | |
53 | | // Number of extents of the given type in this arena in the bucket corresponding to page size index. |
54 | | // Large size class starts at 16384, the extents have three sizes before 16384: 4096, 8192, and 12288, so + 3 |
55 | | int64_t dirty_pages_bytes = 0; |
56 | | for (unsigned i = 0; i < get_jemallctl_value<unsigned>("arenas.nlextents") + 3; i++) { |
57 | | dirty_pages_bytes += get_je_all_arena_extents_metrics(i, "dirty_bytes"); |
58 | | } |
59 | | je_dirty_pages_mem_.store(dirty_pages_bytes, std::memory_order_relaxed); |
60 | | je_tcache_mem_.store(get_je_all_arena_metrics("tcache_bytes")); |
61 | | |
62 | | // Doris uses Jemalloc as default Allocator, Jemalloc Cache consists of two parts: |
63 | | // - Thread Cache, cache a specified number of Pages in Thread Cache. |
64 | | // - Dirty Page, memory Page that can be reused in all Arenas. |
65 | | je_cache_bytes_.store(je_tcache_mem_ + dirty_pages_bytes, std::memory_order_relaxed); |
66 | | // Total number of bytes dedicated to metadata, which comprise base allocations used |
67 | | // for bootstrap-sensitive allocator metadata structures. |
68 | | je_metadata_mem_.store(get_jemallctl_value<int64_t>("stats.metadata"), |
69 | | std::memory_order_relaxed); |
70 | | je_virtual_memory_used_.store(get_jemallctl_value<int64_t>("stats.mapped"), |
71 | | std::memory_order_relaxed); |
72 | | #else |
73 | | je_cache_bytes_.store(get_tc_metrics("tcmalloc.pageheap_free_bytes") + |
74 | | get_tc_metrics("tcmalloc.central_cache_free_bytes") + |
75 | | get_tc_metrics("tcmalloc.transfer_cache_free_bytes") + |
76 | | get_tc_metrics("tcmalloc.thread_cache_free_bytes"), |
77 | | std::memory_order_relaxed); |
78 | | je_virtual_memory_used_.store(get_tc_metrics("generic.total_physical_bytes") + |
79 | | get_tc_metrics("tcmalloc.pageheap_unmapped_bytes"), |
80 | | std::memory_order_relaxed); |
81 | | #endif |
82 | 0 | } |
83 | | |
84 | | #ifdef USE_JEMALLOC |
85 | | void JemallocControl::action_jemallctl(const std::string& name) { |
86 | | try { |
87 | | int err = jemallctl(name.c_str(), nullptr, nullptr, nullptr, 0); |
88 | | if (err) { |
89 | | LOG(WARNING) << fmt::format("Failed, jemallctl action {}", name); |
90 | | } |
91 | | } catch (...) { |
92 | | LOG(WARNING) << fmt::format("Exception, jemallctl action {}", name); |
93 | | } |
94 | | } |
95 | | |
96 | | int64_t JemallocControl::get_je_all_arena_metrics(const std::string& name) { |
97 | | return get_jemallctl_value<int64_t>( |
98 | | fmt::format("stats.arenas.{}.{}", MALLCTL_ARENAS_ALL, name)); |
99 | | } |
100 | | |
101 | | int64_t JemallocControl::get_je_all_arena_extents_metrics(int64_t page_size_index, |
102 | | const std::string& extent_type) { |
103 | | return get_jemallctl_value<int64_t>(fmt::format( |
104 | | "stats.arenas.{}.extents.{}.{}", MALLCTL_ARENAS_ALL, page_size_index, extent_type)); |
105 | | } |
106 | | |
107 | | void JemallocControl::je_purge_all_arena_dirty_pages() { |
108 | | // https://github.com/jemalloc/jemalloc/issues/2470 |
109 | | // If there is a core dump here, it may cover up the real stack, if stack trace indicates heap corruption |
110 | | // (which led to invalid jemalloc metadata), like double free or use-after-free in the application. |
111 | | // Try sanitizers such as ASAN, or build jemalloc with --enable-debug to investigate further. |
112 | | action_jemallctl(fmt::format("arena.{}.purge", MALLCTL_ARENAS_ALL)); |
113 | | } |
114 | | |
115 | | void JemallocControl::je_reset_all_arena_dirty_decay_ms(ssize_t dirty_decay_ms) { |
116 | | // Each time this interface is set, all currently unused dirty pages are considered |
117 | | // to have fully decayed, which causes immediate purging of all unused dirty pages unless |
118 | | // the decay time is set to -1 |
119 | | // |
120 | | // NOTE: Using "arena.MALLCTL_ARENAS_ALL.dirty_decay_ms" to modify all arenas will fail or even crash, |
121 | | // which may be a bug. |
122 | | for (unsigned i = 0; i < get_jemallctl_value<unsigned>("arenas.narenas"); i++) { |
123 | | set_jemallctl_value<ssize_t>(fmt::format("arena.{}.dirty_decay_ms", i), dirty_decay_ms); |
124 | | } |
125 | | } |
126 | | |
127 | | void JemallocControl::je_decay_all_arena_dirty_pages() { |
128 | | // Trigger decay-based purging of unused dirty/muzzy pages for arena <i>, or for all arenas if <i> equals |
129 | | // MALLCTL_ARENAS_ALL. The proportion of unused dirty/muzzy pages to be purged depends on the |
130 | | // current time; see opt.dirty_decay_ms and opt.muzy_decay_ms for details. |
131 | | action_jemallctl(fmt::format("arena.{}.decay", MALLCTL_ARENAS_ALL)); |
132 | | } |
133 | | |
134 | | void JemallocControl::je_thread_tcache_flush() { |
135 | | constexpr size_t TCACHE_LIMIT = (1ULL << 30); // 1G |
136 | | if (je_tcache_mem() > TCACHE_LIMIT) { |
137 | | action_jemallctl("thread.tcache.flush"); |
138 | | } |
139 | | } |
140 | | |
141 | | #else |
142 | 0 | void JemallocControl::action_jemallctl(const std::string& name) {} |
143 | 0 | int64_t JemallocControl::get_je_all_arena_metrics(const std::string& name) { |
144 | 0 | return 0; |
145 | 0 | } |
146 | | int64_t JemallocControl::get_je_all_arena_extents_metrics(int64_t page_size_index, |
147 | 0 | const std::string& extent_type) { |
148 | 0 | return 0; |
149 | 0 | } |
150 | 0 | void JemallocControl::je_purge_all_arena_dirty_pages() {} |
151 | 0 | void JemallocControl::je_reset_all_arena_dirty_decay_ms(ssize_t dirty_decay_ms) {} |
152 | 0 | void JemallocControl::je_decay_all_arena_dirty_pages() {} |
153 | 0 | void JemallocControl::je_thread_tcache_flush() {} |
154 | | #endif |
155 | | |
156 | | #include "common/compile_check_end.h" |
157 | | } // namespace doris |