sharded_cache.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #pragma once
  10. #include <atomic>
  11. #include <string>
  12. #include "port/port.h"
  13. #include "rocksdb/cache.h"
  14. #include "util/hash.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. // Single cache shard interface.
  17. class CacheShard {
  18. public:
  19. CacheShard() = default;
  20. virtual ~CacheShard() = default;
  21. virtual Status Insert(const Slice& key, uint32_t hash, void* value,
  22. size_t charge,
  23. void (*deleter)(const Slice& key, void* value),
  24. Cache::Handle** handle, Cache::Priority priority) = 0;
  25. virtual Cache::Handle* Lookup(const Slice& key, uint32_t hash) = 0;
  26. virtual bool Ref(Cache::Handle* handle) = 0;
  27. virtual bool Release(Cache::Handle* handle, bool force_erase = false) = 0;
  28. virtual void Erase(const Slice& key, uint32_t hash) = 0;
  29. virtual void SetCapacity(size_t capacity) = 0;
  30. virtual void SetStrictCapacityLimit(bool strict_capacity_limit) = 0;
  31. virtual size_t GetUsage() const = 0;
  32. virtual size_t GetPinnedUsage() const = 0;
  33. virtual void ApplyToAllCacheEntries(void (*callback)(void*, size_t),
  34. bool thread_safe) = 0;
  35. virtual void EraseUnRefEntries() = 0;
  36. virtual std::string GetPrintableOptions() const { return ""; }
  37. void set_metadata_charge_policy(
  38. CacheMetadataChargePolicy metadata_charge_policy) {
  39. metadata_charge_policy_ = metadata_charge_policy;
  40. }
  41. protected:
  42. CacheMetadataChargePolicy metadata_charge_policy_ = kDontChargeCacheMetadata;
  43. };
  44. // Generic cache interface which shards cache by hash of keys. 2^num_shard_bits
  45. // shards will be created, with capacity split evenly to each of the shards.
  46. // Keys are sharded by the highest num_shard_bits bits of hash value.
  47. class ShardedCache : public Cache {
  48. public:
  49. ShardedCache(size_t capacity, int num_shard_bits, bool strict_capacity_limit,
  50. std::shared_ptr<MemoryAllocator> memory_allocator = nullptr);
  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 override = 0;
  57. virtual uint32_t GetHash(Handle* handle) const = 0;
  58. virtual void DisownData() override = 0;
  59. virtual void SetCapacity(size_t capacity) override;
  60. virtual void SetStrictCapacityLimit(bool strict_capacity_limit) override;
  61. virtual Status Insert(const Slice& key, void* value, size_t charge,
  62. void (*deleter)(const Slice& key, void* value),
  63. Handle** handle, Priority priority) override;
  64. virtual Handle* Lookup(const Slice& key, Statistics* stats) override;
  65. virtual bool Ref(Handle* handle) override;
  66. virtual bool Release(Handle* handle, bool force_erase = false) override;
  67. virtual void Erase(const Slice& key) override;
  68. virtual uint64_t NewId() override;
  69. virtual size_t GetCapacity() const override;
  70. virtual bool HasStrictCapacityLimit() const override;
  71. virtual size_t GetUsage() const override;
  72. virtual size_t GetUsage(Handle* handle) const override;
  73. virtual size_t GetPinnedUsage() const override;
  74. virtual void ApplyToAllCacheEntries(void (*callback)(void*, size_t),
  75. bool thread_safe) override;
  76. virtual void EraseUnRefEntries() override;
  77. virtual std::string GetPrintableOptions() const override;
  78. int GetNumShardBits() const { return num_shard_bits_; }
  79. private:
  80. static inline uint32_t HashSlice(const Slice& s) {
  81. return static_cast<uint32_t>(GetSliceNPHash64(s));
  82. }
  83. uint32_t Shard(uint32_t hash) {
  84. // Note, hash >> 32 yields hash in gcc, not the zero we expect!
  85. return (num_shard_bits_ > 0) ? (hash >> (32 - num_shard_bits_)) : 0;
  86. }
  87. int num_shard_bits_;
  88. mutable port::Mutex capacity_mutex_;
  89. size_t capacity_;
  90. bool strict_capacity_limit_;
  91. std::atomic<uint64_t> last_id_;
  92. };
  93. extern int GetDefaultCacheShardBits(size_t capacity);
  94. } // namespace ROCKSDB_NAMESPACE