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 #include "cache/sharded_cache.h"
11 
12 #include <string>
13 
14 #include "util/mutexlock.h"
15 
16 namespace ROCKSDB_NAMESPACE {
17 
ShardedCache(size_t capacity,int num_shard_bits,bool strict_capacity_limit,std::shared_ptr<MemoryAllocator> allocator)18 ShardedCache::ShardedCache(size_t capacity, int num_shard_bits,
19                            bool strict_capacity_limit,
20                            std::shared_ptr<MemoryAllocator> allocator)
21     : Cache(std::move(allocator)),
22       num_shard_bits_(num_shard_bits),
23       capacity_(capacity),
24       strict_capacity_limit_(strict_capacity_limit),
25       last_id_(1) {}
26 
SetCapacity(size_t capacity)27 void ShardedCache::SetCapacity(size_t capacity) {
28   int num_shards = 1 << num_shard_bits_;
29   const size_t per_shard = (capacity + (num_shards - 1)) / num_shards;
30   MutexLock l(&capacity_mutex_);
31   for (int s = 0; s < num_shards; s++) {
32     GetShard(s)->SetCapacity(per_shard);
33   }
34   capacity_ = capacity;
35 }
36 
SetStrictCapacityLimit(bool strict_capacity_limit)37 void ShardedCache::SetStrictCapacityLimit(bool strict_capacity_limit) {
38   int num_shards = 1 << num_shard_bits_;
39   MutexLock l(&capacity_mutex_);
40   for (int s = 0; s < num_shards; s++) {
41     GetShard(s)->SetStrictCapacityLimit(strict_capacity_limit);
42   }
43   strict_capacity_limit_ = strict_capacity_limit;
44 }
45 
Insert(const Slice & key,void * value,size_t charge,void (* deleter)(const Slice & key,void * value),Handle ** handle,Priority priority)46 Status ShardedCache::Insert(const Slice& key, void* value, size_t charge,
47                             void (*deleter)(const Slice& key, void* value),
48                             Handle** handle, Priority priority) {
49   uint32_t hash = HashSlice(key);
50   return GetShard(Shard(hash))
51       ->Insert(key, hash, value, charge, deleter, handle, priority);
52 }
53 
Lookup(const Slice & key,Statistics *)54 Cache::Handle* ShardedCache::Lookup(const Slice& key, Statistics* /*stats*/) {
55   uint32_t hash = HashSlice(key);
56   return GetShard(Shard(hash))->Lookup(key, hash);
57 }
58 
Ref(Handle * handle)59 bool ShardedCache::Ref(Handle* handle) {
60   uint32_t hash = GetHash(handle);
61   return GetShard(Shard(hash))->Ref(handle);
62 }
63 
Release(Handle * handle,bool force_erase)64 bool ShardedCache::Release(Handle* handle, bool force_erase) {
65   uint32_t hash = GetHash(handle);
66   return GetShard(Shard(hash))->Release(handle, force_erase);
67 }
68 
Erase(const Slice & key)69 void ShardedCache::Erase(const Slice& key) {
70   uint32_t hash = HashSlice(key);
71   GetShard(Shard(hash))->Erase(key, hash);
72 }
73 
NewId()74 uint64_t ShardedCache::NewId() {
75   return last_id_.fetch_add(1, std::memory_order_relaxed);
76 }
77 
GetCapacity() const78 size_t ShardedCache::GetCapacity() const {
79   MutexLock l(&capacity_mutex_);
80   return capacity_;
81 }
82 
HasStrictCapacityLimit() const83 bool ShardedCache::HasStrictCapacityLimit() const {
84   MutexLock l(&capacity_mutex_);
85   return strict_capacity_limit_;
86 }
87 
GetUsage() const88 size_t ShardedCache::GetUsage() const {
89   // We will not lock the cache when getting the usage from shards.
90   int num_shards = 1 << num_shard_bits_;
91   size_t usage = 0;
92   for (int s = 0; s < num_shards; s++) {
93     usage += GetShard(s)->GetUsage();
94   }
95   return usage;
96 }
97 
GetUsage(Handle * handle) const98 size_t ShardedCache::GetUsage(Handle* handle) const {
99   return GetCharge(handle);
100 }
101 
GetPinnedUsage() const102 size_t ShardedCache::GetPinnedUsage() const {
103   // We will not lock the cache when getting the usage from shards.
104   int num_shards = 1 << num_shard_bits_;
105   size_t usage = 0;
106   for (int s = 0; s < num_shards; s++) {
107     usage += GetShard(s)->GetPinnedUsage();
108   }
109   return usage;
110 }
111 
ApplyToAllCacheEntries(void (* callback)(void *,size_t),bool thread_safe)112 void ShardedCache::ApplyToAllCacheEntries(void (*callback)(void*, size_t),
113                                           bool thread_safe) {
114   int num_shards = 1 << num_shard_bits_;
115   for (int s = 0; s < num_shards; s++) {
116     GetShard(s)->ApplyToAllCacheEntries(callback, thread_safe);
117   }
118 }
119 
EraseUnRefEntries()120 void ShardedCache::EraseUnRefEntries() {
121   int num_shards = 1 << num_shard_bits_;
122   for (int s = 0; s < num_shards; s++) {
123     GetShard(s)->EraseUnRefEntries();
124   }
125 }
126 
GetPrintableOptions() const127 std::string ShardedCache::GetPrintableOptions() const {
128   std::string ret;
129   ret.reserve(20000);
130   const int kBufferSize = 200;
131   char buffer[kBufferSize];
132   {
133     MutexLock l(&capacity_mutex_);
134     snprintf(buffer, kBufferSize, "    capacity : %" ROCKSDB_PRIszt "\n",
135              capacity_);
136     ret.append(buffer);
137     snprintf(buffer, kBufferSize, "    num_shard_bits : %d\n", num_shard_bits_);
138     ret.append(buffer);
139     snprintf(buffer, kBufferSize, "    strict_capacity_limit : %d\n",
140              strict_capacity_limit_);
141     ret.append(buffer);
142   }
143   snprintf(buffer, kBufferSize, "    memory_allocator : %s\n",
144            memory_allocator() ? memory_allocator()->Name() : "None");
145   ret.append(buffer);
146   ret.append(GetShard(0)->GetPrintableOptions());
147   return ret;
148 }
GetDefaultCacheShardBits(size_t capacity)149 int GetDefaultCacheShardBits(size_t capacity) {
150   int num_shard_bits = 0;
151   size_t min_shard_size = 512L * 1024L;  // Every shard is at least 512KB.
152   size_t num_shards = capacity / min_shard_size;
153   while (num_shards >>= 1) {
154     if (++num_shard_bits >= 6) {
155       // No more than 6.
156       return num_shard_bits;
157     }
158   }
159   return num_shard_bits;
160 }
161 
162 }  // namespace ROCKSDB_NAMESPACE
163