Summary: Refactor cache.cc so that I can plugin clock cache (D55581). Mainly move `ShardedCache` to separate file, move `LRUHandle` back to cache.cc and rename it lru_cache.cc. Test Plan: make check -j64 Reviewers: lightmark, sdong Reviewed By: sdong Subscribers: andrewkr, dhruba, leveldb Differential Revision: https://reviews.facebook.net/D59655main
parent
c6a8665b37
commit
4b95253587
@ -1,71 +0,0 @@ |
||||
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under the BSD-style license found in the
|
||||
// LICENSE file in the root directory of this source tree. An additional grant
|
||||
// of patent rights can be found in the PATENTS file in the same directory.
|
||||
//
|
||||
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||||
|
||||
#pragma once |
||||
|
||||
#include <cassert> |
||||
#include <cstdint> |
||||
|
||||
#include "port/port.h" |
||||
#include "rocksdb/slice.h" |
||||
|
||||
namespace rocksdb { |
||||
// An entry is a variable length heap-allocated structure.
|
||||
// Entries are referenced by cache and/or by any external entity.
|
||||
// The cache keeps all its entries in table. Some elements
|
||||
// are also stored on LRU list.
|
||||
//
|
||||
// LRUHandle can be in these states:
|
||||
// 1. Referenced externally AND in hash table.
|
||||
// In that case the entry is *not* in the LRU. (refs > 1 && in_cache == true)
|
||||
// 2. Not referenced externally and in hash table. In that case the entry is
|
||||
// in the LRU and can be freed. (refs == 1 && in_cache == true)
|
||||
// 3. Referenced externally and not in hash table. In that case the entry is
|
||||
// in not on LRU and not in table. (refs >= 1 && in_cache == false)
|
||||
//
|
||||
// All newly created LRUHandles are in state 1. If you call LRUCache::Release
|
||||
// on entry in state 1, it will go into state 2. To move from state 1 to
|
||||
// state 3, either call LRUCache::Erase or LRUCache::Insert with the same key.
|
||||
// To move from state 2 to state 1, use LRUCache::Lookup.
|
||||
// Before destruction, make sure that no handles are in state 1. This means
|
||||
// that any successful LRUCache::Lookup/LRUCache::Insert have a matching
|
||||
// RUCache::Release (to move into state 2) or LRUCache::Erase (for state 3)
|
||||
|
||||
struct LRUHandle { |
||||
void* value; |
||||
void (*deleter)(const Slice&, void* value); |
||||
LRUHandle* next_hash; |
||||
LRUHandle* next; |
||||
LRUHandle* prev; |
||||
size_t charge; // TODO(opt): Only allow uint32_t?
|
||||
size_t key_length; |
||||
uint32_t refs; // a number of refs to this entry
|
||||
// cache itself is counted as 1
|
||||
bool in_cache; // true, if this entry is referenced by the hash table
|
||||
uint32_t hash; // Hash of key(); used for fast sharding and comparisons
|
||||
char key_data[1]; // Beginning of key
|
||||
|
||||
Slice key() const { |
||||
// For cheaper lookups, we allow a temporary Handle object
|
||||
// to store a pointer to a key in "value".
|
||||
if (next == this) { |
||||
return *(reinterpret_cast<Slice*>(value)); |
||||
} else { |
||||
return Slice(key_data, key_length); |
||||
} |
||||
} |
||||
|
||||
void Free() { |
||||
assert((refs == 1 && in_cache) || (refs == 0 && !in_cache)); |
||||
(*deleter)(key(), value); |
||||
delete[] reinterpret_cast<char*>(this); |
||||
} |
||||
}; |
||||
|
||||
} // end namespace rocksdb
|
@ -0,0 +1,117 @@ |
||||
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under the BSD-style license found in the
|
||||
// LICENSE file in the root directory of this source tree. An additional grant
|
||||
// of patent rights can be found in the PATENTS file in the same directory.
|
||||
//
|
||||
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||||
|
||||
#include "util/sharded_cache.h" |
||||
#include "util/mutexlock.h" |
||||
|
||||
namespace rocksdb { |
||||
|
||||
ShardedCache::ShardedCache(size_t capacity, int num_shard_bits, |
||||
bool strict_capacity_limit) |
||||
: num_shard_bits_(num_shard_bits), |
||||
capacity_(capacity), |
||||
strict_capacity_limit_(strict_capacity_limit), |
||||
last_id_(1) {} |
||||
|
||||
void ShardedCache::SetCapacity(size_t capacity) { |
||||
int num_shards = 1 << num_shard_bits_; |
||||
const size_t per_shard = (capacity + (num_shards - 1)) / num_shards; |
||||
MutexLock l(&capacity_mutex_); |
||||
for (int s = 0; s < num_shards; s++) { |
||||
GetShard(s)->SetCapacity(per_shard); |
||||
} |
||||
capacity_ = capacity; |
||||
} |
||||
|
||||
void ShardedCache::SetStrictCapacityLimit(bool strict_capacity_limit) { |
||||
int num_shards = 1 << num_shard_bits_; |
||||
MutexLock l(&capacity_mutex_); |
||||
for (int s = 0; s < num_shards; s++) { |
||||
GetShard(s)->SetStrictCapacityLimit(strict_capacity_limit); |
||||
} |
||||
strict_capacity_limit_ = strict_capacity_limit; |
||||
} |
||||
|
||||
Status ShardedCache::Insert(const Slice& key, void* value, size_t charge, |
||||
void (*deleter)(const Slice& key, void* value), |
||||
Handle** handle) { |
||||
uint32_t hash = HashSlice(key); |
||||
return GetShard(Shard(hash)) |
||||
->Insert(key, hash, value, charge, deleter, handle); |
||||
} |
||||
|
||||
Cache::Handle* ShardedCache::Lookup(const Slice& key) { |
||||
uint32_t hash = HashSlice(key); |
||||
return GetShard(Shard(hash))->Lookup(key, hash); |
||||
} |
||||
|
||||
void ShardedCache::Release(Handle* handle) { |
||||
uint32_t hash = GetHash(handle); |
||||
GetShard(Shard(hash))->Release(handle); |
||||
} |
||||
|
||||
void ShardedCache::Erase(const Slice& key) { |
||||
uint32_t hash = HashSlice(key); |
||||
GetShard(Shard(hash))->Erase(key, hash); |
||||
} |
||||
|
||||
uint64_t ShardedCache::NewId() { |
||||
return last_id_.fetch_add(1, std::memory_order_relaxed); |
||||
} |
||||
|
||||
size_t ShardedCache::GetCapacity() const { |
||||
MutexLock l(&capacity_mutex_); |
||||
return capacity_; |
||||
} |
||||
|
||||
bool ShardedCache::HasStrictCapacityLimit() const { |
||||
MutexLock l(&capacity_mutex_); |
||||
return strict_capacity_limit_; |
||||
} |
||||
|
||||
size_t ShardedCache::GetUsage() const { |
||||
// We will not lock the cache when getting the usage from shards.
|
||||
int num_shards = 1 << num_shard_bits_; |
||||
size_t usage = 0; |
||||
for (int s = 0; s < num_shards; s++) { |
||||
usage += GetShard(s)->GetUsage(); |
||||
} |
||||
return usage; |
||||
} |
||||
|
||||
size_t ShardedCache::GetUsage(Handle* handle) const { |
||||
return GetCharge(handle); |
||||
} |
||||
|
||||
size_t ShardedCache::GetPinnedUsage() const { |
||||
// We will not lock the cache when getting the usage from shards.
|
||||
int num_shards = 1 << num_shard_bits_; |
||||
size_t usage = 0; |
||||
for (int s = 0; s < num_shards; s++) { |
||||
usage += GetShard(s)->GetPinnedUsage(); |
||||
} |
||||
return usage; |
||||
} |
||||
|
||||
void ShardedCache::ApplyToAllCacheEntries(void (*callback)(void*, size_t), |
||||
bool thread_safe) { |
||||
int num_shards = 1 << num_shard_bits_; |
||||
for (int s = 0; s < num_shards; s++) { |
||||
GetShard(s)->ApplyToAllCacheEntries(callback, thread_safe); |
||||
} |
||||
} |
||||
|
||||
void ShardedCache::EraseUnRefEntries() { |
||||
int num_shards = 1 << num_shard_bits_; |
||||
for (int s = 0; s < num_shards; s++) { |
||||
GetShard(s)->EraseUnRefEntries(); |
||||
} |
||||
} |
||||
|
||||
} // namespace rocksdb
|
@ -0,0 +1,93 @@ |
||||
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under the BSD-style license found in the
|
||||
// LICENSE file in the root directory of this source tree. An additional grant
|
||||
// of patent rights can be found in the PATENTS file in the same directory.
|
||||
//
|
||||
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||||
|
||||
#pragma once |
||||
|
||||
#include <atomic> |
||||
|
||||
#include "port/port.h" |
||||
#include "rocksdb/cache.h" |
||||
#include "util/hash.h" |
||||
|
||||
namespace rocksdb { |
||||
|
||||
// Single cache shard interface.
|
||||
class CacheShard { |
||||
public: |
||||
CacheShard() = default; |
||||
virtual ~CacheShard() = default; |
||||
|
||||
virtual Status Insert(const Slice& key, uint32_t hash, void* value, |
||||
size_t charge, |
||||
void (*deleter)(const Slice& key, void* value), |
||||
Cache::Handle** handle) = 0; |
||||
virtual Cache::Handle* Lookup(const Slice& key, uint32_t hash) = 0; |
||||
virtual void Release(Cache::Handle* handle) = 0; |
||||
virtual void Erase(const Slice& key, uint32_t hash) = 0; |
||||
virtual void SetCapacity(size_t capacity) = 0; |
||||
virtual void SetStrictCapacityLimit(bool strict_capacity_limit) = 0; |
||||
virtual size_t GetUsage() const = 0; |
||||
virtual size_t GetPinnedUsage() const = 0; |
||||
virtual void ApplyToAllCacheEntries(void (*callback)(void*, size_t), |
||||
bool thread_safe) = 0; |
||||
virtual void EraseUnRefEntries() = 0; |
||||
}; |
||||
|
||||
// Generic cache interface which shards cache by hash of keys. 2^num_shard_bits
|
||||
// shards will be created, with capacity split evenly to each of the shards.
|
||||
// Keys are sharded by the highest num_shard_bits bits of hash value.
|
||||
class ShardedCache : public Cache { |
||||
public: |
||||
ShardedCache(size_t capacity, int num_shard_bits, bool strict_capacity_limit); |
||||
virtual ~ShardedCache() = default; |
||||
|
||||
virtual CacheShard* GetShard(int shard) = 0; |
||||
virtual const CacheShard* GetShard(int shard) const = 0; |
||||
virtual void* Value(Handle* handle) override = 0; |
||||
virtual size_t GetCharge(Handle* handle) const = 0; |
||||
virtual uint32_t GetHash(Handle* handle) const = 0; |
||||
virtual void DisownData() override = 0; |
||||
|
||||
virtual void SetCapacity(size_t capacity) override; |
||||
virtual void SetStrictCapacityLimit(bool strict_capacity_limit) override; |
||||
|
||||
virtual Status Insert(const Slice& key, void* value, size_t charge, |
||||
void (*deleter)(const Slice& key, void* value), |
||||
Handle** handle) override; |
||||
virtual Handle* Lookup(const Slice& key) override; |
||||
virtual void Release(Handle* handle) override; |
||||
virtual void Erase(const Slice& key) override; |
||||
virtual uint64_t NewId() override; |
||||
virtual size_t GetCapacity() const override; |
||||
virtual bool HasStrictCapacityLimit() const override; |
||||
virtual size_t GetUsage() const override; |
||||
virtual size_t GetUsage(Handle* handle) const override; |
||||
virtual size_t GetPinnedUsage() const override; |
||||
virtual void ApplyToAllCacheEntries(void (*callback)(void*, size_t), |
||||
bool thread_safe) override; |
||||
virtual void EraseUnRefEntries() override; |
||||
|
||||
private: |
||||
static inline uint32_t HashSlice(const Slice& s) { |
||||
return Hash(s.data(), s.size(), 0); |
||||
} |
||||
|
||||
uint32_t Shard(uint32_t hash) { |
||||
// Note, hash >> 32 yields hash in gcc, not the zero we expect!
|
||||
return (num_shard_bits_ > 0) ? (hash >> (32 - num_shard_bits_)) : 0; |
||||
} |
||||
|
||||
int num_shard_bits_; |
||||
mutable port::Mutex capacity_mutex_; |
||||
size_t capacity_; |
||||
bool strict_capacity_limit_; |
||||
std::atomic<uint64_t> last_id_; |
||||
}; |
||||
|
||||
} // namespace rocksdb
|
Loading…
Reference in new issue