Coverage Report

Created: 2025-04-21 12:08

/root/doris/be/src/olap/lru_cache.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5
#include "olap/lru_cache.h"
6
7
#include <stdlib.h>
8
9
#include <mutex>
10
#include <new>
11
#include <sstream>
12
#include <string>
13
14
#include "gutil/bits.h"
15
#include "util/doris_metrics.h"
16
#include "util/time.h"
17
18
using std::string;
19
using std::stringstream;
20
21
namespace doris {
22
23
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(cache_capacity, MetricUnit::BYTES);
24
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(cache_usage, MetricUnit::BYTES);
25
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(cache_element_count, MetricUnit::NOUNIT);
26
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(cache_usage_ratio, MetricUnit::NOUNIT);
27
DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(cache_lookup_count, MetricUnit::OPERATIONS);
28
DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(cache_hit_count, MetricUnit::OPERATIONS);
29
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(cache_hit_ratio, MetricUnit::NOUNIT);
30
31
451k
uint32_t CacheKey::hash(const char* data, size_t n, uint32_t seed) const {
32
    // Similar to murmur hash
33
451k
    const uint32_t m = 0xc6a4a793;
34
451k
    const uint32_t r = 24;
35
451k
    const char* limit = data + n;
36
451k
    uint32_t h = seed ^ (n * m);
37
38
    // Pick up four bytes at a time
39
1.14M
    while (data + 4 <= limit) {
40
692k
        uint32_t w = _decode_fixed32(data);
41
692k
        data += 4;
42
692k
        h += w;
43
692k
        h *= m;
44
692k
        h ^= (h >> 16);
45
692k
    }
46
47
    // Pick up remaining bytes
48
451k
    switch (limit - data) {
49
8.95k
    case 3:
50
8.95k
        h += static_cast<unsigned char>(data[2]) << 16;
51
52
        // fall through
53
11.5k
    case 2:
54
11.5k
        h += static_cast<unsigned char>(data[1]) << 8;
55
56
        // fall through
57
12.3k
    case 1:
58
12.3k
        h += static_cast<unsigned char>(data[0]);
59
12.3k
        h *= m;
60
12.3k
        h ^= (h >> r);
61
12.3k
        break;
62
63
439k
    default:
64
439k
        break;
65
451k
    }
66
67
451k
    return h;
68
451k
}
69
70
316
Cache::~Cache() {}
71
72
9.55k
HandleTable::~HandleTable() {
73
9.55k
    delete[] _list;
74
9.55k
}
75
76
// LRU cache implementation
77
243k
LRUHandle* HandleTable::lookup(const CacheKey& key, uint32_t hash) {
78
243k
    return *_find_pointer(key, hash);
79
243k
}
80
81
207k
LRUHandle* HandleTable::insert(LRUHandle* h) {
82
207k
    LRUHandle** ptr = _find_pointer(h->key(), h->hash);
83
207k
    LRUHandle* old = *ptr;
84
207k
    h->next_hash = old ? old->next_hash : nullptr;
85
207k
    *ptr = h;
86
87
207k
    if (old == nullptr) {
88
207k
        ++_elems;
89
207k
        if (_elems > _length) {
90
            // Since each cache entry is fairly large, we aim for a small
91
            // average linked list length (<= 1).
92
80
            _resize();
93
80
        }
94
207k
    }
95
96
207k
    return old;
97
207k
}
98
99
12
LRUHandle* HandleTable::remove(const CacheKey& key, uint32_t hash) {
100
12
    LRUHandle** ptr = _find_pointer(key, hash);
101
12
    LRUHandle* result = *ptr;
102
103
12
    if (result != nullptr) {
104
5
        *ptr = result->next_hash;
105
5
        _elems--;
106
5
    }
107
108
12
    return result;
109
12
}
110
111
202k
bool HandleTable::remove(const LRUHandle* h) {
112
202k
    LRUHandle** ptr = &(_list[h->hash & (_length - 1)]);
113
203k
    while (*ptr != nullptr && *ptr != h) {
114
91
        ptr = &(*ptr)->next_hash;
115
91
    }
116
117
202k
    LRUHandle* result = *ptr;
118
202k
    if (result != nullptr) {
119
202k
        *ptr = result->next_hash;
120
202k
        _elems--;
121
202k
        return true;
122
202k
    }
123
0
    return false;
124
202k
}
125
126
451k
LRUHandle** HandleTable::_find_pointer(const CacheKey& key, uint32_t hash) {
127
451k
    LRUHandle** ptr = &(_list[hash & (_length - 1)]);
128
732k
    while (*ptr != nullptr && ((*ptr)->hash != hash || key != (*ptr)->key())) {
129
281k
        ptr = &(*ptr)->next_hash;
130
281k
    }
131
132
451k
    return ptr;
133
451k
}
134
135
10.1k
void HandleTable::_resize() {
136
10.1k
    uint32_t new_length = 16;
137
10.4k
    while (new_length < _elems * 1.5) {
138
240
        new_length *= 2;
139
240
    }
140
141
10.1k
    LRUHandle** new_list = new (std::nothrow) LRUHandle*[new_length];
142
10.1k
    memset(new_list, 0, sizeof(new_list[0]) * new_length);
143
144
10.1k
    uint32_t count = 0;
145
18.1k
    for (uint32_t i = 0; i < _length; i++) {
146
7.93k
        LRUHandle* h = _list[i];
147
15.9k
        while (h != nullptr) {
148
8.01k
            LRUHandle* next = h->next_hash;
149
8.01k
            uint32_t hash = h->hash;
150
8.01k
            LRUHandle** ptr = &new_list[hash & (new_length - 1)];
151
8.01k
            h->next_hash = *ptr;
152
8.01k
            *ptr = h;
153
8.01k
            h = next;
154
8.01k
            count++;
155
8.01k
        }
156
7.93k
    }
157
158
10.1k
    DCHECK_EQ(_elems, count);
159
10.1k
    delete[] _list;
160
10.1k
    _list = new_list;
161
10.1k
    _length = new_length;
162
10.1k
}
163
164
36
uint32_t HandleTable::element_count() const {
165
36
    return _elems;
166
36
}
167
168
10.1k
LRUCache::LRUCache(LRUCacheType type) : _type(type) {
169
    // Make empty circular linked list
170
10.1k
    _lru_normal.next = &_lru_normal;
171
10.1k
    _lru_normal.prev = &_lru_normal;
172
10.1k
    _lru_durable.next = &_lru_durable;
173
10.1k
    _lru_durable.prev = &_lru_durable;
174
10.1k
}
175
176
9.55k
LRUCache::~LRUCache() {
177
9.55k
    prune();
178
9.55k
}
179
180
638k
bool LRUCache::_unref(LRUHandle* e) {
181
638k
    DCHECK(e->refs > 0);
182
638k
    e->refs--;
183
638k
    return e->refs == 0;
184
638k
}
185
186
423k
void LRUCache::_lru_remove(LRUHandle* e) {
187
423k
    e->next->prev = e->prev;
188
423k
    e->prev->next = e->next;
189
423k
    e->prev = e->next = nullptr;
190
191
423k
    if (_cache_value_check_timestamp) {
192
48
        if (e->priority == CachePriority::NORMAL) {
193
48
            auto pair = std::make_pair(_cache_value_time_extractor(e->value), e);
194
48
            auto found_it = _sorted_normal_entries_with_timestamp.find(pair);
195
48
            if (found_it != _sorted_normal_entries_with_timestamp.end()) {
196
48
                _sorted_normal_entries_with_timestamp.erase(found_it);
197
48
            }
198
48
        } else if (e->priority == CachePriority::DURABLE) {
199
0
            auto pair = std::make_pair(_cache_value_time_extractor(e->value), e);
200
0
            auto found_it = _sorted_durable_entries_with_timestamp.find(pair);
201
0
            if (found_it != _sorted_durable_entries_with_timestamp.end()) {
202
0
                _sorted_durable_entries_with_timestamp.erase(found_it);
203
0
            }
204
0
        }
205
48
    }
206
423k
}
207
208
428k
void LRUCache::_lru_append(LRUHandle* list, LRUHandle* e) {
209
    // Make "e" newest entry by inserting just before *list
210
428k
    e->next = list;
211
428k
    e->prev = list->prev;
212
428k
    e->prev->next = e;
213
428k
    e->next->prev = e;
214
215
    // _cache_value_check_timestamp is true,
216
    // means evict entry will depends on the timestamp asc set,
217
    // the timestamp is updated by higher level caller,
218
    // and the timestamp of hit entry is different with the insert entry,
219
    // that is why need check timestamp to evict entry,
220
    // in order to keep the survival time of hit entries
221
    // longer than the entries just inserted,
222
    // so use asc set to sorted these entries's timestamp and LRUHandle*
223
428k
    if (_cache_value_check_timestamp) {
224
48
        if (e->priority == CachePriority::NORMAL) {
225
48
            _sorted_normal_entries_with_timestamp.insert(
226
48
                    std::make_pair(_cache_value_time_extractor(e->value), e));
227
48
        } else if (e->priority == CachePriority::DURABLE) {
228
0
            _sorted_durable_entries_with_timestamp.insert(
229
0
                    std::make_pair(_cache_value_time_extractor(e->value), e));
230
0
        }
231
48
    }
232
428k
}
233
234
243k
Cache::Handle* LRUCache::lookup(const CacheKey& key, uint32_t hash) {
235
243k
    std::lock_guard l(_mutex);
236
243k
    ++_lookup_count;
237
243k
    LRUHandle* e = _table.lookup(key, hash);
238
243k
    if (e != nullptr) {
239
        // we get it from _table, so in_cache must be true
240
228k
        DCHECK(e->in_cache);
241
228k
        if (e->refs == 1) {
242
            // only in LRU free list, remove it from list
243
225k
            _lru_remove(e);
244
225k
        }
245
228k
        e->refs++;
246
228k
        ++_hit_count;
247
228k
        e->last_visit_time = UnixMillis();
248
228k
    }
249
243k
    return reinterpret_cast<Cache::Handle*>(e);
250
243k
}
251
252
435k
void LRUCache::release(Cache::Handle* handle) {
253
435k
    if (handle == nullptr) {
254
0
        return;
255
0
    }
256
435k
    LRUHandle* e = reinterpret_cast<LRUHandle*>(handle);
257
435k
    bool last_ref = false;
258
435k
    {
259
435k
        std::lock_guard l(_mutex);
260
435k
        last_ref = _unref(e);
261
435k
        if (last_ref) {
262
2
            _usage -= e->total_size;
263
435k
        } else if (e->in_cache && e->refs == 1) {
264
            // only exists in cache
265
433k
            if (_usage > _capacity) {
266
                // take this opportunity and remove the item
267
4.35k
                bool removed = _table.remove(e);
268
4.35k
                DCHECK(removed);
269
4.35k
                e->in_cache = false;
270
4.35k
                _unref(e);
271
4.35k
                _usage -= e->total_size;
272
4.35k
                last_ref = true;
273
428k
            } else {
274
                // put it to LRU free list
275
428k
                if (e->priority == CachePriority::NORMAL) {
276
428k
                    _lru_append(&_lru_normal, e);
277
428k
                } else if (e->priority == CachePriority::DURABLE) {
278
15
                    _lru_append(&_lru_durable, e);
279
15
                }
280
428k
            }
281
433k
        }
282
435k
    }
283
284
    // free handle out of mutex
285
435k
    if (last_ref) {
286
4.35k
        e->free();
287
4.35k
    }
288
435k
}
289
290
18
void LRUCache::_evict_from_lru_with_time(size_t total_size, LRUHandle** to_remove_head) {
291
    // 1. evict normal cache entries
292
22
    while ((_usage + total_size > _capacity || _check_element_count_limit()) &&
293
22
           !_sorted_normal_entries_with_timestamp.empty()) {
294
4
        auto entry_pair = _sorted_normal_entries_with_timestamp.begin();
295
4
        LRUHandle* remove_handle = entry_pair->second;
296
4
        DCHECK(remove_handle != nullptr);
297
4
        DCHECK(remove_handle->priority == CachePriority::NORMAL);
298
4
        _evict_one_entry(remove_handle);
299
4
        remove_handle->next = *to_remove_head;
300
4
        *to_remove_head = remove_handle;
301
4
    }
302
303
    // 2. evict durable cache entries if need
304
18
    while ((_usage + total_size > _capacity || _check_element_count_limit()) &&
305
18
           !_sorted_durable_entries_with_timestamp.empty()) {
306
0
        auto entry_pair = _sorted_durable_entries_with_timestamp.begin();
307
0
        LRUHandle* remove_handle = entry_pair->second;
308
0
        DCHECK(remove_handle != nullptr);
309
0
        DCHECK(remove_handle->priority == CachePriority::DURABLE);
310
0
        _evict_one_entry(remove_handle);
311
0
        remove_handle->next = *to_remove_head;
312
0
        *to_remove_head = remove_handle;
313
0
    }
314
18
}
315
316
207k
void LRUCache::_evict_from_lru(size_t total_size, LRUHandle** to_remove_head) {
317
    // 1. evict normal cache entries
318
405k
    while ((_usage + total_size > _capacity || _check_element_count_limit()) &&
319
405k
           _lru_normal.next != &_lru_normal) {
320
197k
        LRUHandle* old = _lru_normal.next;
321
197k
        DCHECK(old->priority == CachePriority::NORMAL);
322
197k
        _evict_one_entry(old);
323
197k
        old->next = *to_remove_head;
324
197k
        *to_remove_head = old;
325
197k
    }
326
    // 2. evict durable cache entries if need
327
207k
    while ((_usage + total_size > _capacity || _check_element_count_limit()) &&
328
207k
           _lru_durable.next != &_lru_durable) {
329
5
        LRUHandle* old = _lru_durable.next;
330
5
        DCHECK(old->priority == CachePriority::DURABLE);
331
5
        _evict_one_entry(old);
332
5
        old->next = *to_remove_head;
333
5
        *to_remove_head = old;
334
5
    }
335
207k
}
336
337
198k
void LRUCache::_evict_one_entry(LRUHandle* e) {
338
198k
    DCHECK(e->in_cache);
339
198k
    DCHECK(e->refs == 1); // LRU list contains elements which may be evicted
340
198k
    _lru_remove(e);
341
198k
    bool removed = _table.remove(e);
342
198k
    DCHECK(removed);
343
198k
    e->in_cache = false;
344
198k
    _unref(e);
345
198k
    _usage -= e->total_size;
346
198k
}
347
348
407k
bool LRUCache::_check_element_count_limit() {
349
407k
    return _element_count_capacity != 0 && _table.element_count() >= _element_count_capacity;
350
407k
}
351
352
Cache::Handle* LRUCache::insert(const CacheKey& key, uint32_t hash, void* value, size_t charge,
353
207k
                                CachePriority priority) {
354
207k
    size_t handle_size = sizeof(LRUHandle) - 1 + key.size();
355
207k
    auto* e = reinterpret_cast<LRUHandle*>(malloc(handle_size));
356
207k
    e->value = value;
357
207k
    e->charge = charge;
358
207k
    e->key_length = key.size();
359
    // if LRUCacheType::NUMBER, charge not add handle_size,
360
    // because charge at this time is no longer the memory size, but an weight.
361
207k
    e->total_size = (_type == LRUCacheType::SIZE ? handle_size + charge : charge);
362
207k
    e->hash = hash;
363
207k
    e->refs = 2; // one for the returned handle, one for LRUCache.
364
207k
    e->next = e->prev = nullptr;
365
207k
    e->in_cache = true;
366
207k
    e->priority = priority;
367
207k
    e->type = _type;
368
207k
    memcpy(e->key_data, key.data(), key.size());
369
207k
    e->last_visit_time = UnixMillis();
370
207k
    LRUHandle* to_remove_head = nullptr;
371
207k
    {
372
207k
        std::lock_guard l(_mutex);
373
374
        // Free the space following strict LRU policy until enough space
375
        // is freed or the lru list is empty
376
207k
        if (_cache_value_check_timestamp) {
377
18
            _evict_from_lru_with_time(e->total_size, &to_remove_head);
378
207k
        } else {
379
207k
            _evict_from_lru(e->total_size, &to_remove_head);
380
207k
        }
381
382
        // insert into the cache
383
        // note that the cache might get larger than its capacity if not enough
384
        // space was freed
385
207k
        auto old = _table.insert(e);
386
207k
        _usage += e->total_size;
387
207k
        if (old != nullptr) {
388
46
            old->in_cache = false;
389
46
            if (_unref(old)) {
390
45
                _usage -= old->total_size;
391
                // old is on LRU because it's in cache and its reference count
392
                // was just 1 (Unref returned 0)
393
45
                _lru_remove(old);
394
45
                old->next = to_remove_head;
395
45
                to_remove_head = old;
396
45
            }
397
46
        }
398
207k
    }
399
400
    // we free the entries here outside of mutex for
401
    // performance reasons
402
405k
    while (to_remove_head != nullptr) {
403
197k
        LRUHandle* next = to_remove_head->next;
404
197k
        to_remove_head->free();
405
197k
        to_remove_head = next;
406
197k
    }
407
408
207k
    return reinterpret_cast<Cache::Handle*>(e);
409
207k
}
410
411
9
void LRUCache::erase(const CacheKey& key, uint32_t hash) {
412
9
    LRUHandle* e = nullptr;
413
9
    bool last_ref = false;
414
9
    {
415
9
        std::lock_guard l(_mutex);
416
9
        e = _table.remove(key, hash);
417
9
        if (e != nullptr) {
418
2
            last_ref = _unref(e);
419
2
            if (last_ref) {
420
1
                _usage -= e->total_size;
421
1
                if (e->in_cache) {
422
                    // locate in free list
423
1
                    _lru_remove(e);
424
1
                }
425
1
            }
426
2
            e->in_cache = false;
427
2
        }
428
9
    }
429
    // free handle out of mutex, when last_ref is true, e must not be nullptr
430
9
    if (last_ref) {
431
1
        e->free();
432
1
    }
433
9
}
434
435
9.55k
PrunedInfo LRUCache::prune() {
436
9.55k
    LRUHandle* to_remove_head = nullptr;
437
9.55k
    {
438
9.55k
        std::lock_guard l(_mutex);
439
10.2k
        while (_lru_normal.next != &_lru_normal) {
440
728
            LRUHandle* old = _lru_normal.next;
441
728
            _evict_one_entry(old);
442
728
            old->next = to_remove_head;
443
728
            to_remove_head = old;
444
728
        }
445
9.56k
        while (_lru_durable.next != &_lru_durable) {
446
4
            LRUHandle* old = _lru_durable.next;
447
4
            _evict_one_entry(old);
448
4
            old->next = to_remove_head;
449
4
            to_remove_head = old;
450
4
        }
451
9.55k
    }
452
9.55k
    int64_t pruned_count = 0;
453
9.55k
    int64_t pruned_size = 0;
454
10.2k
    while (to_remove_head != nullptr) {
455
732
        ++pruned_count;
456
732
        pruned_size += to_remove_head->total_size;
457
732
        LRUHandle* next = to_remove_head->next;
458
732
        to_remove_head->free();
459
732
        to_remove_head = next;
460
732
    }
461
9.55k
    return {pruned_count, pruned_size};
462
9.55k
}
463
464
8
PrunedInfo LRUCache::prune_if(CachePrunePredicate pred, bool lazy_mode) {
465
8
    LRUHandle* to_remove_head = nullptr;
466
8
    {
467
8
        std::lock_guard l(_mutex);
468
8
        LRUHandle* p = _lru_normal.next;
469
23
        while (p != &_lru_normal) {
470
18
            LRUHandle* next = p->next;
471
18
            if (pred(p)) {
472
10
                _evict_one_entry(p);
473
10
                p->next = to_remove_head;
474
10
                to_remove_head = p;
475
10
            } else if (lazy_mode) {
476
3
                break;
477
3
            }
478
15
            p = next;
479
15
        }
480
481
8
        p = _lru_durable.next;
482
15
        while (p != &_lru_durable) {
483
10
            LRUHandle* next = p->next;
484
10
            if (pred(p)) {
485
2
                _evict_one_entry(p);
486
2
                p->next = to_remove_head;
487
2
                to_remove_head = p;
488
8
            } else if (lazy_mode) {
489
3
                break;
490
3
            }
491
7
            p = next;
492
7
        }
493
8
    }
494
8
    int64_t pruned_count = 0;
495
8
    int64_t pruned_size = 0;
496
20
    while (to_remove_head != nullptr) {
497
12
        ++pruned_count;
498
12
        pruned_size += to_remove_head->total_size;
499
12
        LRUHandle* next = to_remove_head->next;
500
12
        to_remove_head->free();
501
12
        to_remove_head = next;
502
12
    }
503
8
    return {pruned_count, pruned_size};
504
8
}
505
506
10
void LRUCache::set_cache_value_time_extractor(CacheValueTimeExtractor cache_value_time_extractor) {
507
10
    _cache_value_time_extractor = cache_value_time_extractor;
508
10
}
509
510
10
void LRUCache::set_cache_value_check_timestamp(bool cache_value_check_timestamp) {
511
10
    _cache_value_check_timestamp = cache_value_check_timestamp;
512
10
}
513
514
451k
inline uint32_t ShardedLRUCache::_hash_slice(const CacheKey& s) {
515
451k
    return s.hash(s.data(), s.size(), 0);
516
451k
}
517
518
ShardedLRUCache::ShardedLRUCache(const std::string& name, size_t total_capacity, LRUCacheType type,
519
                                 uint32_t num_shards, uint32_t total_element_count_capacity)
520
        : _name(name),
521
          _num_shard_bits(Bits::FindLSBSetNonZero(num_shards)),
522
          _num_shards(num_shards),
523
          _shards(nullptr),
524
          _last_id(1),
525
326
          _total_capacity(total_capacity) {
526
326
    CHECK(num_shards > 0) << "num_shards cannot be 0";
527
326
    CHECK_EQ((num_shards & (num_shards - 1)), 0)
528
0
            << "num_shards should be power of two, but got " << num_shards;
529
530
326
    const size_t per_shard = (total_capacity + (_num_shards - 1)) / _num_shards;
531
326
    const size_t per_shard_element_count_capacity =
532
326
            (total_element_count_capacity + (_num_shards - 1)) / _num_shards;
533
326
    LRUCache** shards = new (std::nothrow) LRUCache*[_num_shards];
534
10.4k
    for (int s = 0; s < _num_shards; s++) {
535
10.0k
        shards[s] = new LRUCache(type);
536
10.0k
        shards[s]->set_capacity(per_shard);
537
10.0k
        shards[s]->set_element_count_capacity(per_shard_element_count_capacity);
538
10.0k
    }
539
326
    _shards = shards;
540
541
326
    _entity = DorisMetrics::instance()->metric_registry()->register_entity(
542
326
            std::string("lru_cache:") + name, {{"name", name}});
543
326
    _entity->register_hook(name, std::bind(&ShardedLRUCache::update_cache_metrics, this));
544
326
    INT_GAUGE_METRIC_REGISTER(_entity, cache_capacity);
545
326
    INT_GAUGE_METRIC_REGISTER(_entity, cache_usage);
546
326
    INT_GAUGE_METRIC_REGISTER(_entity, cache_element_count);
547
326
    DOUBLE_GAUGE_METRIC_REGISTER(_entity, cache_usage_ratio);
548
326
    INT_COUNTER_METRIC_REGISTER(_entity, cache_lookup_count);
549
326
    INT_COUNTER_METRIC_REGISTER(_entity, cache_hit_count);
550
326
    DOUBLE_GAUGE_METRIC_REGISTER(_entity, cache_hit_ratio);
551
552
326
    _hit_count_bvar.reset(new bvar::Adder<uint64_t>("doris_cache", _name));
553
326
    _hit_count_per_second.reset(new bvar::PerSecond<bvar::Adder<uint64_t>>(
554
326
            "doris_cache", _name + "_persecond", _hit_count_bvar.get(), 60));
555
326
    _lookup_count_bvar.reset(new bvar::Adder<uint64_t>("doris_cache", _name));
556
326
    _lookup_count_per_second.reset(new bvar::PerSecond<bvar::Adder<uint64_t>>(
557
326
            "doris_cache", _name + "_persecond", _lookup_count_bvar.get(), 60));
558
326
}
559
560
ShardedLRUCache::ShardedLRUCache(const std::string& name, size_t total_capacity, LRUCacheType type,
561
                                 uint32_t num_shards,
562
                                 CacheValueTimeExtractor cache_value_time_extractor,
563
                                 bool cache_value_check_timestamp,
564
                                 uint32_t total_element_count_capacity)
565
10
        : ShardedLRUCache(name, total_capacity, type, num_shards, total_element_count_capacity) {
566
20
    for (int s = 0; s < _num_shards; s++) {
567
10
        _shards[s]->set_cache_value_time_extractor(cache_value_time_extractor);
568
10
        _shards[s]->set_cache_value_check_timestamp(cache_value_check_timestamp);
569
10
    }
570
10
}
571
572
316
ShardedLRUCache::~ShardedLRUCache() {
573
316
    _entity->deregister_hook(_name);
574
316
    DorisMetrics::instance()->metric_registry()->deregister_entity(_entity);
575
316
    if (_shards) {
576
9.86k
        for (int s = 0; s < _num_shards; s++) {
577
9.55k
            delete _shards[s];
578
9.55k
        }
579
316
        delete[] _shards;
580
316
    }
581
316
}
582
583
Cache::Handle* ShardedLRUCache::insert(const CacheKey& key, void* value, size_t charge,
584
207k
                                       CachePriority priority) {
585
207k
    const uint32_t hash = _hash_slice(key);
586
207k
    return _shards[_shard(hash)]->insert(key, hash, value, charge, priority);
587
207k
}
588
589
243k
Cache::Handle* ShardedLRUCache::lookup(const CacheKey& key) {
590
243k
    const uint32_t hash = _hash_slice(key);
591
243k
    return _shards[_shard(hash)]->lookup(key, hash);
592
243k
}
593
594
435k
void ShardedLRUCache::release(Handle* handle) {
595
435k
    LRUHandle* h = reinterpret_cast<LRUHandle*>(handle);
596
435k
    _shards[_shard(h->hash)]->release(handle);
597
435k
}
598
599
9
void ShardedLRUCache::erase(const CacheKey& key) {
600
9
    const uint32_t hash = _hash_slice(key);
601
9
    _shards[_shard(hash)]->erase(key, hash);
602
9
}
603
604
232k
void* ShardedLRUCache::value(Handle* handle) {
605
232k
    return reinterpret_cast<LRUHandle*>(handle)->value;
606
232k
}
607
608
2
uint64_t ShardedLRUCache::new_id() {
609
2
    return _last_id.fetch_add(1, std::memory_order_relaxed);
610
2
}
611
612
0
PrunedInfo ShardedLRUCache::prune() {
613
0
    PrunedInfo pruned_info;
614
0
    for (int s = 0; s < _num_shards; s++) {
615
0
        PrunedInfo info = _shards[s]->prune();
616
0
        pruned_info.pruned_count += info.pruned_count;
617
0
        pruned_info.pruned_size += info.pruned_size;
618
0
    }
619
0
    return pruned_info;
620
0
}
621
622
0
PrunedInfo ShardedLRUCache::prune_if(CachePrunePredicate pred, bool lazy_mode) {
623
0
    PrunedInfo pruned_info;
624
0
    for (int s = 0; s < _num_shards; s++) {
625
0
        PrunedInfo info = _shards[s]->prune_if(pred, lazy_mode);
626
0
        pruned_info.pruned_count += info.pruned_count;
627
0
        pruned_info.pruned_size += info.pruned_size;
628
0
    }
629
0
    return pruned_info;
630
0
}
631
632
0
int64_t ShardedLRUCache::get_usage() {
633
0
    size_t total_usage = 0;
634
0
    for (int i = 0; i < _num_shards; i++) {
635
0
        total_usage += _shards[i]->get_usage();
636
0
    }
637
0
    return total_usage;
638
0
}
639
640
0
void ShardedLRUCache::update_cache_metrics() const {
641
0
    size_t total_capacity = 0;
642
0
    size_t total_usage = 0;
643
0
    size_t total_lookup_count = 0;
644
0
    size_t total_hit_count = 0;
645
0
    size_t total_element_count = 0;
646
0
    for (int i = 0; i < _num_shards; i++) {
647
0
        total_capacity += _shards[i]->get_capacity();
648
0
        total_usage += _shards[i]->get_usage();
649
0
        total_lookup_count += _shards[i]->get_lookup_count();
650
0
        total_hit_count += _shards[i]->get_hit_count();
651
0
        total_element_count += _shards[i]->get_element_count();
652
0
    }
653
654
0
    cache_capacity->set_value(total_capacity);
655
0
    cache_usage->set_value(total_usage);
656
0
    cache_element_count->set_value(total_element_count);
657
0
    cache_lookup_count->set_value(total_lookup_count);
658
0
    cache_hit_count->set_value(total_hit_count);
659
0
    cache_usage_ratio->set_value(total_capacity == 0 ? 0 : ((double)total_usage / total_capacity));
660
0
    cache_hit_ratio->set_value(
661
0
            total_lookup_count == 0 ? 0 : ((double)total_hit_count / total_lookup_count));
662
0
}
663
664
Cache::Handle* DummyLRUCache::insert(const CacheKey& key, void* value, size_t charge,
665
176
                                     CachePriority priority) {
666
176
    size_t handle_size = sizeof(LRUHandle);
667
176
    auto* e = reinterpret_cast<LRUHandle*>(malloc(handle_size));
668
176
    e->value = value;
669
176
    e->charge = charge;
670
176
    e->key_length = 0;
671
176
    e->total_size = 0;
672
176
    e->hash = 0;
673
176
    e->refs = 1; // only one for the returned handle
674
176
    e->next = e->prev = nullptr;
675
176
    e->in_cache = false;
676
176
    return reinterpret_cast<Cache::Handle*>(e);
677
176
}
678
679
176
void DummyLRUCache::release(Cache::Handle* handle) {
680
176
    if (handle == nullptr) {
681
0
        return;
682
0
    }
683
176
    auto* e = reinterpret_cast<LRUHandle*>(handle);
684
176
    e->free();
685
176
}
686
687
0
void* DummyLRUCache::value(Handle* handle) {
688
0
    return reinterpret_cast<LRUHandle*>(handle)->value;
689
0
}
690
691
} // namespace doris