be/src/runtime/memory/cache_manager.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 <string> |
21 | | #include <unordered_map> |
22 | | |
23 | | #include "runtime/exec_env.h" |
24 | | #include "runtime/memory/cache_policy.h" |
25 | | #include "runtime/runtime_profile.h" |
26 | | #include "util/time.h" |
27 | | |
28 | | namespace doris { |
29 | | #include "common/compile_check_begin.h" |
30 | | |
31 | | // Hold the list of all caches, for prune when memory not enough or timing. |
32 | | class CacheManager { |
33 | | public: |
34 | 1 | static CacheManager* create_global_instance() { return new CacheManager(); } |
35 | 2.22k | static CacheManager* instance() { return ExecEnv::GetInstance()->get_cache_manager(); } |
36 | | |
37 | 1.11k | void register_cache(CachePolicy* cache) { |
38 | 1.11k | std::lock_guard<std::mutex> l(_caches_lock); |
39 | 1.11k | auto it = _caches.find(cache->type()); |
40 | 1.11k | if (it != _caches.end()) { |
41 | 1.09k | #ifdef BE_TEST |
42 | 1.09k | _caches.erase(it); |
43 | | #else |
44 | | throw Exception(Status::FatalError("Repeat register cache {}", |
45 | | CachePolicy::type_string(cache->type()))); |
46 | | #endif // BE_TEST |
47 | 1.09k | } |
48 | 1.11k | _caches.insert({cache->type(), cache}); |
49 | 1.11k | LOG(INFO) << "Register Cache " << CachePolicy::type_string(cache->type()); |
50 | 1.11k | } |
51 | | |
52 | 1.10k | void unregister_cache(CachePolicy::CacheType type) { |
53 | 1.10k | #ifdef BE_TEST |
54 | 1.10k | return; |
55 | 0 | #endif // BE_TEST |
56 | 0 | std::lock_guard<std::mutex> l(_caches_lock); |
57 | 0 | auto it = _caches.find(type); |
58 | 0 | if (it != _caches.end()) { |
59 | 0 | _caches.erase(it); |
60 | 0 | } |
61 | 0 | LOG(INFO) << "Unregister Cache " << CachePolicy::type_string(type); |
62 | 0 | } |
63 | | |
64 | | int64_t for_each_cache_prune_stale_wrap(std::function<void(CachePolicy* cache_policy)> func, |
65 | | RuntimeProfile* profile = nullptr); |
66 | | |
67 | | int64_t for_each_cache_prune_stale(RuntimeProfile* profile = nullptr); |
68 | | |
69 | | // if force is true, regardless of the two prune interval and cache size, cache will be pruned this time. |
70 | | int64_t for_each_cache_prune_all(RuntimeProfile* profile = nullptr, bool force = false); |
71 | | int64_t cache_prune_all(CachePolicy::CacheType type, bool force = false); |
72 | | |
73 | 0 | bool need_prune(int64_t* last_timestamp, const std::string& type) { |
74 | 0 | int64_t now = UnixSeconds(); |
75 | 0 | if (now - *last_timestamp > config::cache_prune_interval_sec) { |
76 | 0 | *last_timestamp = now; |
77 | 0 | return true; |
78 | 0 | } |
79 | 0 | LOG(INFO) << fmt::format( |
80 | 0 | "[MemoryGC] cache no prune {}, last prune less than interval {}, now {}, last " |
81 | 0 | "timestamp {}", |
82 | 0 | type, config::cache_prune_interval_sec, now, *last_timestamp); |
83 | 0 | return false; |
84 | 0 | } |
85 | | |
86 | | int64_t for_each_cache_refresh_capacity(double adjust_weighted, |
87 | | RuntimeProfile* profile = nullptr); |
88 | | |
89 | | void for_each_cache_reset_initial_capacity(double adjust_weighted); |
90 | | |
91 | | private: |
92 | | std::mutex _caches_lock; |
93 | | std::unordered_map<CachePolicy::CacheType, CachePolicy*> _caches; |
94 | | int64_t _last_prune_stale_timestamp = 0; |
95 | | int64_t _last_prune_all_timestamp = 0; |
96 | | }; |
97 | | |
98 | | #include "common/compile_check_end.h" |
99 | | } // namespace doris |