]>
git.proxmox.com Git - ceph.git/blob - ceph/src/kv/rocksdb_cache/ShardedCache.h
1 // Copyright (c) 2018-Present Red Hat Inc. All rights reserved.
3 // Copyright (c) 2011-2018, Facebook, Inc. All rights reserved.
4 // This source code is licensed under both the GPLv2 and Apache 2.0 License
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
10 #ifndef ROCKSDB_SHARDED_CACHE
11 #define ROCKSDB_SHARDED_CACHE
17 #include "rocksdb/version.h"
18 #include "rocksdb/cache.h"
19 #include "include/ceph_hash.h"
20 #include "common/PriorityCache.h"
23 #ifndef CACHE_LINE_SIZE
24 #define CACHE_LINE_SIZE 64 // XXX arch-specific define
27 namespace rocksdb_cache
{
29 using DeleterFn
= void (*)(const rocksdb::Slice
& key
, void* value
);
31 // Single cache shard interface.
34 CacheShard() = default;
35 virtual ~CacheShard() = default;
37 virtual rocksdb::Status
Insert(const rocksdb::Slice
& key
, uint32_t hash
, void* value
,
40 rocksdb::Cache::Handle
** handle
, rocksdb::Cache::Priority priority
) = 0;
41 virtual rocksdb::Cache::Handle
* Lookup(const rocksdb::Slice
& key
, uint32_t hash
) = 0;
42 virtual bool Ref(rocksdb::Cache::Handle
* handle
) = 0;
43 virtual bool Release(rocksdb::Cache::Handle
* handle
, bool force_erase
= false) = 0;
44 virtual void Erase(const rocksdb::Slice
& key
, uint32_t hash
) = 0;
45 virtual void SetCapacity(size_t capacity
) = 0;
46 virtual void SetStrictCapacityLimit(bool strict_capacity_limit
) = 0;
47 virtual size_t GetUsage() const = 0;
48 virtual size_t GetPinnedUsage() const = 0;
49 virtual void ApplyToAllCacheEntries(
50 const std::function
<void(const rocksdb::Slice
& key
,
53 DeleterFn
)>& callback
,
54 bool thread_safe
) = 0;
55 virtual void EraseUnRefEntries() = 0;
56 virtual std::string
GetPrintableOptions() const { return ""; }
57 virtual DeleterFn
GetDeleter(rocksdb::Cache::Handle
* handle
) const = 0;
60 // Generic cache interface which shards cache by hash of keys. 2^num_shard_bits
61 // shards will be created, with capacity split evenly to each of the shards.
62 // Keys are sharded by the highest num_shard_bits bits of hash value.
63 class ShardedCache
: public rocksdb::Cache
, public PriorityCache::PriCache
{
65 ShardedCache(size_t capacity
, int num_shard_bits
, bool strict_capacity_limit
);
66 virtual ~ShardedCache() = default;
68 virtual const char* Name() const override
= 0;
69 virtual rocksdb::Status
Insert(const rocksdb::Slice
& key
, void* value
, size_t charge
,
71 rocksdb::Cache::Handle
** handle
, Priority priority
) override
;
72 virtual rocksdb::Cache::Handle
* Lookup(const rocksdb::Slice
& key
, rocksdb::Statistics
* stats
) override
;
73 virtual bool Ref(rocksdb::Cache::Handle
* handle
) override
;
74 virtual bool Release(rocksdb::Cache::Handle
* handle
, bool force_erase
= false) override
;
75 virtual void* Value(Handle
* handle
) override
= 0;
76 virtual void Erase(const rocksdb::Slice
& key
) override
;
77 virtual uint64_t NewId() override
;
78 virtual void SetCapacity(size_t capacity
) override
;
79 virtual void SetStrictCapacityLimit(bool strict_capacity_limit
) override
;
80 virtual bool HasStrictCapacityLimit() const override
;
81 virtual size_t GetCapacity() const override
;
82 virtual size_t GetUsage() const override
;
83 virtual size_t GetUsage(rocksdb::Cache::Handle
* handle
) const override
;
84 virtual size_t GetPinnedUsage() const override
;
85 virtual size_t GetCharge(Handle
* handle
) const = 0;
86 #if (ROCKSDB_MAJOR >= 7 || (ROCKSDB_MAJOR == 6 && ROCKSDB_MINOR >= 22))
87 virtual DeleterFn
GetDeleter(Handle
* handle
) const override
;
89 virtual void DisownData() override
= 0;
90 #if (ROCKSDB_MAJOR >= 7 || (ROCKSDB_MAJOR == 6 && ROCKSDB_MINOR >= 22))
91 virtual void ApplyToAllEntries(
92 const std::function
<void(const rocksdb::Slice
& key
, void* value
, size_t charge
,
93 DeleterFn deleter
)>& callback
,
94 const ApplyToAllEntriesOptions
& opts
) override
;
96 virtual void ApplyToAllCacheEntries(void (*callback
)(void*, size_t),
97 bool thread_safe
) override
;
99 virtual void EraseUnRefEntries() override
;
100 virtual std::string
GetPrintableOptions() const override
;
101 virtual CacheShard
* GetShard(int shard
) = 0;
102 virtual const CacheShard
* GetShard(int shard
) const = 0;
103 virtual uint32_t GetHash(Handle
* handle
) const = 0;
105 int GetNumShardBits() const { return num_shard_bits_
; }
107 virtual uint32_t get_bin_count() const = 0;
108 virtual void set_bin_count(uint32_t count
) = 0;
111 virtual int64_t get_cache_bytes(PriorityCache::Priority pri
) const {
112 return cache_bytes
[pri
];
114 virtual int64_t get_cache_bytes() const {
116 for (int i
= 0; i
< PriorityCache::Priority::LAST
+ 1; i
++) {
117 PriorityCache::Priority pri
= static_cast<PriorityCache::Priority
>(i
);
118 total
+= get_cache_bytes(pri
);
122 virtual void set_cache_bytes(PriorityCache::Priority pri
, int64_t bytes
) {
123 cache_bytes
[pri
] = bytes
;
125 virtual void add_cache_bytes(PriorityCache::Priority pri
, int64_t bytes
) {
126 cache_bytes
[pri
] += bytes
;
128 virtual double get_cache_ratio() const {
131 virtual void set_cache_ratio(double ratio
) {
134 virtual uint64_t get_bins(PriorityCache::Priority pri
) const {
135 if (pri
> PriorityCache::Priority::PRI0
&&
136 pri
< PriorityCache::Priority::LAST
) {
141 virtual void set_bins(PriorityCache::Priority pri
, uint64_t end_bin
) {
142 if (pri
<= PriorityCache::Priority::PRI0
||
143 pri
>= PriorityCache::Priority::LAST
) {
148 for (int pri
= 1; pri
< PriorityCache::Priority::LAST
; pri
++) {
149 if (bins
[pri
] > max
) {
155 virtual void import_bins(const std::vector
<uint64_t> &bins_v
) {
157 for (int pri
= 1; pri
< PriorityCache::Priority::LAST
; pri
++) {
158 unsigned i
= (unsigned) pri
- 1;
159 if (i
< bins_v
.size()) {
160 bins
[pri
] = bins_v
[i
];
161 if (bins
[pri
] > max
) {
170 virtual std::string
get_cache_name() const = 0;
173 static inline uint32_t HashSlice(const rocksdb::Slice
& s
) {
174 return ceph_str_hash(CEPH_STR_HASH_RJENKINS
, s
.data(), s
.size());
175 // return Hash(s.data(), s.size(), 0);
178 uint32_t Shard(uint32_t hash
) const {
179 // Note, hash >> 32 yields hash in gcc, not the zero we expect!
180 return (num_shard_bits_
> 0) ? (hash
>> (32 - num_shard_bits_
)) : 0;
183 uint64_t bins
[PriorityCache::Priority::LAST
+1] = {0};
184 int64_t cache_bytes
[PriorityCache::Priority::LAST
+1] = {0};
185 double cache_ratio
= 0;
188 mutable std::mutex capacity_mutex_
;
190 bool strict_capacity_limit_
;
191 std::atomic
<uint64_t> last_id_
;
194 extern int GetDefaultCacheShardBits(size_t capacity
);
196 } // namespace rocksdb_cache
197 #endif // ROCKSDB_SHARDED_CACHE