Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyiZzz committed Dec 24, 2024
1 parent 4e6197c commit 54392ce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
25 changes: 11 additions & 14 deletions be/src/olap/lru_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

#include "olap/lru_cache.h"

#include <stdlib.h>

#include <cstdlib>
#include <mutex>
#include <new>
#include <sstream>
#include <string>

#include "gutil/bits.h"
#include "util/doris_metrics.h"
#include "util/metrics.h"
#include "util/time.h"

using std::string;
using std::stringstream;

namespace doris {
#include "common/compile_check_begin.h"

DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(cache_capacity, MetricUnit::BYTES);
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(cache_usage, MetricUnit::BYTES);
Expand All @@ -35,7 +35,7 @@ uint32_t CacheKey::hash(const char* data, size_t n, uint32_t seed) const {
const uint32_t m = 0xc6a4a793;
const uint32_t r = 24;
const char* limit = data + n;
uint32_t h = seed ^ (n * m);
uint32_t h = seed ^ (static_cast<uint32_t>(n) * m);

// Pick up four bytes at a time
while (data + 4 <= limit) {
Expand Down Expand Up @@ -69,8 +69,6 @@ uint32_t CacheKey::hash(const char* data, size_t n, uint32_t seed) const {
return h;
}

Cache::~Cache() {}

HandleTable::~HandleTable() {
delete[] _list;
}
Expand Down Expand Up @@ -140,7 +138,7 @@ void HandleTable::_resize() {
new_length *= 2;
}

LRUHandle** new_list = new (std::nothrow) LRUHandle*[new_length];
auto** new_list = new (std::nothrow) LRUHandle*[new_length];
memset(new_list, 0, sizeof(new_list[0]) * new_length);

uint32_t count = 0;
Expand Down Expand Up @@ -240,7 +238,7 @@ bool LRUCache::_unref(LRUHandle* e) {
return e->refs == 0;
}

void LRUCache::_lru_remove(LRUHandle* e) {
void LRUCache::_lru_remove(LRUHandle* e) const {
e->next->prev = e->prev;
e->prev->next = e->next;
e->prev = e->next = nullptr;
Expand All @@ -262,7 +260,7 @@ void LRUCache::_lru_remove(LRUHandle* e) {
}
}

void LRUCache::_lru_append(LRUHandle* list, LRUHandle* e) {
void LRUCache::_lru_append(LRUHandle* list, LRUHandle* e) const {
// Make "e" newest entry by inserting just before *list
e->next = list;
e->prev = list->prev;
Expand Down Expand Up @@ -323,7 +321,7 @@ void LRUCache::release(Cache::Handle* handle) {
if (handle == nullptr) {
return;
}
LRUHandle* e = reinterpret_cast<LRUHandle*>(handle);
auto* e = reinterpret_cast<LRUHandle*>(handle);
bool last_ref = false;
{
std::lock_guard l(_mutex);
Expand Down Expand Up @@ -647,17 +645,16 @@ ShardedLRUCache::ShardedLRUCache(const std::string& name, size_t capacity, LRUCa
: _name(name),
_num_shard_bits(Bits::FindLSBSetNonZero(num_shards)),
_num_shards(num_shards),
_shards(nullptr),
_last_id(1),
_capacity(capacity) {
CHECK(num_shards > 0) << "num_shards cannot be 0";
CHECK_EQ((num_shards & (num_shards - 1)), 0)
<< "num_shards should be power of two, but got " << num_shards;

const size_t per_shard = (capacity + (_num_shards - 1)) / _num_shards;
const size_t per_shard_element_count_capacity =
const uint32_t per_shard_element_count_capacity =
(total_element_count_capacity + (_num_shards - 1)) / _num_shards;
LRUCache** shards = new (std::nothrow) LRUCache*[_num_shards];
auto** shards = new (std::nothrow) LRUCache*[_num_shards];
for (int s = 0; s < _num_shards; s++) {
shards[s] = new LRUCache(type, is_lru_k);
shards[s]->set_capacity(per_shard);
Expand Down Expand Up @@ -740,7 +737,7 @@ Cache::Handle* ShardedLRUCache::lookup(const CacheKey& key) {
}

void ShardedLRUCache::release(Handle* handle) {
LRUHandle* h = reinterpret_cast<LRUHandle*>(handle);
auto* h = reinterpret_cast<LRUHandle*>(handle);
_shards[_shard(h->hash)]->release(handle);
}

Expand Down
54 changes: 30 additions & 24 deletions be/src/olap/lru_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

#pragma once

#include <assert.h>
#include <butil/macros.h>
#include <bvar/bvar.h>
#include <glog/logging.h>
#include <gtest/gtest_prod.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <atomic>
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <memory>
#include <set>
Expand All @@ -25,6 +25,7 @@
#include "util/metrics.h"

namespace doris {
#include "common/compile_check_begin.h"

class Cache;
class LRUCachePolicy;
Expand All @@ -42,7 +43,7 @@ static constexpr bool DEFAULT_LRU_CACHE_IS_LRU_K = false;

class CacheKey {
public:
CacheKey() : _data(nullptr), _size(0) {}
CacheKey() : _size(0) {}
// Create a slice that refers to d[0,n-1].
CacheKey(const char* d, size_t n) : _data(d), _size(n) {}

Expand All @@ -52,7 +53,7 @@ class CacheKey {
// Create a slice that refers to s[0,strlen(s)-1]
CacheKey(const char* s) : _data(s), _size(strlen(s)) {}

~CacheKey() {}
~CacheKey() = default;

// Return a pointer to the beginning of the referenced data
const char* data() const { return _data; }
Expand Down Expand Up @@ -84,7 +85,7 @@ class CacheKey {
}

// Return a string that contains the copy of the referenced data.
std::string to_string() const { return std::string(_data, _size); }
std::string to_string() const { return {_data, _size}; }

bool operator==(const CacheKey& other) const {
return ((size() == other.size()) && (memcmp(data(), other.data(), size()) == 0));
Expand Down Expand Up @@ -139,11 +140,11 @@ struct PrunedInfo {

class Cache {
public:
Cache() {}
Cache() = default;

// Destroys all existing entries by calling the "deleter"
// function that was passed to the constructor.
virtual ~Cache();
virtual ~Cache() = default;

// Opaque handle to an entry stored in the cache.
struct Handle {};
Expand Down Expand Up @@ -244,7 +245,7 @@ struct LRUHandle {
if (next == this) {
return *(reinterpret_cast<CacheKey*>(value));
} else {
return CacheKey(key_data, key_length);
return {key_data, key_length};
}
}

Expand All @@ -264,7 +265,7 @@ struct LRUHandle {

class HandleTable {
public:
HandleTable() : _length(0), _elems(0), _list(nullptr) { _resize(); }
HandleTable() { _resize(); }

~HandleTable();

Expand All @@ -287,8 +288,8 @@ class HandleTable {

// The tablet consists of an array of buckets where each bucket is
// a linked list of cache entries that hash into the bucket.
uint32_t _length;
uint32_t _elems;
uint32_t _length {};
uint32_t _elems {};
LRUHandle** _list = nullptr;

// Return a pointer to slot that points to a cache entry that
Expand All @@ -310,6 +311,10 @@ class LRUCache {
LRUCache(LRUCacheType type, bool is_lru_k = DEFAULT_LRU_CACHE_IS_LRU_K);
~LRUCache();

// visits_lru_cache_key is the hash value of CacheKey.
// If there is a hash conflict, a cache entry may be inserted early
// and another cache entry with the same key hash may be inserted later.
// Otherwise, this does not affect the correctness of the cache.
using visits_lru_cache_key = uint32_t;
using visits_lru_cache_pair = std::pair<visits_lru_cache_key, size_t>;

Expand Down Expand Up @@ -342,8 +347,8 @@ class LRUCache {
size_t get_element_count();

private:
void _lru_remove(LRUHandle* e);
void _lru_append(LRUHandle* list, LRUHandle* e);
void _lru_remove(LRUHandle* e) const;
void _lru_append(LRUHandle* list, LRUHandle* e) const;
bool _unref(LRUHandle* e);
void _evict_from_lru(size_t total_size, LRUHandle** to_remove_head);
void _evict_from_lru_with_time(size_t total_size, LRUHandle** to_remove_head);
Expand Down Expand Up @@ -391,14 +396,14 @@ class LRUCache {

class ShardedLRUCache : public Cache {
public:
virtual ~ShardedLRUCache();
virtual Handle* insert(const CacheKey& key, void* value, size_t charge,
CachePriority priority = CachePriority::NORMAL) override;
virtual Handle* lookup(const CacheKey& key) override;
virtual void release(Handle* handle) override;
virtual void erase(const CacheKey& key) override;
virtual void* value(Handle* handle) override;
virtual uint64_t new_id() override;
~ShardedLRUCache() override;
Handle* insert(const CacheKey& key, void* value, size_t charge,
CachePriority priority = CachePriority::NORMAL) override;
Handle* lookup(const CacheKey& key) override;
void release(Handle* handle) override;
void erase(const CacheKey& key) override;
void* value(Handle* handle) override;
uint64_t new_id() override;
PrunedInfo prune() override;
PrunedInfo prune_if(CachePrunePredicate pred, bool lazy_mode = false) override;
int64_t get_usage() override;
Expand All @@ -422,7 +427,7 @@ class ShardedLRUCache : public Cache {

private:
static uint32_t _hash_slice(const CacheKey& s);
uint32_t _shard(uint32_t hash) {
uint32_t _shard(uint32_t hash) const {
return _num_shard_bits > 0 ? (hash >> (32 - _num_shard_bits)) : 0;
}

Expand Down Expand Up @@ -473,3 +478,4 @@ class DummyLRUCache : public Cache {
};

} // namespace doris
#include "common/compile_check_end.h"

0 comments on commit 54392ce

Please sign in to comment.