Coverage Report

Created: 2026-03-13 18:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
8
    static CacheManager* create_global_instance() { return new CacheManager(); }
35
2.49k
    static CacheManager* instance() { return ExecEnv::GetInstance()->get_cache_manager(); }
36
37
126
    void register_cache(CachePolicy* cache) {
38
126
        std::lock_guard<std::mutex> l(_caches_lock);
39
126
        auto it = _caches.find(cache->type());
40
126
        if (it != _caches.end()) {
41
#ifdef BE_TEST
42
            _caches.erase(it);
43
#else
44
0
            throw Exception(Status::FatalError("Repeat register cache {}",
45
0
                                               CachePolicy::type_string(cache->type())));
46
0
#endif // BE_TEST
47
0
        }
48
126
        _caches.insert({cache->type(), cache});
49
126
        LOG(INFO) << "Register Cache " << CachePolicy::type_string(cache->type());
50
126
    }
51
52
54
    void unregister_cache(CachePolicy::CacheType type) {
53
#ifdef BE_TEST
54
        return;
55
#endif // BE_TEST
56
54
        std::lock_guard<std::mutex> l(_caches_lock);
57
54
        auto it = _caches.find(type);
58
54
        if (it != _caches.end()) {
59
54
            _caches.erase(it);
60
54
        }
61
54
        LOG(INFO) << "Unregister Cache " << CachePolicy::type_string(type);
62
54
    }
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
97
    bool need_prune(int64_t* last_timestamp, const std::string& type) {
74
97
        int64_t now = UnixSeconds();
75
97
        if (now - *last_timestamp > config::cache_prune_interval_sec) {
76
97
            *last_timestamp = now;
77
97
            return true;
78
97
        }
79
97
        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
97
    }
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