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