]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/cache/sharded_cache.h
920898b871f4a382f72984c59fd2efe040a633de
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
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.
15 #include "port/port.h"
16 #include "rocksdb/cache.h"
17 #include "util/hash.h"
21 // Single cache shard interface.
24 CacheShard() = default;
25 virtual ~CacheShard() = default;
27 virtual Status
Insert(const Slice
& key
, uint32_t hash
, void* value
,
29 void (*deleter
)(const Slice
& key
, void* value
),
30 Cache::Handle
** handle
, Cache::Priority priority
) = 0;
31 virtual Cache::Handle
* Lookup(const Slice
& key
, uint32_t hash
) = 0;
32 virtual bool Ref(Cache::Handle
* handle
) = 0;
33 virtual bool Release(Cache::Handle
* handle
, bool force_erase
= false) = 0;
34 virtual void Erase(const Slice
& key
, uint32_t hash
) = 0;
35 virtual void SetCapacity(size_t capacity
) = 0;
36 virtual void SetStrictCapacityLimit(bool strict_capacity_limit
) = 0;
37 virtual size_t GetUsage() const = 0;
38 virtual size_t GetPinnedUsage() const = 0;
39 virtual void ApplyToAllCacheEntries(void (*callback
)(void*, size_t),
40 bool thread_safe
) = 0;
41 virtual void EraseUnRefEntries() = 0;
42 virtual std::string
GetPrintableOptions() const { return ""; }
45 // Generic cache interface which shards cache by hash of keys. 2^num_shard_bits
46 // shards will be created, with capacity split evenly to each of the shards.
47 // Keys are sharded by the highest num_shard_bits bits of hash value.
48 class ShardedCache
: public Cache
{
50 ShardedCache(size_t capacity
, int num_shard_bits
, bool strict_capacity_limit
,
51 std::shared_ptr
<MemoryAllocator
> memory_allocator
= nullptr);
52 virtual ~ShardedCache() = default;
53 virtual const char* Name() const override
= 0;
54 virtual CacheShard
* GetShard(int shard
) = 0;
55 virtual const CacheShard
* GetShard(int shard
) const = 0;
56 virtual void* Value(Handle
* handle
) override
= 0;
57 virtual size_t GetCharge(Handle
* handle
) const = 0;
58 virtual uint32_t GetHash(Handle
* handle
) const = 0;
59 virtual void DisownData() override
= 0;
61 virtual void SetCapacity(size_t capacity
) override
;
62 virtual void SetStrictCapacityLimit(bool strict_capacity_limit
) override
;
64 virtual Status
Insert(const Slice
& key
, void* value
, size_t charge
,
65 void (*deleter
)(const Slice
& key
, void* value
),
66 Handle
** handle
, Priority priority
) override
;
67 virtual Handle
* Lookup(const Slice
& key
, Statistics
* stats
) override
;
68 virtual bool Ref(Handle
* handle
) override
;
69 virtual bool Release(Handle
* handle
, bool force_erase
= false) override
;
70 virtual void Erase(const Slice
& key
) override
;
71 virtual uint64_t NewId() override
;
72 virtual size_t GetCapacity() const override
;
73 virtual bool HasStrictCapacityLimit() const override
;
74 virtual size_t GetUsage() const override
;
75 virtual size_t GetUsage(Handle
* handle
) const override
;
76 virtual size_t GetPinnedUsage() const override
;
77 virtual void ApplyToAllCacheEntries(void (*callback
)(void*, size_t),
78 bool thread_safe
) override
;
79 virtual void EraseUnRefEntries() override
;
80 virtual std::string
GetPrintableOptions() const override
;
82 int GetNumShardBits() const { return num_shard_bits_
; }
85 static inline uint32_t HashSlice(const Slice
& s
) {
86 return static_cast<uint32_t>(GetSliceNPHash64(s
));
89 uint32_t Shard(uint32_t hash
) {
90 // Note, hash >> 32 yields hash in gcc, not the zero we expect!
91 return (num_shard_bits_
> 0) ? (hash
>> (32 - num_shard_bits_
)) : 0;
95 mutable port::Mutex capacity_mutex_
;
97 bool strict_capacity_limit_
;
98 std::atomic
<uint64_t> last_id_
;
101 extern int GetDefaultCacheShardBits(size_t capacity
);
103 } // namespace rocksdb