]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/cache/sharded_cache.h
4f9dea2ad0fe670ad68b1374a418ece3eca6915a
[ceph.git] / ceph / src / rocksdb / cache / sharded_cache.h
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).
5 //
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.
9
10 #pragma once
11
12 #include <atomic>
13 #include <string>
14
15 #include "port/port.h"
16 #include "rocksdb/cache.h"
17 #include "util/hash.h"
18
19 namespace rocksdb {
20
21 // Single cache shard interface.
22 class CacheShard {
23 public:
24 CacheShard() = default;
25 virtual ~CacheShard() = default;
26
27 virtual Status Insert(const Slice& key, uint32_t hash, void* value,
28 size_t charge,
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 ""; }
43 };
44
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 {
49 public:
50 ShardedCache(size_t capacity, int num_shard_bits, bool strict_capacity_limit);
51 virtual ~ShardedCache() = default;
52 virtual const char* Name() const override = 0;
53 virtual CacheShard* GetShard(int shard) = 0;
54 virtual const CacheShard* GetShard(int shard) const = 0;
55 virtual void* Value(Handle* handle) override = 0;
56 virtual size_t GetCharge(Handle* handle) const = 0;
57 virtual uint32_t GetHash(Handle* handle) const = 0;
58 virtual void DisownData() override = 0;
59
60 virtual void SetCapacity(size_t capacity) override;
61 virtual void SetStrictCapacityLimit(bool strict_capacity_limit) override;
62
63 virtual Status Insert(const Slice& key, void* value, size_t charge,
64 void (*deleter)(const Slice& key, void* value),
65 Handle** handle, Priority priority) override;
66 virtual Handle* Lookup(const Slice& key, Statistics* stats) override;
67 virtual bool Ref(Handle* handle) override;
68 virtual bool Release(Handle* handle, bool force_erase = false) override;
69 virtual void Erase(const Slice& key) override;
70 virtual uint64_t NewId() override;
71 virtual size_t GetCapacity() const override;
72 virtual bool HasStrictCapacityLimit() const override;
73 virtual size_t GetUsage() const override;
74 virtual size_t GetUsage(Handle* handle) const override;
75 virtual size_t GetPinnedUsage() const override;
76 virtual void ApplyToAllCacheEntries(void (*callback)(void*, size_t),
77 bool thread_safe) override;
78 virtual void EraseUnRefEntries() override;
79 virtual std::string GetPrintableOptions() const override;
80
81 int GetNumShardBits() const { return num_shard_bits_; }
82
83 private:
84 static inline uint32_t HashSlice(const Slice& s) {
85 return Hash(s.data(), s.size(), 0);
86 }
87
88 uint32_t Shard(uint32_t hash) {
89 // Note, hash >> 32 yields hash in gcc, not the zero we expect!
90 return (num_shard_bits_ > 0) ? (hash >> (32 - num_shard_bits_)) : 0;
91 }
92
93 int num_shard_bits_;
94 mutable port::Mutex capacity_mutex_;
95 size_t capacity_;
96 bool strict_capacity_limit_;
97 std::atomic<uint64_t> last_id_;
98 };
99
100 extern int GetDefaultCacheShardBits(size_t capacity);
101
102 } // namespace rocksdb