be/src/runtime/memory/lru_cache_policy.h
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 | | #pragma once |
19 | | |
20 | | #include <fmt/format.h> |
21 | | |
22 | | #include <memory> |
23 | | |
24 | | #include "common/be_mock_util.h" |
25 | | #include "runtime/memory/cache_policy.h" |
26 | | #include "runtime/memory/lru_cache_value_base.h" |
27 | | #include "runtime/memory/mem_tracker_limiter.h" |
28 | | #include "runtime/thread_context.h" |
29 | | #include "util/lru_cache.h" |
30 | | #include "util/time.h" |
31 | | |
32 | | namespace doris { |
33 | | |
34 | | // Base of lru cache, allow prune stale entry and prune all entry. |
35 | | class LRUCachePolicy : public CachePolicy { |
36 | | public: |
37 | | LRUCachePolicy(CacheType type, size_t capacity, LRUCacheType lru_cache_type, |
38 | | uint32_t stale_sweep_time_s, uint32_t num_shards, |
39 | | uint32_t element_count_capacity, bool enable_prune, bool is_lru_k) |
40 | 1.02k | : CachePolicy(type, capacity, stale_sweep_time_s, enable_prune), |
41 | 1.02k | _lru_cache_type(lru_cache_type) { |
42 | 1.02k | if (check_capacity(capacity, num_shards)) { |
43 | 1.00k | _cache = std::shared_ptr<ShardedLRUCache>( |
44 | 1.00k | new ShardedLRUCache(type_string(type), capacity, lru_cache_type, num_shards, |
45 | 1.00k | element_count_capacity, is_lru_k)); |
46 | 1.00k | } else { |
47 | 20 | _cache = std::make_shared<doris::DummyLRUCache>(); |
48 | 20 | } |
49 | 1.02k | _init_mem_tracker(lru_cache_type_string(lru_cache_type)); |
50 | 1.02k | CacheManager::instance()->register_cache(this); |
51 | 1.02k | } |
52 | | |
53 | | LRUCachePolicy(CacheType type, size_t capacity, LRUCacheType lru_cache_type, |
54 | | uint32_t stale_sweep_time_s, uint32_t num_shards, |
55 | | uint32_t element_count_capacity, |
56 | | CacheValueTimeExtractor cache_value_time_extractor, |
57 | | bool cache_value_check_timestamp, bool enable_prune, bool is_lru_k) |
58 | 144 | : CachePolicy(type, capacity, stale_sweep_time_s, enable_prune), |
59 | 144 | _lru_cache_type(lru_cache_type) { |
60 | 144 | if (check_capacity(capacity, num_shards)) { |
61 | 117 | _cache = std::shared_ptr<ShardedLRUCache>( |
62 | 117 | new ShardedLRUCache(type_string(type), capacity, lru_cache_type, num_shards, |
63 | 117 | cache_value_time_extractor, cache_value_check_timestamp, |
64 | 117 | element_count_capacity, is_lru_k)); |
65 | 117 | } else { |
66 | 27 | _cache = std::make_shared<doris::DummyLRUCache>(); |
67 | 27 | } |
68 | 144 | _init_mem_tracker(lru_cache_type_string(lru_cache_type)); |
69 | 144 | CacheManager::instance()->register_cache(this); |
70 | 144 | } |
71 | | |
72 | 0 | void reset_cache() { _cache.reset(); } |
73 | | |
74 | 1.16k | bool check_capacity(size_t capacity, uint32_t num_shards) { |
75 | 1.16k | if (capacity == 0 || capacity < num_shards) { |
76 | 47 | LOG(INFO) << fmt::format( |
77 | 47 | "{} lru cache capacity({} B) {} num_shards({}), will be disabled.", |
78 | 47 | type_string(type()), capacity, capacity == 0 ? "is 0, ignore" : "less than", |
79 | 47 | num_shards); |
80 | 47 | _enable_prune = false; |
81 | 47 | return false; |
82 | 47 | } |
83 | 1.12k | return true; |
84 | 1.16k | } |
85 | | |
86 | 1.16k | static std::string lru_cache_type_string(LRUCacheType type) { |
87 | 1.16k | switch (type) { |
88 | 265 | case LRUCacheType::SIZE: |
89 | 265 | return "size"; |
90 | 902 | case LRUCacheType::NUMBER: |
91 | 902 | return "number"; |
92 | 0 | default: |
93 | 0 | throw Exception( |
94 | 0 | Status::FatalError("not match type of lru cache:{}", static_cast<int>(type))); |
95 | 1.16k | } |
96 | 1.16k | } |
97 | | |
98 | 11.0k | std::shared_ptr<MemTrackerLimiter> mem_tracker() const { |
99 | 11.0k | DCHECK(_mem_tracker != nullptr); |
100 | 11.0k | return _mem_tracker; |
101 | 11.0k | } |
102 | | |
103 | 28 | int64_t mem_consumption() { |
104 | 28 | DCHECK(_mem_tracker != nullptr); |
105 | 28 | return _mem_tracker->consumption(); |
106 | 28 | } |
107 | | |
108 | 0 | int64_t value_mem_consumption() { |
109 | 0 | DCHECK(_value_mem_tracker != nullptr); |
110 | 0 | return _value_mem_tracker->consumption(); |
111 | 0 | } |
112 | | |
113 | | // Insert will consume tracking_bytes to _mem_tracker and cache value destroy will release tracking_bytes. |
114 | | // If LRUCacheType::SIZE, value_tracking_bytes usually equal to charge. |
115 | | // If LRUCacheType::NUMBER, value_tracking_bytes usually not equal to charge, at this time charge is an weight. |
116 | | // If LRUCacheType::SIZE and value_tracking_bytes equals 0, memory must be tracked in Doris Allocator, |
117 | | // cache value is allocated using Alloctor. |
118 | | // If LRUCacheType::NUMBER and value_tracking_bytes equals 0, usually currently cannot accurately tracking memory size, |
119 | | // only tracking handle_size(106). |
120 | | Cache::Handle* insert(const CacheKey& key, void* value, size_t charge, |
121 | | size_t value_tracking_bytes, |
122 | 328k | CachePriority priority = CachePriority::NORMAL) { |
123 | 328k | size_t tracking_bytes = sizeof(LRUHandle) - 1 + key.size() + value_tracking_bytes; |
124 | 328k | if (value != nullptr) { |
125 | 328k | ((LRUCacheValueBase*)value) |
126 | 328k | ->set_tracking_bytes(tracking_bytes, _mem_tracker, value_tracking_bytes, |
127 | 328k | _value_mem_tracker); |
128 | 328k | } |
129 | 328k | return _cache->insert(key, value, charge, priority); |
130 | 328k | } |
131 | | |
132 | 0 | void for_each_entry(const std::function<void(const LRUHandle*)>& visitor) { |
133 | 0 | _cache->for_each_entry(visitor); |
134 | 0 | } |
135 | | |
136 | 354k | Cache::Handle* lookup(const CacheKey& key) { return _cache->lookup(key); } |
137 | | |
138 | 634k | MOCK_FUNCTION void release(Cache::Handle* handle) { _cache->release(handle); } |
139 | | |
140 | 315k | MOCK_FUNCTION void* value(Cache::Handle* handle) { return _cache->value(handle); } |
141 | | |
142 | 204 | void erase(const CacheKey& key) { _cache->erase(key); } |
143 | | |
144 | 16.0k | int64_t get_usage() { return _cache->get_usage(); } |
145 | | |
146 | 10 | size_t get_element_count() { return _cache->get_element_count(); } |
147 | | |
148 | 16.0k | size_t get_capacity() override { return _cache->get_capacity(); } |
149 | | |
150 | 2 | uint64_t new_id() { return _cache->new_id(); }; |
151 | | |
152 | | // Subclass can override this method to determine whether to do the minor or full gc |
153 | 0 | virtual bool exceed_prune_limit() { |
154 | 0 | return _lru_cache_type == LRUCacheType::SIZE ? mem_consumption() > CACHE_MIN_PRUNE_SIZE |
155 | 0 | : get_usage() > CACHE_MIN_PRUNE_NUMBER; |
156 | 0 | } |
157 | | |
158 | | // Try to prune the cache if expired. |
159 | 0 | void prune_stale() override { |
160 | 0 | std::lock_guard<std::mutex> l(_lock); |
161 | 0 | COUNTER_SET(_freed_entrys_counter, (int64_t)0); |
162 | 0 | COUNTER_SET(_freed_memory_counter, (int64_t)0); |
163 | 0 | if (_stale_sweep_time_s <= 0 || std::dynamic_pointer_cast<doris::DummyLRUCache>(_cache)) { |
164 | 0 | return; |
165 | 0 | } |
166 | 0 | if (exceed_prune_limit()) { |
167 | 0 | COUNTER_SET(_cost_timer, (int64_t)0); |
168 | 0 | const int64_t curtime = UnixMillis(); |
169 | 0 | auto pred = [this, curtime](const LRUHandle* handle) -> bool { |
170 | 0 | return static_cast<bool>((handle->last_visit_time + _stale_sweep_time_s * 1000) < |
171 | 0 | curtime); |
172 | 0 | }; |
173 | |
|
174 | 0 | LOG(INFO) << fmt::format("[MemoryGC] {} prune stale start, consumption {}, usage {}", |
175 | 0 | type_string(_type), mem_consumption(), get_usage()); |
176 | 0 | { |
177 | 0 | SCOPED_TIMER(_cost_timer); |
178 | | // Prune cache in lazy mode to save cpu and minimize the time holding write lock |
179 | 0 | PrunedInfo pruned_info = _cache->prune_if(pred, true); |
180 | 0 | COUNTER_SET(_freed_entrys_counter, pruned_info.pruned_count); |
181 | 0 | COUNTER_SET(_freed_memory_counter, pruned_info.pruned_size); |
182 | 0 | } |
183 | 0 | COUNTER_UPDATE(_prune_stale_number_counter, 1); |
184 | 0 | LOG(INFO) << fmt::format( |
185 | 0 | "[MemoryGC] {} prune stale {} entries, {} bytes, cost {}, {} times prune", |
186 | 0 | type_string(_type), _freed_entrys_counter->value(), |
187 | 0 | _freed_memory_counter->value(), _cost_timer->value(), |
188 | 0 | _prune_stale_number_counter->value()); |
189 | 0 | } else { |
190 | 0 | if (_lru_cache_type == LRUCacheType::SIZE) { |
191 | 0 | LOG(INFO) << fmt::format( |
192 | 0 | "[MemoryGC] {} not need prune stale, LRUCacheType::SIZE consumption {} " |
193 | 0 | "less " |
194 | 0 | "than CACHE_MIN_PRUNE_SIZE {}", |
195 | 0 | type_string(_type), mem_consumption(), CACHE_MIN_PRUNE_SIZE); |
196 | 0 | } else if (_lru_cache_type == LRUCacheType::NUMBER) { |
197 | 0 | LOG(INFO) << fmt::format( |
198 | 0 | "[MemoryGC] {} not need prune stale, LRUCacheType::NUMBER usage {} less " |
199 | 0 | "than " |
200 | 0 | "CACHE_MIN_PRUNE_NUMBER {}", |
201 | 0 | type_string(_type), get_usage(), CACHE_MIN_PRUNE_NUMBER); |
202 | 0 | } |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | 0 | void prune_all(bool force) override { |
207 | 0 | std::lock_guard<std::mutex> l(_lock); |
208 | 0 | COUNTER_SET(_freed_entrys_counter, (int64_t)0); |
209 | 0 | COUNTER_SET(_freed_memory_counter, (int64_t)0); |
210 | 0 | if (std::dynamic_pointer_cast<doris::DummyLRUCache>(_cache)) { |
211 | 0 | return; |
212 | 0 | } |
213 | 0 | if ((force && mem_consumption() != 0) || exceed_prune_limit()) { |
214 | 0 | COUNTER_SET(_cost_timer, (int64_t)0); |
215 | 0 | LOG(INFO) << fmt::format("[MemoryGC] {} prune all start, consumption {}, usage {}", |
216 | 0 | type_string(_type), mem_consumption(), get_usage()); |
217 | 0 | { |
218 | 0 | SCOPED_TIMER(_cost_timer); |
219 | 0 | PrunedInfo pruned_info = _cache->prune(); |
220 | 0 | COUNTER_SET(_freed_entrys_counter, pruned_info.pruned_count); |
221 | 0 | COUNTER_SET(_freed_memory_counter, pruned_info.pruned_size); |
222 | 0 | } |
223 | 0 | COUNTER_UPDATE(_prune_all_number_counter, 1); |
224 | 0 | LOG(INFO) << fmt::format( |
225 | 0 | "[MemoryGC] {} prune all {} entries, {} bytes, cost {}, {} times prune, is " |
226 | 0 | "force: {}", |
227 | 0 | type_string(_type), _freed_entrys_counter->value(), |
228 | 0 | _freed_memory_counter->value(), _cost_timer->value(), |
229 | 0 | _prune_all_number_counter->value(), force); |
230 | 0 | } else { |
231 | 0 | if (_lru_cache_type == LRUCacheType::SIZE) { |
232 | 0 | LOG(INFO) << fmt::format( |
233 | 0 | "[MemoryGC] {} not need prune all, force is {}, LRUCacheType::SIZE " |
234 | 0 | "consumption {}, " |
235 | 0 | "CACHE_MIN_PRUNE_SIZE {}", |
236 | 0 | type_string(_type), force, mem_consumption(), CACHE_MIN_PRUNE_SIZE); |
237 | 0 | } else if (_lru_cache_type == LRUCacheType::NUMBER) { |
238 | 0 | LOG(INFO) << fmt::format( |
239 | 0 | "[MemoryGC] {} not need prune all, force is {}, LRUCacheType::NUMBER " |
240 | 0 | "usage {}, CACHE_MIN_PRUNE_NUMBER {}", |
241 | 0 | type_string(_type), force, get_usage(), CACHE_MIN_PRUNE_NUMBER); |
242 | 0 | } |
243 | 0 | } |
244 | 0 | } |
245 | | |
246 | 14 | int64_t adjust_capacity_weighted_unlocked(double adjust_weighted) { |
247 | 14 | auto capacity = |
248 | 14 | static_cast<size_t>(static_cast<double>(_initial_capacity) * adjust_weighted); |
249 | 14 | COUNTER_SET(_freed_entrys_counter, (int64_t)0); |
250 | 14 | COUNTER_SET(_freed_memory_counter, (int64_t)0); |
251 | 14 | COUNTER_SET(_cost_timer, (int64_t)0); |
252 | 14 | if (std::dynamic_pointer_cast<doris::DummyLRUCache>(_cache)) { |
253 | 0 | return 0; |
254 | 0 | } |
255 | 14 | if (!_enable_prune) { |
256 | 0 | LOG(INFO) << "[MemoryGC] " << type_string(_type) |
257 | 0 | << " cache prune disabled, so could not adjust capacity to free memory"; |
258 | 0 | return 0; |
259 | 0 | } |
260 | 14 | size_t old_capacity = get_capacity(); |
261 | 14 | int64_t old_mem_consumption = mem_consumption(); |
262 | 14 | int64_t old_usage = get_usage(); |
263 | 14 | { |
264 | 14 | SCOPED_TIMER(_cost_timer); |
265 | 14 | PrunedInfo pruned_info = _cache->set_capacity(capacity); |
266 | 14 | COUNTER_SET(_freed_entrys_counter, pruned_info.pruned_count); |
267 | 14 | COUNTER_SET(_freed_memory_counter, pruned_info.pruned_size); |
268 | 14 | } |
269 | 14 | COUNTER_UPDATE(_adjust_capacity_weighted_number_counter, 1); |
270 | 14 | LOG(INFO) << fmt::format( |
271 | 14 | "[MemoryGC] {} update capacity, old <capacity {}, consumption {}, usage {}>, " |
272 | 14 | "adjust_weighted {}, new <capacity {}, consumption {}, usage {}>, prune {} " |
273 | 14 | "entries, {} bytes, cost {}, {} times prune", |
274 | 14 | type_string(_type), old_capacity, old_mem_consumption, old_usage, adjust_weighted, |
275 | 14 | get_capacity(), mem_consumption(), get_usage(), _freed_entrys_counter->value(), |
276 | 14 | _freed_memory_counter->value(), _cost_timer->value(), |
277 | 14 | _adjust_capacity_weighted_number_counter->value()); |
278 | 14 | return _freed_entrys_counter->value(); |
279 | 14 | } |
280 | | |
281 | 11 | int64_t adjust_capacity_weighted(double adjust_weighted) override { |
282 | 11 | std::lock_guard<std::mutex> l(_lock); |
283 | 11 | return adjust_capacity_weighted_unlocked(adjust_weighted); |
284 | 11 | } |
285 | | |
286 | 3 | int64_t reset_initial_capacity(double adjust_weighted) override { |
287 | 3 | DCHECK(adjust_weighted != 0.0); // otherwise initial_capacity will always to be 0. |
288 | 3 | std::lock_guard<std::mutex> l(_lock); |
289 | 3 | int64_t prune_num = adjust_capacity_weighted_unlocked(adjust_weighted); |
290 | 3 | size_t old_capacity = _initial_capacity; |
291 | 3 | _initial_capacity = |
292 | 3 | static_cast<size_t>(static_cast<double>(_initial_capacity) * adjust_weighted); |
293 | 3 | LOG(INFO) << fmt::format( |
294 | 3 | "[MemoryGC] {} reset initial capacity, new capacity {}, old capacity {}, prune num " |
295 | 3 | "{}", |
296 | 3 | type_string(_type), _initial_capacity, old_capacity, prune_num); |
297 | 3 | return prune_num; |
298 | 3 | }; |
299 | | |
300 | | protected: |
301 | 1.16k | void _init_mem_tracker(const std::string& type_name) { |
302 | 1.16k | if (std::find(CachePolicy::MetadataCache.begin(), CachePolicy::MetadataCache.end(), |
303 | 1.16k | _type) == CachePolicy::MetadataCache.end()) { |
304 | 1.16k | _mem_tracker = MemTrackerLimiter::create_shared( |
305 | 1.16k | MemTrackerLimiter::Type::CACHE, |
306 | 1.16k | fmt::format("{}[{}]", type_string(_type), type_name)); |
307 | 1.16k | } else { |
308 | 2 | _mem_tracker = MemTrackerLimiter::create_shared( |
309 | 2 | MemTrackerLimiter::Type::METADATA, |
310 | 2 | fmt::format("{}[{}]", type_string(_type), type_name)); |
311 | 2 | } |
312 | 1.16k | _value_mem_tracker = std::make_shared<MemTracker>( |
313 | 1.16k | fmt::format("{}::Value[{}]", type_string(_type), type_name)); |
314 | 1.16k | } |
315 | | |
316 | | // if check_capacity failed, will return dummy lru cache, |
317 | | // compatible with ShardedLRUCache usage, but will not actually cache. |
318 | | std::shared_ptr<Cache> _cache; |
319 | | std::mutex _lock; |
320 | | LRUCacheType _lru_cache_type; |
321 | | |
322 | | std::shared_ptr<MemTrackerLimiter> _mem_tracker; |
323 | | std::shared_ptr<MemTracker> _value_mem_tracker; |
324 | | }; |
325 | | |
326 | | } // namespace doris |